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) 63 #pragma comment(linker, "/alternatename:_nmalloc_get_settings=_nmalloc_get_settings_default") 65 #pragma comment(linker, "/alternatename:nmalloc_get_settings=nmalloc_get_settings_default") 85 kNmallocQueryDump = 0,
86 kNmallocQueryPageSize,
87 kNmallocQueryAllocatedSize,
88 kNmallocQueryFreeSize,
89 kNmallocQuerySystemSize,
90 kNmallocQueryMaxAllocatableSize,
92 kNmallocQueryHeapHash,
93 kNmallocQueryUnifyFreelist,
94 kNmallocQuerySetColor,
95 kNmallocQueryGetColor,
98 kNmallocQueryCacheMin_,
99 kNmallocQueryCheckCache,
100 kNmallocQueryClearCache,
101 kNmallocQueryFinalizeCache,
102 kNmallocQueryFreeSizeNx,
103 kNmallocQueryMaxAllocatableSizeNx,
104 NMALLOC_QUERY_DUMP = kNmallocQueryDump,
105 NMALLOC_QUERY_PAGE_SIZE = kNmallocQueryPageSize,
106 NMALLOC_QUERY_ALLOCATED_SIZE = kNmallocQueryAllocatedSize,
107 NMALLOC_QUERY_FREE_SIZE = kNmallocQueryFreeSize,
108 NMALLOC_QUERY_SYSTEM_SIZE = kNmallocQuerySystemSize,
109 NMALLOC_QUERY_MAX_ALLOCATABLE_SIZE = kNmallocQueryMaxAllocatableSize,
110 NMALLOC_QUERY_IS_CLEAN = kNmallocQueryIsClean,
111 NMALLOC_QUERY_HEAP_HASH = kNmallocQueryHeapHash,
112 NMALLOC_QUERY_UNIFY_FREELIST = kNmallocQueryUnifyFreelist,
113 NMALLOC_QUERY_SET_COLOR = kNmallocQuerySetColor,
114 NMALLOC_QUERY_GET_COLOR = kNmallocQueryGetColor,
115 NMALLOC_QUERY_SET_NAME = kNmallocQuerySetName,
116 NMALLOC_QUERY_GET_NAME = kNmallocQueryGetName,
117 NMALLOC_QUERY_CACHE_MIN_ = kNmallocQueryCacheMin_,
118 NMALLOC_QUERY_CHECK_CACHE = kNmallocQueryCheckCache,
119 NMALLOC_QUERY_CLEAR_CACHE = kNmallocQueryClearCache,
120 NMALLOC_QUERY_FINALIZE_CACHE = kNmallocQueryFinalizeCache,
121 NMALLOC_QUERY_FREE_SIZE_NX = kNmallocQueryFreeSizeNx,
122 NMALLOC_QUERY_MAX_ALLOCATABLE_SIZE_NX = kNmallocQueryMaxAllocatableSizeNx
136 NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
162 template <
typename T>
163 struct NMallocStlAllocator {
164 typedef size_t size_type;
165 typedef T value_type;
167 typedef const T* const_pointer;
168 typedef T& reference;
169 typedef const T& const_reference;
170 typedef ptrdiff_t difference_type;
171 template <
typename U>
173 typedef NMallocStlAllocator<U> other;
175 NMallocStlAllocator() {}
176 explicit NMallocStlAllocator(
const NMallocStlAllocator<T>&) {}
177 template <
class _Other>
178 NMallocStlAllocator(
const NMallocStlAllocator<_Other>&) {}
179 pointer allocate(size_type n,
const void* p = 0);
180 void deallocate(pointer p, size_type n) {
188 void construct(pointer p,
const T& t) {
new ((
void*)p) T(t); }
189 void construct(pointer p) {
new ((
void*)p) T(); }
190 #if defined(__cpp_rvalue_references) && defined(__cpp_variadic_templates) 191 template<
class X,
class... ARGS>
192 void construct(X* p, ARGS&&... args) {
193 new ((
void*)p) X(std::forward<ARGS>(args)...);
196 void destroy(pointer ptr) {
205 size_t max_size()
const {
206 size_t cnt = (size_t)-1 /
sizeof(T);
207 return 0 < cnt ? cnt : 1;
209 pointer address(reference value)
const {
return &value; }
210 const_pointer address(const_reference value)
const {
return &value; }
213 template <
typename T>
214 typename NMallocStlAllocator<T>::pointer NMallocStlAllocator<T>::allocate(size_type n,
217 void* pp =
nmalloc(n *
sizeof(T));
218 return reinterpret_cast<T*
>(pp);
221 template <
class T1,
class T2>
222 inline bool operator==(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
226 template <
class T1,
class T2>
227 inline bool operator!=(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
237 #define NLIB_REPLACE_MALLOC \ 239 void* nlib_malloc(size_t n) { \ 242 void nlib_free(void* p) { \ 245 void* nlib_calloc(size_t nmemb, size_t size) { \ 246 return ncalloc(nmemb, size); \ 248 void* nlib_realloc(void* ptr, size_t size) { \ 249 return nrealloc(ptr, size); \ 251 void* nlib_memalign(size_t alignment, size_t size) { \ 252 return nmalloc_aligned(size, alignment); \ 254 void nlib_free_size(void* ptr, size_t size) { \ 255 nfree_size(ptr, size); \ 259 #ifndef NN_PLATFORM_CTR 260 #define NLIB_REPLACE_MALLOC_NEW \ 261 NLIB_REPLACE_MALLOC \ 262 void* operator new(size_t size) { \ 263 return nlib_malloc(size); \ 265 void operator delete(void* ptr) NLIB_NOEXCEPT { \ 268 void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 269 return nlib_malloc(size); \ 271 void operator delete(void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { \ 274 void* operator new[](size_t size) { return nlib_malloc(size); } \ 275 void operator delete[](void* ptr) NLIB_NOEXCEPT { nlib_free(ptr); } \ 276 void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT \ 277 { return nlib_malloc(size); } \ 278 void operator delete [](void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { nlib_free(ptr); } \ 279 void operator delete(void* ptr, size_t size) NLIB_NOEXCEPT { \ 280 nlib_free_size(ptr, size); \ 282 void operator delete(void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 283 nlib_free_size(ptr, size); \ 285 void operator delete[](void* ptr, size_t size)NLIB_NOEXCEPT { nlib_free_size(ptr, size); } \ 286 void operator delete[](void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 287 nlib_free_size(ptr, size); \ 290 #define NLIB_REPLACE_MALLOC_NEW \ 291 NLIB_REPLACE_MALLOC \ 292 void* operator new(size_t size) throw(std::bad_alloc) { \ 293 return nlib_malloc(size); \ 295 void operator delete(void* ptr) throw() { \ 298 void* operator new(size_t size, const std::nothrow_t&) throw() { \ 299 return nlib_malloc(size); \ 301 void* operator new[](size_t size) throw(std::bad_alloc) { \ 302 return nlib_malloc(size); \ 304 void operator delete[](void* ptr) throw() { \ 307 void* operator new[](size_t size, const std::nothrow_t&) throw() { \ 308 return nlib_malloc(size); \ 312 #define NLIB_REPLACE_MALLOC \ 313 void* nlib_malloc(size_t n) { \ 316 void nlib_free(void* p) { \ 319 void* nlib_calloc(size_t nmemb, size_t size) { \ 320 return ncalloc(nmemb, size); \ 322 void* nlib_realloc(void* ptr, size_t size) { \ 323 return nrealloc(ptr, size); \ 325 void* nlib_memalign(size_t alignment, size_t size) { \ 326 return nmalloc_aligned(size, alignment); \ 328 void nlib_free_size(void* ptr, size_t size) { \ 329 nfree_size(ptr, size); \ 331 #define NLIB_REPLACE_MALLOC_NEW NLIB_REPLACE_MALLOC 334 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 335 #undef NLIB_VIS_PUBLIC 336 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 339 #endif // INCLUDE_NN_NLIB_HEAP_NMALLOC_H_ void nfree_size(void *p, size_t size)
Frees a memory region. Using information about memory sizes makes it possible to free memory quickly...
int(* nmalloc_heapwalk_callback)(void *allocated_ptr, size_t size, void *user_ptr)
User-defined callback function that is called from nmalloc_walk_allocated_ptrs.
Only displays basic information.
void * nrealloc(void *ptr, size_t size)
Changes the memory allocation. Corresponds to the standard C function realloc.
Displays all information.
size_t nmalloc_size(const void *ptr)
Returns the amount of memory actually allocated at the ptr parameter.
size_t alloc_size
Total size of the regions allocated by the user application within the heap.
Displays the addresses and sizes of all memory allocated by the user application. ...
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
errno_t nmalloc_query(int query,...)
Gets and operates detailed data on the heap.
void * ncalloc(size_t nmemb, size_t size)
Allocates an array of memory and elements initialized to 0.
void nfree(void *p)
Frees a memory region. Corresponds to the standard C function free.
unsigned int heap_option
Specifies the heap options. The default is 0.
size_t alloc_count
Number of regions allocated by the user application within the heap.
size_t size
Specifies the maximum amount of memory that can be used by nmalloc. You must specify a multiple of 40...
Declares parameters that are initialized by nmalloc. Set by defining nmalloc_get_settings.
size_t hash
Hash value of the status of memory allocated by the user application within the heap.
void * nmalloc(size_t size)
Allocates a memory region of the specified size (in bytes). This corresponds to the standard C functi...
Displays the usage status for each page.
A file that contains the configuration information for each development environment.
Visually displays the overview of the page allocation density in the entire heap. An area of 1 MB is ...
void * addr
Specifies a pointer to the beginning of the region used by nmalloc.
void nmalloc_get_settings(NMallocSettings *settings)
User applications can define this function to control the initialization settings of nmalloc...
errno_t nmalloc_walk_allocated_ptrs(nmalloc_heapwalk_callback func, void *user_ptr)
The callback function func is called for each region allocated in the heap.
NMallocDumpMode
The type of the value that you specify for the second argument when specifying kNmallocQueryDump in t...
Structure that contains a summary of the memory usage status of the heap used by the user application...
void * nmalloc_aligned(size_t size, size_t algn)
Allocates memory with a specific alignment.