3 #ifndef INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_ 4 #define INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_ 8 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 10 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 20 void FreeAll() NLIB_NOEXCEPT { cur_ = start_; }
24 void*
Alloc(
size_t n,
size_t algn) NLIB_NOEXCEPT {
25 if (n == 0)
return NULL;
26 if (algn == 0 || 0 != (algn & (algn - 1)))
return NULL;
27 uint8_t* ret = this->RoundUp(cur_, algn);
28 uint8_t* new_top = ret + n;
29 if (new_top > end_)
return NULL;
33 void*
Alloc(
size_t n) NLIB_NOEXCEPT {
return this->
Alloc(n, 8); }
35 uint8_t* ptr =
reinterpret_cast<uint8_t*
>(p);
36 if (ptr >= start_ && ptr < cur_) {
46 static uint8_t* RoundUp(uint8_t* ptr,
size_t algn) NLIB_NOEXCEPT {
48 intptr_t tmp =
reinterpret_cast<intptr_t
>(ptr);
49 return reinterpret_cast<uint8_t*
>(tmp + (-tmp & (algn - 1)));
51 void ChangeHeapRange(
void* start,
void* end) NLIB_NOEXCEPT {
52 start_ =
reinterpret_cast<uint8_t*
>(start);
53 end_ =
reinterpret_cast<uint8_t*
>(end);
59 uint32_t heap_option_;
67 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 68 #undef NLIB_VIS_PUBLIC 69 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 72 #endif // INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_ Allocator that allocates memory from the top like a stack.
void * Alloc(size_t n) noexcept
Allocates a memory block.
void * Alloc(size_t n, size_t algn) noexcept
Allocates a memory block with a specific alignment.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
errno_t Free(void *p) noexcept
Moves the location of the next memory allocation back to the address that p points to...
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
void FreeAll() noexcept
Frees all memory and restores the heap to its post-initialization state.