Blender V4.3
mallocn_intern.hh File Reference
#include <malloc.h>
#include "mallocn_inline.hh"

Go to the source code of this file.

Macros

#define UNUSED(x)   UNUSED_##x
 
#define USE_MALLOC_USABLE_SIZE   /* internal, when we have malloc_usable_size() */
 
#define SIZET_FORMAT   "%zu"
 
#define SIZET_ARG(a)   ((size_t)(a))
 
#define SIZET_ALIGN_4(len)   ((len + 3) & ~(size_t)3)
 
#define LIKELY(x)   (x)
 
#define UNLIKELY(x)   (x)
 
#define MEM_INLINE   static inline
 
#define IS_POW2(a)   (((a) & ((a)-1)) == 0)
 
#define MEMHEAD_ALIGN_PADDING(alignment)    ((size_t)alignment - (sizeof(MemHeadAligned) % (size_t)alignment))
 
#define MEMHEAD_REAL_PTR(memh)   ((char *)memh - MEMHEAD_ALIGN_PADDING(memh->alignment))
 
#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT   sizeof(void *)
 

Functions

void * aligned_malloc (size_t size, size_t alignment)
 
void aligned_free (void *ptr)
 
void memory_usage_init (void)
 
void memory_usage_block_alloc (size_t size)
 
void memory_usage_block_free (size_t size)
 
size_t memory_usage_block_num (void)
 
size_t memory_usage_current (void)
 
size_t memory_usage_peak (void)
 
void memory_usage_peak_reset (void)
 
size_t MEM_lockfree_allocN_len (const void *vmemh) ATTR_WARN_UNUSED_RESULT
 
void MEM_lockfree_freeN (void *vmemh, mem_guarded::internal::AllocationType allocation_type)
 
void * MEM_lockfree_dupallocN (const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
 
void * MEM_lockfree_reallocN_id (void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
 
void * MEM_lockfree_recallocN_id (void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
 
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_calloc_arrayN (size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
 
void ATTR_NONNULL (3)
 
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_malloc_arrayN (size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
 
void * MEM_lockfree_mallocN_aligned (size_t len, size_t alignment, const char *str, mem_guarded::internal::AllocationType allocation_type) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
 
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
 
void ATTR_NONNULL (4)
 
void MEM_lockfree_printmemlist_pydict (void)
 
void MEM_lockfree_printmemlist (void)
 
void MEM_lockfree_callbackmemlist (void(*func)(void *))
 
void MEM_lockfree_printmemlist_stats (void)
 
void MEM_lockfree_set_error_callback (void(*func)(const char *))
 
bool MEM_lockfree_consistency_check (void)
 
void MEM_lockfree_set_memory_debug (void)
 
size_t MEM_lockfree_get_memory_in_use (void)
 
unsigned int MEM_lockfree_get_memory_blocks_in_use (void)
 
void MEM_lockfree_reset_peak_memory (void)
 
size_t MEM_lockfree_get_peak_memory (void) ATTR_WARN_UNUSED_RESULT
 
void mem_lockfree_clearmemlist (void)
 
const char * MEM_lockfree_name_ptr (void *vmemh)
 
void MEM_lockfree_name_ptr_set (void *vmemh, const char *str)
 
size_t MEM_guarded_allocN_len (const void *vmemh) ATTR_WARN_UNUSED_RESULT
 
void MEM_guarded_freeN (void *vmemh, mem_guarded::internal::AllocationType allocation_type)
 
void * MEM_guarded_dupallocN (const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
 
void * MEM_guarded_reallocN_id (void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
 
void * MEM_guarded_recallocN_id (void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
 
void * MEM_guarded_callocN (size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
 
void * MEM_guarded_calloc_arrayN (size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
 
void * MEM_guarded_mallocN (size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
 
void * MEM_guarded_malloc_arrayN (size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
 
void * MEM_guarded_mallocN_aligned (size_t len, size_t alignment, const char *str, mem_guarded::internal::AllocationType allocation_type) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
 
void * MEM_guarded_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
 
void MEM_guarded_printmemlist_pydict (void)
 
void MEM_guarded_printmemlist (void)
 
void MEM_guarded_callbackmemlist (void(*func)(void *))
 
void MEM_guarded_printmemlist_stats (void)
 
void MEM_guarded_set_error_callback (void(*func)(const char *))
 
bool MEM_guarded_consistency_check (void)
 
void MEM_guarded_set_memory_debug (void)
 
size_t MEM_guarded_get_memory_in_use (void)
 
unsigned int MEM_guarded_get_memory_blocks_in_use (void)
 
void MEM_guarded_reset_peak_memory (void)
 
size_t MEM_guarded_get_peak_memory (void) ATTR_WARN_UNUSED_RESULT
 
void mem_guarded_clearmemlist (void)
 
const char * MEM_guarded_name_ptr (void *vmemh)
 
void MEM_guarded_name_ptr_set (void *vmemh, const char *str)
 

Variables

bool leak_detector_has_run
 
char free_after_leak_detection_message []
 
void(* mem_clearmemlist )(void)
 

Macro Definition Documentation

◆ ALIGNED_MALLOC_MINIMUM_ALIGNMENT

#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT   sizeof(void *)

◆ IS_POW2

#define IS_POW2 ( a)    (((a) & ((a)-1)) == 0)

Definition at line 72 of file mallocn_intern.hh.

Referenced by MEM_guarded_mallocN_aligned(), and MEM_lockfree_mallocN_aligned().

◆ LIKELY

#define LIKELY ( x)    (x)

Definition at line 52 of file mallocn_intern.hh.

◆ MEM_INLINE

#define MEM_INLINE   static inline

Definition at line 69 of file mallocn_intern.hh.

◆ MEMHEAD_ALIGN_PADDING

#define MEMHEAD_ALIGN_PADDING ( alignment)     ((size_t)alignment - (sizeof(MemHeadAligned) % (size_t)alignment))

◆ MEMHEAD_REAL_PTR

#define MEMHEAD_REAL_PTR ( memh)    ((char *)memh - MEMHEAD_ALIGN_PADDING(memh->alignment))

◆ SIZET_ALIGN_4

◆ SIZET_ARG

◆ SIZET_FORMAT

◆ UNLIKELY

#define UNLIKELY ( x)    (x)

Definition at line 53 of file mallocn_intern.hh.

◆ UNUSED

#define UNUSED ( x)    UNUSED_##x

Definition at line 16 of file mallocn_intern.hh.

◆ USE_MALLOC_USABLE_SIZE

#define USE_MALLOC_USABLE_SIZE   /* internal, when we have malloc_usable_size() */

Definition at line 20 of file mallocn_intern.hh.

Function Documentation

◆ aligned_free()

void aligned_free ( void * ptr)

Definition at line 96 of file mallocn.cc.

References free(), and ptr.

Referenced by MEM_lockfree_freeN(), and rem_memblock().

◆ aligned_malloc()

void * aligned_malloc ( size_t size,
size_t alignment )

◆ ATTR_NONNULL() [1/2]

void * ATTR_NONNULL ( 3 )

◆ ATTR_NONNULL() [2/2]

void ATTR_NONNULL ( 4 )

◆ MEM_guarded_allocN_len()

size_t MEM_guarded_allocN_len ( const void * vmemh)

Definition at line 312 of file mallocn_guarded_impl.cc.

References MemHead::len.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_callbackmemlist()

void MEM_guarded_callbackmemlist ( void(* func )(void *))

◆ MEM_guarded_calloc_arrayN()

void * MEM_guarded_calloc_arrayN ( size_t len,
size_t size,
const char * str )

◆ MEM_guarded_calloc_arrayN_aligned()

void * MEM_guarded_calloc_arrayN_aligned ( size_t len,
size_t size,
size_t alignment,
const char * str )

◆ MEM_guarded_callocN()

void * MEM_guarded_callocN ( size_t len,
const char * str )

◆ mem_guarded_clearmemlist()

void mem_guarded_clearmemlist ( void )

Definition at line 1011 of file mallocn_guarded_impl.cc.

References localListBase::first, localListBase::last, and membase.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_consistency_check()

bool MEM_guarded_consistency_check ( void )

Definition at line 289 of file mallocn_guarded_impl.cc.

References check_memlist(), localListBase::last, and membase.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_dupallocN()

◆ MEM_guarded_freeN()

◆ MEM_guarded_get_memory_blocks_in_use()

unsigned int MEM_guarded_get_memory_blocks_in_use ( void )

Definition at line 1356 of file mallocn_guarded_impl.cc.

References mem_lock_thread(), mem_unlock_thread(), and totblock.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_get_memory_in_use()

size_t MEM_guarded_get_memory_in_use ( void )

Definition at line 1345 of file mallocn_guarded_impl.cc.

References mem_in_use, mem_lock_thread(), and mem_unlock_thread().

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_get_peak_memory()

size_t MEM_guarded_get_peak_memory ( void )

Definition at line 1327 of file mallocn_guarded_impl.cc.

References mem_lock_thread(), mem_unlock_thread(), and peak_mem.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_malloc_arrayN()

void * MEM_guarded_malloc_arrayN ( size_t len,
size_t size,
const char * str )

◆ MEM_guarded_mallocN()

◆ MEM_guarded_mallocN_aligned()

◆ MEM_guarded_name_ptr()

const char * MEM_guarded_name_ptr ( void * vmemh)

Definition at line 1368 of file mallocn_guarded_impl.cc.

References MemHead::name.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_name_ptr_set()

void MEM_guarded_name_ptr_set ( void * vmemh,
const char * str )

Definition at line 1379 of file mallocn_guarded_impl.cc.

References MEMNEXT, MemHead::name, MemHead::prev, and str.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_printmemlist()

void MEM_guarded_printmemlist ( void )

Definition at line 1003 of file mallocn_guarded_impl.cc.

References MEM_guarded_printmemlist_internal().

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_printmemlist_pydict()

void MEM_guarded_printmemlist_pydict ( void )

Definition at line 1007 of file mallocn_guarded_impl.cc.

References MEM_guarded_printmemlist_internal().

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_printmemlist_stats()

◆ MEM_guarded_reallocN_id()

void * MEM_guarded_reallocN_id ( void * vmemh,
size_t len,
const char * str )

◆ MEM_guarded_recallocN_id()

◆ MEM_guarded_reset_peak_memory()

void MEM_guarded_reset_peak_memory ( void )

◆ MEM_guarded_set_error_callback()

void MEM_guarded_set_error_callback ( void(* func )(const char *))

Definition at line 302 of file mallocn_guarded_impl.cc.

References error_callback.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_set_memory_debug()

void MEM_guarded_set_memory_debug ( void )

Definition at line 307 of file mallocn_guarded_impl.cc.

References malloc_debug_memset.

Referenced by MEM_use_guarded_allocator().

◆ MEM_lockfree_allocN_len()

size_t MEM_lockfree_allocN_len ( const void * vmemh)

◆ MEM_lockfree_callbackmemlist()

void MEM_lockfree_callbackmemlist ( void(* func )(void *))

Definition at line 496 of file mallocn_lockfree_impl.cc.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_calloc_arrayN()

void * MEM_lockfree_calloc_arrayN ( size_t len,
size_t size,
const char * str )

◆ MEM_lockfree_calloc_arrayN_aligned()

void * MEM_lockfree_calloc_arrayN_aligned ( size_t len,
size_t size,
size_t alignment,
const char * str )

◆ MEM_lockfree_callocN()

◆ mem_lockfree_clearmemlist()

void mem_lockfree_clearmemlist ( void )

Definition at line 493 of file mallocn_lockfree_impl.cc.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_consistency_check()

bool MEM_lockfree_consistency_check ( void )

Definition at line 520 of file mallocn_lockfree_impl.cc.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_dupallocN()

◆ MEM_lockfree_freeN()

◆ MEM_lockfree_get_memory_blocks_in_use()

unsigned int MEM_lockfree_get_memory_blocks_in_use ( void )

Definition at line 535 of file mallocn_lockfree_impl.cc.

References memory_usage_block_num().

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_get_memory_in_use()

size_t MEM_lockfree_get_memory_in_use ( void )

Definition at line 530 of file mallocn_lockfree_impl.cc.

References memory_usage_current().

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_get_peak_memory()

size_t MEM_lockfree_get_peak_memory ( void )

Definition at line 546 of file mallocn_lockfree_impl.cc.

References memory_usage_peak().

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_malloc_arrayN()

void * MEM_lockfree_malloc_arrayN ( size_t len,
size_t size,
const char * str )

◆ MEM_lockfree_mallocN()

◆ MEM_lockfree_mallocN_aligned()

◆ MEM_lockfree_name_ptr()

const char * MEM_lockfree_name_ptr ( void * vmemh)

Definition at line 552 of file mallocn_lockfree_impl.cc.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_name_ptr_set()

void MEM_lockfree_name_ptr_set ( void * vmemh,
const char * str )

◆ MEM_lockfree_printmemlist()

void MEM_lockfree_printmemlist ( void )

Definition at line 491 of file mallocn_lockfree_impl.cc.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_printmemlist_pydict()

void MEM_lockfree_printmemlist_pydict ( void )

Definition at line 489 of file mallocn_lockfree_impl.cc.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_printmemlist_stats()

void MEM_lockfree_printmemlist_stats ( void )

Definition at line 501 of file mallocn_lockfree_impl.cc.

References memory_usage_current(), memory_usage_peak(), and printf.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_reallocN_id()

◆ MEM_lockfree_recallocN_id()

◆ MEM_lockfree_reset_peak_memory()

void MEM_lockfree_reset_peak_memory ( void )

Definition at line 541 of file mallocn_lockfree_impl.cc.

References memory_usage_peak_reset().

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_set_error_callback()

void MEM_lockfree_set_error_callback ( void(* func )(const char *))

Definition at line 515 of file mallocn_lockfree_impl.cc.

References error_callback.

Referenced by MEM_use_lockfree_allocator().

◆ MEM_lockfree_set_memory_debug()

void MEM_lockfree_set_memory_debug ( void )

Definition at line 525 of file mallocn_lockfree_impl.cc.

References malloc_debug_memset.

Referenced by MEM_use_lockfree_allocator().

◆ memory_usage_block_alloc()

void memory_usage_block_alloc ( size_t size)

◆ memory_usage_block_free()

void memory_usage_block_free ( size_t size)

Definition at line 212 of file memory_usage.cc.

References get_global(), get_local_data(), LIKELY, and use_local_counters.

Referenced by MEM_lockfree_freeN().

◆ memory_usage_block_num()

size_t memory_usage_block_num ( void )

Definition at line 229 of file memory_usage.cc.

References get_global(), and lock.

Referenced by MEM_lockfree_get_memory_blocks_in_use().

◆ memory_usage_current()

◆ memory_usage_init()

void memory_usage_init ( void )

Definition at line 182 of file memory_usage.cc.

References get_local_data().

Referenced by MEM_init_memleak_detection().

◆ memory_usage_peak()

size_t memory_usage_peak ( void )

Get the approximate peak memory usage since the last call to memory_usage_peak_reset. This is approximate, because the peak usage is not updated after every allocation (see peak_update_threshold).

In the worst case, the peak memory usage is underestimated by peak_update_threshold * #threads. After large allocations (larger than the threshold), the peak usage is always updated so those allocations will always be taken into account.

Definition at line 264 of file memory_usage.cc.

References get_global(), and update_global_peak().

Referenced by MEM_lockfree_get_peak_memory(), and MEM_lockfree_printmemlist_stats().

◆ memory_usage_peak_reset()

void memory_usage_peak_reset ( void )

Definition at line 271 of file memory_usage.cc.

References get_global(), and memory_usage_current().

Referenced by MEM_lockfree_reset_peak_memory().

Variable Documentation

◆ free_after_leak_detection_message

char free_after_leak_detection_message[]
extern

Definition at line 19 of file leak_detector.cc.

Referenced by MEM_guarded_freeN(), and MEM_lockfree_freeN().

◆ leak_detector_has_run

bool leak_detector_has_run
extern

Definition at line 18 of file leak_detector.cc.

Referenced by MEM_guarded_freeN(), and MEM_lockfree_freeN().

◆ mem_clearmemlist

void(* mem_clearmemlist) (void) ( void )
extern

Clear the listbase of allocated memory blocks.

WARNING: This will make the whole guardedalloc system fully inconsistent. It is only indented to be called in one place: the destructor of the #MemLeakPrinter class, which is only instantiated once as a static variable by MEM_init_memleak_detection, and therefore destructed once at program exit.

Definition at line 67 of file mallocn.cc.

Referenced by MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().