code review - memory alloc changes

This commit is contained in:
dborth 2008-09-27 07:13:52 +00:00
parent 7f9f088ef3
commit 264dda0965
12 changed files with 119 additions and 77 deletions

View File

@ -223,7 +223,7 @@ LoadBufferFromFAT (char *filepath, bool silent)
int boffset = 0; int boffset = 0;
int read = 0; int read = 0;
ClearSaveBuffer (); AllocSaveBuffer ();
handle = fopen (filepath, "rb"); handle = fopen (filepath, "rb");
@ -246,6 +246,8 @@ LoadBufferFromFAT (char *filepath, bool silent)
fclose (handle); fclose (handle);
FreeSaveBuffer ();
return boffset; return boffset;
} }
@ -257,6 +259,8 @@ SaveBufferToFAT (char *filepath, int datasize, bool silent)
{ {
FILE *handle; FILE *handle;
AllocSaveBuffer ();
if (datasize) if (datasize)
{ {
handle = fopen (filepath, "wb"); handle = fopen (filepath, "wb");
@ -273,6 +277,6 @@ SaveBufferToFAT (char *filepath, int datasize, bool silent)
fclose (handle); fclose (handle);
} }
ClearSaveBuffer (); FreeSaveBuffer ();
return datasize; return datasize;
} }

View File

@ -58,19 +58,30 @@ FILEENTRIES filelist[MAXFILES];
unsigned char *savebuffer = NULL; unsigned char *savebuffer = NULL;
/**************************************************************************** /****************************************************************************
* ClearSaveBuffer () * AllocSaveBuffer ()
* Allocate and clear the savebuffer * Clear and allocate the savebuffer
***************************************************************************/ ***************************************************************************/
void void
ClearSaveBuffer () AllocSaveBuffer ()
{ {
if (savebuffer) if (savebuffer != NULL)
free(savebuffer); free(savebuffer);
savebuffer = (unsigned char *) memalign(32, SAVEBUFFERSIZE); savebuffer = (unsigned char *) memalign(32, SAVEBUFFERSIZE);
memset (savebuffer, 0, SAVEBUFFERSIZE); memset (savebuffer, 0, SAVEBUFFERSIZE);
} }
/****************************************************************************
* FreeSaveBuffer ()
* Free the savebuffer memory
***************************************************************************/
void
FreeSaveBuffer ()
{
if (savebuffer != NULL)
free(savebuffer);
}
/**************************************************************************** /****************************************************************************
* autoLoadMethod() * autoLoadMethod()
* Auto-determines and sets the load method * Auto-determines and sets the load method
@ -220,7 +231,7 @@ void StripExt(char* returnstring, char * inputstring)
strcpy (returnstring, inputstring); strcpy (returnstring, inputstring);
loc_dot = strrchr(returnstring,'.'); loc_dot = strrchr(returnstring,'.');
if (loc_dot != NULL) if (loc_dot != NULL)
*loc_dot = '\0'; // strip file extension loc_dot = 0; // strip file extension
} }
/**************************************************************************** /****************************************************************************

View File

@ -37,7 +37,8 @@ extern int selection;
extern char currentdir[MAXPATHLEN]; extern char currentdir[MAXPATHLEN];
extern int maxfiles; extern int maxfiles;
void ClearSaveBuffer (); void AllocSaveBuffer();
void FreeSaveBuffer();
int OpenROM (int method); int OpenROM (int method);
int autoLoadMethod(); int autoLoadMethod();
int autoSaveMethod(); int autoSaveMethod();

View File

@ -138,7 +138,7 @@ NGCFreezeGame (int method, bool8 silent)
S9xSetSoundMute (TRUE); S9xSetSoundMute (TRUE);
S9xPrepareSoundForSnapshotSave (FALSE); S9xPrepareSoundForSnapshotSave (FALSE);
ClearSaveBuffer (); AllocSaveBuffer ();
NGCFreezeMemBuffer (); // copy freeze mem into savebuffer NGCFreezeMemBuffer (); // copy freeze mem into savebuffer
S9xPrepareSoundForSnapshotSave (TRUE); S9xPrepareSoundForSnapshotSave (TRUE);
@ -198,6 +198,8 @@ NGCFreezeGame (int method, bool8 silent)
offset = SaveBufferToMC ( savebuffer, CARD_SLOTB, filename, woffset, SILENT ); offset = SaveBufferToMC ( savebuffer, CARD_SLOTB, filename, woffset, SILENT );
} }
FreeSaveBuffer ();
if(offset > 0) // save successful! if(offset > 0) // save successful!
{ {
if(!silent) if(!silent)
@ -260,6 +262,8 @@ NGCUnfreezeGame (int method, bool8 silent)
if(method == METHOD_AUTO) if(method == METHOD_AUTO)
method = autoSaveMethod(); // we use 'Save' because snapshot needs R/W method = autoSaveMethod(); // we use 'Save' because snapshot needs R/W
AllocSaveBuffer ();
if (method == METHOD_SD || method == METHOD_USB) // SD & USB if (method == METHOD_SD || method == METHOD_USB) // SD & USB
{ {
if(ChangeFATInterface(method, NOTSILENT)) if(ChangeFATInterface(method, NOTSILENT))
@ -321,10 +325,12 @@ NGCUnfreezeGame (int method, bool8 silent)
} }
} }
int result = 0;
if(offset > 0) if(offset > 0)
{ {
if (S9xUnfreezeGame ("AGAME") == SUCCESS) if (S9xUnfreezeGame ("AGAME") == SUCCESS)
return 1; result = 1;
else else
WaitPrompt((char*) "Error thawing"); WaitPrompt((char*) "Error thawing");
} }
@ -333,5 +339,6 @@ NGCUnfreezeGame (int method, bool8 silent)
if(!silent) if(!silent)
WaitPrompt((char*) "Freeze file not found"); WaitPrompt((char*) "Freeze file not found");
} }
return 0; // if we reached here, nothing was done! FreeSaveBuffer ();
return result;
} }

View File

@ -45,8 +45,6 @@ extern int ConfigRequested;
#define ASSIGN_BUTTON_FALSE( keycode, snescmd ) \ #define ASSIGN_BUTTON_FALSE( keycode, snescmd ) \
S9xMapButton( keycode, cmd = S9xGetCommandT(snescmd), false) S9xMapButton( keycode, cmd = S9xGetCommandT(snescmd), false)
#define MAXJP 12
/*** Gamecube controller Padmap ***/ /*** Gamecube controller Padmap ***/
unsigned int gcpadmap[] = { PAD_BUTTON_A, PAD_BUTTON_B, unsigned int gcpadmap[] = { PAD_BUTTON_A, PAD_BUTTON_B,
PAD_BUTTON_X, PAD_BUTTON_Y, PAD_BUTTON_X, PAD_BUTTON_Y,

View File

@ -18,6 +18,7 @@
#define PI 3.14159265f #define PI 3.14159265f
#define PADCAL 50 #define PADCAL 50
#define MAXJP 12 // # of mappable controller buttons
extern unsigned int gcpadmap[]; extern unsigned int gcpadmap[];
extern unsigned int wmpadmap[]; extern unsigned int wmpadmap[];

View File

@ -14,10 +14,6 @@
#include <ogcsys.h> #include <ogcsys.h>
#include <mxml.h> #include <mxml.h>
#include "snes9x.h"
#include "memmap.h"
#include "srtc.h"
#include "snes9xGX.h" #include "snes9xGX.h"
#include "images/saveicon.h" #include "images/saveicon.h"
#include "menudraw.h" #include "menudraw.h"
@ -25,15 +21,10 @@
#include "fileop.h" #include "fileop.h"
#include "smbop.h" #include "smbop.h"
#include "filesel.h" #include "filesel.h"
#include "input.h"
extern int currconfig[4]; extern int currconfig[4];
// button map configurations
extern unsigned int gcpadmap[];
extern unsigned int wmpadmap[];
extern unsigned int ccpadmap[];
extern unsigned int ncpadmap[];
#define PREFS_FILE_NAME "SNES9xGX.xml" #define PREFS_FILE_NAME "SNES9xGX.xml"
char prefscomment[2][32]; char prefscomment[2][32];
@ -49,7 +40,7 @@ mxml_node_t *section;
mxml_node_t *item; mxml_node_t *item;
mxml_node_t *elem; mxml_node_t *elem;
char temp[200]; char temp[20];
const char * toStr(int i) const char * toStr(int i)
{ {
@ -79,7 +70,7 @@ void createXMLController(unsigned int controller[], const char * name, const cha
mxmlElementSetAttr(item, "description", description); mxmlElementSetAttr(item, "description", description);
// create buttons // create buttons
for(int i=0; i < 12; i++) for(int i=0; i < MAXJP; i++)
{ {
elem = mxmlNewElement(item, "button"); elem = mxmlNewElement(item, "button");
mxmlElementSetAttr(elem, "number", toStr(i)); mxmlElementSetAttr(elem, "number", toStr(i));
@ -119,7 +110,6 @@ int
preparePrefsData (int method) preparePrefsData (int method)
{ {
int offset = 0; int offset = 0;
ClearSaveBuffer ();
// add save icon and comments for Memory Card saves // add save icon and comments for Memory Card saves
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
@ -227,7 +217,7 @@ void loadXMLController(unsigned int controller[], const char * name)
if(item) if(item)
{ {
// populate buttons // populate buttons
for(int i=0; i < 12; i++) for(int i=0; i < MAXJP; i++)
{ {
elem = mxmlFindElement(item, xml, "button", "number", toStr(i), MXML_DESCEND); elem = mxmlFindElement(item, xml, "button", "number", toStr(i), MXML_DESCEND);
if(elem) if(elem)
@ -322,6 +312,7 @@ SavePrefs (int method, bool silent)
int datasize; int datasize;
int offset = 0; int offset = 0;
AllocSaveBuffer ();
datasize = preparePrefsData (method); datasize = preparePrefsData (method);
if (!silent) if (!silent)
@ -349,6 +340,8 @@ SavePrefs (int method, bool silent)
offset = SaveBufferToMC (savebuffer, CARD_SLOTB, (char *)PREFS_FILE_NAME, datasize, silent); offset = SaveBufferToMC (savebuffer, CARD_SLOTB, (char *)PREFS_FILE_NAME, datasize, silent);
} }
FreeSaveBuffer ();
if (offset > 0) if (offset > 0)
{ {
if (!silent) if (!silent)
@ -368,6 +361,8 @@ LoadPrefsFromMethod (int method)
char filepath[1024]; char filepath[1024];
int offset = 0; int offset = 0;
AllocSaveBuffer ();
if(method == METHOD_SD || method == METHOD_USB) if(method == METHOD_SD || method == METHOD_USB)
{ {
if(ChangeFATInterface(method, NOTSILENT)) if(ChangeFATInterface(method, NOTSILENT))
@ -393,6 +388,8 @@ LoadPrefsFromMethod (int method)
if (offset > 0) if (offset > 0)
retval = decodePrefsData (method); retval = decodePrefsData (method);
FreeSaveBuffer ();
return retval; return retval;
} }

View File

@ -246,6 +246,8 @@ SaveBufferToSMB (char *filepath, int datasize, bool silent)
int wrote = 0; int wrote = 0;
int boffset = 0; int boffset = 0;
AllocSaveBuffer();
smbfile = smbfile =
SMB_OpenFile (SMBPath(filepath), SMB_OPEN_WRITING | SMB_DENY_NONE, SMB_OpenFile (SMBPath(filepath), SMB_OPEN_WRITING | SMB_DENY_NONE,
SMB_OF_CREATE | SMB_OF_TRUNCATE, smbconn); SMB_OF_CREATE | SMB_OF_TRUNCATE, smbconn);
@ -273,6 +275,8 @@ SaveBufferToSMB (char *filepath, int datasize, bool silent)
WaitPrompt (msg); WaitPrompt (msg);
} }
FreeSaveBuffer();
return boffset; return boffset;
} }
@ -284,8 +288,10 @@ SaveBufferToSMB (char *filepath, int datasize, bool silent)
int int
LoadBufferFromSMB (char *filepath, bool silent) LoadBufferFromSMB (char *filepath, bool silent)
{ {
ClearSaveBuffer (); AllocSaveBuffer();
return LoadBufferFromSMB((char *)savebuffer, filepath, silent); int result = LoadBufferFromSMB((char *)savebuffer, filepath, silent);
FreeSaveBuffer();
return result;
} }
int int

View File

@ -44,6 +44,7 @@ extern "C" {
#include "controls.h" #include "controls.h"
#include "snes9xGX.h" #include "snes9xGX.h"
#include "aram.h"
#include "dvd.h" #include "dvd.h"
#include "smbop.h" #include "smbop.h"
#include "video.h" #include "video.h"
@ -213,6 +214,10 @@ main ()
unsigned int save_flags; unsigned int save_flags;
int selectedMenu = -1; int selectedMenu = -1;
// Initialise video
InitGCVideo ();
// Controllers
#ifdef HW_RVL #ifdef HW_RVL
WPAD_Init(); WPAD_Init();
// read wiimote accelerometer and IR data // read wiimote accelerometer and IR data
@ -220,8 +225,23 @@ main ()
WPAD_SetVRes(WPAD_CHAN_ALL,640,480); WPAD_SetVRes(WPAD_CHAN_ALL,640,480);
#endif #endif
// Initialise video PAD_Init ();
InitGCVideo ();
// Audio
AUDIO_Init (NULL);
// GC Audio RAM (for ROM and backdrop storage)
AR_Init (NULL, 0);
// Before going any further, let's copy any injected ROM image
int *romptr = (int *) 0x81000000; // location of injected rom
if (memcmp ((char *) romptr, "SNESROM0", 8) == 0)
{
ARAM_ROMSIZE = romptr[2];
romptr = (int *) 0x81000020;
ARAMPut ((char *) romptr, (char *) AR_SNESROM, ARAM_ROMSIZE);
}
// Initialise freetype font system // Initialise freetype font system
if (FT_Init ()) if (FT_Init ())

View File

@ -44,8 +44,6 @@ preparesavedata (int method)
int offset = 0; int offset = 0;
int size; int size;
ClearSaveBuffer ();
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{ {
// Copy in save icon // Copy in save icon
@ -154,6 +152,8 @@ LoadSRAM (int method, bool silent)
char filepath[1024]; char filepath[1024];
int offset = 0; int offset = 0;
AllocSaveBuffer ();
if(method == METHOD_SD || method == METHOD_USB) if(method == METHOD_SD || method == METHOD_USB)
{ {
ChangeFATInterface(method, NOTSILENT); ChangeFATInterface(method, NOTSILENT);
@ -179,15 +179,23 @@ LoadSRAM (int method, bool silent)
{ {
decodesavedata (method, offset); decodesavedata (method, offset);
S9xSoftReset(); S9xSoftReset();
return 1;
} }
FreeSaveBuffer ();
if(offset > 0)
{
return 1;
}
else
{
// if we reached here, nothing was done! // if we reached here, nothing was done!
if(!silent) if(!silent)
WaitPrompt ((char*) "SRAM file not found"); WaitPrompt ((char*) "SRAM file not found");
return 0; return 0;
} }
}
/**************************************************************************** /****************************************************************************
* Save SRAM * Save SRAM
@ -205,6 +213,8 @@ SaveSRAM (int method, bool silent)
int datasize; int datasize;
int offset = 0; int offset = 0;
AllocSaveBuffer ();
datasize = preparesavedata (method); datasize = preparesavedata (method);
if ( datasize ) if ( datasize )
@ -244,5 +254,7 @@ SaveSRAM (int method, bool silent)
if(!silent) if(!silent)
WaitPrompt((char *)"No SRAM data to save!"); WaitPrompt((char *)"No SRAM data to save!");
} }
FreeSaveBuffer ();
return retval; return retval;
} }

View File

@ -446,23 +446,8 @@ UpdatePadsCB ()
void void
InitGCVideo () InitGCVideo ()
{ {
int *romptr = (int *) 0x81000000; // injected rom
// init video // init video
VIDEO_Init (); VIDEO_Init ();
PAD_Init ();
AUDIO_Init (NULL);
AR_Init (NULL, 0);
// Before going any further, let's copy any attached ROM image
if (memcmp ((char *) romptr, "SNESROM0", 8) == 0)
{
ARAM_ROMSIZE = romptr[2];
romptr = (int *) 0x81000020;
ARAMPut ((char *) romptr, (char *) AR_SNESROM, ARAM_ROMSIZE);
}
// get default video mode // get default video mode
vmode = VIDEO_GetPreferredMode(NULL); vmode = VIDEO_GetPreferredMode(NULL);