| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include "config.h" |
| 9 |
|
| 10 |
#define MAGIC 0x1f2e3d4c |
| 11 |
|
| 12 |
struct alist { int magic, size, index; int *end; struct alist *next, *last; }; |
| 13 |
|
| 14 |
static struct alist list = { 0, 0, 0, 0 }; |
| 15 |
|
| 16 |
static int mallocs=0; |
| 17 |
static int reallocs=0; |
| 18 |
static int frees=0; |
| 19 |
|
| 20 |
static int index = 0; |
| 21 |
|
| 22 |
static void |
| 23 |
die(char *msg, int index) |
| 24 |
{ |
| 25 |
fprintf(stderr, msg, index); |
| 26 |
abort(); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
void * |
| 31 |
acalloc(int count, int size) |
| 32 |
{ |
| 33 |
struct alist *ret; |
| 34 |
|
| 35 |
if ( size > 1 ) { |
| 36 |
count *= size; |
| 37 |
size = 1; |
| 38 |
} |
| 39 |
|
| 40 |
if ( ret = calloc(count + sizeof(struct alist) + sizeof(int), size) ) { |
| 41 |
ret->magic = MAGIC; |
| 42 |
ret->size = size * count; |
| 43 |
ret->index = index ++; |
| 44 |
ret->end = (int*)(count + (char*) (ret + 1)); |
| 45 |
*(ret->end) = ~MAGIC; |
| 46 |
if ( list.next ) { |
| 47 |
ret->next = list.next; |
| 48 |
ret->last = &list; |
| 49 |
ret->next->last = ret; |
| 50 |
list.next = ret; |
| 51 |
} |
| 52 |
else { |
| 53 |
ret->last = ret->next = &list; |
| 54 |
list.next = list.last = ret; |
| 55 |
} |
| 56 |
++mallocs; |
| 57 |
return ret+1; |
| 58 |
} |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
void* |
| 64 |
amalloc(int size) |
| 65 |
{ |
| 66 |
return acalloc(size,1); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
void |
| 71 |
afree(void *ptr) |
| 72 |
{ |
| 73 |
struct alist *p2 = ((struct alist*)ptr)-1; |
| 74 |
|
| 75 |
if ( p2->magic == MAGIC ) { |
| 76 |
if ( ! (p2->end && *(p2->end) == ~MAGIC) ) |
| 77 |
die("goddam: corrupted memory block %d in free()!\n", p2->index); |
| 78 |
p2->last->next = p2->next; |
| 79 |
p2->next->last = p2->last; |
| 80 |
++frees; |
| 81 |
free(p2); |
| 82 |
} |
| 83 |
else |
| 84 |
free(ptr); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
void * |
| 89 |
arealloc(void *ptr, int size) |
| 90 |
{ |
| 91 |
struct alist *p2 = ((struct alist*)ptr)-1; |
| 92 |
struct alist save; |
| 93 |
|
| 94 |
if ( p2->magic == MAGIC ) { |
| 95 |
if ( ! (p2->end && *(p2->end) == ~MAGIC) ) |
| 96 |
die("goddam: corrupted memory block %d in realloc()!\n", p2->index); |
| 97 |
save.next = p2->next; |
| 98 |
save.last = p2->last; |
| 99 |
p2 = realloc(p2, sizeof(int) + sizeof(*p2) + size); |
| 100 |
|
| 101 |
if ( p2 ) { |
| 102 |
p2->size = size; |
| 103 |
p2->end = (int*)(size + (char*) (p2 + 1)); |
| 104 |
*(p2->end) = ~MAGIC; |
| 105 |
p2->next->last = p2; |
| 106 |
p2->last->next = p2; |
| 107 |
++reallocs; |
| 108 |
return p2+1; |
| 109 |
} |
| 110 |
else { |
| 111 |
save.next->last = save.last; |
| 112 |
save.last->next = save.next; |
| 113 |
return 0; |
| 114 |
} |
| 115 |
} |
| 116 |
return realloc(ptr, size); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
void |
| 121 |
adump() |
| 122 |
{ |
| 123 |
struct alist *p; |
| 124 |
|
| 125 |
|
| 126 |
for ( p = list.next; p && (p != &list); p = p->next ) { |
| 127 |
fprintf(stderr, "allocated: %d byte%s\n", p->size, (p->size==1) ? "" : "s"); |
| 128 |
fprintf(stderr, " [%.*s]\n", p->size, (char*)(p+1)); |
| 129 |
} |
| 130 |
|
| 131 |
if ( getenv("AMALLOC_STATISTICS") ) { |
| 132 |
fprintf(stderr, "%d malloc%s\n", mallocs, (mallocs==1)?"":"s"); |
| 133 |
fprintf(stderr, "%d realloc%s\n", reallocs, (reallocs==1)?"":"s"); |
| 134 |
fprintf(stderr, "%d free%s\n", frees, (frees==1)?"":"s"); |
| 135 |
} |
| 136 |
} |