/* viDOOM - level editor for DOOM Copyright (C) 2000 Ian Cowburn (ianc@noddybox.demon.co.uk) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ------------------------------------------------------------------------- Memory allocation code */ static const char rcs_id[]="$Id$"; #include "config.h" #include #include #include "mem.h" #include "gfx.h" #include "debug.h" /* Comment out this define to switch off memory stats in the working version */ #define MEMSTAT #ifdef MEMSTAT typedef struct { Long size; Long realloc; Long free; char file[32]; Long line; } MemInfo; static Long grab_call=0; static Long regrab_call=0; static Long release_call=0; static Long copy_call=0; static Long strdup_call=0; static Long total=0; static Long total_grab=0; static Long total_regrab=0; static Long total_copy=0; static Long total_free=0; static Long total_strdup=0; static void status(void) { Debug(("Total calls to Grab() : %lu\n",grab_call)); Debug(("Total calls to Regrab() : %lu\n",regrab_call)); Debug(("Total calls to Release() : %lu\n",release_call)); Debug(("Total calls to Copy() : %lu\n",copy_call)); Debug(("Total calls to Strdup() : %lu\n",strdup_call)); Debug(("Total bytes allocated : %lu\n",total)); Debug(("Total bytes allocated over life : %lu\n",total_grab)); Debug(("Total bytes regrabbed over life : %lu\n",total_regrab)); Debug(("Total bytes copied over life : %lu\n",total_copy)); Debug(("Total bytes released over life : %lu\n",total_free)); Debug(("Total bytes strdupped over life : %lu\n\n",total_strdup)); } #endif #ifdef MEMSTAT void *FGrab(char *fn, int line, int len) { char *ptr; MemInfo *mi; if (len==0) len=1; if (!(ptr=malloc(len+sizeof(MemInfo)))) { status(); GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n" "%s:%d Grab(%d)\n",fn,line,len); } mi=(MemInfo *)ptr; mi->size=len; mi->realloc=0; mi->free=0; ptr+=sizeof(MemInfo); grab_call++; total_grab+=len; total+=len; memset(ptr,0,len); return(ptr); } #else void *FGrab(char *fn, int line, int len) { void *ptr; if (len==0) len=1; if (!(ptr=malloc(len))) GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n" "%s:%d Grab(%d)\n",fn,line,len); memset(ptr,0,len); return(ptr); } #endif #ifdef MEMSTAT void *FReGrab(char *fn, int line, void *ptr, int len) { Long old; MemInfo *mi; char *p; if (len==0) len=1; p=ptr; if (p) p-=sizeof(MemInfo); if (!(p=realloc(p,len+sizeof(MemInfo)))) { status(); GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n" "%s:%d ReGrab(%d)\n",fn,line,len); } regrab_call++; total_regrab+=len; mi=(MemInfo *)p; old=mi->size; mi->size=len; mi->realloc++; total-=old; total+=len; p+=sizeof(MemInfo); return(p); } #else void *FReGrab(char *fn, int line, void *ptr, int len) { if (len==0) len=1; if (!(ptr=realloc(ptr,len))) GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n" "%s:%d ReGrab(%d)\n",fn,line,len); return(ptr); } #endif #ifdef MEMSTAT void FRelease(char *fn, int line, void *p) { char *cp; MemInfo *mi; if (!p) return; cp=p; cp-=sizeof(MemInfo); mi=(MemInfo *)cp; if (mi->free==1) Debug(("%p already freed!!! Current=%s:%d, previous=%s:%d\n", p,fn,line,mi->file,mi->line)); release_call++; total_free+=mi->size; total-=mi->size; strcpy(mi->file,fn); mi->line=line; mi->free=1; free(cp); } #else void FRelease(char *fn, int line, void *p) { free(p); } #endif #ifdef MEMSTAT void *FCopy(char *fn, int line, void *p,int len) { char s[128]; void *ptr; strcpy(s,fn); strcat(s," [FCopy()]"); if (len==0) ptr=FGrab(s,line,1); else ptr=FGrab(s,line,len); copy_call++; total_copy+=len; if (len) memcpy(ptr,p,len); return(ptr); } #else void *FCopy(char *fn, int line, void *p,int len) { void *ptr; if (len==0) ptr=malloc(1); else ptr=malloc(len); if (!ptr) GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n" "%s:%d Copy(%p,%d)\n",fn,line,p,len); if (len) memcpy(ptr,p,len); return(ptr); } #endif #ifdef MEMSTAT char *FStrdup(char *fn, int line, char *p) { char s[128]; char *ptr; strcpy(s,fn); strcat(s," [Strdup()]"); if (p) { ptr=FGrab(s,line,strlen(p)+1); strdup_call++; total_strdup+=strlen(p)+1; strcpy(ptr,p); return(ptr); } else return(NULL); } #else char *FStrdup(char *fn, int line, char *p) { char *ptr; if (p) { if (!(ptr=malloc(strlen(p)+1))) GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n" "%s:%d Stdup(%s)\n",fn,line,p); strcpy(ptr,p); return(ptr); } else return(NULL); } #endif /* END OF FILE */