3 #ifndef INCLUDE_NN_NLIB_HEAP_NMALLOC_H_
4 #define INCLUDE_NN_NLIB_HEAP_NMALLOC_H_
17 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
18 #undef NLIB_VIS_PUBLIC
19 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
22 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_
43 #define NMALLOC_HEAPOPTION_ENABLE_ENV (0x00000001)
44 #define NMALLOC_HEAPOPTION_CACHE_DISABLE (0x00000004)
45 #define NMALLOC_HEAPOPTION_CHECK_0 (0x00000000)
46 #define NMALLOC_HEAPOPTION_CHECK_1 (0x00000008)
52 #pragma comment(linker, "/alternatename:_nmalloc_get_settings=_nmalloc_get_settings_default")
54 #pragma comment(linker, "/alternatename:nmalloc_get_settings=nmalloc_get_settings_default")
83 NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
114 template <
typename T>
115 struct NMallocStlAllocator {
116 typedef size_t size_type;
117 typedef T value_type;
119 typedef const T* const_pointer;
120 typedef T& reference;
121 typedef const T& const_reference;
122 typedef ptrdiff_t difference_type;
123 template <
typename U>
125 typedef NMallocStlAllocator<U> other;
127 NMallocStlAllocator() {}
128 explicit NMallocStlAllocator(
const NMallocStlAllocator<T>&) {}
129 template <
class _Other>
130 NMallocStlAllocator(
const NMallocStlAllocator<_Other>&) {}
131 pointer allocate(size_type n,
const void* p = 0);
132 void deallocate(pointer p, size_type n) {
140 void construct(pointer p,
const T& t) {
new ((
void*)p) T(t); }
141 void construct(pointer p) {
new ((
void*)p) T(); }
142 #if defined(NLIB_CXX11_RVALUE_REFERENCES) && defined(NLIB_CXX11_VARIADIC_TEMPLATES)
143 template<
class X,
class... ARGS>
144 void construct(X* p, ARGS&&... args) {
145 new ((
void*)p) X(std::forward<ARGS>(args)...);
148 void destroy(pointer ptr) {
157 size_t max_size()
const {
158 size_t cnt = (size_t)-1 /
sizeof(T);
159 return 0 < cnt ? cnt : 1;
161 pointer address(reference value)
const {
return &value; }
162 const_pointer address(const_reference value)
const {
return &value; }
165 template <
typename T>
166 typename NMallocStlAllocator<T>::pointer NMallocStlAllocator<T>::allocate(size_type n,
169 void* pp =
nmalloc(n *
sizeof(T));
170 return reinterpret_cast<T*
>(pp);
173 template <
class T1,
class T2>
174 inline bool operator==(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
178 template <
class T1,
class T2>
179 inline bool operator!=(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
189 #define NLIB_REPLACE_MALLOC \
191 void* nlib_malloc(size_t n) { \
194 void nlib_free(void* p) { \
197 void* nlib_calloc(size_t nmemb, size_t size) { \
198 return ncalloc(nmemb, size); \
200 void* nlib_realloc(void* ptr, size_t size) { \
201 return nrealloc(ptr, size); \
203 void* nlib_memalign(size_t alignment, size_t size) { \
204 return nmalloc_aligned(size, alignment); \
206 void nlib_free_size(void* ptr, size_t size) { \
207 nfree_size(ptr, size); \
211 #ifndef NN_PLATFORM_CTR
212 #define NLIB_REPLACE_MALLOC_NEW \
213 NLIB_REPLACE_MALLOC \
214 void* operator new(size_t size) { \
215 return nlib_malloc(size); \
217 void operator delete(void* ptr) NLIB_NOEXCEPT { \
220 void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
221 return nlib_malloc(size); \
223 void operator delete(void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { \
226 void* operator new[](size_t size) { return nlib_malloc(size); } \
227 void operator delete[](void* ptr) NLIB_NOEXCEPT { nlib_free(ptr); } \
228 void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT \
229 { return nlib_malloc(size); } \
230 void operator delete [](void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { nlib_free(ptr); } \
231 void operator delete(void* ptr, size_t size) NLIB_NOEXCEPT { \
232 nlib_free_size(ptr, size); \
234 void operator delete(void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
235 nlib_free_size(ptr, size); \
237 void operator delete[](void* ptr, size_t size)NLIB_NOEXCEPT { nlib_free_size(ptr, size); } \
238 void operator delete[](void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
239 nlib_free_size(ptr, size); \
242 #define NLIB_REPLACE_MALLOC_NEW \
243 NLIB_REPLACE_MALLOC \
244 void* operator new(size_t size) throw(std::bad_alloc) { \
245 return nlib_malloc(size); \
247 void operator delete(void* ptr) throw() { \
250 void* operator new(size_t size, const std::nothrow_t&) throw() { \
251 return nlib_malloc(size); \
253 void* operator new[](size_t size) throw(std::bad_alloc) { \
254 return nlib_malloc(size); \
256 void operator delete[](void* ptr) throw() { \
259 void* operator new[](size_t size, const std::nothrow_t&) throw() { \
260 return nlib_malloc(size); \
264 #define NLIB_REPLACE_MALLOC \
265 void* nlib_malloc(size_t n) { \
268 void nlib_free(void* p) { \
271 void* nlib_calloc(size_t nmemb, size_t size) { \
272 return ncalloc(nmemb, size); \
274 void* nlib_realloc(void* ptr, size_t size) { \
275 return nrealloc(ptr, size); \
277 void* nlib_memalign(size_t alignment, size_t size) { \
278 return nmalloc_aligned(size, alignment); \
280 void nlib_free_size(void* ptr, size_t size) { \
281 nfree_size(ptr, size); \
283 #define NLIB_REPLACE_MALLOC_NEW NLIB_REPLACE_MALLOC
286 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
287 #undef NLIB_VIS_PUBLIC
288 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
291 #endif // INCLUDE_NN_NLIB_HEAP_NMALLOC_H_
void nfree_size(void *p, size_t size)
メモリ領域を解放します。メモリのサイズ情報を利用することで高速にメモリを解放できることがあります。 ...
void * nmalloc_getobjptr(void *raw_ptr)
nmalloc_heapwalk_callbackに渡されたポインタをオブジェクトへのポインタに変換します。 ...
int(* nmalloc_heapwalk_callback)(void *allocated_ptr, size_t size, void *user_ptr)
nmalloc_walk_allocated_ptrs()から呼び出されるユーザー定義のコールバック関数です。 ...
int nmalloc_isclean(void)
ヒープの状態が初期化直後と同じかどうか確認します。
NLIB_CHECK_RESULT void * nrealloc(void *ptr, size_t size)
メモリの割り当てを変更します。realloc()C標準関数に相当します。
void nmalloc_clear_tls(void)
実行中のスレッドにキャッシュされている割り当て可能なメモリを中央ヒープに返します。 ...
void nmalloc_heaphash(HeapHash *hash)
ユーザーによるメモリ確保状況を調べてサマリを作成します。
errno_t nmalloc_setmark1(void *p, uint16_t mark1)
アロケートされたメモリに情報を付加します。
size_t nmalloc_size(const void *ptr)
ptrに実際に割り当てられたメモリ量を返します。
size_t alloc_size
ヒープ内でユーザーによってアロケートされた領域のサイズの合計です。
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
2つのサマリを比較して等価ならば、trueを返します。
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
2つのサマリを比較して等価でなければ、trueを返します。
NLIB_CHECK_RESULT void * ncalloc(size_t nmemb, size_t size)
0に初期化される要素とメモリの配列を割り当てます。
void nfree(void *p)
メモリ領域を解放します。free()C標準関数に相当します。
void nmalloc_dump(void)
ヒープの基本的な状況をダンプします。
unsigned int heap_option
ヒープオプションを指定します。デフォルトは0です。
size_t alloc_count
ヒープ内でユーザーによってアロケートされた領域の数です。
ユーザーによって確保されている全てのメモリのアドレスとサイズを表示します。
void nmalloc_dumpex(NMallocDumpMode mode)
ヒープの状況をダンプします。
size_t size
nmallocが利用する最大のメモリサイズを指定します。4096バイトの倍数を指定する必要があります。 ...
void nmalloc_finalize_tls(void)
実行中のスレッド固有のメモリを全て中央ヒープに返却します。スレッドを終了させるときに呼び出す必要があ...
nmallocの初期設定を行うパラメータを記述します。nmalloc_get_settings()を定義して設定します。 ...
size_t hash
ヒープ内のユーザーによるメモリ確保の状況をハッシュ値にしたものです。
errno_t nmalloc_getmark(const void *ptr, uint16_t *mark1, uint16_t *mark2)
アロケートされたメモリに付加された情報を取得します。
NLIB_CHECK_RESULT void * nmalloc(size_t size)
指定バイト分のメモリ領域を確保します。malloc()C標準関数に相当します。
void * addr
nmallocが利用する領域の先頭へのポインタを指定します。
void nmalloc_get_settings(NMallocSettings *settings)
ユーザーがこの関数を定義することでnmallocの初期化設定をコントロールすることができます。 ...
errno_t nmalloc_setmark2(void *p, uint16_t mark2)
アロケートされたメモリに情報を付加します。
errno_t nmalloc_walk_allocated_ptrs(nmalloc_heapwalk_callback func, void *user_ptr)
ヒープにアロケートされた領域1つずつに対してコールバック関数func を呼び出します。
void nmalloc_dumpex2(NMallocDumpMode mode, nlib_fd fd)
ヒープの状況をダンプします。
NMallocDumpMode
nmalloc_dumpex()関数に渡す引数の型です。
ユーザーが利用しているヒープ内のメモリの利用状況のサマリが記述される構造体です。
NLIB_CHECK_RESULT void * nmalloc_aligned(size_t size, size_t algn)
アライメントを指定してメモリ領域を確保します。