Blender  V3.3
Classes | Macros | Typedefs | Functions | Variables
mallocn_guarded_impl.c 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.h"

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
 

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 (MemHead *memh)
 
static void print_error (const char *str,...)
 
static void mem_lock_thread (void)
 
static void mem_unlock_thread (void)
 
bool MEM_guarded_consistency_check (void)
 
void MEM_guarded_set_error_callback (void(*func)(const char *))
 
void MEM_guarded_set_memory_debug (void)
 
size_t MEM_guarded_allocN_len (const void *vmemh)
 
voidMEM_guarded_dupallocN (const void *vmemh)
 
voidMEM_guarded_reallocN_id (void *vmemh, size_t len, const char *str)
 
voidMEM_guarded_recallocN_id (void *vmemh, size_t len, const char *str)
 
static void make_memhead_header (MemHead *memh, size_t len, const char *str)
 
voidMEM_guarded_mallocN (size_t len, const char *str)
 
voidMEM_guarded_malloc_arrayN (size_t len, size_t size, const char *str)
 
voidMEM_guarded_mallocN_aligned (size_t len, size_t alignment, const char *str)
 
voidMEM_guarded_callocN (size_t len, const char *str)
 
voidMEM_guarded_calloc_arrayN (size_t len, size_t size, 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 (void)
 
static void MEM_guarded_printmemlist_internal (int pydict)
 
void MEM_guarded_callbackmemlist (void(*func)(void *))
 
void MEM_guarded_printmemlist (void)
 
void MEM_guarded_printmemlist_pydict (void)
 
void MEM_guarded_freeN (void *vmemh)
 
size_t MEM_guarded_get_peak_memory (void)
 
void MEM_guarded_reset_peak_memory (void)
 
size_t MEM_guarded_get_memory_in_use (void)
 
unsigned int MEM_guarded_get_memory_blocks_in_use (void)
 
const char * MEM_guarded_name_ptr (void *vmemh)
 

Variables

static unsigned int totblock = 0
 
static size_t mem_in_use = 0
 
static size_t peak_mem = 0
 
static volatile struct localListBase _membase
 
static volatile struct localListBasemembase = &_membase
 
static void(* error_callback )(const char *) = NULL
 
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.c.

Macro Definition Documentation

◆ MAKE_ID

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

Definition at line 133 of file mallocn_guarded_impl.c.

◆ MEMFREE

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

Definition at line 139 of file mallocn_guarded_impl.c.

◆ MEMNEXT

#define MEMNEXT (   x)    ((MemHead *)(((char *)x) - offsetof(MemHead, next)))

Definition at line 141 of file mallocn_guarded_impl.c.

◆ MEMTAG1

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

Definition at line 136 of file mallocn_guarded_impl.c.

◆ MEMTAG2

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

Definition at line 137 of file mallocn_guarded_impl.c.

◆ MEMTAG3

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

Definition at line 138 of file mallocn_guarded_impl.c.

Typedef Documentation

◆ localLink

typedef struct localLink localLink

◆ localListBase

typedef struct localListBase localListBase

◆ MemHead

typedef struct MemHead MemHead

◆ MemHeadAligned

Definition at line 101 of file mallocn_guarded_impl.c.

◆ MemPrintBlock

typedef struct MemPrintBlock MemPrintBlock

◆ MemTail

typedef struct MemTail MemTail

Function Documentation

◆ addtail()

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

◆ check_memlist()

static const char * check_memlist ( MemHead memh)
static

◆ compare_len()

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

Definition at line 590 of file mallocn_guarded_impl.c.

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 582 of file mallocn_guarded_impl.c.

References MemPrintBlock::name.

Referenced by MEM_guarded_printmemlist_stats().

◆ make_memhead_header()

static void make_memhead_header ( MemHead memh,
size_t  len,
const char *  str 
)
static

◆ MEM_guarded_allocN_len()

size_t MEM_guarded_allocN_len ( const void vmemh)

Definition at line 229 of file mallocn_guarded_impl.c.

References MemHead::len.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_callbackmemlist()

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

◆ MEM_guarded_calloc_arrayN()

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

◆ MEM_guarded_callocN()

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

◆ MEM_guarded_consistency_check()

bool MEM_guarded_consistency_check ( void  )

Definition at line 206 of file mallocn_guarded_impl.c.

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

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_dupallocN()

void* MEM_guarded_dupallocN ( const void vmemh)

◆ MEM_guarded_freeN()

void MEM_guarded_freeN ( void vmemh)

◆ MEM_guarded_get_memory_blocks_in_use()

unsigned int MEM_guarded_get_memory_blocks_in_use ( void  )

Definition at line 1180 of file mallocn_guarded_impl.c.

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 1169 of file mallocn_guarded_impl.c.

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 1151 of file mallocn_guarded_impl.c.

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

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

◆ MEM_guarded_mallocN_aligned()

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

◆ MEM_guarded_name_ptr()

const char* MEM_guarded_name_ptr ( void vmemh)

Definition at line 1192 of file mallocn_guarded_impl.c.

References MemHead::name.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_printmemlist()

void MEM_guarded_printmemlist ( void  )

Definition at line 840 of file mallocn_guarded_impl.c.

References MEM_guarded_printmemlist_internal().

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_printmemlist_internal()

static void MEM_guarded_printmemlist_internal ( int  pydict)
static

◆ MEM_guarded_printmemlist_pydict()

void MEM_guarded_printmemlist_pydict ( void  )

Definition at line 844 of file mallocn_guarded_impl.c.

References MEM_guarded_printmemlist_internal().

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_printmemlist_stats()

void MEM_guarded_printmemlist_stats ( void  )

◆ MEM_guarded_reallocN_id()

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

◆ MEM_guarded_recallocN_id()

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

◆ MEM_guarded_reset_peak_memory()

void MEM_guarded_reset_peak_memory ( void  )

◆ MEM_guarded_set_error_callback()

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

Definition at line 219 of file mallocn_guarded_impl.c.

References error_callback.

Referenced by MEM_use_guarded_allocator().

◆ MEM_guarded_set_memory_debug()

void MEM_guarded_set_memory_debug ( void  )

Definition at line 224 of file mallocn_guarded_impl.c.

References malloc_debug_memset.

Referenced by MEM_use_guarded_allocator().

◆ mem_lock_thread()

static void mem_lock_thread ( void  )
static

◆ mem_unlock_thread()

static void mem_unlock_thread ( void  )
static

◆ MemorY_ErroR()

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

Definition at line 1010 of file mallocn_guarded_impl.c.

References error(), and print_error().

Referenced by check_memlist(), and MEM_guarded_freeN().

◆ print_error()

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

◆ rem_memblock()

static void rem_memblock ( MemHead memh)
static

◆ remlink()

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

Variable Documentation

◆ _membase

volatile struct localListBase _membase
static

Definition at line 148 of file mallocn_guarded_impl.c.

◆ error_callback

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

Definition at line 152 of file mallocn_guarded_impl.c.

Referenced by MEM_guarded_set_error_callback(), and print_error().

◆ malloc_debug_memset

bool malloc_debug_memset = false
static

◆ mem_in_use

size_t mem_in_use = 0
static

◆ 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 708 of file mallocn_guarded_impl.c.

Referenced by MEM_guarded_printmemlist_internal().

◆ membase

volatile struct localListBase* membase = &_membase
static

◆ peak_mem

size_t peak_mem = 0
static

◆ thread_lock

pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 194 of file mallocn_guarded_impl.c.

Referenced by mem_lock_thread(), and mem_unlock_thread().

◆ totblock

unsigned int totblock = 0
static