|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 MediaTek Inc. |
| 3 | + * |
| 4 | + * Licensed under the BSD License (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. See the license file in the root |
| 6 | + * directory of this source tree for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "include/NeuronBufferAllocator.h" |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | +#include <cstddef> |
| 13 | +#include <mutex> |
| 14 | + |
| 15 | +namespace executorch { |
| 16 | +namespace backends { |
| 17 | +namespace neuron { |
| 18 | + |
| 19 | +bool loadLibrary() { |
| 20 | + handle = dlopen("libneuron_buffer_allocator.so", RTLD_LAZY); |
| 21 | + if (!handle) { |
| 22 | + std::cerr << "Failed to load library: " << dlerror() << std::endl; |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + create_func = (CreateFunc)dlsym(handle, "neuron_buffer_allocator_create"); |
| 27 | + allocate_func = (AllocateFunc)dlsym(handle, "neuron_buffer_allocate"); |
| 28 | + remove_func = (RemoveFunc)dlsym(handle, "neuron_buffer_remove"); |
| 29 | + find_func = (FindFunc)dlsym(handle, "neuron_buffer_find"); |
| 30 | + clear_func = (ClearFunc)dlsym(handle, "neuron_buffer_clear"); |
| 31 | + |
| 32 | + if (!create_func || !allocate_func || !remove_func || !find_func || |
| 33 | + !clear_func) { |
| 34 | + std::cerr << "Failed to retrieve symbols: " << dlerror() << std::endl; |
| 35 | + dlclose(handle); |
| 36 | + handle = nullptr; |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +void unloadLibrary() { |
| 44 | + if (handle) { |
| 45 | + dlclose(handle); |
| 46 | + handle = nullptr; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +BufferAllocator& BufferAllocator::GetInstance() { |
| 51 | + static BufferAllocator instance; |
| 52 | + |
| 53 | + if (handle == nullptr) { |
| 54 | + loadLibrary(); |
| 55 | + if (allocatorHandle == nullptr) { |
| 56 | + allocatorHandle = create_func(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return instance; |
| 61 | +}; |
| 62 | + |
| 63 | +void* BufferAllocator::Allocate(size_t size) { |
| 64 | + std::scoped_lock Guard(mMutex); |
| 65 | + return allocate_func(allocatorHandle, size); |
| 66 | +} |
| 67 | + |
| 68 | +bool BufferAllocator::RemoveBuffer(void* address) { |
| 69 | + std::scoped_lock Guard(mMutex); |
| 70 | + return remove_func(allocatorHandle, address); |
| 71 | +} |
| 72 | + |
| 73 | +const MemoryUnit* BufferAllocator::Find(void* address) { |
| 74 | + std::scoped_lock Guard(mMutex); |
| 75 | + return static_cast<const MemoryUnit*>(find_func(allocatorHandle, address)); |
| 76 | +} |
| 77 | + |
| 78 | +void BufferAllocator::Clear() { |
| 79 | + std::scoped_lock Guard(mMutex); |
| 80 | + clear_func(allocatorHandle); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace neuron |
| 84 | +} // namespace backends |
| 85 | +} // namespace executorch |
| 86 | + |
| 87 | +namespace { |
| 88 | +static auto& singletonInstance = GET_NEURON_ALLOCATOR; |
| 89 | +} // namespace |
0 commit comments