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 33 void FreeAll() NLIB_NOEXCEPT { cur_ = start_; }
37 void*
Alloc(
size_t n,
size_t algn) NLIB_NOEXCEPT {
38 if (n == 0)
return NULL;
39 if (algn == 0 || 0 != (algn & (algn - 1)))
return NULL;
40 uint8_t* ret = this->RoundUp(cur_, algn);
41 uint8_t* new_top = ret + n;
42 if (new_top > end_)
return NULL;
46 void*
Alloc(
size_t n) NLIB_NOEXCEPT {
return this->
Alloc(n, 8); }
48 uint8_t* ptr =
reinterpret_cast<uint8_t*
>(p);
49 if (ptr >= start_ && ptr < cur_) {
59 static uint8_t* RoundUp(uint8_t* ptr,
size_t algn) NLIB_NOEXCEPT {
61 intptr_t tmp =
reinterpret_cast<intptr_t
>(ptr);
62 return reinterpret_cast<uint8_t*
>(tmp + (-tmp & (algn - 1)));
64 void ChangeHeapRange(
void* start,
void* end) NLIB_NOEXCEPT {
65 start_ =
reinterpret_cast<uint8_t*
>(start);
66 end_ =
reinterpret_cast<uint8_t*
>(end);
72 uint32_t heap_option_;
80 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 81 #undef NLIB_VIS_PUBLIC 82 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 85 #endif // INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_ スタックに積み上げるようにメモリを確保していくアロケータです。
void * Alloc(size_t n) noexcept
メモリブロックを確保します。
void * Alloc(size_t n, size_t algn) noexcept
アライメントを指定してメモリブロックを確保します。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
errno_t Free(void *p) noexcept
次のメモリ確保位置をp の位置まで戻します。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
void FreeAll() noexcept
メモリを全て解放し、ヒープを初期化直後の状態に戻します。