16 #ifndef INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_ 17 #define INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_ 21 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 22 #undef NLIB_VIS_PUBLIC 23 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 32 start_(
nullptr), end_(
nullptr) {}
34 void FreeAll() NLIB_NOEXCEPT { cur_ = start_; }
38 void*
Alloc(
size_t n,
size_t algn) NLIB_NOEXCEPT {
39 if (n == 0)
return nullptr;
40 if (algn == 0 || 0 != (algn & (algn - 1)))
return nullptr;
41 uint8_t* ret = this->RoundUp(cur_, algn);
42 uint8_t* new_top = ret + n;
43 if (new_top > end_)
return nullptr;
47 void*
Alloc(
size_t n) NLIB_NOEXCEPT {
return this->
Alloc(n, 8); }
49 uint8_t* ptr =
static_cast<uint8_t*
>(p);
50 if (ptr >= start_ && ptr < cur_) {
60 static uint8_t* RoundUp(uint8_t* ptr,
size_t algn) NLIB_NOEXCEPT {
62 intptr_t tmp =
reinterpret_cast<intptr_t
>(ptr);
63 return reinterpret_cast<uint8_t*
>(tmp + (-tmp & (algn - 1)));
65 void ChangeHeapRange(
void* start,
void* end) NLIB_NOEXCEPT {
66 start_ =
static_cast<uint8_t*
>(start);
67 end_ =
static_cast<uint8_t*
>(end);
73 uint32_t heap_option_;
82 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 83 #undef NLIB_VIS_PUBLIC 84 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 87 #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.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
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...
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
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.