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)
Frees a memory region. Using information about memory sizes makes it possible to free memory quickly...
void * nmalloc_getobjptr(void *raw_ptr)
Transforms the pointer passed to nmalloc_heapwalk_callback into an object pointer.
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.
int nmalloc_isclean(void)
Checks whether the heap status is the same as it was immediately following initialization.
NLIB_CHECK_RESULT void * nrealloc(void *ptr, size_t size)
Changes the memory allocation. Corresponds to the standard C function realloc.
void nmalloc_clear_tls(void)
Returns allocatable memory that is cached for a running thread to the CentralHeap.
void nmalloc_heaphash(HeapHash *hash)
Looks up the memory allocation status by the user application and creates a summary.
Displays all information.
errno_t nmalloc_setmark1(void *p, uint16_t mark1)
Adds data to allocated memory.
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.
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.
void nmalloc_dump(void)
Dumps the basic heap status.
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. ...
void nmalloc_dumpex(NMallocDumpMode mode)
Dumps the heap status.
size_t size
Specifies the maximum amount of memory that can be used by nmalloc. You must specify a multiple of 40...
void nmalloc_finalize_tls(void)
Returns all thread-specific memory for a running thread to the central heap. You must call this when ...
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.
errno_t nmalloc_getmark(const void *ptr, uint16_t *mark1, uint16_t *mark2)
Gets data that was added to allocated memory.
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...
errno_t nmalloc_setmark2(void *p, uint16_t mark2)
Adds data to allocated memory.
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.
void nmalloc_dumpex2(NMallocDumpMode mode, nlib_fd fd)
Dumps the heap status.
NMallocDumpMode
Type of the argument to pass to nmalloc_dumpex.
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.