45 char *DMtriggeraddr = NULL;
47 #define TRIGGER(p) if( (p) == DMtriggeraddr ) DMtrigger(); 51 #define TABSIZE (16*1024) 52 static char *__memtab[TABSIZE];
54 #define HDRSIZE (2 * sizeof (unsigned long)) 58 static void abort(
void *
s)
65 static freed(
char *ptr,
char *fname,
int line);
76 unsigned long *lptr = (
unsigned long *) ptr;
79 lptr[0] = lptr[1] = size;
83 *(ptr + size) = GUARD;
99 unsigned long size, *lptr = (
unsigned long *) ptr;
103 lptr = (
unsigned long *) ptr;
104 if (lptr[0] != lptr[1]) {
105 if (lptr[0] == (lptr[1] ^ 0x00ff)) {
106 fprintf(stderr,
"%s[%d]: memcheck(0x%x) already freed - exit\n",
107 fname, line, ptr + HDRSIZE);
110 "%s[%d]: memcheck(0x%x) start pointer corrupt - exit\n",
111 fname, line, ptr + HDRSIZE);
116 if (*(ptr + HDRSIZE + size) != GUARD) {
118 "%s[%d]: memcheck(0x%x) end overwritten - exit\n",
119 fname, line, ptr + HDRSIZE);
136 for (i = 0; i < TABSIZE; i++) {
137 if (__memtab[i] != NULL) {
157 if (
DMverbose || (ptr == DMtriggeraddr)) {
158 size = ((
unsigned long *)ptr)[-2];
159 fprintf(stderr,
"%s[%d]: free(0x%x) (%ld bytes)\n",
160 fname, line, ptr, size);
166 ((
unsigned long *)ptr)[1] ^= 0x00ff;
169 freed(ptr + HDRSIZE, fname, line);
187 if ((ptr = (
char *) malloc(size + HDRSIZE + 1)) == NULL) {
188 fprintf(stderr,
"%s[%d]: malloc(%d) OUT OF MEMORY\n", fname, line,
196 if (
DMverbose || (DMtriggeraddr == ptr)) {
197 fprintf(stderr,
"%s[%d]: malloc(%d) = 0x%x\n",
198 fname, line, size, ptr);
215 char *
DMcalloc(
int size,
int nitems,
char *fname,
int line)
224 totalsize = size * nitems;
225 if ((ptr = (
char *) malloc(totalsize + HDRSIZE + 1)) == NULL) {
226 fprintf(stderr,
"%s[%d]: calloc(%d,%d) OUT OF MEMORY\n",
227 fname, line, size, nitems);
234 for (i = 0; i < totalsize; i++) {
238 if (
DMverbose || (ptr == DMtriggeraddr)) {
239 fprintf(stderr,
"%s[%d]: calloc(%d,%d) = 0x%x\n", fname, line,
256 for (i = 0; i < TABSIZE; i++) {
257 if (__memtab[i] == NULL) {
265 fprintf(stderr,
"Memory table record overflow\n");
274 static freed(
char *ptr,
char *fname,
int line)
278 for (i = 0; i < TABSIZE; i++) {
279 if (__memtab[i] == ptr) {
287 fprintf(stderr,
"%s[%d]: freed(0x%x) NOT MALLOCED\n", fname, line,
303 char *
DMrealloc(
char *ptr,
int size,
char *fname,
int line)
310 if ((ptr = (
char *) realloc(ptr, size + HDRSIZE + 1)) == NULL) {
311 fprintf(stderr,
"%s[%d]: realloc(0x%x,%d) OUT OF MEMORY\n",
318 if (
DMverbose || (DMtriggeraddr == ptr) || (DMtriggeraddr == saveptr)) {
319 fprintf(stderr,
"%s[%d]: realloc(0x%x,%d) = 0x%x\n",
320 fname, line, saveptr, size, ptr);
324 freed(saveptr, fname, line);
337 for (i = 0; i < TABSIZE; i++) {
338 if (__memtab[i] != NULL) {
339 printf(
"0x%x\n", __memtab[i]);
static char * guardit(char *ptr, int size)
Add guard word encoding size on start of memory area and a guard byte just past the end of the area...
static char * memorycheck(char *ptr, char *fname, int line)
Check the validity of allocated memory areas and report any problems.
DMtrigger()
Dummy routine with the sole purpose of being available for setting breakpoints from a debugger...
char * DMrealloc(char *ptr, int size, char *fname, int line)
Reallocate memory safely using realloc()
int s
The socket that the CLI will use to communicate.
DMnotfreed()
Print a list of memory pointers not freed - one per line.
static malloced(char *ptr)
record 'ptr's value in a list of malloc-ed memory
DMfree(char *ptr, char *fname, int line)
Free a pointer allocated by DMmalloc()
char * DMcalloc(int size, int nitems, char *fname, int line)
Allocate memory safely using calloc()
int DMverbose
Verbosity level.
char * DMmalloc(int size, char *fname, int line)
Allocate memory safely using malloc()
static freed(char *ptr, char *fname, int line)
remove 'ptr's value from a list of malloc-ed memory - print error and die if it's not in the list at ...
char * DMmemcheck(char *ptr, char *fname, int line)