1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/***************************************
$Header: /home/amb/cxref/src/RCS/memory.c 1.13 2004/06/19 19:03:13 amb Exp $
C Cross Referencing & Documentation tool. Version 1.6.
Memory management functions
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1995,96,97,2004 Andrew M. Bishop
It may be distributed under the GNU Public License, version 2, or
any higher version. See section COPYING of the GNU Public license
for conditions under which this file may be redistributed.
***************************************/
/*+ The amount of debugging, non-zero for totals, 2 for logging, 4 for printing each call. +*/
#define DEBUG 0
/* The configure output */
#include "autoconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <memory.h>
#include "memory.h"
/*+ A private memory heap is used to reduce the number of malloc calls that are made, the Heap type is a pointer to this. +*/
typedef struct _Heap *Heap;
/*+ A structure containing all of the information about the private heap in a linked list. +*/
struct _Heap
{
char* mem; /*+ The memory that is private to the heap. +*/
Heap next; /*+ The next Heap structure. +*/
};
/*+ Local variable to control the usage of the private heap; +*/
static Heap first=NULL; /*+ the first segment of memory on the private heap. +*/
static int heap_left=0; /*+ the amount of space left in the current heap segment. +*/
static char* get_space(unsigned int l);
static Heap add_to_heap(unsigned int l);
#if DEBUG&2
/*+ Variable used for debugging, not a good thing to do. what if more than 16384 mallocs? +*/
static void* addresses[16384];
static char* files[16384];
static int lines[16384];
#endif
#if DEBUG
/*+ Variable used for debugging. +*/
static int malloc_count=0;
static int realloc_count=0;
static int free_count=0;
#endif
/*++++++++++++++++++++++++++++++++++++++
A replacement malloc() function.
void* SafeMalloc Returns the address.
unsigned int size The size of the memory to allocate.
char* file The file that the function is called from.
int line The line number that the function is called from.
++++++++++++++++++++++++++++++++++++++*/
void* SafeMalloc(unsigned int size,char* file,int line)
{
void* rptr=malloc(size);
#if DEBUG&4
printf("$$Malloc #%5d of %4d bytes at %08lx (%s:%3d)\n",malloc_count+1,size,(long)rptr,file,line);
#endif
#if DEBUG&2
if(malloc_count==(sizeof(addresses)/sizeof(addresses[0])))
{fprintf(stderr,"$$Too many Mallocs to log, edit memory.c\n");exit(3);}
addresses[malloc_count]=(void*)rptr;
files[malloc_count]=file;
lines[malloc_count]=line;
#endif
#if DEBUG
malloc_count++;
if(!rptr) printf("$$Warning Malloc() returning NULL (%s:%3d)\n",file,line);
#endif
#if !DEBUG
if(!rptr) printf("Warning Malloc() returning NULL (%s:%3d)\n",file,line);
#endif
return(rptr);
}
/*++++++++++++++++++++++++++++++++++++++
A replacement calloc() function.
void* SafeCalloc Returns the address.
unsigned int n The number of items to allocate.
unsigned int size The size of the memory to allocate.
char* file The file that the function is called from.
int line The line number that the function is called from.
++++++++++++++++++++++++++++++++++++++*/
void* SafeCalloc(unsigned int n,unsigned int size,char* file,int line)
{
void* rptr=calloc(n,size);
#if DEBUG&4
printf("$$Calloc #%5d of %4d bytes at %08lx (%s:%3d)\n",malloc_count+1,size,(long)rptr,file,line);
#endif
#if DEBUG&2
if(malloc_count==(sizeof(addresses)/sizeof(addresses[0])))
{fprintf(stderr,"$$Too many Mallocs to log, edit memory.c\n");exit(3);}
addresses[malloc_count]=(void*)rptr;
files[malloc_count]=file;
lines[malloc_count]=line;
#endif
#if DEBUG
malloc_count++;
if(!rptr) printf("$$Warning Calloc() returning NULL (%s:%3d)\n",file,line);
#endif
#if !DEBUG
if(!rptr) printf("Warning Calloc() returning NULL (%s:%3d)\n",file,line);
#endif
return(rptr);
}
/*++++++++++++++++++++++++++++++++++++++
A replacement realloc() function.
void* SafeRealloc Returns the address.
void* ptr The old pointer.
unsigned int size The size of the new memory to allocate.
char* file The file that the function is called from.
int line The line number that the function is called from.
++++++++++++++++++++++++++++++++++++++*/
void* SafeRealloc(void* ptr,unsigned int size,char* file,int line)
{
void* rptr=realloc(ptr,size);
#if DEBUG&4
printf("$$Realloc #%4d of %4d bytes at %08lx (old %08lx) (%s:%3d)\n",realloc_count+1,size,(long)rptr,(long)ptr,file,line);
#endif
#if DEBUG&2
{
int i;
for(i=0;i<malloc_count;i++)
if(addresses[i]==(void*)ptr)
{addresses[i]=rptr;break;}
if(i==malloc_count)
printf("$$Realloc() called for a non Malloced pointer %08lx (%s:%3d)\n",(long)ptr,file,line);
}
#endif
#if DEBUG
realloc_count++;
if(!rptr) printf("$$Warning Realloc() returning NULL (%s:%3d)\n",file,line);
#endif
#if !DEBUG
if(!rptr) printf("Warning Realloc() returning NULL (%s:%3d)\n",file,line);
#endif
return(rptr);
}
/*++++++++++++++++++++++++++++++++++++++
A replacement free() function.
void* ptr The pointer that is to be freed up.
char* file The file that the function is called from.
int line The line number that the function is called from.
++++++++++++++++++++++++++++++++++++++*/
void SafeFree(void* ptr,char* file,int line)
{
#if DEBUG&4
printf("$$Free #%5d at %08lx (%s:%3d)\n",free_count+1,(long)ptr,file,line);
#endif
#if DEBUG&2
{
int i;
for(i=0;i<malloc_count;i++)
if(addresses[i]==(void*)ptr)
{addresses[i]=(void*)1;break;}
if(i==malloc_count)
printf("$$Free() called for a non Malloced pointer %08lx (%s:%3d)\n",(long)ptr,file,line);
}
#endif
#if DEBUG
free_count++;
if(!ptr) printf("$$Calling Free() on NULL (%s:%3d)\n",file,line);
else
#endif
#if !DEBUG
if(!ptr) printf("Calling Free() on NULL (%s:%3d)\n",file,line);
else
#endif
free(ptr);
}
/*++++++++++++++++++++++++++++++++++++++
A function to copy a string on the public global heap.
char* SafeMallocString Returns the copy of the string.
char* x The string to be copied.
char* file The file that the function is called from.
int line The line number that the function is called from.
++++++++++++++++++++++++++++++++++++++*/
char* SafeMallocString(char* x,char* file,int line)
{
char* t=NULL;
if(x)
{
t=(char*)SafeMalloc(strlen(x)+1,file,line);
strcpy(t,x);
}
return(t);
}
/*++++++++++++++++++++++++++++++++++++++
A function to copy a string on the local private memory heap.
char* CopyString Returns the copy of the string.
char* x The string to be copied.
++++++++++++++++++++++++++++++++++++++*/
char* CopyString(char* x)
{
char* t=NULL;
if(x)
{
t=get_space(strlen(x)+1);
strcpy(t,x);
}
return(t);
}
/*++++++++++++++++++++++++++++++++++++++
A function to concatenate a number of strings.
char* ConcatStrings Returns the a pointer to the new string.
int n The number of strings
char* s The first string.
... The other strings, 'n' including 's'.
Any of the strings that are inputs can be NULL, in this case they are quietly ignored.
++++++++++++++++++++++++++++++++++++++*/
char* ConcatStrings(int n,char* s, ...)
{
char* t=NULL,*str;
unsigned int l=0;
int i;
va_list ap;
#ifdef __STDC__
va_start(ap,s);
#else
va_start(ap);
#endif
for(i=0;i<n;i++)
{
if(i)
str=va_arg(ap, char *);
else
str=s;
if(str)
l+=strlen(str);
}
va_end(ap);
if(l)
{
t=get_space(l+1); t[0]=0;
#ifdef __STDC__
va_start(ap,s);
#else
va_start(ap);
#endif
for(i=0;i<n;i++)
{
if(i)
str=va_arg(ap, char *);
else
str=s;
if(str)
strcat(t,str);
}
va_end(ap);
}
return(t);
}
/*++++++++++++++++++++++++++++++++++++++
Prints out the number of mallocs / reallocs and frees.
++++++++++++++++++++++++++++++++++++++*/
void PrintMemoryStatistics(void)
{
#if DEBUG
printf("\n"
"$$Memory usage : %5d Malloc()/Calloc() calls\n"
"$$ %5d Realloc() calls\n"
"$$ %5d Free() calls\n"
"$$ %5d Net calls (Malloc-Free)\n",
malloc_count,realloc_count,free_count,malloc_count-free_count);
#endif
#if DEBUG&2
{
int i;
for(i=0;i<malloc_count;i++)
if(addresses[i]!=(void*)1)
printf("$$Malloc #%5d at address %08lx is not freed (%s:%3d) = '%s'\n",i+1,(long)addresses[i],files[i],lines[i],(char*)addresses[i]);
}
#endif
}
/*++++++++++++++++++++++++++++++++++++++
Tidies up the local heap of memory.
++++++++++++++++++++++++++++++++++++++*/
void TidyMemory(void)
{
if(first)
{
Heap h=first,n;
do
{
n=h->next;
Free(h->mem);
Free(h);
h=n;
}
while(h);
}
first=NULL;
heap_left=0;
}
/*+ The size of each of the heap allocations +*/
#define HEAP_INC 8192
/*+ The size of a string that is large enough to have it's own mallocation. +*/
#define SMALL_STRING 256
/*++++++++++++++++++++++++++++++++++++++
A function to get some memory for a string, allocate a new heap structure if needed.
char* get_space Returns a pointer to enough space.
unsigned int l The amount of space that is needed.
++++++++++++++++++++++++++++++++++++++*/
static char* get_space(unsigned int l)
{
static Heap current=NULL;
char* r=NULL;
if(l <= SMALL_STRING)
{
if(heap_left < l)
{
current=add_to_heap(HEAP_INC);
heap_left=HEAP_INC;
}
heap_left-=l;
r=¤t->mem[heap_left]; /* Work downwards */
}
else
{
Heap h=add_to_heap(l);
r=h->mem;
}
return(r);
}
/*++++++++++++++++++++++++++++++++++++++
Add some bytes to the privately maintained memory heap.
Heap add_to_heap Returns a pointer to the required memory.
unsigned int l The size of the memory that is required.
++++++++++++++++++++++++++++++++++++++*/
static Heap add_to_heap(unsigned int l)
{
Heap* h=&first;
while(*h)
h=&(*h)->next;
*h=(Heap)Malloc(sizeof(struct _Heap));
(*h)->next=NULL;
(*h)->mem=(char*)Malloc(l);
return(*h);
}
|