nlib
NMalloc.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_HEAP_NMALLOC_H_
4 #define INCLUDE_NN_NLIB_HEAP_NMALLOC_H_
5 
6 #include <errno.h>
7 #include <stddef.h>
8 #include <stdlib.h> // for malloc/free
9 
10 #ifdef __cplusplus
11 #include <new>
12 #include "nn/nlib/Config.h"
13 #else
14 #include "nn/nlib/Platform.h"
15 #endif
16 
17 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
18 #undef NLIB_VIS_PUBLIC
19 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
20 #endif
21 
22 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_
23 // some compilers complain about double declaration
24 typedef int errno_t;
25 #endif
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 typedef struct NLIB_VIS_PUBLIC {
32  size_t alloc_count;
33  size_t alloc_size;
34  size_t hash;
35 } HeapHash;
36 
37 typedef struct NLIB_VIS_PUBLIC {
38  void* addr;
39  size_t size;
40  unsigned int heap_option;
42 
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)
47 
48 #ifdef _MSC_VER
49 void nmalloc_get_settings(NMallocSettings* settings); // weak function
50 NLIB_VIS_PUBLIC void nmalloc_get_settings_default(NMallocSettings* settings);
51 #ifndef _WIN64
52 #pragma comment(linker, "/alternatename:_nmalloc_get_settings=_nmalloc_get_settings_default")
53 #else
54 #pragma comment(linker, "/alternatename:nmalloc_get_settings=nmalloc_get_settings_default")
55 #endif
56 #else
57 NLIB_VIS_PUBLIC void nmalloc_get_settings(NMallocSettings* settings); // weak function
58 #endif
59 
60 typedef enum {
66 
67 typedef int (*nmalloc_heapwalk_callback)(void* allocated_ptr, size_t size, void* user_ptr);
68 
72 NLIB_VIS_PUBLIC void nmalloc_dump(void);
75 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT void* nrealloc(void* ptr, size_t size)
76 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE1(2) NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
77 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT void* nmalloc(size_t size)
78 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE1(1) NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
79 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT void* ncalloc(size_t nmemb, size_t size)
80 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE2(1, 2) NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
81 NLIB_VIS_PUBLIC NLIB_CHECK_RESULT void* nmalloc_aligned(size_t size, size_t algn)
82 NLIB_ATTRIBUTE_MALLOC NLIB_ATTRIBUTE_ALLOC_SIZE1(1) NLIB_ATTRIBUTE_ALLOC_ALIGN(2)
83 NLIB_ATTRIBUTE_ASSUME_ALIGNED(8);
84 NLIB_VIS_PUBLIC size_t nmalloc_size(const void* ptr);
85 NLIB_VIS_PUBLIC void nfree(void* p);
86 NLIB_VIS_PUBLIC void nfree_size(void* p, size_t size);
88 
90 NLIB_VIS_PUBLIC errno_t nmalloc_setmark1(void* p, uint16_t mark1);
91 NLIB_VIS_PUBLIC errno_t nmalloc_setmark2(void* p, uint16_t mark2);
92 NLIB_VIS_PUBLIC errno_t nmalloc_getmark(const void* ptr, uint16_t* mark1, uint16_t* mark2);
93 NLIB_VIS_PUBLIC void* nmalloc_getobjptr(void* raw_ptr);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 #ifdef __cplusplus
100 
101 inline bool operator==(const HeapHash& rhs, const HeapHash& lhs) {
102  return rhs.alloc_count == lhs.alloc_count && rhs.alloc_size == lhs.alloc_size &&
103  rhs.hash == lhs.hash;
104 }
105 
106 inline bool operator!=(const HeapHash& rhs, const HeapHash& lhs) {
107  return rhs.alloc_count != lhs.alloc_count || rhs.alloc_size != lhs.alloc_size ||
108  rhs.hash != lhs.hash;
109 }
110 
111 NLIB_NAMESPACE_BEGIN
112 namespace heap {
113 
114 template <typename T>
115 struct NMallocStlAllocator {
116  typedef size_t size_type;
117  typedef T value_type;
118  typedef T* pointer;
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>
124  struct rebind {
125  typedef NMallocStlAllocator<U> other;
126  };
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) {
133 #if 1
134  nfree_size(p, n * sizeof(T));
135 #else
136  (void)n;
137  nfree(p);
138 #endif
139  }
140  void construct(pointer p, const T& t) { new ((void*)p) T(t); } // NOLINT
141  void construct(pointer p) { new ((void*)p) T(); } // NOLINT
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)...); // NOLINT
146  }
147 #endif
148  void destroy(pointer ptr) {
149  (void)ptr;
150  (*ptr).~T();
151  }
152  template<class X>
153  void destroy(X* p) {
154  NLIB_UNUSED(p); // workaround for VS2012
155  p->~X();
156  }
157  size_t max_size() const {
158  size_t cnt = (size_t)-1 / sizeof(T);
159  return 0 < cnt ? cnt : 1;
160  }
161  pointer address(reference value) const { return &value; }
162  const_pointer address(const_reference value) const { return &value; }
163 };
164 
165 template <typename T>
166 typename NMallocStlAllocator<T>::pointer NMallocStlAllocator<T>::allocate(size_type n,
167  const void* p) {
168  (void)p;
169  void* pp = nmalloc(n * sizeof(T));
170  return reinterpret_cast<T*>(pp);
171 }
172 
173 template <class T1, class T2>
174 inline bool operator==(const NMallocStlAllocator<T1>&, const NMallocStlAllocator<T2>&) {
175  return true;
176 }
177 
178 template <class T1, class T2>
179 inline bool operator!=(const NMallocStlAllocator<T1>&, const NMallocStlAllocator<T2>&) {
180  return false;
181 }
182 
183 } // namespace heap
184 NLIB_NAMESPACE_END
185 
186 #endif
187 
188 #ifdef __cplusplus
189 #define NLIB_REPLACE_MALLOC \
190 extern "C" { \
191 void* nlib_malloc(size_t n) { \
192  return nmalloc(n); \
193  } \
194 void nlib_free(void* p) { \
195  nfree(p); \
196  } \
197 void* nlib_calloc(size_t nmemb, size_t size) { \
198  return ncalloc(nmemb, size); \
199  } \
200 void* nlib_realloc(void* ptr, size_t size) { \
201  return nrealloc(ptr, size); \
202  } \
203 void* nlib_memalign(size_t alignment, size_t size) { \
204  return nmalloc_aligned(size, alignment); \
205  } \
206 void nlib_free_size(void* ptr, size_t size) { \
207  nfree_size(ptr, size); \
208  } \
209 }
210 
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); \
216  } \
217 void operator delete(void* ptr) NLIB_NOEXCEPT { \
218  nlib_free(ptr); \
219  } \
220 void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
221  return nlib_malloc(size); \
222  } \
223 void operator delete(void* ptr, const std::nothrow_t&) NLIB_NOEXCEPT { \
224  nlib_free(ptr); \
225  } \
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); \
233  } \
234 void operator delete(void* ptr, size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
235  nlib_free_size(ptr, size); \
236  } \
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); \
240 }
241 #else
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); \
246  } \
247 void operator delete(void* ptr) throw() { \
248  nlib_free(ptr); \
249  } \
250 void* operator new(size_t size, const std::nothrow_t&) throw() { \
251  return nlib_malloc(size); \
252  } \
253 void* operator new[](size_t size) throw(std::bad_alloc) { \
254  return nlib_malloc(size); \
255  } \
256 void operator delete[](void* ptr) throw() { \
257  nlib_free(ptr); \
258  } \
259 void* operator new[](size_t size, const std::nothrow_t&) throw() { \
260  return nlib_malloc(size); \
261  }
262 #endif
263 #else
264 #define NLIB_REPLACE_MALLOC \
265 void* nlib_malloc(size_t n) { \
266  return nmalloc(n); \
267  } \
268 void nlib_free(void* p) { \
269  nfree(p); \
270  } \
271 void* nlib_calloc(size_t nmemb, size_t size) { \
272  return ncalloc(nmemb, size); \
273  } \
274 void* nlib_realloc(void* ptr, size_t size) { \
275  return nrealloc(ptr, size); \
276  } \
277 void* nlib_memalign(size_t alignment, size_t size) { \
278  return nmalloc_aligned(size, alignment); \
279  } \
280 void nlib_free_size(void* ptr, size_t size) { \
281  nfree_size(ptr, size); \
282  }
283 #define NLIB_REPLACE_MALLOC_NEW NLIB_REPLACE_MALLOC
284 #endif
285 
286 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
287 #undef NLIB_VIS_PUBLIC
288 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
289 #endif
290 
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.
Definition: NMalloc.h:67
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.
Definition: NMalloc.h:64
#define NLIB_ATTRIBUTE_MALLOC
Defines __attribute__((malloc)) if it is available for use.
Definition: Platform_unix.h:73
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.
Definition: NMalloc.h:33
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
Definition: NMalloc.h:101
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
Definition: NMalloc.h:106
C-based declaration of the basic API.
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.
Definition: NMalloc.h:40
int nlib_fd
The original file descriptor of nlib (a 32-bit integer value).
Definition: Platform.h:1051
size_t alloc_count
Number of regions allocated by the user application within the heap.
Definition: NMalloc.h:32
Displays the addresses and sizes of all memory allocated by the user application. ...
Definition: NMalloc.h:63
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...
Definition: NMalloc.h:39
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.
Definition: NMalloc.h:37
size_t hash
Hash value of the status of memory allocated by the user application within the heap.
Definition: NMalloc.h:34
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.
Definition: NMalloc.h:61
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.
Definition: NMalloc.h:38
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.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
Displays the usage status for each page.
Definition: NMalloc.h:62
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.
Definition: NMalloc.h:60
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
Definition: Platform_unix.h:64
Structure that contains a summary of the memory usage status of the heap used by the user application...
Definition: NMalloc.h:31
NLIB_CHECK_RESULT void * nmalloc_aligned(size_t size, size_t algn)
Allocates memory with a specific alignment.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24