Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

Memory.c

Go to the documentation of this file.
00001 /*
00002  *    gui - [gega user interface] the flexible solution for user interface problems
00003  *    Copyright (C) 2002  Gergely Gati
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; version 2 of the License.
00008  *
00009  *    This program is distributed in the hope that it will be useful,
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *    GNU General Public License for more details.
00013  *
00014  *    You should have received a copy of the GNU General Public License
00015  *    along with this program; if not, write to the Free Software
00016  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *    Gergely Gati
00019  *      email:           g.gati@freemail.hu
00020  *      AIM screenname:  GatiGergely
00021  *      ICQ number:      93131690
00022  *
00023  */
00024 #include <stdlib.h>
00025 
00026 #include "debug.h"
00027 
00028 #define INMEM
00029 #include "Memory.h"
00030 #if defined (MEM_OVER)
00031 #include "types.h"
00032 #endif
00033 
00034 
00035 #define POSTALLOC     4*2
00036 #define HEADER        4*3
00037 #define PATTERN       (0xDEADBEEF)
00038 
00039 
00040 #ifdef MEM_OVER
00041   static unsigned long counter=0;
00042   static unsigned long prevcounter=0;
00043 #endif
00044 
00045 #ifndef MEM_MACRO
00046 
00047 void *mem_malloc(size_t size)
00048 {
00049   void *ret=NULL;
00050 
00051   ret=malloc(size);
00052 
00053   return(ret);
00054 }
00055 
00056 void *mem_calloc(size_t nmemb, size_t size)
00057 {
00058   void *ret=NULL;
00059 
00060   ret=calloc(nmemb,size);
00061 
00062   return(ret);
00063 }
00064 
00065 void *mem_realloc(void *ptr, size_t size)
00066 {
00067   void *ret=NULL;
00068 
00069   ret=realloc(ptr,size);
00070 
00071   return(ret);
00072 }
00073 
00074 void mem_free(void *ptr)
00075 {
00076   free(ptr);
00077 }
00078 
00079 #ifdef MEM_OVER
00080 
00081 void *mem_malloc_over(size_t size, char *file, char *func, int line)
00082 {
00083   u8 *pnt;
00084   void *ret;
00085 
00086   prevcounter=counter;
00087   pnt=malloc(size+HEADER+POSTALLOC);
00088   ret=pnt+HEADER;
00089   *((u32 *)pnt)=(u32)size;
00090   *((u32 *)(pnt+4))=(u32)counter;
00091   *((u32 *)(pnt+8))=(u32)PATTERN;
00092   pnt+=HEADER+size;
00093   *((u32 *)pnt)=PATTERN;
00094   *((u32 *)(pnt+4))=PATTERN;
00095 #ifdef MEM_VERBOSE
00096   printf("MEM: A <%08lx> %s: %s line %d: $%p=malloc(%d)\n",counter,file,func,line,ret,size);
00097 #endif
00098   counter++;
00099   if(counter<=prevcounter) printf("MEM: E INTERR counter small!!! %ld+1 ?= %ld\n",prevcounter,counter);
00100   return(ret);
00101 }
00102 
00103 void *mem_calloc_over(size_t nmemb, size_t size, char *file, char *func, int line)
00104 {
00105   u8 *pnt;
00106   void *ret;
00107 
00108   prevcounter=counter;
00109   pnt=calloc((size*nmemb)+HEADER+POSTALLOC,1);
00110   ret=pnt+HEADER;
00111   *((u32 *)pnt)=(u32)(size*nmemb);
00112   *((u32 *)(pnt+4))=(u32)counter;
00113   *((u32 *)(pnt+8))=(u32)PATTERN;
00114   pnt+=HEADER+(size*nmemb);
00115   *((u32 *)pnt)=PATTERN;
00116   *((u32 *)(pnt+4))=PATTERN;
00117 #ifdef MEM_VERBOSE
00118   printf("MEM: A <%08lx> %s: %s line %d: $%p=calloc(%d,%d)\n",counter,file,func,line,ret,nmemb,size);
00119 #endif
00120   counter++;
00121   if(counter<=prevcounter) printf("MEM: E INTERR counter small!!! %ld+1 ?= %ld\n",prevcounter,counter);
00122   return(ret);
00123 }
00124 
00125 void mem_free_over(void *ptr, char *file, char *func, int line)
00126 {
00127   u8 *pnt;
00128   int size,ok=0;
00129   u32 cnt;
00130 
00131   pnt=ptr;
00132   pnt-=HEADER;
00133   size=(int)(*((u32 *)pnt));
00134   cnt=(unsigned long)(*((u32 *)(pnt+4)));
00135   if(PATTERN!=(*((u32 *)(pnt+8))))
00136   {
00137     printf("MEM: E ERR %s: %s line %d: free($%p) size?=%d  cnt?=<%08lx>*** buffer underwritten!\n",file,func,line,ptr,size,cnt);
00138     ok=-1;
00139   }
00140   pnt+=HEADER+size;
00141   if(*((u32 *)pnt)!=PATTERN)
00142   {
00143     printf("MEM: E ERR %s: %s line %d: free($%p) orig_size=%d *** buffer overwritten!\n",file,func,line,ptr,size);
00144     ok=-1;
00145   }
00146 #ifdef MEM_VERBOSE
00147   if(ok==0) printf("MEM: F %s: %s line %d: free($%p) <%08lx> orig_size=%d\n",file,func,line,ptr,cnt,size);
00148 #endif
00149   pnt=ptr;
00150   pnt-=HEADER;
00151   free(pnt);
00152 }
00153 
00154 #endif /* MEM_OVER */
00155 
00156 
00157 void mem_trace_report(void)
00158 {
00159 #ifdef MEM_OVER
00160   printf("MEMORY REPORT\n");
00161   printf("  counter: %ld\n",counter);
00162   fflush(stdout);
00163 #endif /* MEM_OVER */
00164 }
00165 
00166 #endif /*MEM_MACRO*/

Generated on Tue Jan 7 12:11:23 2003 for THEGUI by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002