void * mymalloc(unsigned size, Memory_Type type) { char *memptr; char msg[100]; int offs; if (size == 0) /* size 0 won't play nice with offs */ size = 1; offs = refcount_overhead(type); memptr = (char *) malloc(size + offs); if (!memptr) { sprintf(msg, "memory allocation (size %u) failed!", size); panic(msg); } alloc_num[type]++; if (offs) { memptr += offs; /* actual object starts here */ ((int *) memptr)[-1] = 1; /* initial refcount = 1 */ #ifdef MEMO_STRLEN if (type == M_STRING) ((int *) memptr)[-2] = size - 1; #endif /* MEMO_STRLEN */ } return memptr; }
void myfree(void *ptr, Memory_Type type) { alloc_num[type]--; free((char *) ptr - refcount_overhead(type)); }
void * myrealloc(void *ptr, unsigned size, Memory_Type type) { int offs = refcount_overhead(type); static char msg[100]; #ifdef USE_GNU_MALLOC { extern unsigned malloc_real_size(void *ptr); extern unsigned malloc_size(void *ptr); alloc_size[type] -= malloc_size(ptr); alloc_real_size[type] -= malloc_real_size(ptr); #endif ptr = realloc((char *) ptr - offs, size + offs); if (!ptr) { sprintf(msg, "memory re-allocation (size %u) failed!", size); panic(msg); } #ifdef USE_GNU_MALLOC alloc_size[type] += malloc_size(ptr); alloc_real_size[type] += malloc_real_size(ptr); } #endif return (char *) ptr + offs; }
void * myrealloc(void *ptr, unsigned size, Memory_Type type) { int offs = refcount_overhead(type); static char msg[100]; ptr = realloc((char *) ptr - offs, size + offs); if (!ptr) { sprintf(msg, "memory re-allocation (size %u) failed!", size); panic(msg); } return (char *) ptr + offs; }
void myfree(void *ptr, Memory_Type type) { alloc_num[type]--; #ifdef USE_GNU_MALLOC { extern unsigned malloc_real_size(void *ptr); extern unsigned malloc_size(void *ptr); alloc_size[type] -= malloc_size(ptr); alloc_real_size[type] -= malloc_real_size(ptr); } #endif free((char *) ptr - refcount_overhead(type)); }
void * mymalloc(unsigned size, Memory_Type type) { char *memptr; char msg[100]; int offs; if (size == 0) /* For queasy systems */ size = 1; offs = refcount_overhead(type); memptr = (char *) malloc(size + offs); if (!memptr) { sprintf(msg, "memory allocation (size %u) failed!", size); panic(msg); } alloc_num[type]++; #ifdef USE_GNU_MALLOC { extern unsigned malloc_real_size(void *ptr); extern unsigned malloc_size(void *ptr); alloc_size[type] += malloc_size(memptr); alloc_real_size[type] += malloc_real_size(memptr); } #endif if (offs) { memptr += offs; ((int *) memptr)[-1] = 1; #ifdef MEMO_STRLEN if (type == M_STRING) ((int *) memptr)[-2] = size - 1; #endif /* MEMO_STRLEN */ } return memptr; }