33 char *DMtriggeraddr = NULL;
35 #define TRIGGER(p) if( (p) == DMtriggeraddr ) DMtrigger();
39 #define TABSIZE (16*1024)
40 static char *__memtab[TABSIZE];
42 #define HDRSIZE (2 * sizeof (unsigned long))
46 static void abort(
void *
s)
53 static freed(
char *ptr,
char *fname,
int line);
64 unsigned long *lptr = (
unsigned long *) ptr;
67 lptr[0] = lptr[1] = size;
71 *(ptr + size) = GUARD;
87 unsigned long size, *lptr = (
unsigned long *) ptr;
91 lptr = (
unsigned long *) ptr;
92 if (lptr[0] != lptr[1]) {
93 if (lptr[0] == (lptr[1] ^ 0x00ff)) {
94 fprintf(stderr,
"%s[%d]: memcheck(0x%x) already freed - exit\n",
95 fname, line, ptr + HDRSIZE);
98 "%s[%d]: memcheck(0x%x) start pointer corrupt - exit\n",
99 fname, line, ptr + HDRSIZE);
104 if (*(ptr + HDRSIZE + size) != GUARD) {
106 "%s[%d]: memcheck(0x%x) end overwritten - exit\n",
107 fname, line, ptr + HDRSIZE);
124 for (i = 0; i < TABSIZE; i++) {
125 if (__memtab[i] != NULL) {
145 if (
DMverbose || (ptr == DMtriggeraddr)) {
146 size = ((
unsigned long *)ptr)[-2];
147 fprintf(stderr,
"%s[%d]: free(0x%x) (%ld bytes)\n",
148 fname, line, ptr, size);
154 ((
unsigned long *)ptr)[1] ^= 0x00ff;
157 freed(ptr + HDRSIZE, fname, line);
175 if ((ptr = (
char *) malloc(size + HDRSIZE + 1)) == NULL) {
176 fprintf(stderr,
"%s[%d]: malloc(%d) OUT OF MEMORY\n", fname, line,
184 if (
DMverbose || (DMtriggeraddr == ptr)) {
185 fprintf(stderr,
"%s[%d]: malloc(%d) = 0x%x\n",
186 fname, line, size, ptr);
203 char *
DMcalloc(
int size,
int nitems,
char *fname,
int line)
212 totalsize = size * nitems;
213 if ((ptr = (
char *) malloc(totalsize + HDRSIZE + 1)) == NULL) {
214 fprintf(stderr,
"%s[%d]: calloc(%d,%d) OUT OF MEMORY\n",
215 fname, line, size, nitems);
222 for (i = 0; i < totalsize; i++) {
226 if (
DMverbose || (ptr == DMtriggeraddr)) {
227 fprintf(stderr,
"%s[%d]: calloc(%d,%d) = 0x%x\n", fname, line,
244 for (i = 0; i < TABSIZE; i++) {
245 if (__memtab[i] == NULL) {
253 fprintf(stderr,
"Memory table record overflow\n");
262 static freed(
char *ptr,
char *fname,
int line)
266 for (i = 0; i < TABSIZE; i++) {
267 if (__memtab[i] == ptr) {
275 fprintf(stderr,
"%s[%d]: freed(0x%x) NOT MALLOCED\n", fname, line,
291 char *
DMrealloc(
char *ptr,
int size,
char *fname,
int line)
298 if ((ptr = (
char *) realloc(ptr, size + HDRSIZE + 1)) == NULL) {
299 fprintf(stderr,
"%s[%d]: realloc(0x%x,%d) OUT OF MEMORY\n",
306 if (
DMverbose || (DMtriggeraddr == ptr) || (DMtriggeraddr == saveptr)) {
307 fprintf(stderr,
"%s[%d]: realloc(0x%x,%d) = 0x%x\n",
308 fname, line, saveptr, size, ptr);
312 freed(saveptr, fname, line);
325 for (i = 0; i < TABSIZE; i++) {
326 if (__memtab[i] != NULL) {
327 printf(
"0x%x\n", __memtab[i]);
static malloced(char *ptr)
record 'ptr's value in a list of malloc-ed memory
char * DMcalloc(int size, int nitems, char *fname, int line)
Allocate memory safely using calloc()
char * DMmemcheck(char *ptr, char *fname, int line)
Check the validity of allocated memory areas and report any problems.
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.
char * DMrealloc(char *ptr, int size, char *fname, int line)
Reallocate memory safely using realloc()
DMnotfreed()
Print a list of memory pointers not freed - one per line.
DMfree(char *ptr, char *fname, int line)
Free a pointer allocated by DMmalloc()
DMtrigger()
Dummy routine with the sole purpose of being available for setting breakpoints from a debugger.
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 ...
int s
The socket that the CLI will use to communicate.