Blender V5.3
mallocn_guarded_impl.cc
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
10
11#include <stdarg.h>
12#include <stddef.h> /* offsetof */
13#include <stdio.h> /* printf */
14#include <stdlib.h>
15#include <string.h> /* memcpy */
16#include <sys/types.h>
17
18#include <pthread.h>
19
20#include "MEM_guardedalloc.h"
21#include "MEM_safe_multiply.h"
22
23/* Quiet warnings when dealing with allocated data written into the blend file.
24 * This also rounds up and causes warnings which we don't consider bugs in practice. */
25#ifdef WITH_MEM_VALGRIND
26# include "valgrind/memcheck.h"
27#endif
28
29#include "PRF_profile.hh"
30/* to ensure strict conversions */
31#include "../../source/blender/blenlib/BLI_strict_flags.hh"
32
33#include "atomic_ops.h"
34#include "mallocn_intern.hh"
36
37using namespace mem_guarded::internal;
38
39/* Only for debugging:
40 * store original buffer's name when doing MEM_dupalloc_void
41 * helpful to profile issues with non-freed "dup_alloc" buffers,
42 * but this introduces some overhead to memory header and makes
43 * things slower a bit, so better to keep disabled by default
44 */
45// #define DEBUG_MEMDUPLINAME
46
47/* Only for debugging:
48 * lets you count the allocations so as to find the allocator of unfreed memory
49 * in situations where the leak is predictable */
50
51// #define DEBUG_MEMCOUNTER
52
53/* Only for debugging:
54 * Defining DEBUG_BACKTRACE will display a back-trace from where memory block was allocated and
55 * print this trace for all unfreed blocks. This will only work for ASAN enabled builds. This
56 * option will be on by default for MSVC as it currently does not have LSAN which would normally
57 * report these leaks, off by default on all other platforms because it would report the leaks
58 * twice, once here, and once by LSAN.
59 */
60#if defined(_MSC_VER)
61# ifdef WITH_ASAN
62# define DEBUG_BACKTRACE
63# endif
64#else
65/* Un-comment to report back-traces with leaks, uses ASAN when enabled.
66 * NOTE: The default linking options cause the stack traces only to include addresses.
67 * Use `addr2line` to expand into file, line & function identifiers,
68 * see: `tools/utils/addr2line_backtrace.py` convenience utility. */
69// # define DEBUG_BACKTRACE
70#endif
71
72#ifdef DEBUG_BACKTRACE
73# ifdef WITH_ASAN
74/* Rely on address sanitizer. */
75# else
76# if defined(__linux__) || defined(__APPLE__)
77# define DEBUG_BACKTRACE_EXECINFO
78# else
79# error "DEBUG_BACKTRACE: not supported for this platform!"
80# endif
81# endif
82#endif
83
84#ifdef DEBUG_BACKTRACE_EXECINFO
85# define BACKTRACE_SIZE 100
86#endif
87
88#ifdef DEBUG_MEMCOUNTER
89/* set this to the value that isn't being freed */
90# define DEBUG_MEMCOUNTER_ERROR_VAL 0
91static int _mallocn_count = 0;
92
93/* Break-point here. */
94static void memcount_raise(const char *name)
95{
96 fprintf(stderr, "%s: memcount-leak, %d\n", name, _mallocn_count);
97}
98#endif
99
100/* --------------------------------------------------------------------- */
101/* Data definition */
102/* --------------------------------------------------------------------- */
103/* all memory chunks are put in linked lists */
107
108typedef struct localListBase {
109 void *first, *last;
111
112namespace {
113
114/* NOTE(@hos): keep this struct aligned (e.g., IRIX/GCC). */
115typedef struct MemHead {
116 int tag1;
117 size_t len;
118 MemHead *next, *prev;
119 const char *name;
120 const char *nextname;
121 int tag2;
122 uint16_t flag;
123 /* if non-zero aligned allocation was used and alignment is stored here. */
124 short alignment;
125#ifdef DEBUG_MEMCOUNTER
126 int _count;
127#endif
128
129#ifdef DEBUG_MEMDUPLINAME
130 int need_free_name, pad;
131#endif
132
133#ifdef DEBUG_BACKTRACE_EXECINFO
134 void *backtrace[BACKTRACE_SIZE];
135 int backtrace_size;
136#endif
137
138} MemHead;
139static_assert(MEM_MIN_CPP_ALIGNMENT <= alignof(MemHead), "Bad alignment of MemHead");
140static_assert(MEM_MIN_CPP_ALIGNMENT <= sizeof(MemHead), "Bad size of MemHead");
141
142typedef MemHead MemHeadAligned;
143
144} // namespace
145
146/* #MemHead::flag. */
154
155typedef struct MemTail {
156 int tag3, pad;
158
159#ifdef DEBUG_BACKTRACE_EXECINFO
160# include <execinfo.h>
161#endif
162
163/* --------------------------------------------------------------------- */
164/* local functions */
165/* --------------------------------------------------------------------- */
166
167static void addtail(volatile localListBase *listbase, void *vlink);
168static void remlink(volatile localListBase *listbase, void *vlink);
169static void rem_memblock(MemHead *memh);
170static void MemorY_ErroR(const char *block, const char *error);
171static const char *check_memlist(const MemHead *memh);
172
173/* --------------------------------------------------------------------- */
174/* locally used defines */
175/* --------------------------------------------------------------------- */
176
177/* NOTE: this is endianness-sensitive. */
178#define MAKE_ID(a, b, c, d) (int(d) << 24 | int(c) << 16 | (b) << 8 | (a))
179
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')
184
185#define MEMNEXT(x) ((MemHead *)(((char *)x) - offsetof(MemHead, next)))
186
187/* --------------------------------------------------------------------- */
188/* vars */
189/* --------------------------------------------------------------------- */
190
191static uint totblock = 0;
192static size_t mem_in_use = 0, peak_mem = 0;
193
194static volatile localListBase _membase;
195static volatile localListBase *membase = &_membase;
196static void (*error_callback)(const char *) = nullptr;
197
198static bool malloc_debug_memset = false;
199
200#ifdef malloc
201# undef malloc
202#endif
203
204#ifdef calloc
205# undef calloc
206#endif
207
208#ifdef free
209# undef free
210#endif
211
212/* --------------------------------------------------------------------- */
213/* implementation */
214/* --------------------------------------------------------------------- */
215
216#ifdef __GNUC__
217__attribute__((format(printf, 1, 0)))
218#endif
219static void print_error(const char *message, va_list str_format_args)
220{
221 char buf[512];
222 vsnprintf(buf, sizeof(buf), message, str_format_args);
223 buf[sizeof(buf) - 1] = '\0';
224
225 if (error_callback) {
226 error_callback(buf);
227 }
228 else {
229 fputs(buf, stderr);
230 }
231}
232
233#ifdef __GNUC__
235#endif
236static void print_error(const char *message, ...)
237{
238 va_list str_format_args;
239 va_start(str_format_args, message);
240 print_error(message, str_format_args);
241 va_end(str_format_args);
242}
243
244#ifdef __GNUC__
246#endif
247static void report_error_on_address(const void *vmemh, const char *message, ...)
248{
249 va_list str_format_args;
250
251 va_start(str_format_args, message);
252 print_error(message, str_format_args);
253 va_end(str_format_args);
254
255 if (vmemh == nullptr) {
257 return;
258 }
259
260 const MemHead *memh = static_cast<const MemHead *>(vmemh);
261 memh--;
262 size_t len = memh->len;
263
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;
268 address = MEMHEAD_REAL_PTR(memh_aligned);
269 size = len + sizeof(*memh_aligned) + MEMHEAD_ALIGN_PADDING(memh_aligned->alignment) +
270 sizeof(MemTail);
271 }
273}
274
275static pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER;
276
277static void mem_lock_thread()
278{
279 pthread_mutex_lock(&thread_lock);
280}
281
282static void mem_unlock_thread()
283{
284 pthread_mutex_unlock(&thread_lock);
285}
286
288{
289 const char *err_val = nullptr;
290 const MemHead *listend;
291 /* check_memlist starts from the front, and runs until it finds
292 * the requested chunk. For this test, that's the last one. */
293 listend = static_cast<MemHead *>(membase->last);
294
295 err_val = check_memlist(listend);
296
297 return (err_val == nullptr);
298}
299
300void MEM_guarded_set_error_callback(void (*func)(const char *))
301{
302 error_callback = func;
303}
304
309
310size_t MEM_guarded_allocN_len(const void *vmemh)
311{
312 if (vmemh) {
313 const MemHead *memh = static_cast<const MemHead *>(vmemh);
314
315 memh--;
316 return memh->len;
317 }
318
319 return 0;
320}
321
322void *MEM_guarded_dupallocN(const void *vmemh)
323{
325 void *newp = nullptr;
326
327 if (vmemh) {
328 const MemHead *memh = static_cast<const MemHead *>(vmemh);
329 memh--;
330
331 if ((memh->flag & MEMHEAD_FLAG_NONTRIVIAL_DESTRUCTOR) != 0) {
333 "Attempt to use C-style MEM_dupalloc_void on a pointer created with "
334 "CPP-style MEM_new or new\n");
335 }
336
337#ifndef DEBUG_MEMDUPLINAME
338 if (memh->alignment == 0) [[likely]] {
339 newp = MEM_guarded_mallocN(memh->len, "dupli_alloc");
340 }
341 else {
343 memh->len, size_t(memh->alignment), "dupli_alloc", DestructorType::Trivial);
344 }
345
346 if (newp == nullptr) {
347 return nullptr;
348 }
349#else
350 {
351 MemHead *nmemh;
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);
358
359 if (memh->alignment == 0) [[likely]] {
360 newp = MEM_guarded_mallocN(memh->len, name);
361 }
362 else {
364 memh->len, size_t(memh->alignment), name, DestructorType::Trivial);
365 }
366
367 if (newp == nullptr)
368 return nullptr;
369
370 nmemh = newp;
371 nmemh--;
372
373 nmemh->need_free_name = 1;
374 }
375#endif
376
377 memcpy(newp, vmemh, memh->len);
378 }
379
380 return newp;
381}
382
383void *MEM_guarded_reallocN_id(void *vmemh, size_t len, const char *str)
384{
386 void *newp = nullptr;
387
388 if (vmemh) {
389 MemHead *memh = static_cast<MemHead *>(vmemh);
390 memh--;
391
392 if ((memh->flag & MEMHEAD_FLAG_NONTRIVIAL_DESTRUCTOR) != 0) {
394 vmemh,
395 "Attempt to use C-style MEM_realloc_uninitialized on a pointer created with "
396 "CPP-style MEM_new or new\n");
397 }
398
399 if (memh->alignment == 0) [[likely]] {
400 newp = MEM_guarded_mallocN(len, memh->name);
401 }
402 else {
404 len, size_t(memh->alignment), memh->name, DestructorType::Trivial);
405 }
406
407 if (newp) {
408 if (len < memh->len) {
409 /* shrink */
410 memcpy(newp, vmemh, len);
411 }
412 else {
413 /* grow (or remain same size) */
414 memcpy(newp, vmemh, memh->len);
415 }
416 }
417
419 }
420 else {
421 newp = MEM_guarded_mallocN(len, str);
422 }
423
424 return newp;
425}
426
427void *MEM_guarded_recallocN_id(void *vmemh, size_t len, const char *str)
428{
430 void *newp = nullptr;
431
432 if (vmemh) {
433 MemHead *memh = static_cast<MemHead *>(vmemh);
434 memh--;
435
436 if ((memh->flag & MEMHEAD_FLAG_NONTRIVIAL_DESTRUCTOR) != 0) {
438 vmemh,
439 "Attempt to use C-style MEM_realloc_zeroed on a pointer created with "
440 "CPP-style MEM_new or new\n");
441 }
442
443 if (memh->alignment == 0) [[likely]] {
444 newp = MEM_guarded_mallocN(len, memh->name);
445 }
446 else {
448 len, size_t(memh->alignment), memh->name, DestructorType::Trivial);
449 }
450
451 if (newp) {
452 if (len < memh->len) {
453 /* shrink */
454 memcpy(newp, vmemh, len);
455 }
456 else {
457 memcpy(newp, vmemh, memh->len);
458
459 if (len > memh->len) {
460 /* grow */
461 /* zero new bytes */
462 memset(((char *)newp) + memh->len, 0, len - memh->len);
463 }
464 }
465 }
466
468 }
469 else {
470 newp = MEM_guarded_callocN(len, str);
471 }
472
473 return newp;
474}
475
476#ifdef DEBUG_BACKTRACE_EXECINFO
477static void make_memhead_backtrace(MemHead *memh)
478{
479 memh->backtrace_size = backtrace(memh->backtrace, BACKTRACE_SIZE);
480}
481
482static void print_memhead_backtrace(MemHead *memh)
483{
484 char **strings;
485 int i;
486
487 strings = backtrace_symbols(memh->backtrace, memh->backtrace_size);
488 for (i = 0; i < memh->backtrace_size; i++) {
489 print_error(" %s\n", strings[i]);
490 }
491
492 free(strings);
493}
494#endif /* DEBUG_BACKTRACE_EXECINFO */
495
496static void make_memhead_header(MemHead *memh,
497 size_t len,
498 const char *str,
499 const DestructorType destructor_type)
500{
501 MemTail *memt;
502
503 memh->tag1 = MEMTAG1;
504 memh->name = str;
505 memh->nextname = nullptr;
506 memh->len = len;
507 memh->flag = (destructor_type == DestructorType::NonTrivial ?
509 0);
510 memh->alignment = 0;
511 memh->tag2 = MEMTAG2;
512
513#ifdef DEBUG_MEMDUPLINAME
514 memh->need_free_name = 0;
515#endif
516
517#ifdef DEBUG_BACKTRACE_EXECINFO
518 make_memhead_backtrace(memh);
519#endif
520
521 memt = (MemTail *)(((char *)memh) + sizeof(MemHead) + len);
522 memt->tag3 = MEMTAG3;
523
526
528 addtail(membase, &memh->next);
529 if (memh->next) {
530 memh->nextname = MEMNEXT(memh->next)->name;
531 }
534}
535
536void *MEM_guarded_mallocN(size_t len, const char *str)
537{
539 MemHead *memh;
540
541#ifdef WITH_MEM_VALGRIND
542 const size_t len_unaligned = len;
543#endif
545
546 memh = (MemHead *)malloc(len + sizeof(MemHead) + sizeof(MemTail));
547 PRF_memory_alloc(memh, len + sizeof(MemHead) + sizeof(MemTail));
548
549 if (memh) [[likely]] {
551
552 if (len) [[likely]] {
553 if (malloc_debug_memset) [[unlikely]] {
554 memset(memh + 1, 255, len);
555 }
556#ifdef WITH_MEM_VALGRIND
558 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
559 }
560 else {
561 VALGRIND_MAKE_MEM_DEFINED((const char *)(memh + 1) + len_unaligned, len - len_unaligned);
562 }
563#endif /* WITH_MEM_VALGRIND */
564 }
565
566#ifdef DEBUG_MEMCOUNTER
567 if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) {
568 memcount_raise(__func__);
569 }
570 memh->_count = _mallocn_count++;
571#endif
572 return (++memh);
573 }
574 print_error("Malloc returns null: len=" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n",
575 SIZET_ARG(len),
576 str,
577 mem_in_use);
578 return nullptr;
579}
580
581void *MEM_guarded_malloc_arrayN(size_t len, size_t size, const char *str)
582{
584 size_t total_size;
585 if (!MEM_size_safe_multiply(len, size, &total_size)) [[unlikely]] {
587 "Malloc array aborted due to integer overflow: "
588 "len=" SIZET_FORMAT "x" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n",
589 SIZET_ARG(len),
590 SIZET_ARG(size),
591 str,
592 mem_in_use);
593 abort();
594 return nullptr;
595 }
596
597 return MEM_guarded_mallocN(total_size, str);
598}
599
601 size_t alignment,
602 const char *str,
603 const DestructorType destructor_type)
604{
606 /* Huge alignment values doesn't make sense and they wouldn't fit into 'short' used in the
607 * MemHead. */
608 assert(alignment < 1024);
609
610 /* We only support alignments that are a power of two. */
611 assert(IS_POW2(alignment));
612
613 /* Some OS specific aligned allocators require a certain minimal alignment. */
614 /* And #MEM_guarded_freeN also checks that it is freeing a pointer aligned with `sizeof(void *)`.
615 */
616 if (alignment < ALIGNED_MALLOC_MINIMUM_ALIGNMENT) {
618 }
619
620 /* It's possible that MemHead's size is not properly aligned,
621 * do extra padding to deal with this.
622 *
623 * We only support small alignments which fits into short in
624 * order to save some bits in MemHead structure.
625 */
626 size_t extra_padding = MEMHEAD_ALIGN_PADDING(alignment);
627
628#ifdef WITH_MEM_VALGRIND
629 const size_t len_unaligned = len;
630#endif
632
633 MemHead *memh = (MemHead *)aligned_malloc(
634 len + extra_padding + sizeof(MemHead) + sizeof(MemTail), alignment);
635 PRF_memory_alloc(memh, len + extra_padding + sizeof(MemHead) + sizeof(MemTail));
636
637 if (memh) [[likely]] {
638 /* We keep padding in the beginning of MemHead,
639 * this way it's always possible to get MemHead
640 * from the data pointer.
641 */
642 memh = (MemHead *)((char *)memh + extra_padding);
643
644 make_memhead_header(memh, len, str, destructor_type);
645 memh->alignment = short(alignment);
646 if (len) [[likely]] {
647 if (malloc_debug_memset) [[unlikely]] {
648 memset(memh + 1, 255, len);
649 }
650#ifdef WITH_MEM_VALGRIND
652 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
653 }
654 else {
655 VALGRIND_MAKE_MEM_DEFINED((const char *)(memh + 1) + len_unaligned, len - len_unaligned);
656 }
657#endif /* WITH_MEM_VALGRIND */
658 }
659
660#ifdef DEBUG_MEMCOUNTER
661 if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) {
662 memcount_raise(__func__);
663 }
664 memh->_count = _mallocn_count++;
665#endif
666 return (++memh);
667 }
668 print_error("aligned_malloc returns null: len=" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n",
669 SIZET_ARG(len),
670 str,
671 mem_in_use);
672 return nullptr;
673}
674
675void *MEM_guarded_callocN(size_t len, const char *str)
676{
678 MemHead *memh;
679
681
682 memh = (MemHead *)calloc(len + sizeof(MemHead) + sizeof(MemTail), 1);
683 PRF_memory_alloc(memh, len + sizeof(MemHead) + sizeof(MemTail));
684
685 if (memh) {
687#ifdef DEBUG_MEMCOUNTER
688 if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) {
689 memcount_raise(__func__);
690 }
691 memh->_count = _mallocn_count++;
692#endif
693 return (++memh);
694 }
695 print_error("Calloc returns null: len=" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n",
696 SIZET_ARG(len),
697 str,
698 mem_in_use);
699 return nullptr;
700}
701
702void *MEM_guarded_calloc_arrayN(size_t len, size_t size, const char *str)
703{
705 size_t total_size;
706 if (!MEM_size_safe_multiply(len, size, &total_size)) [[unlikely]] {
708 "Calloc array aborted due to integer overflow: "
709 "len=" SIZET_FORMAT "x" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n",
710 SIZET_ARG(len),
711 SIZET_ARG(size),
712 str,
713 mem_in_use);
714 abort();
715 return nullptr;
716 }
717
718 return MEM_guarded_callocN(total_size, str);
719}
720
721static void *mem_guarded_malloc_arrayN_aligned(const size_t len,
722 const size_t size,
723 const size_t alignment,
724 const char *str,
725 size_t &r_bytes_num)
726{
727 if (!MEM_size_safe_multiply(len, size, &r_bytes_num)) [[unlikely]] {
729 "Calloc array aborted due to integer overflow: "
730 "len=" SIZET_FORMAT "x" SIZET_FORMAT " in %s, total " SIZET_FORMAT "\n",
731 SIZET_ARG(len),
732 SIZET_ARG(size),
733 str,
734 mem_in_use);
735 abort();
736 return nullptr;
737 }
738 if (alignment <= MEM_MIN_CPP_ALIGNMENT) {
739 return mem_mallocN(r_bytes_num, str);
740 }
741 return MEM_new_uninitialized_aligned(r_bytes_num, alignment, str);
742}
743
745 const size_t size,
746 const size_t alignment,
747 const char *str)
748{
750 size_t bytes_num;
751 return mem_guarded_malloc_arrayN_aligned(len, size, alignment, str, bytes_num);
752}
753
755 const size_t size,
756 const size_t alignment,
757 const char *str)
758{
760 size_t bytes_num;
761 /* There is no lower level #calloc with an alignment parameter, so we have to fall back to using
762 * #memset unfortunately. */
763 void *ptr = mem_guarded_malloc_arrayN_aligned(len, size, alignment, str, bytes_num);
764 if (!ptr) {
765 return nullptr;
766 }
767 memset(ptr, 0, bytes_num);
768 return ptr;
769}
770
771/* Memory statistics print */
772typedef struct MemPrintBlock {
773 const char *name;
774 uintptr_t len;
775 int items;
777
778static int compare_name(const void *p1, const void *p2)
779{
780 const MemPrintBlock *pb1 = (const MemPrintBlock *)p1;
781 const MemPrintBlock *pb2 = (const MemPrintBlock *)p2;
782
783 return strcmp(pb1->name, pb2->name);
784}
785
786static int compare_len(const void *p1, const void *p2)
787{
788 const MemPrintBlock *pb1 = (const MemPrintBlock *)p1;
789 const MemPrintBlock *pb2 = (const MemPrintBlock *)p2;
790
791 if (pb1->len < pb2->len) {
792 return 1;
793 }
794 if (pb1->len == pb2->len) {
795 return 0;
796 }
797
798 return -1;
799}
800
802{
803 MemHead *membl;
804 MemPrintBlock *pb, *printblock;
805 uint totpb, a, b;
806 size_t mem_in_use_slop = 0;
807
809
810 if (totblock != 0) {
811 /* put memory blocks into array */
812 printblock = static_cast<MemPrintBlock *>(malloc(sizeof(MemPrintBlock) * totblock));
813
814 if (!printblock) [[unlikely]] {
816 print_error("malloc returned null while generating stats");
817 return;
818 }
819 }
820 else {
821 printblock = nullptr;
822 }
823
824 pb = printblock;
825 totpb = 0;
826
827 membl = static_cast<MemHead *>(membase->first);
828 if (membl) {
829 membl = MEMNEXT(membl);
830 }
831
832 while (membl && pb) {
833 pb->name = membl->name;
834 pb->len = membl->len;
835 pb->items = 1;
836
837 totpb++;
838 pb++;
839
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)) -
843 membl->len;
844 }
845#endif
846
847 if (membl->next) {
848 membl = MEMNEXT(membl->next);
849 }
850 else {
851 break;
852 }
853 }
854
855 /* sort by name and add together blocks with the same name */
856 if (totpb > 1) {
857 qsort(printblock, totpb, sizeof(MemPrintBlock), compare_name);
858 }
859
860 for (a = 0, b = 0; a < totpb; a++) {
861 if (a == b) {
862 continue;
863 }
864 if (strcmp(printblock[a].name, printblock[b].name) == 0) {
865 printblock[b].len += printblock[a].len;
866 printblock[b].items++;
867 }
868 else {
869 b++;
870 memcpy(&printblock[b], &printblock[a], sizeof(MemPrintBlock));
871 }
872 }
873 totpb = b + 1;
874
875 /* sort by length and print */
876 if (totpb > 1) {
877 qsort(printblock, totpb, sizeof(MemPrintBlock), compare_len);
878 }
879
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",
886 pb->items,
887 double(pb->len) / double(1024 * 1024),
888 double(pb->len) / 1024.0 / double(pb->items),
889 pb->name);
890 }
891
892 if (printblock != nullptr) {
893 free(printblock);
894 }
895
897
898#ifdef HAVE_MALLOC_STATS
899 printf("System Statistics:\n");
900 malloc_stats();
901#endif
902}
903
905 "mb_userinfo = {}\n"
906 "totmem = 0\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";
923
924/* Prints in python syntax for easy */
926{
927 MemHead *membl;
928
930
931 membl = static_cast<MemHead *>(membase->first);
932 if (membl) {
933 membl = MEMNEXT(membl);
934 }
935
936 if (pydict) {
937 print_error("# membase_debug.py\n");
938 print_error("membase = [\n");
939 }
940 while (membl) {
941 if (pydict) {
942 print_error(" {'len':" SIZET_FORMAT
943 ", "
944 "'name':'''%s''', "
945 "'pointer':'%p'},\n",
946 SIZET_ARG(membl->len),
947 membl->name,
948 (void *)(membl + 1));
949 }
950 else {
951#ifdef DEBUG_MEMCOUNTER
952 print_error("%s len: " SIZET_FORMAT " %p, count: %d\n",
953 membl->name,
954 SIZET_ARG(membl->len),
955 membl + 1,
956 membl->_count);
957#else
958 print_error("%s len: " SIZET_FORMAT " %p\n",
959 membl->name,
960 SIZET_ARG(membl->len),
961 (void *)(membl + 1));
962#endif
963
964#ifdef DEBUG_BACKTRACE_EXECINFO
965 print_memhead_backtrace(membl);
966#elif defined(DEBUG_BACKTRACE) && defined(WITH_ASAN)
967 __asan_describe_address(membl);
968#endif
969 }
970 if (membl->next) {
971 membl = MEMNEXT(membl->next);
972 }
973 else {
974 break;
975 }
976 }
977 if (pydict) {
978 print_error("]\n\n");
980 }
981
983}
984
985void MEM_guarded_callbackmemlist(void (*func)(void *))
986{
987 MemHead *membl;
988
990
991 membl = static_cast<MemHead *>(membase->first);
992 if (membl) {
993 membl = MEMNEXT(membl);
994 }
995
996 while (membl) {
997 func(membl + 1);
998 if (membl->next) {
999 membl = MEMNEXT(membl->next);
1000 }
1001 else {
1002 break;
1003 }
1004 }
1005
1007}
1008
1009#if 0
1010short MEM_guarded_testN(void *vmemh)
1011{
1012 MemHead *membl;
1013
1015
1016 membl = membase->first;
1017 if (membl) {
1018 membl = MEMNEXT(membl);
1019 }
1020
1021 while (membl) {
1022 if (vmemh == membl + 1) {
1024 return 1;
1025 }
1026
1027 if (membl->next)
1028 membl = MEMNEXT(membl->next);
1029 else
1030 break;
1031 }
1032
1034
1035 print_error("Memoryblock %p: pointer not in memlist\n", vmemh);
1036 return 0;
1037}
1038#endif
1039
1049{
1050 membase->first = nullptr;
1051 membase->last = nullptr;
1052}
1053
1054void MEM_guarded_freeN(void *vmemh, const DestructorType destructor_type)
1055{
1056 MemTail *memt;
1057 MemHead *memh = static_cast<MemHead *>(vmemh);
1058 const char *name;
1059
1060 if (memh == nullptr) {
1061 MemorY_ErroR("free", "attempt to free nullptr pointer");
1062 // print_error(err_stream, "%d\n", (memh+4000)->tag1);
1063 return;
1064 }
1065
1066 if (sizeof(intptr_t) == 8) {
1067 if (intptr_t(memh) & 0x7) {
1068 MemorY_ErroR("free", "attempt to free illegal pointer");
1069 return;
1070 }
1071 }
1072 else {
1073 if (intptr_t(memh) & 0x3) {
1074 MemorY_ErroR("free", "attempt to free illegal pointer");
1075 return;
1076 }
1077 }
1078
1079 memh--;
1080
1081 if (destructor_type != DestructorType::NonTrivial &&
1083 {
1085 "Attempt to use C-style MEM_delete_void on a pointer created with "
1086 "CPP-style MEM_new or new\n");
1087 }
1088
1089 if (memh->tag1 == MEMFREE && memh->tag2 == MEMFREE) {
1090 MemorY_ErroR(memh->name, "double free");
1091 return;
1092 }
1093
1094 if ((memh->tag1 == MEMTAG1) && (memh->tag2 == MEMTAG2) && ((memh->len & 0x3) == 0)) {
1095 memt = (MemTail *)(((char *)memh) + sizeof(MemHead) + memh->len);
1096 if (memt->tag3 == MEMTAG3) {
1097
1100 }
1101
1102 memh->tag1 = MEMFREE;
1103 memh->tag2 = MEMFREE;
1104 memt->tag3 = MEMFREE;
1105 /* after tags !!! */
1106 rem_memblock(memh);
1107
1108 return;
1109 }
1110 MemorY_ErroR(memh->name, "end corrupt");
1111 name = check_memlist(memh);
1112 if (name != nullptr) {
1113 if (name != memh->name) {
1114 MemorY_ErroR(name, "is also corrupt");
1115 }
1116 }
1117 }
1118 else {
1120 name = check_memlist(memh);
1122 if (name == nullptr) {
1123 MemorY_ErroR("free", "pointer not in memlist");
1124 }
1125 else {
1126 MemorY_ErroR(name, "error in header");
1127 }
1128 }
1129
1130 totblock--;
1131 /* here a DUMP should happen */
1132}
1133
1134/* --------------------------------------------------------------------- */
1135/* local functions */
1136/* --------------------------------------------------------------------- */
1137
1138static void addtail(volatile localListBase *listbase, void *vlink)
1139{
1140 localLink *link = static_cast<localLink *>(vlink);
1141
1142 /* for a generic API error checks here is fine but
1143 * the limited use here they will never be nullptr */
1144#if 0
1145 if (link == nullptr) {
1146 return;
1147 }
1148 if (listbase == nullptr) {
1149 return;
1150 }
1151#endif
1152
1153 link->next = nullptr;
1154 link->prev = static_cast<localLink *>(listbase->last);
1155
1156 if (listbase->last) {
1157 ((localLink *)listbase->last)->next = link;
1158 }
1159 if (listbase->first == nullptr) {
1160 listbase->first = link;
1161 }
1162 listbase->last = link;
1163}
1164
1165static void remlink(volatile localListBase *listbase, void *vlink)
1166{
1167 localLink *link = static_cast<localLink *>(vlink);
1168
1169 /* for a generic API error checks here is fine but
1170 * the limited use here they will never be nullptr */
1171#if 0
1172 if (link == nullptr) {
1173 return;
1174 }
1175 if (listbase == nullptr) {
1176 return;
1177 }
1178#endif
1179
1180 if (link->next) {
1181 link->next->prev = link->prev;
1182 }
1183 if (link->prev) {
1184 link->prev->next = link->next;
1185 }
1186
1187 if (listbase->last == link) {
1188 listbase->last = link->prev;
1189 }
1190 if (listbase->first == link) {
1191 listbase->first = link->next;
1192 }
1193}
1194
1195static void rem_memblock(MemHead *memh)
1196{
1198 remlink(membase, &memh->next);
1199 if (memh->prev) {
1200 if (memh->next) {
1201 MEMNEXT(memh->prev)->nextname = MEMNEXT(memh->next)->name;
1202 }
1203 else {
1204 MEMNEXT(memh->prev)->nextname = nullptr;
1205 }
1206 }
1208
1211
1212#ifdef DEBUG_MEMDUPLINAME
1213 if (memh->need_free_name) {
1214 free((char *)memh->name);
1215 }
1216#endif
1217
1218 if (malloc_debug_memset && memh->len) [[unlikely]] {
1219 memset(memh + 1, 255, memh->len);
1220 }
1221 if (memh->alignment == 0) [[likely]] {
1222 PRF_memory_free(memh);
1223 free(memh);
1224 }
1225 else {
1227 }
1228}
1229
1230static void MemorY_ErroR(const char *block, const char *error)
1231{
1232 print_error("Memoryblock %s: %s\n", block, error);
1233
1234#ifdef WITH_ASSERT_ABORT
1235 abort();
1236#endif
1237}
1238
1239static const char *check_memlist(const MemHead *memh)
1240{
1241 MemHead *forw, *back, *forwok, *backok;
1242 const char *name;
1243
1244 forw = static_cast<MemHead *>(membase->first);
1245 if (forw) {
1246 forw = MEMNEXT(forw);
1247 }
1248 forwok = nullptr;
1249 while (forw) {
1250 if (forw->tag1 != MEMTAG1 || forw->tag2 != MEMTAG2) {
1251 break;
1252 }
1253 forwok = forw;
1254 if (forw->next) {
1255 forw = MEMNEXT(forw->next);
1256 }
1257 else {
1258 forw = nullptr;
1259 }
1260 }
1261
1262 back = (MemHead *)membase->last;
1263 if (back) {
1264 back = MEMNEXT(back);
1265 }
1266 backok = nullptr;
1267 while (back) {
1268 if (back->tag1 != MEMTAG1 || back->tag2 != MEMTAG2) {
1269 break;
1270 }
1271 backok = back;
1272 if (back->prev) {
1273 back = MEMNEXT(back->prev);
1274 }
1275 else {
1276 back = nullptr;
1277 }
1278 }
1279
1280 if (forw != back) {
1281 return ("MORE THAN 1 MEMORYBLOCK CORRUPT");
1282 }
1283
1284 if (forw == nullptr && back == nullptr) {
1285 /* no wrong headers found then but in search of memblock */
1286
1287 forw = static_cast<MemHead *>(membase->first);
1288 if (forw) {
1289 forw = MEMNEXT(forw);
1290 }
1291 forwok = nullptr;
1292 while (forw) {
1293 if (forw == memh) {
1294 break;
1295 }
1296 if (forw->tag1 != MEMTAG1 || forw->tag2 != MEMTAG2) {
1297 break;
1298 }
1299 forwok = forw;
1300 if (forw->next) {
1301 forw = MEMNEXT(forw->next);
1302 }
1303 else {
1304 forw = nullptr;
1305 }
1306 }
1307 if (forw == nullptr) {
1308 return nullptr;
1309 }
1310
1311 back = (MemHead *)membase->last;
1312 if (back) {
1313 back = MEMNEXT(back);
1314 }
1315 backok = nullptr;
1316 while (back) {
1317 if (back == memh) {
1318 break;
1319 }
1320 if (back->tag1 != MEMTAG1 || back->tag2 != MEMTAG2) {
1321 break;
1322 }
1323 backok = back;
1324 if (back->prev) {
1325 back = MEMNEXT(back->prev);
1326 }
1327 else {
1328 back = nullptr;
1329 }
1330 }
1331 }
1332
1333 if (forwok) {
1334 name = forwok->nextname;
1335 }
1336 else {
1337 name = "No name found";
1338 }
1339
1340 if (forw == memh) {
1341 /* to be sure but this block is removed from the list */
1342 if (forwok) {
1343 if (backok) {
1344 forwok->next = (MemHead *)&backok->next;
1345 backok->prev = (MemHead *)&forwok->next;
1346 forwok->nextname = backok->name;
1347 }
1348 else {
1349 forwok->next = nullptr;
1350 membase->last = (localLink *)&forwok->next;
1351 }
1352 }
1353 else {
1354 if (backok) {
1355 backok->prev = nullptr;
1356 membase->first = &backok->next;
1357 }
1358 else {
1359 membase->first = nullptr;
1360 membase->last = nullptr;
1361 }
1362 }
1363 }
1364 else {
1365 MemorY_ErroR(name, "Additional error in header");
1366 return ("Additional error in header");
1367 }
1368
1369 return name;
1370}
1371
1373{
1374 size_t _peak_mem;
1375
1377 _peak_mem = peak_mem;
1379
1380 return _peak_mem;
1381}
1382
1389
1391{
1392 size_t _mem_in_use;
1393
1395 _mem_in_use = mem_in_use;
1397
1398 return _mem_in_use;
1399}
1400
1402{
1403 uint _totblock;
1404
1406 _totblock = totblock;
1408
1409 return _totblock;
1410}
1411
1412#ifndef NDEBUG
1413const char *MEM_guarded_name_ptr(void *vmemh)
1414{
1415 if (vmemh) {
1416 MemHead *memh = static_cast<MemHead *>(vmemh);
1417 memh--;
1418 return memh->name;
1419 }
1420
1421 return "MEM_guarded_name_ptr(nullptr)";
1422}
1423
1424void MEM_guarded_name_ptr_set(void *vmemh, const char *str)
1425{
1426 if (!vmemh) {
1427 return;
1428 }
1429
1430 MemHead *memh = static_cast<MemHead *>(vmemh);
1431 memh--;
1432 memh->name = str;
1433 if (memh->prev) {
1434 MEMNEXT(memh->prev)->nextname = str;
1435 }
1436}
1437#endif /* !NDEBUG */
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 printf
#define str(s)
#define assert(assertion)
bool leak_detector_has_run
char free_after_leak_detection_message[]
void aligned_free(void *ptr)
Definition mallocn.cc:99
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str)
Definition mallocn.cc:134
void * aligned_malloc(size_t size, size_t alignment)
Definition mallocn.cc:77
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 size_t mem_in_use
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 *)
static size_t peak_mem
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 *))
#define MEMNEXT(x)
bool MEM_guarded_consistency_check()
static bool malloc_debug_memset
#define MEMTAG2
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,...)
#define MEMFREE
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)
#define MEMTAG3
static void addtail(volatile localListBase *listbase, void *vlink)
static uint totblock
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)
#define MEMTAG1
MEM_INLINE void MEM_trigger_error_on_memory_block(const void *, const size_t)
#define MEMHEAD_REAL_PTR(memh)
#define IS_POW2(a)
#define MEMHEAD_ALIGN_PADDING(alignment)
#define SIZET_ARG(a)
#define SIZET_ALIGN_4(len)
#define SIZET_FORMAT
#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void free(struct bMotionPath *mpath)
unsigned short uint16_t
Definition BKE_text.h:80
std::string format(const std::string &fmt, Args &&...args)
Definition diagnostic.hh:43
void *(*) mem_mallocN(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
Definition mallocn.cc:41