Blender V4.3
mallocn_guarded_impl.cc File Reference
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pthread.h>
#include "MEM_guardedalloc.h"
#include "../../source/blender/blenlib/BLI_strict_flags.h"
#include "atomic_ops.h"
#include "mallocn_intern.hh"
#include "mallocn_intern_function_pointers.hh"

Go to the source code of this file.

Classes

struct  localLink
 
struct  localListBase
 
struct  MemHead
 
struct  MemTail
 
struct  MemPrintBlock
 

Macros

#define MAKE_ID(a, b, c, d)   (int(d) << 24 | int(c) << 16 | (b) << 8 | (a))
 
#define MEMTAG1   MAKE_ID('M', 'E', 'M', 'O')
 
#define MEMTAG2   MAKE_ID('R', 'Y', 'B', 'L')
 
#define MEMTAG3   MAKE_ID('O', 'C', 'K', '!')
 
#define MEMFREE   MAKE_ID('F', 'R', 'E', 'E')
 
#define MEMNEXT(x)   ((MemHead *)(((char *)x) - offsetof(MemHead, next)))
 

Typedefs

typedef struct localLink localLink
 
typedef struct localListBase localListBase
 
typedef struct MemHead MemHead
 
typedef MemHead MemHeadAligned
 
typedef struct MemTail MemTail
 
typedef struct MemPrintBlock MemPrintBlock
 

Enumerations

enum  MemHeadFlag { MEMHEAD_FLAG_FROM_CPP_NEW = 1 << 1 }
 

Functions

static void addtail (volatile localListBase *listbase, void *vlink)
 
static void remlink (volatile localListBase *listbase, void *vlink)
 
static void rem_memblock (MemHead *memh)
 
static void MemorY_ErroR (const char *block, const char *error)
 
static const char * check_memlist (const MemHead *memh)
 
static void print_error (const char *message, va_list str_format_args)
 
static void print_error (const char *message,...)
 
static void report_error_on_address (const void *vmemh, const char *message,...)
 
static void mem_lock_thread ()
 
static void mem_unlock_thread ()
 
bool MEM_guarded_consistency_check ()
 
void MEM_guarded_set_error_callback (void(*func)(const char *))
 
void MEM_guarded_set_memory_debug ()
 
size_t MEM_guarded_allocN_len (const void *vmemh)
 
void * MEM_guarded_dupallocN (const void *vmemh)
 
void * MEM_guarded_reallocN_id (void *vmemh, size_t len, const char *str)
 
void * MEM_guarded_recallocN_id (void *vmemh, size_t len, const char *str)
 
static void make_memhead_header (MemHead *memh, size_t len, const char *str, const AllocationType allocation_type)
 
void * MEM_guarded_mallocN (size_t len, const char *str)
 
void * MEM_guarded_malloc_arrayN (size_t len, size_t size, const char *str)
 
void * MEM_guarded_mallocN_aligned (size_t len, size_t alignment, const char *str, const AllocationType allocation_type)
 
void * MEM_guarded_callocN (size_t len, const char *str)
 
void * MEM_guarded_calloc_arrayN (size_t len, size_t size, const char *str)
 
void * MEM_guarded_calloc_arrayN_aligned (const size_t len, const size_t size, const size_t alignment, const char *str)
 
static int compare_name (const void *p1, const void *p2)
 
static int compare_len (const void *p1, const void *p2)
 
void MEM_guarded_printmemlist_stats ()
 
static void MEM_guarded_printmemlist_internal (int pydict)
 
void MEM_guarded_callbackmemlist (void(*func)(void *))
 
void MEM_guarded_printmemlist ()
 
void MEM_guarded_printmemlist_pydict ()
 
void mem_guarded_clearmemlist ()
 
void MEM_guarded_freeN (void *vmemh, const AllocationType allocation_type)
 
size_t MEM_guarded_get_peak_memory ()
 
void MEM_guarded_reset_peak_memory ()
 
size_t MEM_guarded_get_memory_in_use ()
 
uint MEM_guarded_get_memory_blocks_in_use ()
 
const char * MEM_guarded_name_ptr (void *vmemh)
 
void MEM_guarded_name_ptr_set (void *vmemh, const char *str)
 

Variables

static uint totblock = 0
 
static size_t mem_in_use = 0
 
static size_t peak_mem = 0
 
static volatile localListBase _membase
 
static volatile localListBasemembase = &_membase
 
static void(* error_callback )(const char *) = nullptr
 
static bool malloc_debug_memset = false
 
static pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER
 
static const char mem_printmemlist_pydict_script []
 

Detailed Description

Guarded memory allocation, and boundary-write detection.

Definition in file mallocn_guarded_impl.cc.

Macro Definition Documentation

◆ MAKE_ID

#define MAKE_ID ( a,
b,
c,
d )   (int(d) << 24 | int(c) << 16 | (b) << 8 | (a))

Definition at line 176 of file mallocn_guarded_impl.cc.

◆ MEMFREE

#define MEMFREE   MAKE_ID('F', 'R', 'E', 'E')

Definition at line 182 of file mallocn_guarded_impl.cc.

Referenced by MEM_guarded_freeN().

◆ MEMNEXT

◆ MEMTAG1

#define MEMTAG1   MAKE_ID('M', 'E', 'M', 'O')

Definition at line 179 of file mallocn_guarded_impl.cc.

Referenced by check_memlist(), make_memhead_header(), and MEM_guarded_freeN().

◆ MEMTAG2

#define MEMTAG2   MAKE_ID('R', 'Y', 'B', 'L')

Definition at line 180 of file mallocn_guarded_impl.cc.

Referenced by check_memlist(), make_memhead_header(), and MEM_guarded_freeN().

◆ MEMTAG3

#define MEMTAG3   MAKE_ID('O', 'C', 'K', '!')

Definition at line 181 of file mallocn_guarded_impl.cc.

Referenced by make_memhead_header(), and MEM_guarded_freeN().

Typedef Documentation

◆ localLink

typedef struct localLink localLink

◆ localListBase

typedef struct localListBase localListBase

◆ MemHead

typedef struct MemHead MemHead

◆ MemHeadAligned

typedef MemHead MemHeadAligned

Definition at line 138 of file mallocn_guarded_impl.cc.

◆ MemPrintBlock

typedef struct MemPrintBlock MemPrintBlock

◆ MemTail

typedef struct MemTail MemTail

Enumeration Type Documentation

◆ MemHeadFlag

Enumerator
MEMHEAD_FLAG_FROM_CPP_NEW 

This block of memory has been allocated from CPP new (e.g. #MEM_new, or some guardedalloc-overloaded new operator). It mainly checks that MEM_freeN is not directly called on it (#MEM_delete or some guardedalloc-overloaded delete operator should always be used instead).

Definition at line 141 of file mallocn_guarded_impl.cc.

Function Documentation

◆ addtail()

static void addtail ( volatile localListBase * listbase,
void * vlink )
static

◆ check_memlist()

◆ compare_len()

static int compare_len ( const void * p1,
const void * p2 )
static

Definition at line 750 of file mallocn_guarded_impl.cc.

References MemPrintBlock::len.

Referenced by MEM_guarded_printmemlist_stats().

◆ compare_name()

static int compare_name ( const void * p1,
const void * p2 )
static

Definition at line 742 of file mallocn_guarded_impl.cc.

References MemPrintBlock::name.

Referenced by MEM_guarded_printmemlist_stats().

◆ make_memhead_header()

◆ 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 ( const size_t len,
const size_t size,
const 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()

uint 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_internal()

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

◆ mem_unlock_thread()

◆ MemorY_ErroR()

static void MemorY_ErroR ( const char * block,
const char * error )
static

Definition at line 1186 of file mallocn_guarded_impl.cc.

References error(), and print_error().

Referenced by check_memlist(), and MEM_guarded_freeN().

◆ print_error() [1/2]

◆ print_error() [2/2]

static void print_error ( const char * message,
... )
static

Definition at line 237 of file mallocn_guarded_impl.cc.

References print_error().

◆ rem_memblock()

◆ remlink()

static void remlink ( volatile localListBase * listbase,
void * vlink )
static

◆ report_error_on_address()

static void report_error_on_address ( const void * vmemh,
const char * message,
... )
static

Variable Documentation

◆ _membase

volatile localListBase _membase
static

Definition at line 193 of file mallocn_guarded_impl.cc.

◆ error_callback

void(* error_callback) (const char *) ( const char * ) = nullptr
static

◆ malloc_debug_memset

bool malloc_debug_memset = false
static

◆ mem_in_use

◆ mem_printmemlist_pydict_script

const char mem_printmemlist_pydict_script[]
static
Initial value:
=
"mb_userinfo = {}\n"
"totmem = 0\n"
"for mb_item in membase:\n"
" mb_item_user_size = mb_userinfo.setdefault(mb_item['name'], [0,0])\n"
" mb_item_user_size[0] += 1 # Add a user\n"
" mb_item_user_size[1] += mb_item['len'] # Increment the size\n"
" totmem += mb_item['len']\n"
"print('(membase) items:', len(membase), '| unique-names:',\n"
" len(mb_userinfo), '| total-mem:', totmem)\n"
"mb_userinfo_sort = list(mb_userinfo.items())\n"
"for sort_name, sort_func in (('size', lambda a: -a[1][1]),\n"
" ('users', lambda a: -a[1][0]),\n"
" ('name', lambda a: a[0])):\n"
" print('\\nSorting by:', sort_name)\n"
" mb_userinfo_sort.sort(key = sort_func)\n"
" for item in mb_userinfo_sort:\n"
" print('name:%%s, users:%%i, len:%%i' %%\n"
" (item[0], item[1][0], item[1][1]))\n"

Definition at line 868 of file mallocn_guarded_impl.cc.

Referenced by MEM_guarded_printmemlist_internal().

◆ membase

◆ peak_mem

◆ thread_lock

pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 277 of file mallocn_guarded_impl.cc.

Referenced by mem_lock_thread(), and mem_unlock_thread().

◆ totblock