16 #ifndef INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_ 17 #define INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_ 21 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 22 #undef NLIB_VIS_PUBLIC 23 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 31 typedef void (*DestructorHandler)(
void* start,
void* end) NLIB_NOEXCEPT_FUNCPTR;
33 kAllocOptionAllocHead = 0x00000000,
34 kAllocOptionAllocTail = 0x00000001
40 return _[0] == rhs._[0] && _[1] == rhs._[1];
43 return _[0] != rhs._[0] || _[1] != rhs._[1];
47 cur_.interlocked._[0] = cur_.interlocked._[1] = 0;
51 (*handler_)(start_, end_);
54 errno_t Init(
void* start,
size_t size, DestructorHandler handler,
59 to.tb.botidx = end_ - start_;
62 void FreeTop() NLIB_NOEXCEPT { cur_.tb.topidx = 0; }
64 size_t bot = end_ - start_;
72 void*
Alloc(
size_t n) NLIB_NOEXCEPT {
return this->
Alloc(n, 8, 0); }
73 void*
Alloc(
size_t n,
size_t algn) NLIB_NOEXCEPT {
return this->
Alloc(n, algn, 0); }
74 void* Alloc(
size_t n,
size_t algn, uint32_t alloc_option)
NLIB_NOEXCEPT;
85 static uint8_t* RoundUp(uint8_t* ptr,
size_t algn) NLIB_NOEXCEPT {
87 intptr_t tmp =
reinterpret_cast<intptr_t
>(ptr);
88 return reinterpret_cast<uint8_t*
>(tmp + (-tmp & (algn - 1)));
90 static uint8_t* RoundDown(uint8_t* ptr,
size_t algn) NLIB_NOEXCEPT {
92 intptr_t tmp =
reinterpret_cast<intptr_t
>(ptr);
93 return reinterpret_cast<uint8_t*
>(tmp & (~algn + 1));
95 void ChangeHeapRange(
void* start,
void* end) NLIB_NOEXCEPT {
96 start_ =
reinterpret_cast<uint8_t*
>(start);
97 end_ =
reinterpret_cast<uint8_t*
>(end);
102 uint32_t heap_option_;
105 DestructorHandler handler_;
111 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 112 #undef NLIB_VIS_PUBLIC 113 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 116 #endif // INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_ bool operator==(const State &rhs) noexcept
Compares heap states and returns true if they are equal.
void FreeAll() noexcept
Frees all memory and restores the heap to its post-initialization state.
void FreeBot() noexcept
Frees all memory regions that were allocated from bottom of the heap.
void FreeTop() noexcept
Frees all memory regions that were allocated from top of the heap.
State SaveState() noexcept
Saves the current heap state.
Lists the state of the heap.
bool operator!=(const State &rhs) noexcept
Compares heap states and returns true if they are unequal.
Allows memory to be allocated from both sides of the heap.
void RestoreState(State state) noexcept
Restores a saved heap state.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
A file that contains the configuration information for each development environment.
void * Alloc(size_t n, size_t algn) noexcept
Allocates a memory block with a specific alignment.
AllocOption
Specifies which side of the heap to allocate from.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
void * Alloc(size_t n) noexcept
Allocates a memory block.