| 1 | /** |
| 2 | * @file mem.h |
| 3 | * @brief Memory allocation functions. |
| 4 | */ |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <nn/os.h> |
| 9 | #include <nn/types.h> |
| 10 | #include <nn/util/util_TypedStorage.h> |
| 11 | |
| 12 | namespace nn { |
| 13 | namespace nlibsdk { |
| 14 | namespace heap { |
| 15 | class CentralHeap; |
| 16 | } // namespace heap |
| 17 | } // namespace nlibsdk |
| 18 | |
| 19 | namespace mem { |
| 20 | class StandardAllocator { |
| 21 | public: |
| 22 | StandardAllocator(); |
| 23 | StandardAllocator(void* address, size_t size); |
| 24 | StandardAllocator(void* address, size_t size, bool enableCache); |
| 25 | |
| 26 | ~StandardAllocator() { |
| 27 | if (mIsInitialized) { |
| 28 | Finalize(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void Initialize(void* address, size_t size); |
| 33 | void Initialize(void* address, size_t size, bool enableCache); |
| 34 | void Finalize(); |
| 35 | void* Reallocate(void* address, size_t newSize); |
| 36 | void* Allocate(size_t size); |
| 37 | void* Allocate(size_t size, size_t alignment); |
| 38 | void Free(void* address); |
| 39 | |
| 40 | size_t GetSizeOf(const void* address) const; |
| 41 | void ClearThreadCache() const; |
| 42 | void CleanUpManagementArea() const; |
| 43 | size_t GetTotalFreeSize() const; |
| 44 | size_t GetAllocatableSize() const; |
| 45 | void Dump() const; |
| 46 | |
| 47 | bool mIsInitialized; |
| 48 | bool mIsEnabledThreadCache; |
| 49 | uintptr_t mAllocAddr; |
| 50 | nn::os::TlsSlot mTlsSlot; |
| 51 | nn::util::TypedStorage<nn::nlibsdk::heap::CentralHeap, 48, 8> mCentralHeapStorage; |
| 52 | }; |
| 53 | |
| 54 | static_assert(sizeof(StandardAllocator) == 0x48); |
| 55 | |
| 56 | } // namespace mem |
| 57 | } // namespace nn |