/*************************************** $Header: /home/amb/cxref/RCS/memory.c 1.12 1999/01/16 11:09:59 amb Exp $ C Cross Referencing & Documentation tool. Version 1.5. Memory management functions ******************/ /****************** Written by Andrew M. Bishop This file Copyright 1995,96,97 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 #include #include #ifdef USE_STD_ARG #include #else #include #endif #include #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;inext; 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); }