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) 50 #pragma comment(linker, "/alternatename:_nmalloc_get_settings=_nmalloc_get_settings_default") 52 #pragma comment(linker, "/alternatename:nmalloc_get_settings=nmalloc_get_settings_default") 67 NMALLOC_QUERY_DUMP = 0,
68 NMALLOC_QUERY_PAGE_SIZE,
69 NMALLOC_QUERY_ALLOCATED_SIZE,
70 NMALLOC_QUERY_FREE_SIZE,
71 NMALLOC_QUERY_SYSTEM_SIZE,
72 NMALLOC_QUERY_MAX_ALLOCATABLE_SIZE,
73 NMALLOC_QUERY_IS_CLEAN,
74 NMALLOC_QUERY_HEAP_HASH,
75 NMALLOC_QUERY_UNIFY_FREELIST,
76 NMALLOC_QUERY_SET_COLOR,
77 NMALLOC_QUERY_GET_COLOR,
78 NMALLOC_QUERY_SET_NAME,
79 NMALLOC_QUERY_GET_NAME,
80 NMALLOC_QUERY_CACHE_MIN_,
81 NMALLOC_QUERY_CHECK_CACHE,
82 NMALLOC_QUERY_CLEAR_CACHE,
83 NMALLOC_QUERY_FINALIZE_CACHE
97 NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
123 template <
typename T>
124 struct NMallocStlAllocator {
125 typedef size_t size_type;
126 typedef T value_type;
128 typedef const T* const_pointer;
129 typedef T& reference;
130 typedef const T& const_reference;
131 typedef ptrdiff_t difference_type;
132 template <
typename U>
134 typedef NMallocStlAllocator<U> other;
136 NMallocStlAllocator() {}
137 explicit NMallocStlAllocator(
const NMallocStlAllocator<T>&) {}
138 template <
class _Other>
139 NMallocStlAllocator(
const NMallocStlAllocator<_Other>&) {}
140 pointer allocate(size_type n,
const void* p = 0);
141 void deallocate(pointer p, size_type n) {
149 void construct(pointer p,
const T& t) {
new ((
void*)p) T(t); }
150 void construct(pointer p) {
new ((
void*)p) T(); }
151 #if defined(NLIB_CXX11_RVALUE_REFERENCES) && defined(NLIB_CXX11_VARIADIC_TEMPLATES) 152 template<
class X,
class... ARGS>
153 void construct(X* p, ARGS&&... args) {
154 new ((
void*)p) X(std::forward<ARGS>(args)...);
157 void destroy(pointer ptr) {
166 size_t max_size()
const {
167 size_t cnt = (size_t)-1 /
sizeof(T);
168 return 0 < cnt ? cnt : 1;
170 pointer address(reference value)
const {
return &value; }
171 const_pointer address(const_reference value)
const {
return &value; }
174 template <
typename T>
175 typename NMallocStlAllocator<T>::pointer NMallocStlAllocator<T>::allocate(size_type n,
178 void* pp =
nmalloc(n *
sizeof(T));
179 return reinterpret_cast<T*
>(pp);
182 template <
class T1,
class T2>
183 inline bool operator==(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
187 template <
class T1,
class T2>
188 inline bool operator!=(
const NMallocStlAllocator<T1>&,
const NMallocStlAllocator<T2>&) {
198 #define NLIB_REPLACE_MALLOC \ 200 void* nlib_malloc(size_t n) { \ 203 void nlib_free(void* p) { \ 206 void* nlib_calloc(size_t nmemb, size_t size) { \ 207 return ncalloc(nmemb, size); \ 209 void* nlib_realloc(void* ptr, size_t size) { \ 210 return nrealloc(ptr, size); \ 212 void* nlib_memalign(size_t alignment, size_t size) { \ 213 return nmalloc_aligned(size, alignment); \ 215 void nlib_free_size(void* ptr, size_t size) { \ 216 nfree_size(ptr, size); \ 220 #ifndef NN_PLATFORM_CTR 221 #define NLIB_REPLACE_MALLOC_NEW \ 222 NLIB_REPLACE_MALLOC \ 223 void* operator new(size_t size) { \ 224 return nlib_malloc(size); \ 226 void operator delete(void* ptr) NLIB_NOEXCEPT { \ 229 void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 230 return nlib_malloc(size); \ 232 void operator delete(void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { \ 235 void* operator new[](size_t size) { return nlib_malloc(size); } \ 236 void operator delete[](void* ptr) NLIB_NOEXCEPT { nlib_free(ptr); } \ 237 void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT \ 238 { return nlib_malloc(size); } \ 239 void operator delete [](void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { nlib_free(ptr); } \ 240 void operator delete(void* ptr, size_t size) NLIB_NOEXCEPT { \ 241 nlib_free_size(ptr, size); \ 243 void operator delete(void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 244 nlib_free_size(ptr, size); \ 246 void operator delete[](void* ptr, size_t size)NLIB_NOEXCEPT { nlib_free_size(ptr, size); } \ 247 void operator delete[](void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \ 248 nlib_free_size(ptr, size); \ 251 #define NLIB_REPLACE_MALLOC_NEW \ 252 NLIB_REPLACE_MALLOC \ 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); \ 262 void* operator new[](size_t size) throw(std::bad_alloc) { \ 263 return nlib_malloc(size); \ 265 void operator delete[](void* ptr) throw() { \ 268 void* operator new[](size_t size, const std::nothrow_t&) throw() { \ 269 return nlib_malloc(size); \ 273 #define NLIB_REPLACE_MALLOC \ 274 void* nlib_malloc(size_t n) { \ 277 void nlib_free(void* p) { \ 280 void* nlib_calloc(size_t nmemb, size_t size) { \ 281 return ncalloc(nmemb, size); \ 283 void* nlib_realloc(void* ptr, size_t size) { \ 284 return nrealloc(ptr, size); \ 286 void* nlib_memalign(size_t alignment, size_t size) { \ 287 return nmalloc_aligned(size, alignment); \ 289 void nlib_free_size(void* ptr, size_t size) { \ 290 nfree_size(ptr, size); \ 292 #define NLIB_REPLACE_MALLOC_NEW NLIB_REPLACE_MALLOC 295 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS) 296 #undef NLIB_VIS_PUBLIC 297 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 300 #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.
NLIB_CHECK_RESULT 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.
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.
Visually displays the overview of the page allocation density in the entire heap. An area of 1 MB is ...
errno_t nmalloc_query(int query,...)
Gets and operates detailed data on the heap.
NLIB_CHECK_RESULT 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.
Displays the addresses and sizes of all memory allocated by the user application. ...
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.
NLIB_CHECK_RESULT void * nmalloc(size_t size)
Allocates a memory region of the specified size (in bytes). This corresponds to the standard C functi...
Only displays basic information.
A file that contains the configuration information for each development environment.
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...
Displays the usage status for each page.
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 NMALLOC_QUERY_DUMP in ...
Structure that contains a summary of the memory usage status of the heap used by the user application...
NLIB_CHECK_RESULT void * nmalloc_aligned(size_t size, size_t algn)
Allocates memory with a specific alignment.