xref: /aosp_15_r20/external/armnn/include/armnn/backends/ICustomAllocator.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <cstddef>
9 #include <memory>
10 #include <armnn/MemorySources.hpp>
11 #include <armnn/utility/IgnoreUnused.hpp>
12 
13 namespace armnn
14 {
15 /** Custom Allocator interface */
16 class ICustomAllocator
17 {
18 public:
19     /** Default virtual destructor. */
20     virtual ~ICustomAllocator() = default;
21 
22     /** Interface to be implemented by the child class to allocate bytes
23      *
24      * @param[in] size      Size to allocate
25      * @param[in] alignment Alignment that the returned pointer should comply with
26      *
27      * @return A pointer to the allocated memory
28      * The returned pointer must be host write accessible
29      */
30     virtual void* allocate(size_t size, size_t alignment) = 0;
31 
32     /** Interface to be implemented by the child class to free the allocated bytes */
33     virtual void free(void* ptr) = 0;
34 
35     /**  Used to specify what type of memory is being allocated by this allocator.
36      *  Supported types are:
37      *       MemorySource::Malloc
38      *       MemorySource::DmaBuf
39      *       MemorySource::DmaBufProtected
40      */
41     virtual armnn::MemorySource GetMemorySourceType() = 0;
42 
43     /** Interface that may be implemented to allow retrieval of Memory Region
44      *  from allocated buffer at a certain offset
45      */
GetMemoryRegionAtOffset(void * buffer,size_t offset,size_t alignment=0)46     virtual void* GetMemoryRegionAtOffset(void* buffer, size_t offset, size_t alignment = 0)
47     {
48         IgnoreUnused(offset);
49         IgnoreUnused(alignment);
50         IgnoreUnused(buffer);
51         throw armnn::Exception(
52                 "ICustomerAllocator::GetMemoryRegionAtOffset(): This function should be overridden in subclass.");
53     }
54 
55 };
56 } // namespace armnn