kduma.c

Summary
KDUMA version string
MEMORY_CREATION_SIZE is the amount of memory to get from the operating system at one time.
State of slot values (empty, free, etc)
Where did we get file info
Whats are initialization start
Memory region of slot.
Struct Slot contains all of the information about a malloc buffer except for the contents of its memory.
DUMA_ALIGNMENT is a global variable used to control the default alignment of buffers returned by malloc(), calloc(), and realloc().
DUMA_PROTECT_BELOW is used to modify the behavior of the allocator.
DUMA_FILL is set to 0-255 if DUMA should fill all new allocated memory with the specified value.
DUMA_SLACKFILL is set to 0-255.
DUMA_PROTECT_FREE is used to control the disposition of memory that is released using free().
DUMA_MAX_ALLOC is used to control the maximum memory print of the program in total: When the sum of allocated and protected memory would exceed this value in kB, the protected memory is freed/deleted.
DUMA_ALLOW_MALLOC_0 is set if DUMA is to allow malloc(0).
DUMA_MALLOC_FAILEXIT controls the behaviour of DUMA when malloc() fails and would return NULL.
DUMA_FREE_ACCESS is set if DUMA is to write access memory before freeing it.
DUMA_SHOW_ALLOC is set if DUMA is to print all allocations and deallocations to the console.
_DUMA_allocList points to the array of slot structures used to manage the malloc arena.
_duma_allocListSize is the size of the allocation list.
slotCount is the number of Slot structures in allocationList.
unUsedSlots is the number of Slot structures that are currently available to represent new malloc buffers.
slotsPerPage is the number of slot structures that fit in a virtual memory page.
internal variable: sum of allocated -freed +protected memory in kB
internal variable: sum of allocated memory in kB
internal variable: sum of protected memory in kB
internal variable: number of deallocations processed so far
internal variable: number of allocations processed so far
internal variable: state of initialization
Print message and halt program execution in crazy way.
duma_init sets configuration settings.
_duma_init sets up the memory allocation arena and the run-time configuration information.
allocateMoreSlots is called when there are only enough slot structures left to support the allocation of a single malloc buffer.
This is the memory allocator.
Deallocate allocated memory after running some checks, then open slot for use.
A version of kmalloc.

Variables

version

static const char version[]

KDUMA version string

MEMORY_CREATION_SIZE

MEMORY_CREATION_SIZE is the amount of memory to get from the operating system at one time.  We’ll break that memory down into smaller pieces for malloc buffers.  One megabyte is probably a good value.

Enumerations

_DUMA_SlotState

State of slot values (empty, free, etc)

_DUMA_Slot_FileSource

Where did we get file info

_DUMA_InitState

Whats are initialization start

_DUMA_MemRegion

Memory region of slot.  Really we should only hang onto NORMAL memory.  DMA should be released fast.

_DUMA_Slot

Struct Slot contains all of the information about a malloc buffer except for the contents of its memory.

Summary
DUMA_ALIGNMENT is a global variable used to control the default alignment of buffers returned by malloc(), calloc(), and realloc().
DUMA_PROTECT_BELOW is used to modify the behavior of the allocator.
DUMA_FILL is set to 0-255 if DUMA should fill all new allocated memory with the specified value.
DUMA_SLACKFILL is set to 0-255.
DUMA_PROTECT_FREE is used to control the disposition of memory that is released using free().
DUMA_MAX_ALLOC is used to control the maximum memory print of the program in total: When the sum of allocated and protected memory would exceed this value in kB, the protected memory is freed/deleted.
DUMA_ALLOW_MALLOC_0 is set if DUMA is to allow malloc(0).
DUMA_MALLOC_FAILEXIT controls the behaviour of DUMA when malloc() fails and would return NULL.
DUMA_FREE_ACCESS is set if DUMA is to write access memory before freeing it.
DUMA_SHOW_ALLOC is set if DUMA is to print all allocations and deallocations to the console.
_DUMA_allocList points to the array of slot structures used to manage the malloc arena.
_duma_allocListSize is the size of the allocation list.
slotCount is the number of Slot structures in allocationList.
unUsedSlots is the number of Slot structures that are currently available to represent new malloc buffers.
slotsPerPage is the number of slot structures that fit in a virtual memory page.
internal variable: sum of allocated -freed +protected memory in kB
internal variable: sum of allocated memory in kB
internal variable: sum of protected memory in kB
internal variable: number of deallocations processed so far
internal variable: number of allocations processed so far
internal variable: state of initialization
Print message and halt program execution in crazy way.
duma_init sets configuration settings.
_duma_init sets up the memory allocation arena and the run-time configuration information.
allocateMoreSlots is called when there are only enough slot structures left to support the allocation of a single malloc buffer.
This is the memory allocator.
Deallocate allocated memory after running some checks, then open slot for use.
A version of kmalloc.

Variables

DUMA_ALIGNMENT

size_t DUMA_ALIGNMENT

DUMA_ALIGNMENT is a global variable used to control the default alignment of buffers returned by malloc(), calloc(), and realloc().  It is all-caps so that its name matches the name of the environment variable that is used to set it.  This gives the programmer one less name to remember.

DUMA_PROTECT_BELOW

int DUMA_PROTECT_BELOW

DUMA_PROTECT_BELOW is used to modify the behavior of the allocator.  When its value is non-zero, the allocator will place an inaccessable page immediately before the malloc buffer in the address space, instead of after it.  Use this to detect malloc buffer under-runs, rather than over-runs.  It won’t detect both at the same time, so you should test your software twice, once with this value clear, and once with it set.

DUMA_FILL

int DUMA_FILL

DUMA_FILL is set to 0-255 if DUMA should fill all new allocated memory with the specified value.  Set to -1 when DUMA should not initialise allocated memory. default is set to initialise with 255, cause many programs rely on initialisation to 0!

DUMA_SLACKFILL

static int DUMA_SLACKFILL

DUMA_SLACKFILL is set to 0-255.  The slack / no mans land of all new allocated memory is filled with the specified value. default is set to initialise with 0xAA (=binary 10101010) initialisation to 0!

DUMA_PROTECT_FREE

static long DUMA_PROTECT_FREE

DUMA_PROTECT_FREE is used to control the disposition of memory that is released using free().  It is all-caps so that its name matches the name of the environment variable that is used to set it.  If its value is non-zero, memory released by free is made inaccessable.  Any software that touches free memory will then get a segmentation fault.  Depending on your application and your resources you may tell DUMA not to use this memory ever again by setting a negative value f.e.  -1.  You can tell DUMA to limit the sum of protected memory by setting a positive value, which is interpreted in kB.  If its value is zero, freed memory will be available for reallocation, but will still be inaccessable until it is reallocated.

DUMA_MAX_ALLOC

static long DUMA_MAX_ALLOC

DUMA_MAX_ALLOC is used to control the maximum memory print of the program in total: When the sum of allocated and protected memory would exceed this value in kB, the protected memory is freed/deleted.

DUMA_ALLOW_MALLOC_0

static int DUMA_ALLOW_MALLOC_0

DUMA_ALLOW_MALLOC_0 is set if DUMA is to allow malloc(0).  I trap malloc(0) by default because it is a common source of bugs.  But you should know the allocation with size 0 is ANSI conform.

DUMA_MALLOC_FAILEXIT

static int DUMA_MALLOC_FAILEXIT

DUMA_MALLOC_FAILEXIT controls the behaviour of DUMA when malloc() fails and would return NULL.  But most applications don’t check the return value for errors ... so default to Exit on Fail

DUMA_FREE_ACCESS

static int DUMA_FREE_ACCESS

DUMA_FREE_ACCESS is set if DUMA is to write access memory before freeing it.  This makes easier using watch expressions in debuggers as the process is interrupted even if the memory is going to be freed.

DUMA_SHOW_ALLOC

static int DUMA_SHOW_ALLOC

DUMA_SHOW_ALLOC is set if DUMA is to print all allocations and deallocations to the console.  Although this generates a lot of messages, the option can be useful to detect inefficient code containing many allocations / deallocations

_duma_allocList

struct _DUMA_Slot * _duma_allocList

_DUMA_allocList points to the array of slot structures used to manage the malloc arena.

_duma_allocListSize

static size_t _duma_allocListSize

_duma_allocListSize is the size of the allocation list.  This will always be a multiple of the page size.

slotCount

static size_t slotCount

slotCount is the number of Slot structures in allocationList.

unUsedSlots

static size_t unUsedSlots

unUsedSlots is the number of Slot structures that are currently available to represent new malloc buffers.  When this number gets too low, we will create new slots.

slotsPerPage

static size_t slotsPerPage

slotsPerPage is the number of slot structures that fit in a virtual memory page.

sumAllocatedMem

static long sumAllocatedMem

internal variable: sum of allocated -freed +protected memory in kB

sumTotalAllocatedMem

static long sumTotalAllocatedMem

internal variable: sum of allocated memory in kB

sumProtectedMem

static long sumProtectedMem

internal variable: sum of protected memory in kB

numDeallocs

static long numDeallocs

internal variable: number of deallocations processed so far

numAllocs

static long numAllocs

internal variable: number of allocations processed so far

duma_init_done

internal variable: state of initialization

Functions

_duma_assert

void _duma_assert( const  char  * exprstr,
const  char  * filename,
int  lineno )

Print message and halt program execution in crazy way.

duma_init

void duma_init( void )

duma_init sets configuration settings.  Can sometimes cause problems when called from _duma_init.

See Also: _duma_init

_duma_init

void _duma_init( void )

_duma_init sets up the memory allocation arena and the run-time configuration information.  We will call duma_init unless DUMA_EXPLICIT_INIT is defined at compile time.

See Also: duma_init

allocateMoreSlots

static void allocateMoreSlots( void )

allocateMoreSlots is called when there are only enough slot structures left to support the allocation of a single malloc buffer.

See Also: _duma_allocate

_duma_allocate

void * _duma_allocate( size_t  alignment,
size_t  userSize,
int  protectBelow,
int  fillByte,
int  protectAllocList,
enum  _DUMA_Allocator  allocator,
enum _DUMA_FailReturn  fail  DUMA_PARAMLIST_FL )

This is the memory allocator.  When asked to allocate a buffer, allocate it in such a way that the end of the buffer is followed by an inaccessable memory page.  If software overruns that buffer, it will touch the bad page and get an immediate segmentation fault.  It’s then easy to zero in on the offending code with a debugger.

There are a few complications.  If the user asks for an odd-sized buffer, we would have to have that buffer start on an odd address if the byte after the end of the buffer was to be on the inaccessable page.  Unfortunately, there is lots of software that asks for odd-sized buffers and then requires that the returned address be word-aligned, or the size of the buffer be a multiple of the word size.  An example are the string-processing functions on Sun systems, which do word references to the string memory and may refer to memory up to three bytes beyond the end of the string.  For this reason, I take the alignment requests to memalign() and valloc() seriously, and

DUMA wastes lots of memory.

See Also: _duma_deallocate

_duma_deallocate

void _duma_deallocate( void  * address,
int  protectAllocList,
enum _DUMA_Allocator  allocator  DUMA_PARAMLIST_FL )

Deallocate allocated memory after running some checks, then open slot for use.  Uses Page_Delete to free the underlying memory.

See Also: Page_Delete _duma_allocate

_duma_kmalloc

void * _duma_kmalloc( size_t  size,
int  flags  DUMA_PARAMLIST_FL )

A version of kmalloc.

static const char version[]
KDUMA version string
size_t DUMA_ALIGNMENT
DUMA_ALIGNMENT is a global variable used to control the default alignment of buffers returned by malloc(), calloc(), and realloc().
int DUMA_PROTECT_BELOW
DUMA_PROTECT_BELOW is used to modify the behavior of the allocator.
int DUMA_FILL
DUMA_FILL is set to 0-255 if DUMA should fill all new allocated memory with the specified value.
static int DUMA_SLACKFILL
DUMA_SLACKFILL is set to 0-255.
static long DUMA_PROTECT_FREE
DUMA_PROTECT_FREE is used to control the disposition of memory that is released using free().
static long DUMA_MAX_ALLOC
DUMA_MAX_ALLOC is used to control the maximum memory print of the program in total: When the sum of allocated and protected memory would exceed this value in kB, the protected memory is freed/deleted.
static int DUMA_ALLOW_MALLOC_0
DUMA_ALLOW_MALLOC_0 is set if DUMA is to allow malloc(0).
static int DUMA_MALLOC_FAILEXIT
DUMA_MALLOC_FAILEXIT controls the behaviour of DUMA when malloc() fails and would return NULL.
static int DUMA_FREE_ACCESS
DUMA_FREE_ACCESS is set if DUMA is to write access memory before freeing it.
static int DUMA_SHOW_ALLOC
DUMA_SHOW_ALLOC is set if DUMA is to print all allocations and deallocations to the console.
struct _DUMA_Slot * _duma_allocList
_DUMA_allocList points to the array of slot structures used to manage the malloc arena.
static size_t _duma_allocListSize
_duma_allocListSize is the size of the allocation list.
static size_t slotCount
slotCount is the number of Slot structures in allocationList.
static size_t unUsedSlots
unUsedSlots is the number of Slot structures that are currently available to represent new malloc buffers.
static size_t slotsPerPage
slotsPerPage is the number of slot structures that fit in a virtual memory page.
static long sumAllocatedMem
internal variable: sum of allocated -freed +protected memory in kB
static long sumTotalAllocatedMem
internal variable: sum of allocated memory in kB
static long sumProtectedMem
internal variable: sum of protected memory in kB
static long numDeallocs
internal variable: number of deallocations processed so far
static long numAllocs
internal variable: number of allocations processed so far
void _duma_assert( const  char  * exprstr,
const  char  * filename,
int  lineno )
Print message and halt program execution in crazy way.
void duma_init( void )
duma_init sets configuration settings.
void _duma_init( void )
_duma_init sets up the memory allocation arena and the run-time configuration information.
static void allocateMoreSlots( void )
allocateMoreSlots is called when there are only enough slot structures left to support the allocation of a single malloc buffer.
void * _duma_allocate( size_t  alignment,
size_t  userSize,
int  protectBelow,
int  fillByte,
int  protectAllocList,
enum  _DUMA_Allocator  allocator,
enum _DUMA_FailReturn  fail  DUMA_PARAMLIST_FL )
This is the memory allocator.
void _duma_deallocate( void  * address,
int  protectAllocList,
enum _DUMA_Allocator  allocator  DUMA_PARAMLIST_FL )
Deallocate allocated memory after running some checks, then open slot for use.
void * _duma_kmalloc( size_t  size,
int  flags  DUMA_PARAMLIST_FL )
A version of kmalloc.
static void Page_Delete( void  * address,
size_t  size )
Free’s DUMA allocated memory.