From 0d63ef203ac8cac748f50721a598cc5ca1e5572e Mon Sep 17 00:00:00 2001 From: qeed Date: Sun, 19 Sep 2010 00:00:38 +0000 Subject: [PATCH] added wrapper around places that uses malloc to use FCEU_dmalloc, for debugging purpose if the need comes up --- src/cheat.cpp | 16 +++-- src/conddebug.cpp | 22 ++++-- src/config.cpp | 7 +- src/debug.cpp | 2 + src/drivers/common/vidblit.cpp | 22 +++--- src/drivers/sdl/sdl-opengl.cpp | 7 +- src/drivers/sdl/sdl-sound.cpp | 6 +- src/drivers/sdl/sdl-video.cpp | 4 +- src/drivers/sdl/unix-netplay.cpp | 11 +-- src/fceu.cpp | 2 +- src/ines.cpp | 6 +- src/lua-engine.cpp | 4 +- src/netplay.cpp | 11 +-- src/utils/memory.cpp | 112 +++++++++++++++++-------------- src/utils/memory.h | 5 ++ src/video.cpp | 6 +- 16 files changed, 144 insertions(+), 99 deletions(-) diff --git a/src/cheat.cpp b/src/cheat.cpp index b0cdb1d2..5a3520e1 100644 --- a/src/cheat.cpp +++ b/src/cheat.cpp @@ -30,8 +30,8 @@ #include "fceu.h" #include "file.h" #include "cart.h" -#include "memory.h" #include "driver.h" +#include "utils/memory.h" using namespace std; @@ -163,7 +163,7 @@ static void CheatMemErr(void) static int AddCheatEntry(char *name, uint32 addr, uint8 val, int compare, int status, int type) { struct CHEATF *temp; - if(!(temp=(struct CHEATF *)malloc(sizeof(struct CHEATF)))) + if(!(temp=(struct CHEATF *)FCEU_dmalloc(sizeof(struct CHEATF)))) { CheatMemErr(); return(0); @@ -248,7 +248,8 @@ void FCEU_LoadGameCheats(FILE *override) char *neo=&tbuf[4+2+2+1+1+1]; if(sscanf(tbuf,"%04x%*[:]%02x%*[:]%02x",&addr,&val,&compare)!=3) continue; - namebuf=(char *)malloc(strlen(neo)+1); + if (!(namebuf=(char *)FCEU_dmalloc(strlen(neo)+1))) + return; strcpy(namebuf,neo); } else @@ -256,7 +257,8 @@ void FCEU_LoadGameCheats(FILE *override) char *neo=&tbuf[4+2+1+1]; if(sscanf(tbuf,"%04x%*[:]%02x",&addr,&val)!=2) continue; - namebuf=(char *)malloc(strlen(neo)+1); + if (!(namebuf=(char *)FCEU_dmalloc(strlen(neo)+1))) + return; strcpy(namebuf,neo); } @@ -364,7 +366,7 @@ int FCEUI_AddCheat(const char *name, uint32 addr, uint8 val, int compare, int ty { char *t; - if(!(t=(char *)malloc(strlen(name)+1))) + if(!(t=(char *)FCEU_dmalloc(strlen(name)+1))) { CheatMemErr(); return(0); @@ -660,7 +662,7 @@ static int InitCheatComp(void) { uint32 x; - CheatComp=(uint16*)malloc(65536*sizeof(uint16)); + CheatComp=(uint16*)FCEU_dmalloc(65536*sizeof(uint16)); if(!CheatComp) { CheatMemErr(); @@ -949,4 +951,4 @@ void UpdateFrozenList(void) //FCEU_printf("Address %d: %d \n",x,FrozenAddresses[x]); //Debug } //FCEUI_DispMessage("FrozenCount: %d",0,FrozenAddressCount);//Debug -} \ No newline at end of file +} diff --git a/src/conddebug.cpp b/src/conddebug.cpp index c93b4c0c..2acea4eb 100644 --- a/src/conddebug.cpp +++ b/src/conddebug.cpp @@ -46,6 +46,8 @@ #include #include "conddebug.h" +#include "types.h" +#include "utils/memory.h" // Next non-whitespace character in string char next; @@ -93,7 +95,9 @@ Condition* InfixOperator(const char** str, Condition(*nextPart(const char**)), i return 0; } - mid = (Condition*)malloc(sizeof(Condition)); + mid = (Condition*)FCEU_dmalloc(sizeof(Condition)); + if (!mid) + return NULL; memset(mid, 0, sizeof(Condition)); mid->lhs = t; @@ -317,9 +321,13 @@ Condition* Primitive(const char** str, Condition* c) /* Handle * and / operators */ Condition* Term(const char** str) { - Condition* t = (Condition*)malloc(sizeof(Condition)); - Condition* t1; + Condition* t; + Condition* t1; Condition* mid; + + t = (Condition*)FCEU_dmalloc(sizeof(Condition)); + if (!t) + return NULL; memset(t, 0, sizeof(Condition)); @@ -335,7 +343,9 @@ Condition* Term(const char** str) scan(str); - t1 = (Condition*)malloc(sizeof(Condition)); + if (!(t1 = (Condition*)FCEU_dmalloc(sizeof(Condition)))) + return NULL; + memset(t1, 0, sizeof(Condition)); if (!Primitive(str, t1)) @@ -345,7 +355,9 @@ Condition* Term(const char** str) return 0; } - mid = (Condition*)malloc(sizeof(Condition)); + if (!(mid = (Condition*)FCEU_dmalloc(sizeof(Condition)))) + return NULL; + memset(mid, 0, sizeof(Condition)); mid->lhs = t; diff --git a/src/config.cpp b/src/config.cpp index 233cb9d4..ebf2d004 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -9,6 +9,7 @@ #include "version.h" #include "fceu.h" #include "driver.h" +#include "utils/memory.h" static char *aboutString = 0; @@ -43,7 +44,9 @@ FCEU TAS+ - Luke Gustafson\n\ const char *compilerString = FCEUD_GetCompilerString(); //allocate the string and concatenate the template with the compiler string - aboutString = (char*)malloc(strlen(aboutTemplate) + strlen(compilerString) + 1); - sprintf(aboutString,"%s%s",aboutTemplate,compilerString); + if (!(aboutString = (char*)FCEU_dmalloc(strlen(aboutTemplate) + strlen(compilerString) + 1))) + return NULL; + + sprintf(aboutString,"%s%s",aboutTemplate,compilerString); return aboutString; } diff --git a/src/debug.cpp b/src/debug.cpp index 6c46fb8a..51b9e46b 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -135,6 +135,8 @@ int checkCondition(const char* condition, int num) { watchpoint[num].cond = c; watchpoint[num].condText = (char*)malloc(strlen(condition) + 1); + if (!watchpoint[num].condText) + return 0; strcpy(watchpoint[num].condText, condition); } else diff --git a/src/drivers/common/vidblit.cpp b/src/drivers/common/vidblit.cpp index 07f655e2..b1b5ffa7 100644 --- a/src/drivers/common/vidblit.cpp +++ b/src/drivers/common/vidblit.cpp @@ -24,7 +24,7 @@ #include "hq3x.h" #include "../../types.h" - +#include "../../utils/memory.h" #include "nes_ntsc.h" nes_ntsc_t* nes_ntsc; @@ -108,12 +108,12 @@ int InitBlitToHigh(int b, uint32 rmask, uint32 gmask, uint32 bmask, int efx, int } - nes_ntsc = (nes_ntsc_t*) malloc( sizeof (nes_ntsc_t) ); + nes_ntsc = (nes_ntsc_t*) FCEU_dmalloc( sizeof (nes_ntsc_t) ); if ( nes_ntsc ) { nes_ntsc_init( nes_ntsc, &ntsc_setup, b, 2 ); - ntscblit = (uint8*)malloc(256*257*b*multi); //Need to add multiplier for larger sizes + ntscblit = (uint8*)FCEU_dmalloc(256*257*b*multi); //Need to add multiplier for larger sizes } } // -Video Modes Tag- @@ -121,7 +121,7 @@ int InitBlitToHigh(int b, uint32 rmask, uint32 gmask, uint32 bmask, int efx, int { int multi = ((specfilt == 2) ? 2 * 2 : 3 * 3); - specbuf8bpp = (uint8*)malloc(256*240*multi); //mbg merge 7/17/06 added cast + specbuf8bpp = (uint8*)FCEU_dmalloc(256*240*multi); //mbg merge 7/17/06 added cast } // -Video Modes Tag- else if(specfilt == 1 || specfilt == 4) // hq2x and hq3x @@ -157,8 +157,8 @@ int InitBlitToHigh(int b, uint32 rmask, uint32 gmask, uint32 bmask, int efx, int // End iffy code } // -Video Modes Tag- - if(specfilt == 1) specbuf32bpp = (uint32*)malloc(256*240*4*sizeof(uint32)); //mbg merge 7/17/06 added cast - else if(specfilt == 4) specbuf32bpp = (uint32*)malloc(256*240*9*sizeof(uint32)); //mbg merge 7/17/06 added cast + if(specfilt == 1) specbuf32bpp = (uint32*)FCEU_dmalloc(256*240*4*sizeof(uint32)); //mbg merge 7/17/06 added cast + else if(specfilt == 4) specbuf32bpp = (uint32*)FCEU_dmalloc(256*240*9*sizeof(uint32)); //mbg merge 7/17/06 added cast } efx=0; @@ -173,7 +173,7 @@ int InitBlitToHigh(int b, uint32 rmask, uint32 gmask, uint32 bmask, int efx, int else hq2x_InitLUTs(); - specbuf=(uint16*)malloc(256*240*sizeof(uint16)); //mbg merge 7/17/06 added cast + specbuf=(uint16*)FCEU_dmalloc(256*240*sizeof(uint16)); //mbg merge 7/17/06 added cast } silt = specfilt; @@ -188,16 +188,16 @@ int InitBlitToHigh(int b, uint32 rmask, uint32 gmask, uint32 bmask, int efx, int if(efx&FVB_BLUR) { if(Bpp==2) - palettetranslate=(uint32 *)malloc(65536*4); + palettetranslate=(uint32 *)FCEU_dmalloc(65536*4); else if(Bpp>=3) - palettetranslate=(uint32 *)malloc(65536*4); + palettetranslate=(uint32 *)FCEU_dmalloc(65536*4); } else { if(Bpp==2) - palettetranslate=(uint32*)malloc(65536*4); + palettetranslate=(uint32*)FCEU_dmalloc(65536*4); else if(Bpp>=3) - palettetranslate=(uint32*)malloc(256*4); + palettetranslate=(uint32*)FCEU_dmalloc(256*4); } if(!palettetranslate) diff --git a/src/drivers/sdl/sdl-opengl.cpp b/src/drivers/sdl/sdl-opengl.cpp index 7f700d16..53187751 100644 --- a/src/drivers/sdl/sdl-opengl.cpp +++ b/src/drivers/sdl/sdl-opengl.cpp @@ -14,6 +14,7 @@ #include "sdl.h" #include "sdl-opengl.h" #include "../common/vidblit.h" +#include "../../utils/memory.h" #ifndef APIENTRY #define APIENTRY @@ -213,7 +214,7 @@ InitOpenGL(int l, { if(!(efx&2)) // Don't want to print out a warning message in this case... FCEU_printf("Paletted texture extension not found. Using slower texture format...\n"); - HiBuffer=malloc(4*256*256); + HiBuffer=FCEU_malloc(4*256*256); memset(HiBuffer,0x00,4*256*256); #ifndef LSB_FIRST InitBlitToHigh(4,0xFF000000,0xFF0000,0xFF00,efx&2,0,0); @@ -246,7 +247,7 @@ InitOpenGL(int l, p_glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,ipolate?GL_LINEAR:GL_NEAREST); p_glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,ipolate?GL_LINEAR:GL_NEAREST); - buf=(uint8*)malloc(256*(256*2)*4); + buf=(uint8*)FCEU_dmalloc(256*(256*2)*4); for(y=0;y<(256*2);y++) for(x=0;x<256;x++) @@ -259,7 +260,7 @@ InitOpenGL(int l, } p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, (scanlines==2)?256*4:512, 0, GL_RGBA,GL_UNSIGNED_BYTE,buf); - free(buf); + FCEU_dfree(buf); } p_glBindTexture(GL_TEXTURE_2D, textures[0]); diff --git a/src/drivers/sdl/sdl-sound.cpp b/src/drivers/sdl/sdl-sound.cpp index 55537743..8a36502c 100644 --- a/src/drivers/sdl/sdl-sound.cpp +++ b/src/drivers/sdl/sdl-sound.cpp @@ -28,6 +28,8 @@ #include "sdl.h" #include "../common/configSys.h" +#include "../../utils/memory.h" + extern Config *g_config; static volatile int *s_Buffer = 0; @@ -122,7 +124,9 @@ InitSound() if (s_BufferSize < spec.samples * 2) s_BufferSize = spec.samples * 2; - s_Buffer = (int *)malloc(sizeof(int) * s_BufferSize); + s_Buffer = (int *)FCEU_dmalloc(sizeof(int) * s_BufferSize); + if (!s_Buffer) + return 0; s_BufferRead = s_BufferWrite = s_BufferIn = 0; //printf("SDL Size: %d, Internal size: %d\n",spec.samples,s_BufferSize); diff --git a/src/drivers/sdl/sdl-video.cpp b/src/drivers/sdl/sdl-video.cpp index 3126ad86..427130d3 100644 --- a/src/drivers/sdl/sdl-video.cpp +++ b/src/drivers/sdl/sdl-video.cpp @@ -31,6 +31,8 @@ #include "../../fceu.h" #include "../../version.h" +#include "../../utils/memory.h" + #include "sdl-icon.h" #include "dface.h" @@ -650,7 +652,7 @@ BlitScreen(uint8 *XBuf) if(!result || resultsize != width*height*3*2) { if(result) free(result); - result = (unsigned char*) malloc(resultsize = width*height*3*2); + result = (unsigned char*) FCEU_dmalloc(resultsize = width*height*3*2); } switch(s_curbpp) { diff --git a/src/drivers/sdl/unix-netplay.cpp b/src/drivers/sdl/unix-netplay.cpp index 32e6d409..0bef01ab 100644 --- a/src/drivers/sdl/unix-netplay.cpp +++ b/src/drivers/sdl/unix-netplay.cpp @@ -47,6 +47,7 @@ #include "../../fceu.h" #include "../../utils/md5.h" +#include "../../utils/memory.h" #include #include "../common/configSys.h" @@ -173,7 +174,7 @@ FCEUD_NetworkConnect(void) uint32 sblen; sblen = 4 + 16 + 16 + 64 + 1 + username.size(); - sendbuf = (uint8 *)malloc(sblen); + sendbuf = (uint8 *)FCEU_dmalloc(sblen); memset(sendbuf, 0, sblen); // XXX soules - should use htons instead of en32() from above! @@ -218,7 +219,7 @@ FCEUD_NetworkConnect(void) #else send(s_Socket, sendbuf, sblen, 0); #endif - free(sendbuf); + FCEU_dfree(sendbuf); #ifdef WIN32 recv(s_Socket, (char*)buf, 1, 0); @@ -338,8 +339,10 @@ FCEUD_NetworkClose(void) void FCEUD_NetplayText(uint8 *text) { - char *tot = (char *)malloc(strlen((const char *)text) + 1); + char *tot = (char *)FCEU_dmalloc(strlen((const char *)text) + 1); char *tmp; + if (!tot) + return; strcpy(tot, (const char *)text); tmp = tot; @@ -350,5 +353,5 @@ FCEUD_NetplayText(uint8 *text) tmp++; } puts(tot); - free(tot); + FCEU_dfree(tot); } diff --git a/src/fceu.cpp b/src/fceu.cpp index ea6d9d6b..04162109 100644 --- a/src/fceu.cpp +++ b/src/fceu.cpp @@ -442,7 +442,7 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode) ResetGameLoaded(); if (!AutosaveStatus) - AutosaveStatus = (int*)malloc(sizeof(int)*AutosaveQty); + AutosaveStatus = (int*)FCEU_dmalloc(sizeof(int)*AutosaveQty); for (AutosaveIndex=0; AutosaveIndex #include -#include -#include "../types.h" -#include "../fceu.h" -#include "memory.h" - +#include +#include "../types.h" +#include "../fceu.h" +#include "memory.h" + ///allocates the specified number of bytes. exits process if this fails -void *FCEU_gmalloc(uint32 size) -{ - - void *ret; - ret=malloc(size); - if(!ret) - { - FCEU_PrintError("Error allocating memory! Doing a hard exit."); - exit(1); - } - //mbg 6/17/08 - sometimes this memory is used as RAM or somesuch without clearing first. - //this yields different behavior in debug and release modes. - //specifically, saveram wasnt getting cleared so the games thought their savefiles were initialized - //so we are going to clear it here. - memset(ret,0,size); - return ret; -} - +void *FCEU_gmalloc(uint32 size) +{ + + void *ret; + ret=malloc(size); + if(!ret) + { + FCEU_PrintError("Error allocating memory! Doing a hard exit."); + exit(1); + } + //mbg 6/17/08 - sometimes this memory is used as RAM or somesuch without clearing first. + //this yields different behavior in debug and release modes. + //specifically, saveram wasnt getting cleared so the games thought their savefiles were initialized + //so we are going to clear it here. + memset(ret,0,size); + return ret; +} + ///allocates the specified number of bytes. returns null if this fails -void *FCEU_malloc(uint32 size) -{ - void *ret; - ret=malloc(size); - if(!ret) - { - FCEU_PrintError("Error allocating memory!"); - return(0); - } - //mbg 6/17/08 - sometimes this memory is used as RAM or somesuch without clearing first. - //this yields different behavior in debug and release modes. - //specifically, saveram wasnt getting cleared so the games thought their savefiles were initialized - //so we are going to clear it here. - memset(ret,0,size); - return ret; -} - -///frees memory allocated with FCEU_gmalloc -void FCEU_gfree(void *ptr) -{ - free(ptr); -} - -///frees memory allocated with FCEU_malloc -void FCEU_free(void *ptr) // Might do something with this and FCEU_malloc later... -{ - free(ptr); -} +void *FCEU_malloc(uint32 size) +{ + void *ret; + ret=malloc(size); + if(!ret) + { + FCEU_PrintError("Error allocating memory!"); + return(0); + } + //mbg 6/17/08 - sometimes this memory is used as RAM or somesuch without clearing first. + //this yields different behavior in debug and release modes. + //specifically, saveram wasnt getting cleared so the games thought their savefiles were initialized + //so we are going to clear it here. + memset(ret,0,size); + return ret; +} + +///frees memory allocated with FCEU_gmalloc +void FCEU_gfree(void *ptr) +{ + free(ptr); +} + +///frees memory allocated with FCEU_malloc +void FCEU_free(void *ptr) // Might do something with this and FCEU_malloc later... +{ + free(ptr); +} + +void *FCEU_dmalloc(uint32 size) +{ + return malloc(size); +} + +void FCEU_dfree(void *ptr) +{ + free(ptr); +} diff --git a/src/utils/memory.h b/src/utils/memory.h index 5bf9ca16..38cbdc87 100644 --- a/src/utils/memory.h +++ b/src/utils/memory.h @@ -29,3 +29,8 @@ void *FCEU_gmalloc(uint32 size); void FCEU_gfree(void *ptr); void FCEU_free(void *ptr); void FCEU_memmove(void *d, void *s, uint32 l); + +// wrapper for debugging when its needed, otherwise act like +// normal malloc/free +void *FCEU_dmalloc(uint32 size); +void FCEU_dfree(void *ptr); diff --git a/src/video.cpp b/src/video.cpp index de06b33f..b300bc05 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -628,7 +628,7 @@ int SaveSnapshot(void) uint8 *tmp=XBuf+FSettings.FirstSLine*256; uint8 *dest,*mal,*mork; - if(!(mal=mork=dest=(uint8 *)malloc((totallines<<8)+totallines))) + if(!(mal=mork=dest=(uint8 *)FCEU_dmalloc((totallines<<8)+totallines))) goto PNGerr; // mork=dest=XBuf; @@ -723,7 +723,7 @@ int SaveSnapshot(char fileName[512]) uint8 *tmp=XBuf+FSettings.FirstSLine*256; uint8 *dest,*mal,*mork; - if(!(mal=mork=dest=(uint8 *)malloc((totallines<<8)+totallines))) + if(!(mal=mork=dest=(uint8 *)FCEU_dmalloc((totallines<<8)+totallines))) goto PNGerr; // mork=dest=XBuf; @@ -780,4 +780,4 @@ void ShowFPS(void) // It's not averaging FPS over exactly 1 second, but it's close enough. boopcount = (boopcount + 1) % booplimit; } -#endif \ No newline at end of file +#endif