Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef SRC_COMMON_ALLOCATORWRAPPER_H |
| 25 | #define SRC_COMMON_ALLOCATORWRAPPER_H |
| 26 | |
| 27 | #include "arm_compute/AclTypes.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | /** Default malloc allocator implementation */ |
| 32 | class AllocatorWrapper final |
| 33 | { |
| 34 | public: |
| 35 | /** Default Constructor |
| 36 | * |
| 37 | * @param[in] backing_allocator Backing memory allocator to be used |
| 38 | */ |
| 39 | AllocatorWrapper(const AclAllocator &backing_allocator) noexcept; |
| 40 | AllocatorWrapper(const AllocatorWrapper &) noexcept = default; |
| 41 | AllocatorWrapper(AllocatorWrapper &&) noexcept = default; |
| 42 | AllocatorWrapper &operator=(const AllocatorWrapper &) noexcept = delete; |
| 43 | AllocatorWrapper &operator=(AllocatorWrapper &&other) noexcept = default; |
| 44 | /** Allocate a chunk of memory of a given size in bytes |
| 45 | * |
| 46 | * @param[in] size Size of memory to allocate in bytes |
| 47 | * |
| 48 | * @return A pointer to the allocated memory if successful else nullptr |
| 49 | */ |
| 50 | void *alloc(size_t size); |
| 51 | /** Free an allocated memory block |
| 52 | * |
| 53 | * @param[in] ptr Pointer to allocated memory |
| 54 | */ |
| 55 | void free(void *ptr); |
| 56 | /** Allocate a chunk of memory of a given size in bytes, |
| 57 | * while honoring a given alignment requirement |
| 58 | * |
| 59 | * @param[in] size Size of memory to allocate in bytes |
| 60 | * @param[in] alignment Alignment requirements |
| 61 | * |
| 62 | * @return A pointer to the allocated memory if successful else nullptr |
| 63 | */ |
| 64 | void *aligned_alloc(size_t size, size_t alignment); |
| 65 | /** Free an aligned memory block |
| 66 | * |
| 67 | * @param[in] ptr Pointer to the memory to release |
| 68 | */ |
| 69 | void aligned_free(void *ptr); |
| 70 | /** Set user data to be used by the allocator |
| 71 | * |
| 72 | * @param[in] user_data User data to be used by the allocator |
| 73 | */ |
| 74 | void set_user_data(void *user_data); |
| 75 | |
| 76 | private: |
| 77 | AclAllocator _backing_allocator; |
| 78 | }; |
| 79 | } // namespace arm_compute |
| 80 | |
| 81 | #endif /* SRC_COMMON_ALLOCATORWRAPPER_H */ |