You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pybind11_allocator.h 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Tencent is pleased to support the open source community by making ncnn available.
  2. *
  3. * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. *
  5. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. * in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * https://opensource.org/licenses/BSD-3-Clause
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. */
  15. #ifndef PYBIND11_NCNN_ALLOCATOR_H
  16. #define PYBIND11_NCNN_ALLOCATOR_H
  17. #include <allocator.h>
  18. template<class Base = ncnn::Allocator>
  19. class PyAllocator : public Base
  20. {
  21. public:
  22. using Base::Base; // Inherit constructors
  23. void* fastMalloc(size_t size) override
  24. {
  25. PYBIND11_OVERRIDE_PURE(void*, Base, fastMalloc, size);
  26. }
  27. void fastFree(void* ptr) override
  28. {
  29. PYBIND11_OVERRIDE_PURE(void, Base, fastFree, ptr);
  30. }
  31. };
  32. template<class Other>
  33. class PyAllocatorOther : public PyAllocator<Other>
  34. {
  35. public:
  36. using PyAllocator<Other>::PyAllocator;
  37. void* fastMalloc(size_t size) override
  38. {
  39. PYBIND11_OVERRIDE(void*, Other, fastMalloc, size);
  40. }
  41. void fastFree(void* ptr) override
  42. {
  43. PYBIND11_OVERRIDE(void, Other, fastFree, ptr);
  44. }
  45. };
  46. #if NCNN_VULKAN
  47. template<class Base = ncnn::VkAllocator>
  48. class PyVkAllocator : public Base
  49. {
  50. public:
  51. using Base::Base; // Inherit constructors
  52. void clear() override
  53. {
  54. PYBIND11_OVERRIDE(void, Base, clear, );
  55. }
  56. ncnn::VkBufferMemory* fastMalloc(size_t size) override
  57. {
  58. PYBIND11_OVERRIDE_PURE(ncnn::VkBufferMemory*, Base, fastMalloc, size);
  59. }
  60. void fastFree(ncnn::VkBufferMemory* ptr) override
  61. {
  62. PYBIND11_OVERRIDE_PURE(void, Base, fastFree, ptr);
  63. }
  64. int flush(ncnn::VkBufferMemory* ptr) override
  65. {
  66. PYBIND11_OVERRIDE(int, Base, flush, ptr);
  67. }
  68. int invalidate(ncnn::VkBufferMemory* ptr) override
  69. {
  70. PYBIND11_OVERRIDE(int, Base, invalidate, ptr);
  71. }
  72. };
  73. template<class Other>
  74. class PyVkAllocatorOther : public PyVkAllocator<Other>
  75. {
  76. public:
  77. using PyVkAllocator<Other>::PyVkAllocator;
  78. void clear() override
  79. {
  80. PYBIND11_OVERRIDE(void, Other, clear, );
  81. }
  82. ncnn::VkBufferMemory* fastMalloc(size_t size) override
  83. {
  84. PYBIND11_OVERRIDE(ncnn::VkBufferMemory*, Other, fastMalloc, size);
  85. }
  86. void fastFree(ncnn::VkBufferMemory* ptr) override
  87. {
  88. PYBIND11_OVERRIDE(void, Other, fastFree, ptr);
  89. }
  90. };
  91. template<class Base = ncnn::VkBlobAllocator>
  92. class PyVkBlobAllocator : public Base
  93. {
  94. public:
  95. using Base::Base; // Inherit constructors
  96. void clear() override
  97. {
  98. PYBIND11_OVERRIDE(void, Base, clear, );
  99. }
  100. ncnn::VkImageMemory* fastMalloc(int width, int height, VkFormat format) override
  101. {
  102. PYBIND11_OVERRIDE_PURE(ncnn::VkImageMemory*, Base, fastMalloc, width, height, format);
  103. }
  104. void fastFree(ncnn::VkImageMemory* ptr) override
  105. {
  106. PYBIND11_OVERRIDE_PURE(void, Base, fastFree, ptr);
  107. }
  108. };
  109. //template<class Other>
  110. //class PyVkImageAllocatorOther : public PyVkImageAllocator<Other>
  111. //{
  112. //public:
  113. // using PyVkImageAllocator<Other>::PyVkImageAllocator;
  114. // ncnn::VkImageMemory* fastMalloc(int width, int height,
  115. // VkFormat format) override
  116. // {
  117. // PYBIND11_OVERRIDE(ncnn::VkImageMemory*, Other, fastMalloc, width, height, format);
  118. // }
  119. // void fastFree(ncnn::VkImageMemory* ptr) override
  120. // {
  121. // PYBIND11_OVERRIDE(void, Other, fastFree, ptr);
  122. // }
  123. //};
  124. #endif // NCNN_VULKAN
  125. #endif