| 1 |
/* file : memory.c |
| 2 |
* C code for memory debugging, to be used in lieu |
| 3 |
* of standard memory functions |
| 4 |
*/ |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
#include <assert.h> |
| 9 |
|
| 10 |
#include "memory.h" |
| 11 |
|
| 12 |
|
| 13 |
typedef struct memstack { |
| 14 |
size_t size; |
| 15 |
void *ptr; |
| 16 |
int nxt; |
| 17 |
char call[30]; |
| 18 |
} Memstack; |
| 19 |
|
| 20 |
typedef Memstack * pMemstack; |
| 21 |
|
| 22 |
const int MAXMEM = 300; |
| 23 |
pMemstack mstack; |
| 24 |
int stack,cur; |
| 25 |
|
| 26 |
|
| 27 |
int M_memLeak() { |
| 28 |
int i,c=0; |
| 29 |
|
| 30 |
for (i=1; i<=MAXMEM; i++) |
| 31 |
if (mstack[i].ptr) c++; |
| 32 |
return(c); |
| 33 |
} |
| 34 |
|
| 35 |
/* print out allocated pointers */ |
| 36 |
void M_memDump() { |
| 37 |
size_t size; |
| 38 |
int i,c; |
| 39 |
static long mega = 1024 * 1024; |
| 40 |
static long kilo = 1024; |
| 41 |
|
| 42 |
fprintf(stdout,"\n -- MEMORY USAGE\n"); |
| 43 |
fprintf(stdout," Allocated pointers\n"); |
| 44 |
size = 0; |
| 45 |
c = 0; |
| 46 |
for (i=1; i<=MAXMEM; i++) |
| 47 |
if ( mstack[i].ptr ) { |
| 48 |
fprintf(stdout," %3d %3d Pointer %10p size ",++c,i,mstack[i].ptr); |
| 49 |
if (mstack[i].size > mega) |
| 50 |
fprintf(stdout," %10d Mbytes ",(int)(mstack[i].size/mega)); |
| 51 |
else if (mstack[i].size > kilo) |
| 52 |
fprintf(stdout," %10d Kbytes ",(int)(mstack[i].size/kilo)); |
| 53 |
else |
| 54 |
fprintf(stdout," %10d bytes ",(int)(mstack[i].size)); |
| 55 |
fprintf(stdout,"(%s)\n",mstack[i].call); |
| 56 |
size += mstack[i].size; |
| 57 |
} |
| 58 |
fprintf(stdout," Memory leaks "); |
| 59 |
if ( size > mega ) |
| 60 |
fprintf(stdout," %10d Mbytes %d pointers\n",(int)(size/mega),c); |
| 61 |
else if ( size > kilo ) |
| 62 |
fprintf(stdout," %10d Kbytes %d pointers\n",(int)(size/kilo),c); |
| 63 |
else if ( size ) |
| 64 |
fprintf(stdout," %10d bytes %d pointers\n",(int)size,c); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
/* Returns allocated memory space in bytes */ |
| 69 |
size_t M_memSize() { |
| 70 |
size_t size; |
| 71 |
int i; |
| 72 |
|
| 73 |
size = 0; |
| 74 |
for (i=1; i<=MAXMEM; i++) |
| 75 |
if ( mstack[i].ptr ) |
| 76 |
size += mstack[i].size; |
| 77 |
return size; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/* Allocates space for a block of at least size bytes, |
| 82 |
but does not initialize the space. */ |
| 83 |
void *M_malloc(size_t size,char *call) { |
| 84 |
int i; |
| 85 |
|
| 86 |
/* check if first call */ |
| 87 |
if ( !mstack ) { |
| 88 |
mstack = (Memstack *)calloc((1+MAXMEM),sizeof(Memstack)); |
| 89 |
assert(mstack); |
| 90 |
for (i=1; i<MAXMEM; i++) |
| 91 |
mstack[i].nxt = i+1; |
| 92 |
cur = 1; |
| 93 |
stack = 0; |
| 94 |
} |
| 95 |
|
| 96 |
/* store pointer, size */ |
| 97 |
if ( stack < MAXMEM ) { |
| 98 |
mstack[cur].ptr = malloc(size); |
| 99 |
assert(mstack[cur].ptr); |
| 100 |
mstack[cur].size = size; |
| 101 |
/* i.e. mstack[cur].call = strdup(call) */ |
| 102 |
/* mstack[cur].call = (char*)malloc((strlen(call)+1) * sizeof(char)); |
| 103 |
assert(mstack[cur].call); */ |
| 104 |
strncpy(mstack[cur].call,call,19); |
| 105 |
i = cur; |
| 106 |
cur = mstack[cur].nxt; |
| 107 |
++stack; |
| 108 |
#ifdef MEMDEBUG |
| 109 |
fprintf(stdout,"M_malloc: allocate %p of size %10d (%g,%d)\n", |
| 110 |
mstack[cur].ptr,size,stack,cur); |
| 111 |
#endif |
| 112 |
return(mstack[i].ptr); |
| 113 |
} |
| 114 |
else { |
| 115 |
fprintf(stderr,"M_malloc: unable to store %10Zd bytes pointer. table full\n", |
| 116 |
size); |
| 117 |
return(0); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/* Allocates space for an array of nelem elements, each of size |
| 123 |
elsize bytes, and initializes the space to zeros. |
| 124 |
Actual amount of space allocated is >= nelem * elsize bytes. */ |
| 125 |
void *M_calloc(size_t nelem, size_t elsize,char *call) { |
| 126 |
int i; |
| 127 |
|
| 128 |
/* check if first call */ |
| 129 |
if ( !mstack ) { |
| 130 |
mstack = (Memstack *)calloc((1+MAXMEM),sizeof(Memstack)); |
| 131 |
assert(mstack); |
| 132 |
for (i=1; i<MAXMEM; i++) |
| 133 |
mstack[i].nxt = i+1; |
| 134 |
cur = 1; |
| 135 |
stack = 0; |
| 136 |
} |
| 137 |
|
| 138 |
/* store pointer, size */ |
| 139 |
if ( stack < MAXMEM ) { |
| 140 |
mstack[cur].ptr = calloc(nelem,elsize); |
| 141 |
if ( !mstack[cur].ptr ) return(0); |
| 142 |
|
| 143 |
/*assert(mstack[cur].ptr);*/ |
| 144 |
mstack[cur].size = nelem * elsize; |
| 145 |
/* mstack[cur].call = (char*)malloc((strlen(call)+1) * sizeof(char)); |
| 146 |
assert(mstack[cur].call); */ |
| 147 |
strncpy(mstack[cur].call,call,19); |
| 148 |
i = cur; |
| 149 |
cur = mstack[cur].nxt; |
| 150 |
++stack; |
| 151 |
#ifdef MEMDEBUG |
| 152 |
fprintf(stdout,"M_calloc: allocate %p of size %d (%d,%d)\n", |
| 153 |
mstack[cur].ptr,nelem*elsize,stack,cur); |
| 154 |
#endif |
| 155 |
return(mstack[i].ptr); |
| 156 |
} |
| 157 |
else { |
| 158 |
fprintf(stderr,"M_calloc: unable to allocate %10Zd bytes. table full\n", |
| 159 |
nelem*elsize); |
| 160 |
return(0); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/* Changes the size of the block pointed to by ptr to size bytes |
| 165 |
and returns a pointer to the (possibly moved) block. Existing |
| 166 |
contents are unchanged up to the lesser of the new and old sizes. */ |
| 167 |
void *M_realloc(void *ptr, size_t size,char *call) { |
| 168 |
int i; |
| 169 |
|
| 170 |
if ( !ptr ) |
| 171 |
return 0; |
| 172 |
|
| 173 |
for (i=1; i<=MAXMEM; i++) { |
| 174 |
if (ptr == mstack[i].ptr) { |
| 175 |
/* free(mstack[i].call); |
| 176 |
mstack[cur].call = (char*)malloc((strlen(call)+1) * sizeof(char)); |
| 177 |
assert(mstack[cur].call); */ |
| 178 |
strncpy(mstack[i].call,call,19); |
| 179 |
mstack[i].ptr = realloc(mstack[i].ptr,size); |
| 180 |
if (size) |
| 181 |
assert(mstack[i].ptr); |
| 182 |
mstack[i].size = size; |
| 183 |
#ifdef MEMDEBUG |
| 184 |
fprintf(stdout,"M_realloc: reallocate %p of size %d (%d)\n", |
| 185 |
mstack[i].ptr,mstack[i].size,size); |
| 186 |
#endif |
| 187 |
return(mstack[i].ptr); |
| 188 |
} |
| 189 |
} |
| 190 |
#ifdef MEMDEBUG |
| 191 |
fprintf(stderr,"M_realloc: pointer %p not found\n",ptr); |
| 192 |
#endif |
| 193 |
return(0); |
| 194 |
} |
| 195 |
|
| 196 |
/* Deallocates the space pointed to by ptr (a pointer to a block |
| 197 |
previously allocated by malloc() and makes the space available |
| 198 |
for further allocation. If ptr is NULL, no action occurs. */ |
| 199 |
void M_free(void *ptr) { |
| 200 |
int i; |
| 201 |
|
| 202 |
assert(ptr); |
| 203 |
for (i=1; i<=MAXMEM; i++) { |
| 204 |
if (mstack[i].ptr && ptr == mstack[i].ptr) { |
| 205 |
--stack; |
| 206 |
free(mstack[i].ptr); |
| 207 |
mstack[i].ptr = 0; |
| 208 |
mstack[i].size = 0; |
| 209 |
mstack[i].nxt = cur; |
| 210 |
mstack[i].call[0] = '\0'; |
| 211 |
cur = i; |
| 212 |
#ifdef MEMDEBUG |
| 213 |
fprintf(stdout,"M_free: deallocate %p of size %d (%d,%d)\n", |
| 214 |
ptr,mstack[i].size,stack,cur); |
| 215 |
#endif |
| 216 |
return; |
| 217 |
} |
| 218 |
} |
| 219 |
#ifdef MEMDEBUG |
| 220 |
fprintf(stderr,"M_free: pointer %p not found\n",ptr); |
| 221 |
#endif |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
/* dump memory requirements */ |
| 226 |
void primem(int np) { |
| 227 |
int memsize; |
| 228 |
|
| 229 |
memsize = M_memSize(); |
| 230 |
if ( memsize ) { |
| 231 |
fprintf(stdout,"\n -- MEMORY REQUIREMENTS\n"); |
| 232 |
if (memsize > 1024*1024) |
| 233 |
fprintf(stdout," Total size : %10Zd Mbytes", |
| 234 |
(long int)(memsize/(1024.*1024.))); |
| 235 |
else if (memsize > 1024) |
| 236 |
fprintf(stdout," Total size : %10Zd Kbytes",(long int)(memsize/1024.)); |
| 237 |
else |
| 238 |
fprintf(stdout," Total size : %10Zd bytes ",(long int)memsize); |
| 239 |
fprintf(stdout," (i.e. %d bytes/point)\n",memsize / np); |
| 240 |
} |
| 241 |
} |