File indexing completed on 2024-05-05 11:56:27
0001 /* 0002 * debugging malloc()/realloc()/calloc()/free() that attempts 0003 * to keep track of just what's been allocated today. 0004 */ 0005 0006 #include <stdio.h> 0007 #include <stdlib.h> 0008 #include "config.h" 0009 0010 #define MAGIC 0x1f2e3d4c 0011 0012 struct alist { int magic, size, index; int *end; struct alist *next, *last; }; 0013 0014 static struct alist list = { 0, 0, 0, 0 }; 0015 0016 static int mallocs=0; 0017 static int reallocs=0; 0018 static int frees=0; 0019 0020 static int index = 0; 0021 0022 static void 0023 die(char *msg, int index) 0024 { 0025 fprintf(stderr, msg, index); 0026 abort(); 0027 } 0028 0029 0030 void * 0031 acalloc(int count, int size) 0032 { 0033 struct alist *ret; 0034 0035 if ( size > 1 ) { 0036 count *= size; 0037 size = 1; 0038 } 0039 0040 if ( ret = calloc(count + sizeof(struct alist) + sizeof(int), size) ) { 0041 ret->magic = MAGIC; 0042 ret->size = size * count; 0043 ret->index = index ++; 0044 ret->end = (int*)(count + (char*) (ret + 1)); 0045 *(ret->end) = ~MAGIC; 0046 if ( list.next ) { 0047 ret->next = list.next; 0048 ret->last = &list; 0049 ret->next->last = ret; 0050 list.next = ret; 0051 } 0052 else { 0053 ret->last = ret->next = &list; 0054 list.next = list.last = ret; 0055 } 0056 ++mallocs; 0057 return ret+1; 0058 } 0059 return 0; 0060 } 0061 0062 0063 void* 0064 amalloc(int size) 0065 { 0066 return acalloc(size,1); 0067 } 0068 0069 0070 void 0071 afree(void *ptr) 0072 { 0073 struct alist *p2 = ((struct alist*)ptr)-1; 0074 0075 if ( p2->magic == MAGIC ) { 0076 if ( ! (p2->end && *(p2->end) == ~MAGIC) ) 0077 die("goddam: corrupted memory block %d in free()!\n", p2->index); 0078 p2->last->next = p2->next; 0079 p2->next->last = p2->last; 0080 ++frees; 0081 free(p2); 0082 } 0083 else 0084 free(ptr); 0085 } 0086 0087 0088 void * 0089 arealloc(void *ptr, int size) 0090 { 0091 struct alist *p2 = ((struct alist*)ptr)-1; 0092 struct alist save; 0093 0094 if ( p2->magic == MAGIC ) { 0095 if ( ! (p2->end && *(p2->end) == ~MAGIC) ) 0096 die("goddam: corrupted memory block %d in realloc()!\n", p2->index); 0097 save.next = p2->next; 0098 save.last = p2->last; 0099 p2 = realloc(p2, sizeof(int) + sizeof(*p2) + size); 0100 0101 if ( p2 ) { 0102 p2->size = size; 0103 p2->end = (int*)(size + (char*) (p2 + 1)); 0104 *(p2->end) = ~MAGIC; 0105 p2->next->last = p2; 0106 p2->last->next = p2; 0107 ++reallocs; 0108 return p2+1; 0109 } 0110 else { 0111 save.next->last = save.last; 0112 save.last->next = save.next; 0113 return 0; 0114 } 0115 } 0116 return realloc(ptr, size); 0117 } 0118 0119 0120 void 0121 adump() 0122 { 0123 struct alist *p; 0124 0125 0126 for ( p = list.next; p && (p != &list); p = p->next ) { 0127 fprintf(stderr, "allocated: %d byte%s\n", p->size, (p->size==1) ? "" : "s"); 0128 fprintf(stderr, " [%.*s]\n", p->size, (char*)(p+1)); 0129 } 0130 0131 if ( getenv("AMALLOC_STATISTICS") ) { 0132 fprintf(stderr, "%d malloc%s\n", mallocs, (mallocs==1)?"":"s"); 0133 fprintf(stderr, "%d realloc%s\n", reallocs, (reallocs==1)?"":"s"); 0134 fprintf(stderr, "%d free%s\n", frees, (frees==1)?"":"s"); 0135 } 0136 }