#include "dnxHeap.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
Go to the source code of this file.
Data Structures | |
struct | overhead_ |
Debug heap block bookkeeping header structure. More... | |
Defines | |
#define | PICKETSZ 8 |
Picket fence size in bytes. | |
#define | PICKET1 0xAA |
Overhead front picket fence fill value. | |
#define | PICKET2 0xBB |
Overhead rear picket fence fill value. | |
#define | PICKET3 0xCC |
User block footer picket fence fill value. | |
#define | ALIGNED 0xDD |
Rear alignment buffer fill value. | |
#define | ALLOCED 0xEE |
User space allocated fill value (malloc). | |
#define | FREED 0xFF |
User space freed memory fill value. | |
#define | ALIGNSZ(sz) (((sz) + 15) & (-1L << 4)) |
Align a block to a paragraph boundary. | |
Typedefs | |
typedef struct overhead_ | overhead |
Debug heap block bookkeeping header structure. | |
Functions | |
static void | link_block (overhead *ohp) |
Link a newly allocated block into the block list. | |
static int | unlink_block (overhead *ohp) |
Locate and remove a block by address from the global block list. | |
static size_t | hex_dump (char *buf, void *p, size_t sz) |
Format a number of bytes as a hexadecimal ascii dump into a text buffer. | |
static void | dump_block (overhead *ohp, char *msg) |
Dump a memory block with an optional debug(1) message. | |
static int | memscan (void *p, unsigned char ch, size_t sz) |
Scan a range of memory for the first non-recognized character. | |
static int | check_block (overhead *ohp) |
Check the integrity of a memory block. | |
void * | dnxMalloc (size_t sz, char *file, int line) |
Allocate and track a new heap memory block. | |
void * | dnxCalloc (size_t n, size_t sz, char *file, int line) |
Allocate, zero and track a new heap memory block. | |
void * | dnxRealloc (void *p, size_t sz, char *file, int line) |
Reallocate (and track) an existing heap memory block. | |
char * | dnxStrdup (char *str, char *file, int line) |
Duplicate and track a string from the heap. | |
void | dnxFree (void *p) |
Free and previously allocated (and tracked) heap memory block. | |
int | dnxCheckHeap (void) |
Check the heap and display any unfreed blocks on the global list. | |
Variables | |
static overhead * | head = 0 |
The head of the global heap block list. | |
static int | blkcnt = 0 |
The number of blocks in the global list. | |
static pthread_mutex_t | list_lock = PTHREAD_MUTEX_INITIALIZER |
The global heap block list lock. |
Definition in file dnxHeap.c.
#define ALIGNED 0xDD |
Rear alignment buffer fill value.
Definition at line 42 of file dnxHeap.c.
Referenced by dnxMalloc().
#define ALIGNSZ | ( | sz | ) | (((sz) + 15) & (-1L << 4)) |
Align a block to a paragraph boundary.
Add 15 and AND the result with 0x...FFFFFFF0 to clear the bottom 4 bits. Works on any word-sized architecture (32, 64, etc).
Definition at line 50 of file dnxHeap.c.
Referenced by dnxMalloc().
#define ALLOCED 0xEE |
User space allocated fill value (malloc).
Definition at line 43 of file dnxHeap.c.
Referenced by dnxMalloc().
#define PICKET1 0xAA |
Overhead front picket fence fill value.
Definition at line 39 of file dnxHeap.c.
Referenced by check_block(), and dnxMalloc().
#define PICKET2 0xBB |
Overhead rear picket fence fill value.
Definition at line 40 of file dnxHeap.c.
Referenced by check_block(), and dnxMalloc().
#define PICKET3 0xCC |
User block footer picket fence fill value.
Definition at line 41 of file dnxHeap.c.
Referenced by check_block(), and dnxMalloc().
#define PICKETSZ 8 |
Picket fence size in bytes.
Definition at line 37 of file dnxHeap.c.
Referenced by check_block(), dnxMalloc(), and dump_block().
static int check_block | ( | overhead * | ohp | ) | [static] |
Check the integrity of a memory block.
[in] | ohp | - a pointer to the block to be checked. |
Definition at line 199 of file dnxHeap.c.
References dump_block(), memscan(), PICKET1, overhead_::picket1, PICKET2, overhead_::picket2, PICKET3, PICKETSZ, and overhead_::reqsz.
Referenced by dnxCheckHeap(), and dnxFree().
void* dnxCalloc | ( | size_t | n, | |
size_t | sz, | |||
char * | file, | |||
int | line | |||
) |
Allocate, zero and track a new heap memory block.
Calloc is sort of a strange bird - it should be the same as malloc with the addition of zeroing the block before returning, but K&R must have decided that the X x Y block allocation strategry had some value for heap blocks that need to be cleared...
[in] | n | - the number of blocks of sz bytes to be allocated. |
[in] | sz | - the size of each of the n blocks to be allocated. |
[in] | file | - the file name from where this function was called. |
[in] | line | - the line number from where this function was called. |
Definition at line 253 of file dnxHeap.c.
References dnxMalloc().
int dnxCheckHeap | ( | void | ) |
Check the heap and display any unfreed blocks on the global list.
Definition at line 334 of file dnxHeap.c.
References blkcnt, check_block(), dnxDebug(), dump_block(), list_lock, and overhead_::next.
void dnxFree | ( | void * | p | ) |
Free and previously allocated (and tracked) heap memory block.
Note that there are serveral good programming-practice reasons to call free with a p
argument value of null. This debug routine allows it.
[in] | p | - a pointer to the debug heap block to be freed. |
Definition at line 311 of file dnxHeap.c.
References check_block(), dnxDebug(), overhead_::file, and unlink_block().
Referenced by dnxRealloc().
void* dnxMalloc | ( | size_t | sz, | |
char * | file, | |||
int | line | |||
) |
Allocate and track a new heap memory block.
malloc allows sz to be zero, in which case, it returns NULL. But there's really no reason to call malloc with a zero value unless the programmer made a mistake, so this routine asserts that sz
is non-zero.
[in] | sz | - the size in bytes of the block to be allocated. |
[in] | file | - the file name from where this function was called. |
[in] | line | - the line number from where this function was called. |
Definition at line 219 of file dnxHeap.c.
References overhead_::actualsz, ALIGNED, ALIGNSZ, ALLOCED, dnxDebug(), overhead_::file, overhead_::line, link_block(), overhead_::next, PICKET1, overhead_::picket1, PICKET2, overhead_::picket2, PICKET3, PICKETSZ, and overhead_::reqsz.
Referenced by dnxCalloc(), dnxRealloc(), and dnxStrdup().
void* dnxRealloc | ( | void * | p, | |
size_t | sz, | |||
char * | file, | |||
int | line | |||
) |
Reallocate (and track) an existing heap memory block.
Realloc - the all-in-one heap management function. If p
is NULL, realloc acts like malloc, if sz
is zero, realloc acts like free.
Since there's no reason except programmer error that would have both p
and sz
be zero at the same time, this routine asserts one or the other is non-zero.
[in] | p | - a pointer to the block to be reallocated/resized. |
[in] | sz | - the new size of the block. |
[in] | file | - the file name from where this function was called. |
[in] | line | - the line number from where this function was called. |
Definition at line 270 of file dnxHeap.c.
References dnxFree(), dnxMalloc(), and overhead_::reqsz.
char* dnxStrdup | ( | char * | str, | |
char * | file, | |||
int | line | |||
) |
Duplicate and track a string from the heap.
[in] | str | - a pointer to the zero-terminated string to be duplicated. |
[in] | file | - the file name from where this function was called. |
[in] | line | - the line number from where this function was called. |
Definition at line 292 of file dnxHeap.c.
References dnxMalloc().
static void dump_block | ( | overhead * | ohp, | |
char * | msg | |||
) | [static] |
Dump a memory block with an optional debug(1) message.
[in] | ohp | - a pointer to the block to be displayed. |
[in] | msg | - a pointer to the message to be displayed. |
Definition at line 143 of file dnxHeap.c.
References dnxDebug(), overhead_::file, hex_dump(), overhead_::line, overhead_::picket1, overhead_::picket2, PICKETSZ, and overhead_::reqsz.
Referenced by check_block(), and dnxCheckHeap().
static size_t hex_dump | ( | char * | buf, | |
void * | p, | |||
size_t | sz | |||
) | [static] |
Format a number of bytes as a hexadecimal ascii dump into a text buffer.
[out] | buf | - a pointer to the text output buffer. |
[in] | p | - a pointer to the bytes to be formatted into buf . |
[in] | sz | - the number of bytes of p to be formatted into buf . |
buf
that were used, minus the trailing null-terminator; buf
+ this return value is a pointer to the trailing null-terminator just written. Definition at line 123 of file dnxHeap.c.
Referenced by dump_block().
static void link_block | ( | overhead * | ohp | ) | [static] |
Link a newly allocated block into the block list.
[in] | ohp | - a pointer to the block to be linked into the global list. |
Definition at line 77 of file dnxHeap.c.
References blkcnt, list_lock, and overhead_::next.
Referenced by dnxMalloc().
static int memscan | ( | void * | p, | |
unsigned char | ch, | |||
size_t | sz | |||
) | [static] |
Scan a range of memory for the first non-recognized character.
[in] | p | - a pointer to the buffer to be scanned. |
[in] | ch | - the expected fill character. |
[in] | sz | - the number of bytes to be scanned in p . |
sz
bytes of p
are equal to ch
. Definition at line 183 of file dnxHeap.c.
Referenced by check_block().
static int unlink_block | ( | overhead * | ohp | ) | [static] |
Locate and remove a block by address from the global block list.
[in] | ohp | - a pointer to the block to be located and removed. |
Definition at line 94 of file dnxHeap.c.
References blkcnt, list_lock, and overhead_::next.
Referenced by dnxFree().
int blkcnt = 0 [static] |
The number of blocks in the global list.
Definition at line 65 of file dnxHeap.c.
Referenced by dnxCheckHeap(), link_block(), and unlink_block().
pthread_mutex_t list_lock = PTHREAD_MUTEX_INITIALIZER [static] |
The global heap block list lock.
Definition at line 66 of file dnxHeap.c.
Referenced by dnxCheckHeap(), link_block(), and unlink_block().