Blender V5.3
MEM_guardedalloc.h File Reference

Read Blender memory allocation and freeing. More...

Go to the source code of this file.

Untyped Allocation API.

Defines the 'C-style' part of the API, where memory management is fully untyped (i.e. done with void pointers and explicit size values).

This API should usually not be used anymore in C++ code, unless some form of raw memory management is necessary (e.g. for allocation of various ID types based on their #IDTypeInfo::struct_size data).

#define MEM_realloc_uninitialized(vmemh, len)
#define MEM_realloc_zeroed(vmemh, len)
size_t(*) MEM_allocN_len (const void *vmemh) ATTR_WARN_UNUSED_RESULT
void *(*) MEM_realloc_uninitialized_id (void *vmemh, size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void *(*) MEM_realloc_zeroed_id (void *vmemh, size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void *(*) MEM_new_array_uninitialized_aligned (size_t len, size_t size, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void *(*) MEM_new_array_zeroed_aligned (size_t len, size_t size, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void MEM_delete_void (void *vmemh)
void * MEM_dupalloc_void (const void *vmemh) ATTR_WARN_UNUSED_RESULT
void * MEM_new_zeroed (size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void * MEM_new_array_zeroed (size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void ATTR_NONNULL (3)
void * MEM_new_uninitialized (size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void * MEM_new_array_uninitialized (size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * MEM_new_uninitialized_aligned (size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
void * ATTR_NONNULL (4)

Various Helpers.

These functions allow to control the behavior of the guarded allocator, and to retrieve (debug) information about allocated memory.

#define MEM_SIZE_OVERHEAD   sizeof(size_t)
#define MEM_SIZE_OPTIMAL(size)
void(*) MEM_printmemlist_pydict ()
void(*) MEM_printmemlist ()
void(*) MEM_callbackmemlist (void(*func)(void *))
void(*) MEM_printmemlist_stats ()
void(*) MEM_set_error_callback (void(*func)(const char *))
bool(*) MEM_consistency_check ()
void(*) MEM_set_memory_debug ()
size_t(*) MEM_get_memory_in_use ()
unsigned int(*) MEM_get_memory_blocks_in_use ()
void(*) MEM_reset_peak_memory ()
size_t(*) MEM_get_peak_memory () ATTR_WARN_UNUSED_RESULT
const char *(*) MEM_name_ptr (void *vmemh)
void(*) MEM_name_ptr_set (void *vmemh, const char *str) ATTR_NONNULL()
void MEM_init_memleak_detection (void)
void MEM_enable_fail_on_memleak (void)
void MEM_use_lockfree_allocator (void)
void MEM_use_guarded_allocator (void)

Detailed Description

Macro Definition Documentation

◆ MEM_realloc_uninitialized

#define MEM_realloc_uninitialized ( vmemh,
len )
Value:
void *(*) MEM_realloc_uninitialized_id(void *vmemh, size_t len, const char *str)
Definition mallocn.cc:31
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263

Definition at line 118 of file bmesh_polygon.hh.

◆ MEM_realloc_zeroed

#define MEM_realloc_zeroed ( vmemh,
len )
Value:
MEM_realloc_zeroed_id(vmemh, len, __func__)
void *(*) MEM_realloc_zeroed_id(void *vmemh, size_t len, const char *str)
Definition mallocn.cc:34

Definition at line 119 of file bmesh_polygon.hh.

◆ MEM_SIZE_OPTIMAL

#define MEM_SIZE_OPTIMAL ( size)
Value:
#define MEM_SIZE_OVERHEAD

Definition at line 238 of file bmesh_polygon.hh.

◆ MEM_SIZE_OVERHEAD

#define MEM_SIZE_OVERHEAD   sizeof(size_t)

Overhead for lockfree allocator (use to avoid slop-space).

Definition at line 237 of file bmesh_polygon.hh.

Function Documentation

◆ ATTR_NONNULL() [1/2]

◆ ATTR_NONNULL() [2/2]

◆ MEM_delete_void()

void MEM_delete_void ( void * vmemh)

Release memory previously allocated by the C-style functions of this module.

It is illegal to call this function with data allocated by #MEM_new.

Definition at line 109 of file mallocn.cc.

References MEM_delete_void(), mem_guarded::internal::mem_freeN_ex, and mem_guarded::internal::Trivial.

Referenced by ccl_try_align(), GuardedAllocator< U >::deallocate(), MEM_Allocator< _Tp1 >::deallocate(), main(), MEM_delete_void(), and util_aligned_free().

◆ MEM_dupalloc_void()

void * MEM_dupalloc_void ( const void * vmemh)

Duplicates a block of memory, and returns a pointer to the newly allocated block. NULL-safe; will return NULL when receiving a NULL pointer.

Use this version only for void pointers.

Definition at line 139 of file mallocn.cc.

References MEM_dupalloc_void(), and mem_guarded::internal::mem_dupallocN.

Referenced by MEM_dupalloc_void().

◆ MEM_enable_fail_on_memleak()

void MEM_enable_fail_on_memleak ( void )

When this has been called and memory leaks have been detected, the process will have an exit code that indicates failure. This can be used for when checking for memory leaks with automated tests.

Definition at line 82 of file leak_detector.cc.

References MEM_enable_fail_on_memleak().

Referenced by ATTR_NONNULL(), and MEM_enable_fail_on_memleak().

◆ MEM_init_memleak_detection()

void MEM_init_memleak_detection ( void )

This should be called as early as possible in the program. When it has been called, information about memory leaks will be printed on exit.

This variable is constructed when this function is first called. This should happen as soon as possible when the program starts.

It is destructed when the program exits. During destruction, it will print information about leaked memory blocks. Static variables are destructed in reversed order of their construction. Therefore, all static variables that own memory have to be constructed after this function has been called.

Definition at line 60 of file leak_detector.cc.

References mem_guarded::internal::add_memleak_data(), MEM_init_memleak_detection(), and memory_usage_init().

Referenced by ATTR_NONNULL(), main(), main(), and MEM_init_memleak_detection().

◆ MEM_new_array_uninitialized()

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

Allocate a block of memory of size (len * size), with tag name str, aborting in case of integer overflow to prevent vulnerabilities. The name must be a static, because only a pointer to it is stored!

References ATTR_ALLOC_SIZE, ATTR_NONNULL, ATTR_WARN_UNUSED_RESULT, len(), MEM_new_array_uninitialized(), MEM_new_array_uninitialized_aligned, MEM_new_uninitialized_aligned(), size(), and str.

◆ MEM_new_array_zeroed()

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

Allocate a block of memory of size (len * size), with tag name str, aborting in case of integer overflows to prevent vulnerabilities. The memory is cleared. The name must be static, because only a pointer to it is stored!

References ATTR_WARN_UNUSED_RESULT, len(), MEM_new_array_zeroed(), size(), and str.

◆ MEM_new_uninitialized()

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

Allocate a block of memory of size len, with tag name str. The name must be a static, because only a pointer to it is stored!

Definition at line 124 of file mallocn.cc.

References len(), mem_guarded::internal::mem_mallocN, MEM_new_uninitialized(), and str.

Referenced by MEM_Allocator< _Tp1 >::allocate(), ATTR_NONNULL(), and MEM_new_uninitialized().

◆ MEM_new_uninitialized_aligned()

void * MEM_new_uninitialized_aligned ( size_t len,
size_t alignment,
const char * str )

◆ MEM_new_zeroed()

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

Allocate a block of memory of size len, with tag name str. The memory is cleared. The name must be static, because only a pointer to it is stored!

Definition at line 114 of file mallocn.cc.

References len(), mem_guarded::internal::mem_callocN, MEM_new_zeroed(), and str.

Referenced by CLG_ctx_init(), clg_ctx_type_filter_append(), clg_ctx_type_register(), main(), and MEM_new_zeroed().

◆ MEM_use_guarded_allocator()

void MEM_use_guarded_allocator ( void )

Switch allocator to slow fully guarded mode.

Use for debug purposes. This allocator contains lock section around every allocator call, which makes it slow. What is gained with this is the ability to have list of allocated blocks (in an addition to the tracking of number of allocations and amount of allocated bytes).

Note
The switch between allocator types can only happen before any allocation did happen.

Definition at line 200 of file mallocn.cc.

References assert_for_allocator_change(), MEM_allocN_len, MEM_callbackmemlist, mem_guarded::internal::mem_calloc_arrayN, mem_guarded::internal::mem_callocN, mem_clearmemlist, MEM_consistency_check, mem_guarded::internal::mem_dupallocN, mem_guarded::internal::mem_freeN_ex, MEM_get_memory_blocks_in_use, MEM_get_memory_in_use, MEM_get_peak_memory, MEM_guarded_allocN_len(), MEM_guarded_callbackmemlist(), MEM_guarded_calloc_arrayN(), MEM_guarded_calloc_arrayN_aligned(), MEM_guarded_callocN(), mem_guarded_clearmemlist(), MEM_guarded_consistency_check(), MEM_guarded_dupallocN(), MEM_guarded_freeN(), MEM_guarded_get_memory_blocks_in_use(), MEM_guarded_get_memory_in_use(), MEM_guarded_get_peak_memory(), MEM_guarded_malloc_arrayN(), MEM_guarded_malloc_arrayN_aligned(), MEM_guarded_mallocN(), MEM_guarded_mallocN_aligned(), MEM_guarded_name_ptr(), MEM_guarded_name_ptr_set(), MEM_guarded_printmemlist(), MEM_guarded_printmemlist_pydict(), MEM_guarded_printmemlist_stats(), MEM_guarded_reallocN_id(), MEM_guarded_recallocN_id(), MEM_guarded_reset_peak_memory(), MEM_guarded_set_error_callback(), MEM_guarded_set_memory_debug(), mem_guarded::internal::mem_malloc_arrayN, mem_guarded::internal::mem_mallocN, mem_guarded::internal::mem_mallocN_aligned_ex, MEM_name_ptr, MEM_name_ptr_set, MEM_new_array_uninitialized_aligned, MEM_new_array_zeroed_aligned, MEM_printmemlist, MEM_printmemlist_pydict, MEM_printmemlist_stats, MEM_realloc_uninitialized_id, MEM_realloc_zeroed_id, MEM_reset_peak_memory, MEM_set_error_callback, MEM_set_memory_debug, and MEM_use_guarded_allocator().

Referenced by ATTR_NONNULL(), main(), MEM_use_guarded_allocator(), and GuardedAllocatorTest::SetUp().

◆ MEM_use_lockfree_allocator()

void MEM_use_lockfree_allocator ( void )

Switch allocator to fast mode, with less tracking.

Use in the production code where performance is the priority, and exact details about allocation is not. This allocator keeps track of number of allocation and amount of allocated bytes, but it does not track of names of allocated blocks.

Note
The switch between allocator types can only happen before any allocation did happen.

Definition at line 159 of file mallocn.cc.

References assert_for_allocator_change(), MEM_allocN_len, MEM_callbackmemlist, mem_guarded::internal::mem_calloc_arrayN, mem_guarded::internal::mem_callocN, mem_clearmemlist, MEM_consistency_check, mem_guarded::internal::mem_dupallocN, mem_guarded::internal::mem_freeN_ex, MEM_get_memory_blocks_in_use, MEM_get_memory_in_use, MEM_get_peak_memory, MEM_lockfree_allocN_len(), MEM_lockfree_callbackmemlist(), MEM_lockfree_calloc_arrayN(), MEM_lockfree_calloc_arrayN_aligned(), MEM_lockfree_callocN(), mem_lockfree_clearmemlist(), MEM_lockfree_consistency_check(), MEM_lockfree_dupallocN(), MEM_lockfree_freeN(), MEM_lockfree_get_memory_blocks_in_use(), MEM_lockfree_get_memory_in_use(), MEM_lockfree_get_peak_memory(), MEM_lockfree_malloc_arrayN(), MEM_lockfree_malloc_arrayN_aligned(), MEM_lockfree_mallocN(), MEM_lockfree_mallocN_aligned(), MEM_lockfree_name_ptr(), MEM_lockfree_name_ptr_set(), MEM_lockfree_printmemlist(), MEM_lockfree_printmemlist_pydict(), MEM_lockfree_printmemlist_stats(), MEM_lockfree_reallocN_id(), MEM_lockfree_recallocN_id(), MEM_lockfree_reset_peak_memory(), MEM_lockfree_set_error_callback(), MEM_lockfree_set_memory_debug(), mem_guarded::internal::mem_malloc_arrayN, mem_guarded::internal::mem_mallocN, mem_guarded::internal::mem_mallocN_aligned_ex, MEM_name_ptr, MEM_name_ptr_set, MEM_new_array_uninitialized_aligned, MEM_new_array_zeroed_aligned, MEM_printmemlist, MEM_printmemlist_pydict, MEM_printmemlist_stats, MEM_realloc_uninitialized_id, MEM_realloc_zeroed_id, MEM_reset_peak_memory, MEM_set_error_callback, MEM_set_memory_debug, and MEM_use_lockfree_allocator().

Referenced by ATTR_NONNULL(), MEM_use_lockfree_allocator(), and LockFreeAllocatorTest::SetUp().

Variable Documentation

◆ MEM_allocN_len

size_t(*) MEM_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT ( const void * vmemh)
extern

Returns the length of the allocated memory segment pointed at by vmemh. If the pointer was not previously allocated by this module, the result is undefined.

Definition at line 27 of file mallocn.cc.

Referenced by MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_callbackmemlist

void(*) MEM_callbackmemlist(void(*func)(void *)) ( void(* func )(void *))
extern

calls the function on all allocated memory blocks.

Definition at line 60 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_consistency_check

bool(*) MEM_consistency_check() ( void )
extern

Are the start/end block markers still correct ?

Return values
truefor correct memory, false for corrupted memory.

Definition at line 63 of file mallocn.cc.

Referenced by ATTR_NONNULL(), main(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_get_memory_blocks_in_use

unsigned int(*) MEM_get_memory_blocks_in_use() ( void )
extern

Get amount of memory blocks in use.

Definition at line 66 of file mallocn.cc.

Referenced by assert_for_allocator_change(), ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_get_memory_in_use

◆ MEM_get_peak_memory

size_t(*) MEM_get_peak_memory() ATTR_WARN_UNUSED_RESULT ( void )
extern

Get the peak memory usage in bytes, including mmap allocations.

Definition at line 68 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), MEM_use_lockfree_allocator(), and Freestyle::Controller::RenderStrokes().

◆ MEM_name_ptr

const char *(*) MEM_name_ptr(void *vmemh) ( void * vmemh)
extern

Definition at line 73 of file mallocn.cc.

Referenced by MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_name_ptr_set

void(*) MEM_name_ptr_set(void *vmemh, const char *str) ATTR_NONNULL() ( void * vmemh,
const char * str )
extern

Change the debugging name/string assigned to the memory allocated at vmemh. Only affects the guarded allocator. The name must be a static string, because only a pointer to it is stored!

Handy when debugging leaking memory allocated by some often called, generic function with a unspecific name. A caller with more info can set a more specific name, and see which call to the generic function allocates the leaking memory.

Definition at line 74 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_new_array_uninitialized_aligned

void *(*) MEM_new_array_uninitialized_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1 ( size_t len,
size_t size,
size_t alignment,
const char * str )
extern

Allocate an aligned block of memory that remains uninitialized.

Definition at line 50 of file mallocn.cc.

Referenced by MEM_new_array_uninitialized(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_new_array_zeroed_aligned

void *(*) MEM_new_array_zeroed_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1 ( size_t len,
size_t size,
size_t alignment,
const char * str )
extern

Allocate an aligned block of memory that is initialized with zeros.

Definition at line 54 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_printmemlist

void(*) MEM_printmemlist() ( void )
extern

Print a list of the names and sizes of all allocated memory blocks.

Definition at line 59 of file mallocn.cc.

Referenced by ATTR_NONNULL(), main(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_printmemlist_pydict

void(*) MEM_printmemlist_pydict() ( void )
extern

Print a list of the names and sizes of all allocated memory blocks. as a python dict for easy investigation.

Definition at line 58 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_printmemlist_stats

void(*) MEM_printmemlist_stats() ( void )
extern

Print statistics about memory usage

Definition at line 61 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_realloc_uninitialized_id

void *(*) MEM_realloc_uninitialized_id(void *vmemh, size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2) ( void * vmemh,
size_t len,
const char * str )
extern

Reallocates a block of memory, and returns pointer to the newly allocated block, the old one is freed. this is not as optimized as a system realloc but just makes a new allocation and copies over from existing memory.

Definition at line 31 of file mallocn.cc.

Referenced by MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_realloc_zeroed_id

void *(*) MEM_realloc_zeroed_id(void *vmemh, size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2) ( void * vmemh,
size_t len,
const char * str )
extern

A variant of realloc which zeros new bytes

Definition at line 34 of file mallocn.cc.

Referenced by MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_reset_peak_memory

void(*) MEM_reset_peak_memory() ( void )
extern

Reset the peak memory statistic to zero.

Definition at line 67 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_set_error_callback

void(*) MEM_set_error_callback(void(*func)(const char *)) ( void(* func )(const char *))
extern

Set the callback function for error output.

Definition at line 62 of file mallocn.cc.

Referenced by ATTR_NONNULL(), main(), main(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_set_memory_debug

void(*) MEM_set_memory_debug() ( void )
extern

Attempt to enforce OSX (or other OS's) to have malloc and stack nonzero

Definition at line 64 of file mallocn.cc.

Referenced by ATTR_NONNULL(), MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().