ArmNN
 22.05
ClBackendDefaultAllocator.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 #include <cstddef>
7 #include <memory>
10 
11 namespace armnn
12 {
13 /** Default Memory Allocator class returned from IBackendInternal::GetDefaultAllocator(MemorySource) */
15 {
16 public:
17  ClBackendDefaultAllocator() = default;
18 
19  void* allocate(size_t size, size_t alignment = 0) override
20  {
21  IgnoreUnused(alignment);
22  cl_mem buf{ clCreateBuffer(arm_compute::CLScheduler::get().context().get(),
23  CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE,
24  size,
25  nullptr,
26  nullptr)};
27  return static_cast<void *>(buf);
28  }
29 
30  void free(void* ptr) override
31  {
32  ARM_COMPUTE_ERROR_ON(ptr == nullptr);
33  clReleaseMemObject(static_cast<cl_mem>(ptr));
34  }
35 
37  {
39  }
40 
41  void* GetMemoryRegionAtOffset(void* buffer, size_t offset, size_t alignment = 0) override
42  {
43  IgnoreUnused(alignment);
44  return static_cast<char*>(buffer) + offset;
45  }
46 };
47 } // namespace armnn
Custom Allocator interface.
Default Memory Allocator class returned from IBackendInternal::GetDefaultAllocator(MemorySource) ...
void free(void *ptr) override
Interface to be implemented by the child class to free the allocated bytes.
armnn::MemorySource GetMemorySourceType() override
Used to specify what type of memory is being allocated by this allocator.
void * allocate(size_t size, size_t alignment=0) override
Interface to be implemented by the child class to allocate bytes.
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
void * GetMemoryRegionAtOffset(void *buffer, size_t offset, size_t alignment=0) override
Interface that may be implemented to allow retrieval of Memory Region from allocated buffer at a cert...
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:230