#ifndef __DEBUGALLOC_H__ #define __DEBUGALLOC_H__ /* WARNING: NOT MULTYTHEAD SAFE! */ typedef struct { /* Prepend memory for user with this. */ int sze; /* Size of the current allocated block. */ int lne; /* Line number of malloc call. */ int mgc; /* Magic, put in last. */ #define _ALC_MAGIC 0xCAFED0D0 } _alc_t; #define _alc_offset (sizeof(_alc_t)) static long _alc_pending = 0; /* Number of pending malloc() calls. */ static long _alc_usage = 0; /* Total number of bytes. */ static _alc_t *_alc_tmp; static size_t _alc_size; #define malloc(n) \ (void *)( \ _alc_pending++, \ _alc_size = (n), \ _alc_usage += _alc_size, \ (NULL == (_alc_tmp = mall##oc(_alc_size + _alc_offset))) ? \ fprintf(stderr, "M:%s(%d): malloc returned NULL pointer.\n", \ __FILE__, __LINE__), (char *)NULL \ : ( \ _alc_tmp->sze = _alc_size, \ _alc_tmp->lne = __LINE__, \ _alc_tmp->mgc = _ALC_MAGIC, \ fprintf(stderr, "M:%s(%d) allocating %d bytes at %p.\n", \ __FILE__, __LINE__, _alc_size,(char*)_alc_tmp + _alc_offset), \ (char *)_alc_tmp + _alc_offset \ ) \ ) #define calloc(n) \ (void *)( \ _alc_pending++, \ _alc_size = (n), \ _alc_usage += _alc_size, \ (NULL == (_alc_tmp = call##oc(_alc_size + _alc_offset))) ? \ fprintf(stderr, "M:%s(%d): calloc returned NULL pointer.\n", \ __FILE__, __LINE__), (char *)NULL \ : ( \ _alc_tmp->sze = _alc_size, \ _alc_tmp->lne = __LINE__, \ _alc_tmp->mgc = _ALC_MAGIC, \ fprintf(stderr, "M:%s(%d) allocating %d bytes at %p.\n", \ __FILE__, __LINE__, _alc_size,(char*)_alc_tmp + _alc_offset), \ (char *)_alc_tmp + _alc_offset \ ) \ ) #define realloc(p, n) \ (void *)( \ _alc_size = (n), \ NULL == (_alc_tmp=(_alc_t*)(p)) ? \ fprintf(stderr, "M:%s(%d): called realloc() with NULL pointer.\n", \ __FILE__, __LINE__):0, \ _alc_tmp =(_alc_t*)((char*)_alc_tmp - _alc_offset), \ _alc_tmp->mgc != _ALC_MAGIC ? \ fprintf(stderr, "M:%s(%d): called realloc() with wrong pointer (%p)" \ " or memory overrun.\n", \ __FILE__, __LINE__, (char*)_alc_tmp+_alc_offset) \ : ( \ _alc_usage -= _alc_tmp->sze + _alc_size, \ _alc_pending--, \ fprintf(stderr, "M:%s(%d): called realloc(%p,%d), alloced at line" \ " %d.\n", __FILE__, __LINE__, (char*)_alc_tmp + _alc_offset, \ _alc_size, _alc_tmp->lne) \ ), \ fprintf(stderr, "M: %ld calles pending, %ld bytes of memory in use.\n", \ _alc_pending, _alc_usage), \ fflush(stderr), \ fr##ee(_alc_tmp) \ _alc_pending++, \ _alc_usage += _alc_size, \ (NULL == (_alc_tmp = mall##oc(_alc_size + _alc_offset))) ? \ fprintf(stderr, "M:%s(%d): malloc returned NULL pointer.\n", \ __FILE__, __LINE__), (char *)NULL \ : ( \ _alc_tmp->sze = _alc_size, \ _alc_tmp->lne = __LINE__, \ _alc_tmp->mgc = _ALC_MAGIC, \ fprintf(stderr, "M:%s(%d) allocating %d bytes at %p.\n", \ __FILE__, __LINE__, _alc_size,(char*)_alc_tmp + _alc_offset), \ (char *)_alc_tmp + _alc_offset \ ) \ ) #define free(p) \ ( \ NULL == (_alc_tmp=(_alc_t*)(p)) ? \ fprintf(stderr, "M:%s(%d): called free() with NULL pointer.\n", \ __FILE__, __LINE__):0, \ _alc_tmp =(_alc_t*)((char*)_alc_tmp - _alc_offset), \ _alc_tmp->mgc != _ALC_MAGIC ? \ fprintf(stderr, "M:%s(%d): called free() with wrong pointer (%p)" \ " or memory overrun.\n", \ __FILE__, __LINE__, (char*)_alc_tmp+_alc_offset) \ : ( \ _alc_usage -= _alc_tmp->sze, \ _alc_pending--, \ fprintf(stderr, "M:%s(%d): called free(%p), alloced at line %d.\n", \ __FILE__, __LINE__, (char*)_alc_tmp + _alc_offset, \ _alc_tmp->lne) \ ), \ fprintf(stderr, "M: %ld calles pending, %ld bytes of memory in use.\n", \ _alc_pending, _alc_usage), \ fflush(stderr), \ fr##ee(_alc_tmp) \ ) #endif