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 { m_Cur = m_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(m_Cur, algn);
28 uint8_t* newTop = ret + n;
29 if (newTop > m_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 >= m_Start && ptr < m_Cur) {
43 void Dump() NLIB_NOEXCEPT;
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)));
52 m_Start =
reinterpret_cast<uint8_t*
>(start);
53 m_End =
reinterpret_cast<uint8_t*
>(end);
59 uint32_t m_HeapOption;
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_
スタックに積み上げるようにメモリを確保していくアロケータです。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
void * Alloc(size_t n) noexcept
メモリブロックを確保します。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
void * Alloc(size_t n, size_t algn) noexcept
アライメントを指定してメモリブロックを確保します。
errno_t Free(void *p) noexcept
次のメモリ確保位置をp の位置まで戻します。
void FreeAll() noexcept
メモリを全て解放し、ヒープを初期化直後の状態に戻します。