Blender V5.3
#include "MEM_guardedalloc.h"
#include "PRF_profile.hh"
#include "../../source/blender/blenlib/BLI_strict_flags.hh"
#include <cassert>
#include "mallocn_intern.hh"
#include "mallocn_intern_function_pointers.hh"

Go to the source code of this file.

Functions

void * aligned_malloc (size_t size, size_t alignment)
void aligned_free (void *ptr)
void MEM_delete_void (void *vmemh)
void * MEM_new_zeroed (size_t len, const char *str)
void * MEM_new_array_zeroed (size_t len, size_t size, const char *str)
void * MEM_new_uninitialized (size_t len, const char *str)
void * MEM_new_array_uninitialized (size_t len, size_t size, const char *str)
void * MEM_new_uninitialized_aligned (size_t len, size_t alignment, const char *str)
void * MEM_dupalloc_void (const void *vmemh)
static void assert_for_allocator_change ()
void MEM_use_lockfree_allocator ()
void MEM_use_guarded_allocator ()

Variables

size_t(*) MEM_allocN_len (const void *vmemh) = MEM_lockfree_allocN_len
void *(*) MEM_realloc_uninitialized_id (void *vmemh, size_t len, const char *str) = MEM_lockfree_reallocN_id
void *(*) MEM_realloc_zeroed_id (void *vmemh, size_t len, const char *str) = MEM_lockfree_recallocN_id
void *(*) MEM_new_array_uninitialized_aligned (size_t len, size_t size, size_t alignment, const char *str) = MEM_lockfree_malloc_arrayN_aligned
void *(*) MEM_new_array_zeroed_aligned (size_t len, size_t size, size_t alignment, const char *str) = MEM_lockfree_calloc_arrayN_aligned
void(*) MEM_printmemlist_pydict (void) = MEM_lockfree_printmemlist_pydict
void(*) MEM_printmemlist (void) = MEM_lockfree_printmemlist
void(*) MEM_callbackmemlist (void(*func)(void *)) = MEM_lockfree_callbackmemlist
void(*) MEM_printmemlist_stats (void) = MEM_lockfree_printmemlist_stats
void(*) MEM_set_error_callback (void(*func)(const char *)) = MEM_lockfree_set_error_callback
bool(*) MEM_consistency_check (void) = MEM_lockfree_consistency_check
void(*) MEM_set_memory_debug (void) = MEM_lockfree_set_memory_debug
size_t(*) MEM_get_memory_in_use (void) = MEM_lockfree_get_memory_in_use
uint(*) MEM_get_memory_blocks_in_use (void) = MEM_lockfree_get_memory_blocks_in_use
void(*) MEM_reset_peak_memory (void) = MEM_lockfree_reset_peak_memory
size_t(*) MEM_get_peak_memory (void) = MEM_lockfree_get_peak_memory
void(*) mem_clearmemlist (void) = mem_lockfree_clearmemlist
const char *(*) MEM_name_ptr (void *vmemh) = MEM_lockfree_name_ptr
void(*) MEM_name_ptr_set (void *vmemh, const char *str) = MEM_lockfree_name_ptr_set

Detailed Description

Guarded memory allocation, and boundary-write detection.

Definition in file mallocn.cc.

Function Documentation

◆ aligned_free()

void aligned_free ( void * ptr)

Definition at line 99 of file mallocn.cc.

References aligned_free(), and PRF_memory_free.

Referenced by aligned_free(), MEM_lockfree_freeN(), and rem_memblock().

◆ aligned_malloc()

void * aligned_malloc ( size_t size,
size_t alignment )

◆ assert_for_allocator_change()

void assert_for_allocator_change ( )
static

Perform assert checks on allocator type change.

Helps catching issues (in debug build) caused by an unintended allocator type change when there are allocation happened.

Definition at line 150 of file mallocn.cc.

References assert, assert_for_allocator_change(), and MEM_get_memory_blocks_in_use.

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

◆ 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_new_array_uninitialized()

◆ MEM_new_array_zeroed()

void * MEM_new_array_zeroed ( size_t len,
size_t size,
const char * 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) ( const void * vmemh) = MEM_lockfree_allocN_len

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 *)) = MEM_lockfree_callbackmemlist

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_clearmemlist

void(*) mem_clearmemlist(void) ( void ) = mem_lockfree_clearmemlist

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 70 of file mallocn.cc.

Referenced by MEM_use_guarded_allocator(), and MEM_use_lockfree_allocator().

◆ MEM_consistency_check

bool(*) MEM_consistency_check(void) ( void ) = MEM_lockfree_consistency_check

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

uint(*) MEM_get_memory_blocks_in_use(void) ( void ) = MEM_lockfree_get_memory_blocks_in_use

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(void) ( void ) = MEM_lockfree_get_peak_memory

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) = MEM_lockfree_name_ptr

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) ( void * vmemh,
const char * str ) = MEM_lockfree_name_ptr_set

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) ( size_t len,
size_t size,
size_t alignment,
const char * str ) = MEM_lockfree_malloc_arrayN_aligned

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) ( size_t len,
size_t size,
size_t alignment,
const char * str ) = MEM_lockfree_calloc_arrayN_aligned

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) ( void ) = MEM_lockfree_printmemlist

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) ( void ) = MEM_lockfree_printmemlist_pydict

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) ( void ) = MEM_lockfree_printmemlist_stats

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) ( void * vmemh,
size_t len,
const char * str ) = MEM_lockfree_reallocN_id

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) ( void * vmemh,
size_t len,
const char * str ) = MEM_lockfree_recallocN_id

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) ( void ) = MEM_lockfree_reset_peak_memory

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 *)) = MEM_lockfree_set_error_callback

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) ( void ) = MEM_lockfree_set_memory_debug

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().