| 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 | |
| 16 | #ifndef PYBIND11_NCNN_ALLOCATOR_H |
| 17 | #define PYBIND11_NCNN_ALLOCATOR_H |
| 18 | |
| 19 | #include <allocator.h> |
| 20 | |
| 21 | template<class Base = ncnn::Allocator> |
| 22 | class PyAllocator : public Base |
| 23 | { |
| 24 | public: |
| 25 | using Base::Base; // Inherit constructors |
| 26 | void* fastMalloc(size_t size) override |
| 27 | { |
| 28 | PYBIND11_OVERRIDE_PURE(void*, Base, fastMalloc, size); |
| 29 | } |
| 30 | void fastFree(void* ptr) override |
| 31 | { |
| 32 | PYBIND11_OVERRIDE_PURE(void, Base, fastFree, ptr); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | template<class Other> |
| 37 | class PyAllocatorOther : public PyAllocator<Other> |
| 38 | { |
| 39 | public: |
| 40 | using PyAllocator<Other>::PyAllocator; |
| 41 | void* fastMalloc(size_t size) override |
| 42 | { |
| 43 | PYBIND11_OVERRIDE(void*, Other, fastMalloc, size); |
| 44 | } |
| 45 | void fastFree(void* ptr) override |
| 46 | { |
| 47 | PYBIND11_OVERRIDE(void, Other, fastFree, ptr); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | #if NCNN_VULKAN |
| 52 | template<class Base = ncnn::VkAllocator> |
| 53 | class PyVkAllocator : public Base |
| 54 | { |
| 55 | public: |
| 56 | using Base::Base; // Inherit constructors |
| 57 | void clear() override |
| 58 | { |
| 59 | PYBIND11_OVERRIDE(void, Base, clear, ); |
| 60 | } |
| 61 | ncnn::VkBufferMemory* fastMalloc(size_t size) override |
| 62 | { |
| 63 | PYBIND11_OVERRIDE_PURE(ncnn::VkBufferMemory*, Base, fastMalloc, size); |
| 64 | } |
| 65 | void fastFree(ncnn::VkBufferMemory* ptr) override |
| 66 | { |
| 67 | PYBIND11_OVERRIDE_PURE(void, Base, fastFree, ptr); |
| 68 | } |
| 69 | int flush(ncnn::VkBufferMemory* ptr) override |
| 70 | { |
| 71 | PYBIND11_OVERRIDE(int, Base, flush, ptr); |
| 72 | } |
| 73 | int invalidate(ncnn::VkBufferMemory* ptr) override |
| 74 | { |
| 75 | PYBIND11_OVERRIDE(int, Base, invalidate, ptr); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | template<class Other> |
| 80 | class PyVkAllocatorOther : public PyVkAllocator<Other> |
| 81 | { |
| 82 | public: |
| 83 | using PyVkAllocator<Other>::PyVkAllocator; |
| 84 | void clear() override |
| 85 | { |
| 86 | PYBIND11_OVERRIDE(void, Other, clear, ); |
| 87 | } |
| 88 | ncnn::VkBufferMemory* fastMalloc(size_t size) override |
| 89 | { |
| 90 | PYBIND11_OVERRIDE(ncnn::VkBufferMemory*, Other, fastMalloc, size); |
| 91 | } |
| 92 | void fastFree(ncnn::VkBufferMemory* ptr) override |
| 93 | { |
| 94 | PYBIND11_OVERRIDE(void, Other, fastFree, ptr); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | template<class Base = ncnn::VkBlobAllocator> |
| 99 | class PyVkBlobAllocator : public Base |
| 100 | { |
| 101 | public: |
| 102 | using Base::Base; // Inherit constructors |
| 103 | void clear() override |
| 104 | { |
| 105 | PYBIND11_OVERRIDE(void, Base, clear, ); |
| 106 | } |
| 107 | ncnn::VkImageMemory* fastMalloc(int width, int height, VkFormat format) override |
| 108 | { |
| 109 | PYBIND11_OVERRIDE_PURE(ncnn::VkImageMemory*, Base, fastMalloc, width, height, format); |
| 110 | } |
| 111 | void fastFree(ncnn::VkImageMemory* ptr) override |
| 112 | { |
| 113 | PYBIND11_OVERRIDE_PURE(void, Base, fastFree, ptr); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | //template<class Other> |
| 118 | //class PyVkImageAllocatorOther : public PyVkImageAllocator<Other> |
| 119 | //{ |
| 120 | //public: |
| 121 | // using PyVkImageAllocator<Other>::PyVkImageAllocator; |
| 122 | // ncnn::VkImageMemory* fastMalloc(int width, int height, |
| 123 | // VkFormat format) override |
| 124 | // { |
| 125 | // PYBIND11_OVERRIDE(ncnn::VkImageMemory*, Other, fastMalloc, width, height, format); |
| 126 | // } |
| 127 | // void fastFree(ncnn::VkImageMemory* ptr) override |
| 128 | // { |
| 129 | // PYBIND11_OVERRIDE(void, Other, fastFree, ptr); |
| 130 | // } |
| 131 | //}; |
| 132 | #endif // NCNN_VULKAN |
| 133 | |
| 134 | #endif |