24#ifdef WITH_MEM_VALGRIND
25# include "valgrind/memcheck.h"
29#include "../../source/blender/blenlib/BLI_strict_flags.hh"
39typedef struct MemHead {
43static_assert(MEM_MIN_CPP_ALIGNMENT <=
alignof(MemHead),
"Bad alignment of MemHead");
44static_assert(MEM_MIN_CPP_ALIGNMENT <=
sizeof(MemHead),
"Bad size of MemHead");
46typedef struct MemHeadAligned {
50static_assert(MEM_MIN_CPP_ALIGNMENT <=
alignof(MemHeadAligned),
"Bad alignment of MemHeadAligned");
51static_assert(MEM_MIN_CPP_ALIGNMENT <=
sizeof(MemHeadAligned),
"Bad size of MemHeadAligned");
75#define MEMHEAD_FROM_PTR(ptr) (((MemHead *)ptr) - 1)
76#define PTR_FROM_MEMHEAD(memhead) (memhead + 1)
77#define MEMHEAD_ALIGNED_FROM_PTR(ptr) (((MemHeadAligned *)ptr) - 1)
78#define MEMHEAD_IS_ALIGNED(memhead) ((memhead)->len & size_t(MEMHEAD_FLAG_ALIGN))
79#define MEMHEAD_HAS_NONTRIVIAL_DESTRUCTOR(memhead) \
80 ((memhead)->len & size_t(MEMHEAD_FLAG_NONTRIVIAL_DESTRUCTOR))
81#define MEMHEAD_LEN(memhead) ((memhead)->len & ~size_t(MEMHEAD_FLAG_MASK))
86static void print_error(
const char *message, va_list str_format_args)
89 vsnprintf(buf,
sizeof(buf), message, str_format_args);
90 buf[
sizeof(buf) - 1] =
'\0';
102 va_list str_format_args;
103 va_start(str_format_args, message);
105 va_end(str_format_args);
113 va_list str_format_args;
115 va_start(str_format_args, message);
117 va_end(str_format_args);
119 if (vmemh ==
nullptr) {
127 const void *address = memh;
128 size_t size =
len +
sizeof(*memh);
139 if (vmemh) [[likely]] {
153 if (vmemh ==
nullptr) [[unlikely]] {
163 "Attempt to use C-style MEM_delete_void on a pointer created with "
164 "CPP-style MEM_new or new\n");
170 memset(memh + 1, 255,
len);
185 void *newp =
nullptr;
192 "Attempt to use C-style MEM_dupalloc_void on a pointer created with "
193 "CPP-style MEM_new or new\n");
204 memcpy(newp, vmemh, prev_size);
212 void *newp =
nullptr;
221 "Attempt to use C-style MEM_realloc_uninitialized on a pointer created with "
222 "CPP-style MEM_new or new\n");
237 memcpy(newp, vmemh,
len);
241 memcpy(newp, vmemh, old_len);
257 void *newp =
nullptr;
266 "Attempt to use C-style MEM_realloc_zeroed on a pointer created with "
267 "CPP-style MEM_new or new\n");
282 memcpy(newp, vmemh,
len);
285 memcpy(newp, vmemh, old_len);
290 memset(((
char *)newp) + old_len, 0,
len - old_len);
311 memh = (MemHead *)calloc(1,
len +
sizeof(MemHead));
314 if (memh) [[likely]] {
333 "Calloc array aborted due to integer overflow: "
351#ifdef WITH_MEM_VALGRIND
352 const size_t len_unaligned =
len;
356 memh = (MemHead *)malloc(
len +
sizeof(MemHead));
359 if (memh) [[likely]] {
361 if (
len) [[likely]] {
363 memset(memh + 1, 255,
len);
365#ifdef WITH_MEM_VALGRIND
367 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
370 VALGRIND_MAKE_MEM_DEFINED((
const char *)(memh + 1) + len_unaligned,
len - len_unaligned);
393 "Malloc array aborted due to integer overflow: "
432#ifdef WITH_MEM_VALGRIND
433 const size_t len_unaligned =
len;
438 len + extra_padding +
sizeof(MemHeadAligned), alignment);
441 if (memh) [[likely]] {
446 memh = (MemHeadAligned *)((
char *)memh + extra_padding);
448 if (
len) [[likely]] {
450 memset(memh + 1, 255,
len);
452#ifdef WITH_MEM_VALGRIND
454 VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
457 VALGRIND_MAKE_MEM_DEFINED((
const char *)(memh + 1) + len_unaligned,
len - len_unaligned);
466 memh->alignment = short(alignment);
480 const size_t alignment,
486 "Calloc array aborted due to integer overflow: "
495 if (alignment <= MEM_MIN_CPP_ALIGNMENT) {
504 const size_t alignment,
514 const size_t alignment,
520 if (alignment <= MEM_MIN_CPP_ALIGNMENT) {
529 memset(ptr, 0, bytes_num);
551 "\nFor more detailed per-block statistics run Blender with memory debugging command line "
554#ifdef HAVE_MALLOC_STATS
555 printf(
"System Statistics:\n");
601 return "unknown block name ptr";
604 return "MEM_lockfree_name_ptr(nullptr)";
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...
struct AtomicSpinLock __attribute__((aligned(32))) AtomicSpinLock
#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)
@ MEMHEAD_FLAG_NONTRIVIAL_DESTRUCTOR
MEM_INLINE void MEM_trigger_error_on_memory_block(const void *, const size_t)
size_t memory_usage_current(void)
void * MEM_lockfree_malloc_arrayN(size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define MEMHEAD_REAL_PTR(memh)
#define MEMHEAD_ALIGN_PADDING(alignment)
void * MEM_lockfree_dupallocN(const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void memory_usage_block_alloc(size_t size)
void MEM_lockfree_reset_peak_memory(void)
void mem_lockfree_clearmemlist(void)
void MEM_lockfree_name_ptr_set(void *vmemh, const char *str)
void * MEM_lockfree_calloc_arrayN(size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void MEM_lockfree_set_error_callback(void(*func)(const char *))
void MEM_lockfree_printmemlist_stats(void)
#define SIZET_ALIGN_4(len)
void MEM_lockfree_freeN(void *vmemh, mem_guarded::internal::DestructorType destructor_type)
size_t memory_usage_block_num(void)
void MEM_lockfree_set_memory_debug(void)
void * MEM_lockfree_reallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
bool MEM_lockfree_consistency_check(void)
void * MEM_lockfree_callocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void * MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str, mem_guarded::internal::DestructorType destructor_type) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
void memory_usage_block_free(size_t size)
size_t memory_usage_peak(void)
void MEM_lockfree_printmemlist_pydict(void)
#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT
void * MEM_lockfree_recallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void memory_usage_peak_reset(void)
void * MEM_lockfree_malloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * MEM_lockfree_mallocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void MEM_lockfree_printmemlist(void)
size_t MEM_lockfree_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT
const char * MEM_lockfree_name_ptr(void *vmemh)
void MEM_lockfree_callbackmemlist(void(*func)(void *))
void * MEM_lockfree_calloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
size_t MEM_lockfree_get_memory_in_use(void)
unsigned int MEM_lockfree_get_memory_blocks_in_use(void)
size_t MEM_lockfree_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT
static void * mem_lockfree_malloc_arrayN_aligned(const size_t len, const size_t size, const size_t alignment, const char *str, size_t &r_bytes_num)
static void(*) error_callback(const char *)
#define PTR_FROM_MEMHEAD(memhead)
static bool malloc_debug_memset
#define MEMHEAD_HAS_NONTRIVIAL_DESTRUCTOR(memhead)
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 MEMHEAD_LEN(memhead)
#define MEMHEAD_IS_ALIGNED(memhead)
#define MEMHEAD_FROM_PTR(ptr)
#define MEMHEAD_ALIGNED_FROM_PTR(ptr)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
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)