Blender  V3.3
MEM_guardedalloc.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
33 #ifndef __MEM_GUARDEDALLOC_H__
34 #define __MEM_GUARDEDALLOC_H__
35 
36 /* Needed for uintptr_t and attributes, exception, don't use BLI anywhere else in `MEM_*` */
37 #include "../../source/blender/blenlib/BLI_compiler_attrs.h"
38 #include "../../source/blender/blenlib/BLI_sys_types.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
49 extern size_t (*MEM_allocN_len)(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
50 
54 extern void (*MEM_freeN)(void *vmemh);
55 
56 #if 0 /* UNUSED */
60 extern short (*MEM_testN)(void *vmemh);
61 #endif
62 
67 extern void *(*MEM_dupallocN)(const void *vmemh) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT;
68 
74 extern void *(*MEM_reallocN_id)(void *vmemh,
75  size_t len,
76  const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
77  ATTR_ALLOC_SIZE(2);
78 
82 extern void *(*MEM_recallocN_id)(void *vmemh,
83  size_t len,
84  const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
85  ATTR_ALLOC_SIZE(2);
86 
87 #define MEM_reallocN(vmemh, len) MEM_reallocN_id(vmemh, len, __func__)
88 #define MEM_recallocN(vmemh, len) MEM_recallocN_id(vmemh, len, __func__)
89 
95 extern void *(*MEM_callocN)(size_t len, const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
97 
103 extern void *(*MEM_calloc_arrayN)(size_t len,
104  size_t size,
105  const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
107 
112 extern void *(*MEM_mallocN)(size_t len, const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
114 
120 extern void *(*MEM_malloc_arrayN)(size_t len,
121  size_t size,
122  const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
123  ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
124 
129 extern void *(*MEM_mallocN_aligned)(size_t len,
130  size_t alignment,
131  const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
133 
138 extern void (*MEM_printmemlist_pydict)(void);
139 
143 extern void (*MEM_printmemlist)(void);
144 
146 extern void (*MEM_callbackmemlist)(void (*func)(void *));
147 
149 extern void (*MEM_printmemlist_stats)(void);
150 
152 extern void (*MEM_set_error_callback)(void (*func)(const char *));
153 
159 extern bool (*MEM_consistency_check)(void);
160 
162 extern void (*MEM_set_memory_debug)(void);
163 
165 extern size_t (*MEM_get_memory_in_use)(void);
167 extern unsigned int (*MEM_get_memory_blocks_in_use)(void);
168 
170 extern void (*MEM_reset_peak_memory)(void);
171 
173 extern size_t (*MEM_get_peak_memory)(void) ATTR_WARN_UNUSED_RESULT;
174 
175 #ifdef __GNUC__
176 # define MEM_SAFE_FREE(v) \
177  do { \
178  typeof(&(v)) _v = &(v); \
179  if (*_v) { \
180  /* Cast so we can free constant arrays. */ \
181  MEM_freeN((void *)*_v); \
182  *_v = NULL; \
183  } \
184  } while (0)
185 #else
186 # define MEM_SAFE_FREE(v) \
187  do { \
188  void **_v = (void **)&(v); \
189  if (*_v) { \
190  MEM_freeN(*_v); \
191  *_v = NULL; \
192  } \
193  } while (0)
194 #endif
195 
196 /* overhead for lockfree allocator (use to avoid slop-space) */
197 #define MEM_SIZE_OVERHEAD sizeof(size_t)
198 #define MEM_SIZE_OPTIMAL(size) ((size)-MEM_SIZE_OVERHEAD)
199 
200 #ifndef NDEBUG
201 extern const char *(*MEM_name_ptr)(void *vmemh);
202 #endif
203 
208 void MEM_init_memleak_detection(void);
209 
215 
221 void MEM_enable_fail_on_memleak(void);
222 
223 /* Switch allocator to fast mode, with less tracking.
224  *
225  * Use in the production code where performance is the priority, and exact details about allocation
226  * is not. This allocator keeps track of number of allocation and amount of allocated bytes, but it
227  * does not track of names of allocated blocks.
228  *
229  * NOTE: The switch between allocator types can only happen before any allocation did happen. */
230 void MEM_use_lockfree_allocator(void);
231 
232 /* Switch allocator to slow fully guarded mode.
233  *
234  * Use for debug purposes. This allocator contains lock section around every allocator call, which
235  * makes it slow. What is gained with this is the ability to have list of allocated blocks (in an
236  * addition to the tracking of number of allocations and amount of allocated bytes).
237  *
238  * NOTE: The switch between allocator types can only happen before any allocation did happen. */
239 void MEM_use_guarded_allocator(void);
240 
241 #ifdef __cplusplus
242 }
243 #endif /* __cplusplus */
244 
245 #ifdef __cplusplus
246 
247 # include <new>
248 # include <type_traits>
249 # include <utility>
250 
262 template<typename T, typename... Args>
263 inline T *MEM_new(const char *allocation_name, Args &&...args)
264 {
265  void *buffer = MEM_mallocN(sizeof(T), allocation_name);
266  return new (buffer) T(std::forward<Args>(args)...);
267 }
268 
275 template<typename T> inline T *MEM_cnew(const char *allocation_name)
276 {
277  static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new should be used.");
278  return static_cast<T *>(MEM_callocN(sizeof(T), allocation_name));
279 }
280 
291 template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &other)
292 {
293  static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new should be used.");
294  T *new_object = static_cast<T *>(MEM_mallocN(sizeof(T), allocation_name));
295  memcpy(new_object, &other, sizeof(T));
296  return new_object;
297 }
298 
303 template<typename T> inline void MEM_delete(const T *ptr)
304 {
305  if (ptr == nullptr) {
306  /* Support #ptr being null, because C++ `delete` supports that as well. */
307  return;
308  }
309  /* C++ allows destruction of const objects, so the pointer is allowed to be const. */
310  ptr->~T();
311  MEM_freeN(const_cast<T *>(ptr));
312 }
313 
314 /* Allocation functions (for C++ only). */
315 # define MEM_CXX_CLASS_ALLOC_FUNCS(_id) \
316  public: \
317  void *operator new(size_t num_bytes) \
318  { \
319  return MEM_mallocN(num_bytes, _id); \
320  } \
321  void operator delete(void *mem) \
322  { \
323  if (mem) { \
324  MEM_freeN(mem); \
325  } \
326  } \
327  void *operator new[](size_t num_bytes) \
328  { \
329  return MEM_mallocN(num_bytes, _id "[]"); \
330  } \
331  void operator delete[](void *mem) \
332  { \
333  if (mem) { \
334  MEM_freeN(mem); \
335  } \
336  } \
337  void *operator new(size_t /*count*/, void *ptr) \
338  { \
339  return ptr; \
340  } \
341  /* This is the matching delete operator to the placement-new operator above. Both parameters \
342  * will have the same value. Without this, we get the warning C4291 on windows. */ \
343  void operator delete(void * /*ptr_to_free*/, void * /*ptr*/) \
344  { \
345  }
346 
347 #endif /* __cplusplus */
348 
349 #endif /* __MEM_GUARDEDALLOC_H__ */
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_ALLOC_SIZE(...)
void MEM_use_guarded_allocator(void)
Definition: mallocn.c:134
void MEM_use_memleak_detection(bool enabled)
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Definition: mallocn.c:32
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void(* MEM_reset_peak_memory)(void)
Definition: mallocn.c:47
void MEM_enable_fail_on_memleak(void)
size_t(* MEM_get_memory_in_use)(void)
Definition: mallocn.c:45
void(* MEM_printmemlist_stats)(void)
Definition: mallocn.c:41
void(* MEM_set_memory_debug)(void)
Definition: mallocn.c:44
size_t(* MEM_get_peak_memory)(void) ATTR_WARN_UNUSED_RESULT
Definition: mallocn.c:48
void MEM_init_memleak_detection(void)
void *(* MEM_mallocN)(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
Definition: mallocn.c:33
void(* MEM_set_error_callback)(void(*func)(const char *))
Definition: mallocn.c:42
size_t(* MEM_allocN_len)(const void *vmemh) ATTR_WARN_UNUSED_RESULT
Definition: mallocn.c:26
void MEM_use_lockfree_allocator(void)
Definition: mallocn.c:98
bool(* MEM_consistency_check)(void)
Definition: mallocn.c:43
unsigned int(* MEM_get_memory_blocks_in_use)(void)
Definition: mallocn.c:46
void(* MEM_printmemlist_pydict)(void)
Definition: mallocn.c:38
void *(* MEM_callocN)(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
Definition: mallocn.c:31
void *(* ATTR_NONNULL)(3)
Iterator New.
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Definition: mallocn.c:34
void *(* MEM_mallocN_aligned)(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
Definition: mallocn.c:35
void(* MEM_callbackmemlist)(void(*func)(void *))
Definition: mallocn.c:40
void(* MEM_printmemlist)(void)
Definition: mallocn.c:39
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SyclQueue void void size_t num_bytes void
int len
Definition: draw_manager.c:108
#define str(s)
bool enabled
ccl_global float * buffer
#define T
PointerRNA * ptr
Definition: wm_files.c:3480