Blender V5.3
MEM_guardedalloc.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
45
46#ifndef __MEM_GUARDEDALLOC_H__
47#define __MEM_GUARDEDALLOC_H__
48
49/* Needed for uintptr_t and attributes, exception, don't use BLI anywhere else in `MEM_*` */
52
53#include <cstring>
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59/* -------------------------------------------------------------------- */
71
77extern size_t (*MEM_allocN_len)(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
78
84void MEM_delete_void(void *vmemh);
85
86#if 0 /* UNUSED */
90extern short (*MEM_testN)(void *vmemh);
91#endif
92
99void *MEM_dupalloc_void(const void *vmemh) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT;
100
106extern void *(*MEM_realloc_uninitialized_id)(
107 void *vmemh, size_t len, const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
109
113extern void *(*MEM_realloc_zeroed_id)(void *vmemh,
114 size_t len,
115 const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
117
118#define MEM_realloc_uninitialized(vmemh, len) MEM_realloc_uninitialized_id(vmemh, len, __func__)
119#define MEM_realloc_zeroed(vmemh, len) MEM_realloc_zeroed_id(vmemh, len, __func__)
120
127 ATTR_NONNULL(2);
128
135 size_t size,
136 const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
138
143void *MEM_new_uninitialized(size_t len, const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
145
152 size_t size,
153 const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
155
161 size_t alignment,
162 const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
164
169 size_t len,
170 size_t size,
171 size_t alignment,
172 const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1, 2)
174
178extern void *(*MEM_new_array_zeroed_aligned)(
179 size_t len,
180 size_t size,
181 size_t alignment,
182 const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1, 2)
183 ATTR_NONNULL(4);
184
186
187/* -------------------------------------------------------------------- */
194
199extern void (*MEM_printmemlist_pydict)();
200
204extern void (*MEM_printmemlist)();
205
207extern void (*MEM_callbackmemlist)(void (*func)(void *));
208
210extern void (*MEM_printmemlist_stats)();
211
213extern void (*MEM_set_error_callback)(void (*func)(const char *));
214
220extern bool (*MEM_consistency_check)();
221
223extern void (*MEM_set_memory_debug)();
224
226extern size_t (*MEM_get_memory_in_use)();
228extern unsigned int (*MEM_get_memory_blocks_in_use)();
229
231extern void (*MEM_reset_peak_memory)();
232
235
237#define MEM_SIZE_OVERHEAD sizeof(size_t)
238#define MEM_SIZE_OPTIMAL(size) ((size) - MEM_SIZE_OVERHEAD)
239
240#ifndef NDEBUG
241extern const char *(*MEM_name_ptr)(void *vmemh);
250extern void (*MEM_name_ptr_set)(void *vmemh, const char *str) ATTR_NONNULL();
251#endif
252
258
265
276
287
289
290#ifdef __cplusplus
291}
292#endif /* __cplusplus */
293
294#ifdef __cplusplus
295
296# include <any>
297# include <memory>
298# include <new>
299# include <type_traits>
300# include <utility>
301
303
309# define MEM_MIN_CPP_ALIGNMENT \
310 (__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(void *) ? __STDCPP_DEFAULT_NEW_ALIGNMENT__ : \
311 alignof(void *))
312
313/* -------------------------------------------------------------------- */
329
330namespace mem_guarded::internal {
331/* Note that we intentionally don't care about a non-trivial default constructor here. */
332template<typename T>
333constexpr bool is_trivial_after_construction = std::is_trivially_copyable_v<T> &&
334 std::is_trivially_destructible_v<T>;
335} // namespace mem_guarded::internal
336
351template<typename T, typename... Args>
352inline T *MEM_new(const char *allocation_name, Args &&...args)
353{
355 sizeof(T),
356 alignof(T),
357 allocation_name,
358 std::is_trivially_destructible_v<T> ? mem_guarded::internal::DestructorType::Trivial :
360 return new (buffer) T(std::forward<Args>(args)...);
361}
362
372template<typename T> inline T *MEM_new_array(const size_t length, const char *allocation_name)
373{
374# ifdef _MSC_VER
375 static_assert(
376 std::is_trivially_destructible_v<T>,
377 "For non-trivially copyable and destructible types, use higher level types like Vector.");
378# else
379 static_assert(
380 mem_guarded::internal::is_trivial_after_construction<T>,
381 "For non-trivially copyable and destructible types, use higher level types like Vector.");
382# endif
383 T *buffer = static_cast<T *>(
384 MEM_new_array_uninitialized_aligned(length, sizeof(T), alignof(T), allocation_name));
385 for (size_t i = 0; i < length; i++) {
386 new (buffer + i) T();
387 }
388 return buffer;
389}
390
400template<typename T> inline void MEM_delete(const T *ptr)
401{
402 static_assert(
403 !std::is_void_v<T>,
404 "MEM_delete on a void pointer is not possible, `static_cast` it to the correct type");
405 if (ptr == nullptr) {
406 return;
407 }
408 const void *complete_ptr = [ptr]() {
409 if constexpr (std::is_polymorphic_v<T>) {
410 /* Polymorphic objects lifetime can be managed with pointers to their most derived type or
411 * with pointers to any of their ancestor types in their hierarchy tree that define a virtual
412 * destructor, however ancestor pointers may differ in a offset from the same derived object.
413 * For freeing the correct memory allocated with #MEM_new, we need to ensure that the given
414 * pointer is equal to the pointer to the most derived object, which can be obtained with
415 * `dynamic_cast<void *>(ptr)`. */
416 return dynamic_cast<const void *>(ptr);
417 }
418 else {
419 return static_cast<const void *>(ptr);
420 }
421 }();
422 /* Explicitly don't call destructor when not needed, also because it doesn't work
423 * for pointers like float (*x)[2]. */
424 if constexpr (!std::is_trivially_destructible_v<T>) {
425 ptr->~T();
426 }
427 /* C++ allows destruction of `const` objects, so the pointer is allowed to be `const`. */
428 mem_guarded::internal::mem_freeN_ex(const_cast<void *>(complete_ptr),
430}
431
436# define MEM_SAFE_DELETE(v) \
437 do { \
438 if (v) { \
439 MEM_delete(v); \
440 (v) = nullptr; \
441 } \
442 } while (0)
443
448# define MEM_SAFE_DELETE_VOID(v) \
449 do { \
450 if (v) { \
451 MEM_delete_void(v); \
452 (v) = nullptr; \
453 } \
454 } while (0)
455
457template<typename T> struct MEM_smart_ptr_deleter {
458 void operator()(T *pointer) const noexcept
459 {
460 MEM_SAFE_DELETE(pointer);
461 }
462};
463
465# define MEM_CXX_CLASS_ALLOC_FUNCS(_id) \
466 public: \
467 void *operator new(size_t num_bytes) \
468 { \
469 return mem_guarded::internal::mem_mallocN_aligned_ex( \
470 num_bytes, \
471 __STDCPP_DEFAULT_NEW_ALIGNMENT__, \
472 _id, \
473 mem_guarded::internal::DestructorType::NonTrivial); \
474 } \
475 void *operator new(size_t num_bytes, std::align_val_t alignment) \
476 { \
477 return mem_guarded::internal::mem_mallocN_aligned_ex( \
478 num_bytes, size_t(alignment), _id, mem_guarded::internal::DestructorType::NonTrivial); \
479 } \
480 void operator delete(void *mem) \
481 { \
482 if (mem) { \
483 mem_guarded::internal::mem_freeN_ex(mem, \
484 mem_guarded::internal::DestructorType::NonTrivial); \
485 } \
486 } \
487 void *operator new[](size_t num_bytes) \
488 { \
489 return mem_guarded::internal::mem_mallocN_aligned_ex( \
490 num_bytes, \
491 __STDCPP_DEFAULT_NEW_ALIGNMENT__, \
492 _id "[]", \
493 mem_guarded::internal::DestructorType::NonTrivial); \
494 } \
495 void *operator new[](size_t num_bytes, std::align_val_t alignment) \
496 { \
497 return mem_guarded::internal::mem_mallocN_aligned_ex( \
498 num_bytes, \
499 size_t(alignment), \
500 _id "[]", \
501 mem_guarded::internal::DestructorType::NonTrivial); \
502 } \
503 void operator delete[](void *mem) \
504 { \
505 if (mem) { \
506 mem_guarded::internal::mem_freeN_ex(mem, \
507 mem_guarded::internal::DestructorType::NonTrivial); \
508 } \
509 } \
510 void *operator new(size_t /*count*/, void *ptr) \
511 { \
512 return ptr; \
513 } \
514 \
519 void operator delete(void * /*ptr_to_free*/, void * /*ptr*/) {}
520
522
523/* -------------------------------------------------------------------- */
538
550template<typename T> inline T *MEM_new_zeroed(const char *allocation_name)
551{
552# ifdef _MSC_VER
553 static_assert(std::is_trivially_constructible_v<T>,
554 "For non-trivial types, MEM_new must be used.");
555# else
556 static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new must be used.");
557# endif
558 return static_cast<T *>(MEM_new_array_zeroed_aligned(1, sizeof(T), alignof(T), allocation_name));
559}
560
566template<typename T>
567inline T *MEM_new_array_zeroed(const size_t length, const char *allocation_name)
568{
569# ifdef _MSC_VER
570 static_assert(std::is_trivially_constructible_v<T>,
571 "For non-trivial types, MEM_new must be used.");
572# else
573 static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new must be used.");
574# endif
575 return static_cast<T *>(
576 MEM_new_array_zeroed_aligned(length, sizeof(T), alignof(T), allocation_name));
577}
578
590template<typename T> inline T *MEM_new_uninitialized(const char *allocation_name)
591{
592# ifdef _MSC_VER
593 static_assert(std::is_trivially_constructible_v<T>,
594 "For non-trivial types, MEM_new must be used.");
595# else
596 static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new must be used.");
597# endif
598 return static_cast<T *>(
599 MEM_new_array_uninitialized_aligned(1, sizeof(T), alignof(T), allocation_name));
600}
601
608template<typename T>
609inline T *MEM_new_array_uninitialized(const size_t length, const char *allocation_name)
610{
611# ifdef _MSC_VER
612 static_assert(std::is_trivially_constructible_v<T>,
613 "For non-trivial types, MEM_new must be used.");
614# else
615 static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new must be used.");
616# endif
617 return static_cast<T *>(
618 MEM_new_array_uninitialized_aligned(length, sizeof(T), alignof(T), allocation_name));
619}
620
627template<typename T> inline T *MEM_dupalloc(const T *other)
628{
629# ifdef _MSC_VER
630 /* TODO: Add back is_trivially_copyable_v condition, temporarily disabled
631 * because of build error on MSVC. */
632 static_assert(/*std::is_trivially_copyable_v<T> &&*/ std::is_trivially_destructible_v<T>,
633 "MEM_dupalloc can only duplicate types that are trivially copyable and "
634 "destructible, use MEM_new instead.");
635# else
636 static_assert(std::is_trivially_copyable_v<T> &&
637 mem_guarded::internal::is_trivial_after_construction<T>,
638 "MEM_dupalloc can only duplicate types that are trivially copyable and "
639 "destructible, use MEM_new instead.");
640# endif
641
642 static_assert(!std::is_void_v<T>);
643
644 return static_cast<T *>(MEM_dupalloc_void(other));
645}
646
647template<typename T> inline void MEM_delete_void(T *ptr)
648{
649 static_assert(std::is_void_v<T>,
650 "MEM_delete_void only supported for void pointer, use MEM_delete instead");
651 mem_guarded::internal::mem_freeN_ex(const_cast<void *>(static_cast<const void *>(ptr)),
653}
654
656
666template<typename T, typename... Args> T &MEM_construct_leak_detection_data(Args &&...args)
667{
668 std::shared_ptr<T> data = std::make_shared<T>(std::forward<Args>(args)...);
669 std::any any_data = std::make_any<std::shared_ptr<T>>(data);
671 return *data;
672}
673
674#endif /* __cplusplus */
675
676#endif /* __MEM_GUARDEDALLOC_H__ */
#define ATTR_ALLOC_SIZE(...)
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_NONNULL(...)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
#define str(s)
float length(VecOp< float, D >) RET
void MEM_enable_fail_on_memleak()
void MEM_init_memleak_detection()
void MEM_use_lockfree_allocator()
Definition mallocn.cc:159
void(*) MEM_set_memory_debug(void)
Definition mallocn.cc:64
size_t(*) MEM_allocN_len(const void *vmemh)
Definition mallocn.cc:27
bool(*) MEM_consistency_check(void)
Definition mallocn.cc:63
void MEM_delete_void(void *vmemh)
Definition mallocn.cc:109
size_t(*) MEM_get_memory_in_use(void)
Definition mallocn.cc:65
void * MEM_new_zeroed(size_t len, const char *str)
Definition mallocn.cc:114
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str)
Definition mallocn.cc:119
void(*) MEM_printmemlist(void)
Definition mallocn.cc:59
void(*) MEM_printmemlist_pydict(void)
Definition mallocn.cc:58
uint(*) MEM_get_memory_blocks_in_use(void)
Definition mallocn.cc:66
size_t(*) MEM_get_peak_memory(void)
Definition mallocn.cc:68
void MEM_use_guarded_allocator()
Definition mallocn.cc:200
void(*) MEM_name_ptr_set(void *vmemh, const char *str)
Definition mallocn.cc:74
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:134
void(*) MEM_set_error_callback(void(*func)(const char *))
Definition mallocn.cc:62
void(*) MEM_reset_peak_memory(void)
Definition mallocn.cc:67
void *(*) MEM_new_array_uninitialized_aligned(size_t len, size_t size, size_t alignment, const char *str)
Definition mallocn.cc:50
void(*) MEM_callbackmemlist(void(*func)(void *))
Definition mallocn.cc:60
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str)
Definition mallocn.cc:129
void * MEM_dupalloc_void(const void *vmemh)
Definition mallocn.cc:139
void * MEM_new_uninitialized(size_t len, const char *str)
Definition mallocn.cc:124
void *(*) MEM_new_array_zeroed_aligned(size_t len, size_t size, size_t alignment, const char *str)
Definition mallocn.cc:54
void(*) MEM_printmemlist_stats(void)
Definition mallocn.cc:61
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define T
BMesh const char void * data
void *(*) mem_mallocN_aligned_ex(size_t len, size_t alignment, const char *str, DestructorType destructor_type)
Definition mallocn.cc:45
void(*) mem_freeN_ex(void *vmemh, DestructorType destructor_type)
Definition mallocn.cc:28
void add_memleak_data(std::any data)