25#ifdef WITH_MEM_VALGRIND
26# include "valgrind/memcheck.h"
31#include "../../source/blender/blenlib/BLI_strict_flags.hh"
62# define DEBUG_BACKTRACE
76# if defined(__linux__) || defined(__APPLE__)
77# define DEBUG_BACKTRACE_EXECINFO
79# error "DEBUG_BACKTRACE: not supported for this platform!"
84#ifdef DEBUG_BACKTRACE_EXECINFO
85# define BACKTRACE_SIZE 100
88#ifdef DEBUG_MEMCOUNTER
90# define DEBUG_MEMCOUNTER_ERROR_VAL 0
91static int _mallocn_count = 0;
94static void memcount_raise(
const char *name)
96 fprintf(stderr,
"%s: memcount-leak, %d\n", name, _mallocn_count);
115typedef struct MemHead {
118 MemHead *next, *prev;
120 const char *nextname;
125#ifdef DEBUG_MEMCOUNTER
129#ifdef DEBUG_MEMDUPLINAME
130 int need_free_name,
pad;
133#ifdef DEBUG_BACKTRACE_EXECINFO
134 void *backtrace[BACKTRACE_SIZE];
139static_assert(MEM_MIN_CPP_ALIGNMENT <=
alignof(MemHead),
"Bad alignment of MemHead");
140static_assert(MEM_MIN_CPP_ALIGNMENT <=
sizeof(MemHead),
"Bad size of MemHead");
142typedef MemHead MemHeadAligned;
159#ifdef DEBUG_BACKTRACE_EXECINFO
160# include <execinfo.h>
170static void MemorY_ErroR(
const char *block,
const char *error);
178#define MAKE_ID(a, b, c, d) (int(d) << 24 | int(c) << 16 | (b) << 8 | (a))
180#define MEMTAG1 MAKE_ID('M', 'E', 'M', 'O')
181#define MEMTAG2 MAKE_ID('R', 'Y', 'B', 'L')
182#define MEMTAG3 MAKE_ID('O', 'C', 'K', '!')
183#define MEMFREE MAKE_ID('F', 'R', 'E', 'E')
185#define MEMNEXT(x) ((MemHead *)(((char *)x) - offsetof(MemHead, next)))
219static void print_error(
const char *message, va_list str_format_args)
222 vsnprintf(buf,
sizeof(buf), message, str_format_args);
223 buf[
sizeof(buf) - 1] =
'\0';
238 va_list str_format_args;
239 va_start(str_format_args, message);
241 va_end(str_format_args);
249 va_list str_format_args;
251 va_start(str_format_args, message);
253 va_end(str_format_args);
255 if (vmemh ==
nullptr) {
260 const MemHead *memh =
static_cast<const MemHead *
>(vmemh);
262 size_t len = memh->len;
264 const void *address = memh;
265 size_t size =
len +
sizeof(*memh) +
sizeof(
MemTail);
266 if (memh->alignment > 0) [[unlikely]] {
267 const MemHeadAligned *memh_aligned = memh;
289 const char *err_val =
nullptr;
290 const MemHead *listend;
297 return (err_val ==
nullptr);
313 const MemHead *memh =
static_cast<const MemHead *
>(vmemh);
325 void *newp =
nullptr;
328 const MemHead *memh =
static_cast<const MemHead *
>(vmemh);
333 "Attempt to use C-style MEM_dupalloc_void on a pointer created with "
334 "CPP-style MEM_new or new\n");
337#ifndef DEBUG_MEMDUPLINAME
338 if (memh->alignment == 0) [[likely]] {
346 if (newp ==
nullptr) {
352 const char name_prefix[] =
"dupli_alloc ";
353 const size_t name_prefix_len =
sizeof(name_prefix) - 1;
354 const size_t name_size = strlen(memh->name) + 1;
355 char *name = malloc(name_prefix_len + name_size);
356 memcpy(name, name_prefix,
sizeof(name_prefix));
357 memcpy(name + name_prefix_len, memh->name, name_size);
359 if (memh->alignment == 0) [[likely]] {
373 nmemh->need_free_name = 1;
377 memcpy(newp, vmemh, memh->len);
386 void *newp =
nullptr;
389 MemHead *memh =
static_cast<MemHead *
>(vmemh);
395 "Attempt to use C-style MEM_realloc_uninitialized on a pointer created with "
396 "CPP-style MEM_new or new\n");
399 if (memh->alignment == 0) [[likely]] {
410 memcpy(newp, vmemh,
len);
414 memcpy(newp, vmemh, memh->len);
430 void *newp =
nullptr;
433 MemHead *memh =
static_cast<MemHead *
>(vmemh);
439 "Attempt to use C-style MEM_realloc_zeroed on a pointer created with "
440 "CPP-style MEM_new or new\n");
443 if (memh->alignment == 0) [[likely]] {
454 memcpy(newp, vmemh,
len);
457 memcpy(newp, vmemh, memh->len);
459 if (
len > memh->len) {
462 memset(((
char *)newp) + memh->len, 0,
len - memh->len);
476#ifdef DEBUG_BACKTRACE_EXECINFO
477static void make_memhead_backtrace(MemHead *memh)
479 memh->backtrace_size = backtrace(memh->backtrace, BACKTRACE_SIZE);
482static void print_memhead_backtrace(MemHead *memh)
487 strings = backtrace_symbols(memh->backtrace, memh->backtrace_size);
488 for (i = 0; i < memh->backtrace_size; i++) {
505 memh->nextname =
nullptr;
513#ifdef DEBUG_MEMDUPLINAME
514 memh->need_free_name = 0;
517#ifdef DEBUG_BACKTRACE_EXECINFO
518 make_memhead_backtrace(memh);
521 memt = (
MemTail *)(((
char *)memh) +
sizeof(MemHead) +
len);
530 memh->nextname =
MEMNEXT(memh->next)->name;
541#ifdef WITH_MEM_VALGRIND
542 const size_t len_unaligned =
len;
546 memh = (MemHead *)malloc(
len +
sizeof(MemHead) +
sizeof(
MemTail));
549 if (memh) [[likely]] {
552 if (
len) [[likely]] {
554 memset(memh + 1, 255,
len);
556#ifdef WITH_MEM_VALGRIND
558 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
561 VALGRIND_MAKE_MEM_DEFINED((
const char *)(memh + 1) + len_unaligned,
len - len_unaligned);
566#ifdef DEBUG_MEMCOUNTER
567 if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) {
568 memcount_raise(__func__);
570 memh->_count = _mallocn_count++;
587 "Malloc array aborted due to integer overflow: "
628#ifdef WITH_MEM_VALGRIND
629 const size_t len_unaligned =
len;
634 len + extra_padding +
sizeof(MemHead) +
sizeof(
MemTail), alignment);
637 if (memh) [[likely]] {
642 memh = (MemHead *)((
char *)memh + extra_padding);
645 memh->alignment = short(alignment);
646 if (
len) [[likely]] {
648 memset(memh + 1, 255,
len);
650#ifdef WITH_MEM_VALGRIND
652 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
655 VALGRIND_MAKE_MEM_DEFINED((
const char *)(memh + 1) + len_unaligned,
len - len_unaligned);
660#ifdef DEBUG_MEMCOUNTER
661 if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) {
662 memcount_raise(__func__);
664 memh->_count = _mallocn_count++;
682 memh = (MemHead *)calloc(
len +
sizeof(MemHead) +
sizeof(
MemTail), 1);
687#ifdef DEBUG_MEMCOUNTER
688 if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) {
689 memcount_raise(__func__);
691 memh->_count = _mallocn_count++;
708 "Calloc array aborted due to integer overflow: "
723 const size_t alignment,
729 "Calloc array aborted due to integer overflow: "
738 if (alignment <= MEM_MIN_CPP_ALIGNMENT) {
746 const size_t alignment,
756 const size_t alignment,
767 memset(ptr, 0, bytes_num);
783 return strcmp(pb1->
name, pb2->
name);
791 if (pb1->
len < pb2->
len) {
794 if (pb1->
len == pb2->
len) {
806 size_t mem_in_use_slop = 0;
814 if (!printblock) [[unlikely]] {
816 print_error(
"malloc returned null while generating stats");
821 printblock =
nullptr;
832 while (membl && pb) {
833 pb->
name = membl->name;
834 pb->
len = membl->len;
840#ifdef USE_MALLOC_USABLE_SIZE
841 if (membl->alignment == 0) {
842 mem_in_use_slop += (
sizeof(MemHead) +
sizeof(
MemTail) + malloc_usable_size((
void *)membl)) -
860 for (a = 0,
b = 0; a < totpb; a++) {
864 if (strcmp(printblock[a].name, printblock[
b].name) == 0) {
865 printblock[
b].
len += printblock[a].
len;
880 printf(
"\ntotal memory len: %.3f MB\n",
double(
mem_in_use) /
double(1024 * 1024));
881 printf(
"peak memory len: %.3f MB\n",
double(
peak_mem) /
double(1024 * 1024));
882 printf(
"slop memory len: %.3f MB\n",
double(mem_in_use_slop) /
double(1024 * 1024));
883 printf(
" ITEMS TOTAL-MiB AVERAGE-KiB TYPE\n");
884 for (a = 0, pb = printblock; a < totpb; a++, pb++) {
885 printf(
"%6d (%8.3f %8.3f) %s\n",
887 double(pb->
len) /
double(1024 * 1024),
888 double(pb->
len) / 1024.0 /
double(pb->
items),
892 if (printblock !=
nullptr) {
898#ifdef HAVE_MALLOC_STATS
899 printf(
"System Statistics:\n");
907 "for mb_item in membase:\n"
908 " mb_item_user_size = mb_userinfo.setdefault(mb_item['name'], [0,0])\n"
909 " mb_item_user_size[0] += 1 # Add a user\n"
910 " mb_item_user_size[1] += mb_item['len'] # Increment the size\n"
911 " totmem += mb_item['len']\n"
912 "print('(membase) items:', len(membase), '| unique-names:',\n"
913 " len(mb_userinfo), '| total-mem:', totmem)\n"
914 "mb_userinfo_sort = list(mb_userinfo.items())\n"
915 "for sort_name, sort_func in (('size', lambda a: -a[1][1]),\n"
916 " ('users', lambda a: -a[1][0]),\n"
917 " ('name', lambda a: a[0])):\n"
918 " print('\\nSorting by:', sort_name)\n"
919 " mb_userinfo_sort.sort(key = sort_func)\n"
920 " for item in mb_userinfo_sort:\n"
921 " print('name:%%s, users:%%i, len:%%i' %%\n"
922 " (item[0], item[1][0], item[1][1]))\n";
945 "'pointer':'%p'},\n",
948 (
void *)(membl + 1));
951#ifdef DEBUG_MEMCOUNTER
961 (
void *)(membl + 1));
964#ifdef DEBUG_BACKTRACE_EXECINFO
965 print_memhead_backtrace(membl);
966#elif defined(DEBUG_BACKTRACE) && defined(WITH_ASAN)
967 __asan_describe_address(membl);
1010short MEM_guarded_testN(
void *vmemh)
1022 if (vmemh == membl + 1) {
1035 print_error(
"Memoryblock %p: pointer not in memlist\n", vmemh);
1057 MemHead *memh =
static_cast<MemHead *
>(vmemh);
1060 if (memh ==
nullptr) {
1061 MemorY_ErroR(
"free",
"attempt to free nullptr pointer");
1066 if (
sizeof(intptr_t) == 8) {
1067 if (intptr_t(memh) & 0x7) {
1068 MemorY_ErroR(
"free",
"attempt to free illegal pointer");
1073 if (intptr_t(memh) & 0x3) {
1074 MemorY_ErroR(
"free",
"attempt to free illegal pointer");
1085 "Attempt to use C-style MEM_delete_void on a pointer created with "
1086 "CPP-style MEM_new or new\n");
1094 if ((memh->tag1 ==
MEMTAG1) && (memh->tag2 ==
MEMTAG2) && ((memh->len & 0x3) == 0)) {
1095 memt = (
MemTail *)(((
char *)memh) +
sizeof(MemHead) + memh->len);
1112 if (name !=
nullptr) {
1113 if (name != memh->name) {
1122 if (name ==
nullptr) {
1145 if (link ==
nullptr) {
1148 if (listbase ==
nullptr) {
1153 link->
next =
nullptr;
1156 if (listbase->
last) {
1159 if (listbase->
first ==
nullptr) {
1160 listbase->
first = link;
1162 listbase->
last = link;
1172 if (link ==
nullptr) {
1175 if (listbase ==
nullptr) {
1187 if (listbase->
last == link) {
1190 if (listbase->
first == link) {
1204 MEMNEXT(memh->prev)->nextname =
nullptr;
1212#ifdef DEBUG_MEMDUPLINAME
1213 if (memh->need_free_name) {
1214 free((
char *)memh->name);
1219 memset(memh + 1, 255, memh->len);
1221 if (memh->alignment == 0) [[likely]] {
1232 print_error(
"Memoryblock %s: %s\n", block, error);
1234#ifdef WITH_ASSERT_ABORT
1241 MemHead *forw, *back, *forwok, *backok;
1281 return (
"MORE THAN 1 MEMORYBLOCK CORRUPT");
1284 if (forw ==
nullptr && back ==
nullptr) {
1307 if (forw ==
nullptr) {
1334 name = forwok->nextname;
1337 name =
"No name found";
1344 forwok->next = (MemHead *)&backok->next;
1345 backok->prev = (MemHead *)&forwok->next;
1346 forwok->nextname = backok->name;
1349 forwok->next =
nullptr;
1355 backok->prev =
nullptr;
1366 return (
"Additional error in header");
1416 MemHead *memh =
static_cast<MemHead *
>(vmemh);
1421 return "MEM_guarded_name_ptr(nullptr)";
1430 MemHead *memh =
static_cast<MemHead *
>(vmemh);
Read Blender memory allocation and freeing.
bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
#define PRF_memory_free(ptr)
#define PRF_scope(category)
#define PRF_memory_alloc(ptr, size)
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE size_t atomic_add_and_fetch_z(size_t *p, size_t x)
ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x)
ATOMIC_INLINE unsigned int atomic_add_and_fetch_u(unsigned int *p, unsigned int x)
ATOMIC_INLINE unsigned int atomic_sub_and_fetch_u(unsigned int *p, unsigned int x)
struct AtomicSpinLock __attribute__((aligned(32))) AtomicSpinLock
int pad[32 - sizeof(int)]
#define assert(assertion)
bool leak_detector_has_run
char free_after_leak_detection_message[]
void aligned_free(void *ptr)
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str)
void * aligned_malloc(size_t size, size_t alignment)
static pthread_mutex_t thread_lock
static void MEM_guarded_printmemlist_internal(int pydict)
void * MEM_guarded_calloc_arrayN_aligned(const size_t len, const size_t size, const size_t alignment, const char *str)
static void rem_memblock(MemHead *memh)
static const char mem_printmemlist_pydict_script[]
size_t MEM_guarded_allocN_len(const void *vmemh)
static int compare_len(const void *p1, const void *p2)
void * MEM_guarded_mallocN(size_t len, const char *str)
size_t MEM_guarded_get_peak_memory()
void MEM_guarded_freeN(void *vmemh, const DestructorType destructor_type)
static int compare_name(const void *p1, const void *p2)
static void remlink(volatile localListBase *listbase, void *vlink)
static void(*) error_callback(const char *)
void * MEM_guarded_calloc_arrayN(size_t len, size_t size, const char *str)
void MEM_guarded_reset_peak_memory()
void MEM_guarded_set_error_callback(void(*func)(const char *))
bool MEM_guarded_consistency_check()
static bool malloc_debug_memset
void MEM_guarded_printmemlist_pydict()
void MEM_guarded_callbackmemlist(void(*func)(void *))
void MEM_guarded_printmemlist()
uint MEM_guarded_get_memory_blocks_in_use()
static void MemorY_ErroR(const char *block, const char *error)
static void mem_unlock_thread()
void * MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str, const DestructorType destructor_type)
void * MEM_guarded_callocN(size_t len, const char *str)
void * MEM_guarded_reallocN_id(void *vmemh, size_t len, const char *str)
static void print_error(const char *message, va_list str_format_args)
static void report_error_on_address(const void *vmemh, const char *message,...)
void MEM_guarded_printmemlist_stats()
static const char * check_memlist(const MemHead *memh)
static void * mem_guarded_malloc_arrayN_aligned(const size_t len, const size_t size, const size_t alignment, const char *str, size_t &r_bytes_num)
void * MEM_guarded_malloc_arrayN(size_t len, size_t size, const char *str)
void * MEM_guarded_dupallocN(const void *vmemh)
size_t MEM_guarded_get_memory_in_use()
static void make_memhead_header(MemHead *memh, size_t len, const char *str, const DestructorType destructor_type)
void * MEM_guarded_recallocN_id(void *vmemh, size_t len, const char *str)
static void addtail(volatile localListBase *listbase, void *vlink)
static volatile localListBase _membase
void MEM_guarded_name_ptr_set(void *vmemh, const char *str)
@ MEMHEAD_FLAG_NONTRIVIAL_DESTRUCTOR
static volatile localListBase * membase
void MEM_guarded_set_memory_debug()
const char * MEM_guarded_name_ptr(void *vmemh)
void mem_guarded_clearmemlist()
static void mem_lock_thread()
void * MEM_guarded_malloc_arrayN_aligned(const size_t len, const size_t size, const size_t alignment, const char *str)
MEM_INLINE void MEM_trigger_error_on_memory_block(const void *, const size_t)
#define MEMHEAD_REAL_PTR(memh)
#define MEMHEAD_ALIGN_PADDING(alignment)
#define SIZET_ALIGN_4(len)
#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
void free(struct bMotionPath *mpath)
std::string format(const std::string &fmt, Args &&...args)
void *(*) mem_mallocN(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)