21#ifdef WITH_MEM_VALGRIND
22# include "valgrind/memcheck.h"
26#include "../../source/blender/blenlib/BLI_strict_flags.h"
38static_assert(MEM_MIN_CPP_ALIGNMENT <=
alignof(
MemHead),
"Bad alignment of MemHead");
39static_assert(MEM_MIN_CPP_ALIGNMENT <=
sizeof(
MemHead),
"Bad size of MemHead");
45static_assert(MEM_MIN_CPP_ALIGNMENT <=
alignof(
MemHeadAligned),
"Bad alignment of MemHeadAligned");
46static_assert(MEM_MIN_CPP_ALIGNMENT <=
sizeof(
MemHeadAligned),
"Bad size of MemHeadAligned");
70#define MEMHEAD_FROM_PTR(ptr) (((MemHead *)ptr) - 1)
71#define PTR_FROM_MEMHEAD(memhead) (memhead + 1)
72#define MEMHEAD_ALIGNED_FROM_PTR(ptr) (((MemHeadAligned *)ptr) - 1)
73#define MEMHEAD_IS_ALIGNED(memhead) ((memhead)->len & size_t(MEMHEAD_FLAG_ALIGN))
74#define MEMHEAD_IS_FROM_CPP_NEW(memhead) ((memhead)->len & size_t(MEMHEAD_FLAG_FROM_CPP_NEW))
75#define MEMHEAD_LEN(memhead) ((memhead)->len & ~size_t(MEMHEAD_FLAG_MASK))
84 vsnprintf(buf,
sizeof(buf), message, str_format_args);
85 buf[
sizeof(buf) - 1] =
'\0';
98 va_list str_format_args;
99 va_start(str_format_args, message);
101 va_end(str_format_args);
110 va_list str_format_args;
112 va_start(str_format_args, message);
114 va_end(str_format_args);
116 if (vmemh ==
nullptr) {
124 const void *address = memh;
125 size_t size =
len +
sizeof(*memh);
160 "Attempt to use C-style MEM_freeN on a pointer created with CPP-style MEM_new or new\n");
166 memset(memh + 1, 255,
len);
179 void *newp =
nullptr;
186 "Attempt to use C-style MEM_dupallocN on a pointer created with "
187 "CPP-style MEM_new or new\n");
193 prev_size,
size_t(memh_aligned->
alignment),
"dupli_malloc", AllocationType::ALLOC_FREE);
198 memcpy(newp, vmemh, prev_size);
205 void *newp =
nullptr;
213 "Attempt to use C-style MEM_reallocN on a pointer created with "
214 "CPP-style MEM_new or new\n");
223 len,
size_t(memh_aligned->
alignment),
"realloc", AllocationType::ALLOC_FREE);
229 memcpy(newp, vmemh,
len);
233 memcpy(newp, vmemh, old_len);
248 void *newp =
nullptr;
256 "Attempt to use C-style MEM_recallocN on a pointer created with "
257 "CPP-style MEM_new or new\n");
266 len,
size_t(memh_aligned->
alignment),
"recalloc", AllocationType::ALLOC_FREE);
272 memcpy(newp, vmemh,
len);
275 memcpy(newp, vmemh, old_len);
280 memset(((
char *)newp) + old_len, 0,
len - old_len);
320 "Calloc array aborted due to integer overflow: "
337#ifdef WITH_MEM_VALGRIND
338 const size_t len_unaligned =
len;
348 memset(memh + 1, 255,
len);
350#ifdef WITH_MEM_VALGRIND
352 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
355 VALGRIND_MAKE_MEM_DEFINED((
const char *)(memh + 1) + len_unaligned,
len - len_unaligned);
377 "Malloc array aborted due to integer overflow: "
397 assert(alignment < 1024);
415#ifdef WITH_MEM_VALGRIND
416 const size_t len_unaligned =
len;
432 memset(memh + 1, 255,
len);
434#ifdef WITH_MEM_VALGRIND
436 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
439 VALGRIND_MAKE_MEM_DEFINED((
const char *)(memh + 1) + len_unaligned,
len - len_unaligned);
461 const size_t alignment,
467 "Calloc array aborted due to integer overflow: "
476 if (alignment <= MEM_MIN_CPP_ALIGNMENT) {
485 memset(
ptr, 0, bytes_num);
506 "\nFor more detailed per-block statistics run Blender with memory debugging command line "
509#ifdef HAVE_MALLOC_STATS
510 printf(
"System Statistics:\n");
555 return "unknown block name ptr";
558 return "MEM_lockfree_name_ptr(nullptr)";
void BLI_kdtree_nd_ free(KDTree *tree)
Read Guarded memory(de)allocation.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
static __attribute__((constructor)) void cpu_check()
bool leak_detector_has_run
char free_after_leak_detection_message[]
void aligned_free(void *ptr)
void * MEM_mallocN_aligned(size_t len, size_t alignment, const char *str)
void *(* MEM_callocN)(size_t len, const char *str)
void * aligned_malloc(size_t size, size_t alignment)
MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
MEM_INLINE void MEM_trigger_error_on_memory_block(const void *, const size_t)
size_t memory_usage_current(void)
#define MEMHEAD_REAL_PTR(memh)
#define MEMHEAD_ALIGN_PADDING(alignment)
void memory_usage_block_alloc(size_t size)
#define SIZET_ALIGN_4(len)
size_t memory_usage_block_num(void)
void memory_usage_block_free(size_t size)
size_t memory_usage_peak(void)
#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT
void memory_usage_peak_reset(void)
uint MEM_lockfree_get_memory_blocks_in_use()
void * MEM_lockfree_mallocN(size_t len, const char *str)
void MEM_lockfree_printmemlist_pydict()
void * MEM_lockfree_dupallocN(const void *vmemh)
void * MEM_lockfree_malloc_arrayN(size_t len, size_t size, const char *str)
void MEM_lockfree_reset_peak_memory()
void MEM_lockfree_set_error_callback(void(*func)(const char *))
void MEM_lockfree_name_ptr_set(void *UNUSED(vmemh), const char *UNUSED(str))
void * MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str, const AllocationType allocation_type)
#define PTR_FROM_MEMHEAD(memhead)
void * MEM_lockfree_calloc_arrayN_aligned(const size_t len, const size_t size, const size_t alignment, const char *str)
static bool malloc_debug_memset
size_t MEM_lockfree_get_peak_memory()
@ MEMHEAD_FLAG_FROM_CPP_NEW
static void(* error_callback)(const char *)
void * MEM_lockfree_reallocN_id(void *vmemh, size_t len, const char *str)
static void print_error(const char *message, va_list str_format_args)
void * MEM_lockfree_callocN(size_t len, const char *str)
void * MEM_lockfree_calloc_arrayN(size_t len, size_t size, const char *str)
static void report_error_on_address(const void *vmemh, const char *message,...)
void MEM_lockfree_set_memory_debug()
bool MEM_lockfree_consistency_check()
void MEM_lockfree_printmemlist()
#define MEMHEAD_LEN(memhead)
#define MEMHEAD_IS_ALIGNED(memhead)
void * MEM_lockfree_recallocN_id(void *vmemh, size_t len, const char *str)
const char * MEM_lockfree_name_ptr(void *vmemh)
struct MemHeadAligned MemHeadAligned
void MEM_lockfree_callbackmemlist(void(*func)(void *))
#define MEMHEAD_FROM_PTR(ptr)
size_t MEM_lockfree_get_memory_in_use()
void MEM_lockfree_freeN(void *vmemh, AllocationType allocation_type)
#define MEMHEAD_IS_FROM_CPP_NEW(memhead)
#define MEMHEAD_ALIGNED_FROM_PTR(ptr)
void mem_lockfree_clearmemlist()
size_t MEM_lockfree_allocN_len(const void *vmemh)
void MEM_lockfree_printmemlist_stats()