BRL-CAD

Parallel-protected debugging-enhanced wrapper around system malloc(). More...

#include "common.h"
#include <stdlib.h>
#include <string.h>
#include "errno.h"
#include "bio.h"
#include "bu/debug.h"
#include "bu/list.h"
#include "bu/log.h"
#include "bu/malloc.h"
#include "bu/parallel.h"
Include dependency graph for malloc.c:

Go to the source code of this file.

Data Structures

struct  memdebug
 
struct  memqdebug
 

Macros

#define MALLOC_NOT_MP_SAFE   1
 
#define MDB_MAGIC   0x12348969
 
#define MEMDEBUG_NULL   ((struct memdebug *)0)
 
#define MEMQDEBUG_NULL   ((struct memqdebug *)0)
 
#define ALIGN   8
 

Enumerations

enum  alloc_t { MALLOC, CALLOC }
 

Functions

HIDDEN void memdebug_add (void *ptr, size_t cnt, const char *str)
 
HIDDEN struct memdebugmemdebug_check (register void *ptr, const char *str)
 
int bu_bomb_failsafe_init (void)
 
HIDDEN void * alloc (alloc_t type, size_t cnt, size_t sz, const char *str)
 
void * bu_malloc (size_t size, const char *str)
 
void * bu_calloc (size_t nelem, size_t elsize, const char *str)
 
void bu_free (void *ptr, const char *str)
 
void * bu_realloc (register void *ptr, size_t siz, const char *str)
 
void bu_prmem (const char *str)
 
int bu_malloc_len_roundup (register int nbytes)
 
void bu_ck_malloc_ptr (void *ptr, const char *str)
 
int bu_mem_barriercheck (void)
 
int bu_shmget (int *shmid, char **shared_memory, int key, size_t size)
 

Variables

const char bu_vls_message []
 
const char bu_strdup_message []
 

Detailed Description

Parallel-protected debugging-enhanced wrapper around system malloc().

Provides a parallel-safe interface to the system memory allocator with standardized error checking, optional memory-use logging, and optional run-time pointer and memory corruption testing.

The bu_*alloc() routines can't use bu_log() because that uses the bu_vls() routines which depend on bu_malloc(). So it goes direct to stderr, semaphore protected.

Definition in file malloc.c.

Macro Definition Documentation

#define MALLOC_NOT_MP_SAFE   1

this controls whether to semaphore protect malloc calls

FIXME: we really don't need to do this any more, especially if compiling against glibc+pthreads. Windows, however, needs might need some build flag encouragement.

Definition at line 47 of file malloc.c.

#define MDB_MAGIC   0x12348969
#define MEMDEBUG_NULL   ((struct memdebug *)0)

Definition at line 70 of file malloc.c.

Referenced by bu_free(), bu_realloc(), and memdebug_check().

#define MEMQDEBUG_NULL   ((struct memqdebug *)0)

Definition at line 80 of file malloc.c.

#define ALIGN   8

Referenced by alloc().

Enumeration Type Documentation

enum alloc_t

used by the memory allocation routines going through alloc() to indicate whether allocated memory should be zero'd.

Enumerator
MALLOC 
CALLOC 

Definition at line 54 of file malloc.c.

Function Documentation

HIDDEN void memdebug_add ( void *  ptr,
size_t  cnt,
const char *  str 
)

Add another entry to the memory debug table

Definition at line 91 of file malloc.c.

References bu_bomb(), BU_SEM_MALLOC, bu_semaphore_acquire(), bu_semaphore_release(), LIKELY, memdebug::magic, memdebug::mdb_addr, memdebug::mdb_len, MDB_MAGIC, memdebug::mdb_str, memset(), and top().

Referenced by alloc().

Here is the call graph for this function:

HIDDEN struct memdebug* memdebug_check ( register void *  ptr,
const char *  str 
)

Check an entry against the memory debug table, based upon its address.

Definition at line 156 of file malloc.c.

References bu_bomb(), BU_SEM_MALLOC, bu_semaphore_acquire(), bu_semaphore_release(), memdebug::magic, memdebug::mdb_addr, memdebug::mdb_len, MDB_MAGIC, memdebug::mdb_str, and MEMDEBUG_NULL.

Referenced by bu_free(), and bu_realloc().

Here is the call graph for this function:

int bu_bomb_failsafe_init ( void  )

Definition at line 71 of file bomb.c.

Referenced by alloc().

HIDDEN void* alloc ( alloc_t  type,
size_t  cnt,
size_t  sz,
const char *  str 
)

This routine only returns on successful allocation. We promise never to return a NULL pointer; caller doesn't have to check.

Requesting allocation of zero bytes is considered a irrecoverable mistake in order to fulfill the non-NULL promise.

Failure results in bu_bomb() being called.

type is 0 for malloc, 1 for calloc

Definition at line 202 of file malloc.c.

References ALIGN, bu_bomb(), bu_bomb_failsafe_init(), bu_debug, BU_DEBUG_MEM_CHECK, BU_DEBUG_MEM_LOG, BU_DEBUG_MEM_QCHECK, BU_LIST_APPEND, BU_LIST_INIT, BU_LIST_MAGIC_SET, BU_LIST_NULL, bu_n_malloc, BU_SEM_MALLOC, bu_semaphore_acquire(), bu_semaphore_release(), CALLOC, memqdebug::m, memdebug::magic, MALLOC, memdebug::mdb_addr, memdebug::mdb_len, MDB_MAGIC, memdebug::mdb_str, memdebug_add(), memset(), memqdebug::q, and UNLIKELY.

Referenced by bu_calloc(), and bu_malloc().

Here is the call graph for this function:

void* bu_realloc ( register void *  ptr,
size_t  siz,
const char *  str 
)
int bu_malloc_len_roundup ( register int  nbytes)

Definition at line 585 of file malloc.c.

Variable Documentation

const char bu_vls_message[]

used by malloc and vls as the bu_malloc/bu_free debug string.

NOT published in a public header.

Definition at line 57 of file globals.c.

Referenced by bu_prmem().

const char bu_strdup_message[]

used by malloc and vls as the bu_strdup debug string.

NOT published in a public header.

Definition at line 64 of file globals.c.

Referenced by bu_prmem().