16 #ifndef INCLUDE_NN_NLIB_HEAP_NMALLOC_H_ 17 #define INCLUDE_NN_NLIB_HEAP_NMALLOC_H_ 30 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 31 #undef NLIB_VIS_PUBLIC 32 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 35 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_ 56 #define NMALLOC_HEAPOPTION_ENABLE_ENV (0x00000001) 57 #define NMALLOC_HEAPOPTION_CACHE_DISABLE (0x00000004) 64 #pragma comment(linker, "/alternatename:_nmalloc_get_settings=_nmalloc_get_settings_default") 66 #pragma comment(linker, "/alternatename:nmalloc_get_settings=nmalloc_get_settings_default") 84 kNmallocQueryDump = 0,
85 kNmallocQueryPageSize,
86 kNmallocQueryAllocatedSize,
87 kNmallocQueryFreeSize,
88 kNmallocQuerySystemSize,
89 kNmallocQueryMaxAllocatableSize,
91 kNmallocQueryHeapHash,
92 kNmallocQueryUnifyFreelist,
93 kNmallocQuerySetColor,
94 kNmallocQueryGetColor,
97 kNmallocQueryCacheMin_,
98 kNmallocQueryCheckCache,
99 kNmallocQueryClearCache,
100 kNmallocQueryFinalizeCache,
101 kNmallocQueryFreeSizeNx,
102 kNmallocQueryMaxAllocatableSizeNx,
103 kNmallocQueryDumpJson
110 NLIB_ATTRIBUTE_ALLOC_SIZE1(2) NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
112 NLIB_ATTRIBUTE_ALLOC_SIZE1(1) NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
114 NLIB_ATTRIBUTE_ALLOC_SIZE2(1, 2) NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
117 NLIB_ATTRIBUTE_ALLOC_ALIGN(2) NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
144 struct NMallocStlAllocator {
145 typedef size_t size_type;
146 typedef T value_type;
148 typedef const T* const_pointer;
149 typedef T& reference;
150 typedef const T& const_reference;
151 typedef ptrdiff_t difference_type;
154 typedef NMallocStlAllocator<U> other;
156 NMallocStlAllocator() {}
157 explicit NMallocStlAllocator(
const NMallocStlAllocator<T>&) {}
158 template<
class _Other>
159 NMallocStlAllocator(
const NMallocStlAllocator<_Other>&) {}
160 pointer allocate(size_type n,
const void* p = 0);
161 void deallocate(pointer p, size_type n) {
169 void construct(pointer p,
const T& t) {
new ((
void*)p) T(t); }
170 void construct(pointer p) {
new ((
void*)p) T(); }
171 #if defined(__cpp_rvalue_references) && defined(__cpp_variadic_templates) 172 template<
class X,
class... ARGS>
173 void construct(X* p, ARGS&&... args) {
174 new ((
void*)p) X(std::forward<ARGS>(args)...);
177 void destroy(pointer ptr) {
186 size_t max_size()
const {
187 size_t cnt = (size_t)-1 /
sizeof(T);
188 return 0 < cnt ? cnt : 1;
190 pointer address(reference value)
const {
return &value; }
191 const_pointer address(const_reference value)
const {
return &value; }
195 typename NMallocStlAllocator<T>::pointer
196 NMallocStlAllocator<T>::allocate(size_type n,
const void* p) {
198 void* pp =
nmalloc(n *
sizeof(T));
199 return reinterpret_cast<T*
>(pp);
202 template<
class T1,
class T2>
203 inline bool operator==(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
207 template<
class T1,
class T2>
208 inline bool operator!=(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
218 #define NLIB_REPLACE_MALLOC \ 220 void* nlib_malloc(size_t n) { return nmalloc(n); } \ 221 void nlib_free(void* p) { nfree(p); } \ 222 void* nlib_calloc(size_t nmemb, size_t size) { return ncalloc(nmemb, size); } \ 223 void* nlib_realloc(void* ptr, size_t size) { return nrealloc(ptr, size); } \ 224 void* nlib_memalign(size_t alignment, size_t size) { \ 225 return nmalloc_aligned(size, alignment); \ 227 void nlib_free_size(void* ptr, size_t size) { nfree_size(ptr, size); } \ 230 #ifndef NN_PLATFORM_CTR 231 #define NLIB_REPLACE_MALLOC_NEW \ 232 NLIB_REPLACE_MALLOC \ 233 void* operator new(size_t size) { return nlib_malloc(size); } \ 234 void operator delete(void* ptr)NLIB_NOEXCEPT { nlib_free(ptr); } \ 235 void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 236 return nlib_malloc(size); \ 238 void operator delete(void* ptr, const std::nothrow_t&)NLIB_NOEXCEPT { nlib_free(ptr); } \ 239 void* operator new[](size_t size) { return nlib_malloc(size); } \ 240 void operator delete[](void* ptr) NLIB_NOEXCEPT { nlib_free(ptr); } \ 241 void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 242 return nlib_malloc(size); \ 244 void operator delete[](void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { nlib_free(ptr); } \ 245 void operator delete(void* ptr, size_t size)NLIB_NOEXCEPT { nlib_free_size(ptr, size); } \ 246 void operator delete(void* ptr, size_t size, const std::nothrow_t&)NLIB_NOEXCEPT { \ 247 nlib_free_size(ptr, size); \ 249 void operator delete[](void* ptr, size_t size) NLIB_NOEXCEPT { nlib_free_size(ptr, size); } \ 250 void operator delete[](void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 251 nlib_free_size(ptr, size); \ 254 #define NLIB_REPLACE_MALLOC_NEW \ 255 NLIB_REPLACE_MALLOC \ 256 void* operator new(size_t size) throw(std::bad_alloc) { return nlib_malloc(size); } \ 257 void operator delete(void* ptr) throw() { nlib_free(ptr); } \ 258 void* operator new(size_t size, const std::nothrow_t&) throw() { return nlib_malloc(size); } \ 259 void* operator new[](size_t size) throw(std::bad_alloc) { return nlib_malloc(size); } \ 260 void operator delete[](void* ptr) throw() { nlib_free(ptr); } \ 261 void* operator new[](size_t size, const std::nothrow_t&) throw() { return nlib_malloc(size); } 264 #define NLIB_REPLACE_MALLOC \ 265 void* nlib_malloc(size_t n) { return nmalloc(n); } \ 266 void nlib_free(void* p) { nfree(p); } \ 267 void* nlib_calloc(size_t nmemb, size_t size) { return ncalloc(nmemb, size); } \ 268 void* nlib_realloc(void* ptr, size_t size) { return nrealloc(ptr, size); } \ 269 void* nlib_memalign(size_t alignment, size_t size) { \ 270 return nmalloc_aligned(size, alignment); \ 272 void nlib_free_size(void* ptr, size_t size) { nfree_size(ptr, size); } 273 #define NLIB_REPLACE_MALLOC_NEW NLIB_REPLACE_MALLOC 277 #define NMALLOC_GET_SETTINGS \ 278 void nmalloc_get_settings(NMallocSettings*); \ 279 void __stdcall _nlib_tls_callback_heap(void* h, DWORD dwReason, void* r) { \ 282 static __declspec(thread) int nlib_dummy_nmalloc; \ 283 if (dwReason == DLL_PROCESS_ATTACH) { \ 284 extern void (*g_nmalloc_get_settings_func)(NMallocSettings*); \ 285 nlib_dummy_nmalloc = 1; \ 286 g_nmalloc_get_settings_func = nmalloc_get_settings; \ 289 __pragma(section(".CRT$XLB", long, read)) __declspec(allocate(".CRT$XLB")) \ 290 PIMAGE_TLS_CALLBACK _xl_b = _nlib_tls_callback_heap; \ 291 void nmalloc_get_settings(NMallocSettings* settings) 294 #define NMALLOC_GET_SETTINGS extern "C" void nmalloc_get_settings(NMallocSettings* settings) 296 #define NMALLOC_GET_SETTINGS void nmalloc_get_settings(NMallocSettings* settings) 300 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 301 #undef NLIB_VIS_PUBLIC 302 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 305 #endif // INCLUDE_NN_NLIB_HEAP_NMALLOC_H_ void nfree_size(void *p, size_t size)
メモリ領域を解放します。メモリのサイズ情報を利用することで高速にメモリを解放できることがあります。 ...
int(* nmalloc_heapwalk_callback)(void *allocated_ptr, size_t size, void *user_ptr)
nmalloc_walk_allocated_ptrs()から呼び出されるユーザー定義のコールバック関数です。 ...
void * nrealloc(void *ptr, size_t size)
メモリの割り当てを変更します。realloc()C標準関数に相当します。
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を返します。
errno_t nmalloc_query(int query,...)
ヒープに関する詳細なデータの取得や操作を行います。
void * ncalloc(size_t nmemb, size_t size)
0に初期化される要素とメモリの配列を割り当てます。
void nfree(void *p)
メモリ領域を解放します。free()C標準関数に相当します。
unsigned int heap_option
ヒープオプションを指定します。デフォルトは0です。
size_t alloc_count
ヒープ内でユーザーによってアロケートされた領域の数です。
size_t size
nmallocが利用する最大のメモリサイズを指定します。4096バイトの倍数を指定する必要があります。 ...
nmallocの初期設定を行うパラメータを記述します。nmalloc_get_settings()を定義して設定します。 ...
size_t hash
ヒープ内のユーザーによるメモリ確保の状況をハッシュ値にしたものです。
void * nmalloc(size_t size)
指定バイト分のメモリ領域を確保します。malloc()C標準関数に相当します。
ヒープ全体のページの割当密度の概要を視覚的に表示します。1MBを1つのASCIIキャラクタで表現しています。 ...
void * addr
nmallocが利用する領域の先頭へのポインタを指定します。
void nmalloc_get_settings(NMallocSettings *settings)
ユーザーがこの関数を定義することでnmallocの初期化設定をコントロールすることができます。 ...
errno_t nmalloc_walk_allocated_ptrs(nmalloc_heapwalk_callback func, void *user_ptr)
ヒープにアロケートされた領域1つずつに対してコールバック関数func を呼び出します。
NMallocDumpMode
nmalloc_query()関数でkNmallocQueryDumpを指定した場合に第2引数に指定する値の型です。 ...
ユーザーが利用しているヒープ内のメモリの利用状況のサマリが記述される構造体です。
void * nmalloc_aligned(size_t size, size_t algn)
アライメントを指定してメモリ領域を確保します。