mirror of
https://github.com/dborth/snes9xgx.git
synced 2024-11-01 00:15:14 +01:00
code review - memory alloc changes
This commit is contained in:
parent
7f9f088ef3
commit
264dda0965
@ -223,7 +223,7 @@ LoadBufferFromFAT (char *filepath, bool silent)
|
||||
int boffset = 0;
|
||||
int read = 0;
|
||||
|
||||
ClearSaveBuffer ();
|
||||
AllocSaveBuffer ();
|
||||
|
||||
handle = fopen (filepath, "rb");
|
||||
|
||||
@ -246,6 +246,8 @@ LoadBufferFromFAT (char *filepath, bool silent)
|
||||
|
||||
fclose (handle);
|
||||
|
||||
FreeSaveBuffer ();
|
||||
|
||||
return boffset;
|
||||
}
|
||||
|
||||
@ -257,6 +259,8 @@ SaveBufferToFAT (char *filepath, int datasize, bool silent)
|
||||
{
|
||||
FILE *handle;
|
||||
|
||||
AllocSaveBuffer ();
|
||||
|
||||
if (datasize)
|
||||
{
|
||||
handle = fopen (filepath, "wb");
|
||||
@ -273,6 +277,6 @@ SaveBufferToFAT (char *filepath, int datasize, bool silent)
|
||||
fclose (handle);
|
||||
}
|
||||
|
||||
ClearSaveBuffer ();
|
||||
FreeSaveBuffer ();
|
||||
return datasize;
|
||||
}
|
||||
|
@ -58,19 +58,30 @@ FILEENTRIES filelist[MAXFILES];
|
||||
unsigned char *savebuffer = NULL;
|
||||
|
||||
/****************************************************************************
|
||||
* ClearSaveBuffer ()
|
||||
* Allocate and clear the savebuffer
|
||||
* AllocSaveBuffer ()
|
||||
* Clear and allocate the savebuffer
|
||||
***************************************************************************/
|
||||
void
|
||||
ClearSaveBuffer ()
|
||||
AllocSaveBuffer ()
|
||||
{
|
||||
if (savebuffer)
|
||||
if (savebuffer != NULL)
|
||||
free(savebuffer);
|
||||
|
||||
savebuffer = (unsigned char *) memalign(32, SAVEBUFFERSIZE);
|
||||
memset (savebuffer, 0, SAVEBUFFERSIZE);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* FreeSaveBuffer ()
|
||||
* Free the savebuffer memory
|
||||
***************************************************************************/
|
||||
void
|
||||
FreeSaveBuffer ()
|
||||
{
|
||||
if (savebuffer != NULL)
|
||||
free(savebuffer);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* autoLoadMethod()
|
||||
* Auto-determines and sets the load method
|
||||
@ -128,7 +139,7 @@ int autoSaveMethod()
|
||||
int UpdateDirName(int method)
|
||||
{
|
||||
int size=0;
|
||||
char *test;
|
||||
char * test;
|
||||
char temp[1024];
|
||||
|
||||
// update DVD directory (does not utilize 'currentdir')
|
||||
@ -220,7 +231,7 @@ void StripExt(char* returnstring, char * inputstring)
|
||||
strcpy (returnstring, inputstring);
|
||||
loc_dot = strrchr(returnstring,'.');
|
||||
if (loc_dot != NULL)
|
||||
*loc_dot = '\0'; // strip file extension
|
||||
loc_dot = 0; // strip file extension
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -37,7 +37,8 @@ extern int selection;
|
||||
extern char currentdir[MAXPATHLEN];
|
||||
extern int maxfiles;
|
||||
|
||||
void ClearSaveBuffer ();
|
||||
void AllocSaveBuffer();
|
||||
void FreeSaveBuffer();
|
||||
int OpenROM (int method);
|
||||
int autoLoadMethod();
|
||||
int autoSaveMethod();
|
||||
|
@ -138,7 +138,7 @@ NGCFreezeGame (int method, bool8 silent)
|
||||
S9xSetSoundMute (TRUE);
|
||||
S9xPrepareSoundForSnapshotSave (FALSE);
|
||||
|
||||
ClearSaveBuffer ();
|
||||
AllocSaveBuffer ();
|
||||
NGCFreezeMemBuffer (); // copy freeze mem into savebuffer
|
||||
|
||||
S9xPrepareSoundForSnapshotSave (TRUE);
|
||||
@ -198,6 +198,8 @@ NGCFreezeGame (int method, bool8 silent)
|
||||
offset = SaveBufferToMC ( savebuffer, CARD_SLOTB, filename, woffset, SILENT );
|
||||
}
|
||||
|
||||
FreeSaveBuffer ();
|
||||
|
||||
if(offset > 0) // save successful!
|
||||
{
|
||||
if(!silent)
|
||||
@ -260,6 +262,8 @@ NGCUnfreezeGame (int method, bool8 silent)
|
||||
if(method == METHOD_AUTO)
|
||||
method = autoSaveMethod(); // we use 'Save' because snapshot needs R/W
|
||||
|
||||
AllocSaveBuffer ();
|
||||
|
||||
if (method == METHOD_SD || method == METHOD_USB) // SD & USB
|
||||
{
|
||||
if(ChangeFATInterface(method, NOTSILENT))
|
||||
@ -321,10 +325,12 @@ NGCUnfreezeGame (int method, bool8 silent)
|
||||
}
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
|
||||
if(offset > 0)
|
||||
{
|
||||
if (S9xUnfreezeGame ("AGAME") == SUCCESS)
|
||||
return 1;
|
||||
result = 1;
|
||||
else
|
||||
WaitPrompt((char*) "Error thawing");
|
||||
}
|
||||
@ -333,5 +339,6 @@ NGCUnfreezeGame (int method, bool8 silent)
|
||||
if(!silent)
|
||||
WaitPrompt((char*) "Freeze file not found");
|
||||
}
|
||||
return 0; // if we reached here, nothing was done!
|
||||
FreeSaveBuffer ();
|
||||
return result;
|
||||
}
|
||||
|
@ -45,8 +45,6 @@ extern int ConfigRequested;
|
||||
#define ASSIGN_BUTTON_FALSE( keycode, snescmd ) \
|
||||
S9xMapButton( keycode, cmd = S9xGetCommandT(snescmd), false)
|
||||
|
||||
#define MAXJP 12
|
||||
|
||||
/*** Gamecube controller Padmap ***/
|
||||
unsigned int gcpadmap[] = { PAD_BUTTON_A, PAD_BUTTON_B,
|
||||
PAD_BUTTON_X, PAD_BUTTON_Y,
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
#define PI 3.14159265f
|
||||
#define PADCAL 50
|
||||
#define MAXJP 12 // # of mappable controller buttons
|
||||
|
||||
extern unsigned int gcpadmap[];
|
||||
extern unsigned int wmpadmap[];
|
||||
|
@ -211,11 +211,11 @@ DrawText (int x, int y, char *text)
|
||||
void
|
||||
setfontcolour (u8 r, u8 g, u8 b)
|
||||
{
|
||||
u32 fontcolour;
|
||||
u32 fontcolour;
|
||||
|
||||
fontcolour = getcolour (r, g, b);
|
||||
fonthi = fontcolour & 0xffff0000;
|
||||
fontlo = fontcolour & 0xffff;
|
||||
fontcolour = getcolour (r, g, b);
|
||||
fonthi = fontcolour & 0xffff0000;
|
||||
fontlo = fontcolour & 0xffff;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -285,25 +285,25 @@ Credits ()
|
||||
unsigned int
|
||||
getcolour (u8 r1, u8 g1, u8 b1)
|
||||
{
|
||||
int y1, cb1, cr1, y2, cb2, cr2, cb, cr;
|
||||
u8 r2, g2, b2;
|
||||
int y1, cb1, cr1, y2, cb2, cr2, cb, cr;
|
||||
u8 r2, g2, b2;
|
||||
|
||||
r2 = r1;
|
||||
g2 = g1;
|
||||
b2 = b1;
|
||||
r2 = r1;
|
||||
g2 = g1;
|
||||
b2 = b1;
|
||||
|
||||
y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000;
|
||||
cb1 = (-16874 * r1 - 33126 * g1 + 50000 * b1 + 12800000) / 100000;
|
||||
cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000;
|
||||
y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000;
|
||||
cb1 = (-16874 * r1 - 33126 * g1 + 50000 * b1 + 12800000) / 100000;
|
||||
cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000;
|
||||
|
||||
y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000;
|
||||
cb2 = (-16874 * r2 - 33126 * g2 + 50000 * b2 + 12800000) / 100000;
|
||||
cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000;
|
||||
y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000;
|
||||
cb2 = (-16874 * r2 - 33126 * g2 + 50000 * b2 + 12800000) / 100000;
|
||||
cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000;
|
||||
|
||||
cb = (cb1 + cb2) >> 1;
|
||||
cr = (cr1 + cr2) >> 1;
|
||||
cb = (cb1 + cb2) >> 1;
|
||||
cr = (cr1 + cr2) >> 1;
|
||||
|
||||
return ((y1 << 24) | (cb << 16) | (y2 << 8) | cr);
|
||||
return ((y1 << 24) | (cb << 16) | (y2 << 8) | cr);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -14,10 +14,6 @@
|
||||
#include <ogcsys.h>
|
||||
#include <mxml.h>
|
||||
|
||||
#include "snes9x.h"
|
||||
#include "memmap.h"
|
||||
#include "srtc.h"
|
||||
|
||||
#include "snes9xGX.h"
|
||||
#include "images/saveicon.h"
|
||||
#include "menudraw.h"
|
||||
@ -25,15 +21,10 @@
|
||||
#include "fileop.h"
|
||||
#include "smbop.h"
|
||||
#include "filesel.h"
|
||||
#include "input.h"
|
||||
|
||||
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"
|
||||
|
||||
char prefscomment[2][32];
|
||||
@ -49,7 +40,7 @@ mxml_node_t *section;
|
||||
mxml_node_t *item;
|
||||
mxml_node_t *elem;
|
||||
|
||||
char temp[200];
|
||||
char temp[20];
|
||||
|
||||
const char * toStr(int i)
|
||||
{
|
||||
@ -79,7 +70,7 @@ void createXMLController(unsigned int controller[], const char * name, const cha
|
||||
mxmlElementSetAttr(item, "description", description);
|
||||
|
||||
// create buttons
|
||||
for(int i=0; i < 12; i++)
|
||||
for(int i=0; i < MAXJP; i++)
|
||||
{
|
||||
elem = mxmlNewElement(item, "button");
|
||||
mxmlElementSetAttr(elem, "number", toStr(i));
|
||||
@ -119,7 +110,6 @@ int
|
||||
preparePrefsData (int method)
|
||||
{
|
||||
int offset = 0;
|
||||
ClearSaveBuffer ();
|
||||
|
||||
// add save icon and comments for Memory Card saves
|
||||
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
||||
@ -227,7 +217,7 @@ void loadXMLController(unsigned int controller[], const char * name)
|
||||
if(item)
|
||||
{
|
||||
// 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);
|
||||
if(elem)
|
||||
@ -322,6 +312,7 @@ SavePrefs (int method, bool silent)
|
||||
int datasize;
|
||||
int offset = 0;
|
||||
|
||||
AllocSaveBuffer ();
|
||||
datasize = preparePrefsData (method);
|
||||
|
||||
if (!silent)
|
||||
@ -349,6 +340,8 @@ SavePrefs (int method, bool silent)
|
||||
offset = SaveBufferToMC (savebuffer, CARD_SLOTB, (char *)PREFS_FILE_NAME, datasize, silent);
|
||||
}
|
||||
|
||||
FreeSaveBuffer ();
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
if (!silent)
|
||||
@ -368,6 +361,8 @@ LoadPrefsFromMethod (int method)
|
||||
char filepath[1024];
|
||||
int offset = 0;
|
||||
|
||||
AllocSaveBuffer ();
|
||||
|
||||
if(method == METHOD_SD || method == METHOD_USB)
|
||||
{
|
||||
if(ChangeFATInterface(method, NOTSILENT))
|
||||
@ -393,6 +388,8 @@ LoadPrefsFromMethod (int method)
|
||||
if (offset > 0)
|
||||
retval = decodePrefsData (method);
|
||||
|
||||
FreeSaveBuffer ();
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
@ -246,6 +246,8 @@ SaveBufferToSMB (char *filepath, int datasize, bool silent)
|
||||
int wrote = 0;
|
||||
int boffset = 0;
|
||||
|
||||
AllocSaveBuffer();
|
||||
|
||||
smbfile =
|
||||
SMB_OpenFile (SMBPath(filepath), SMB_OPEN_WRITING | SMB_DENY_NONE,
|
||||
SMB_OF_CREATE | SMB_OF_TRUNCATE, smbconn);
|
||||
@ -273,6 +275,8 @@ SaveBufferToSMB (char *filepath, int datasize, bool silent)
|
||||
WaitPrompt (msg);
|
||||
}
|
||||
|
||||
FreeSaveBuffer();
|
||||
|
||||
return boffset;
|
||||
}
|
||||
|
||||
@ -284,8 +288,10 @@ SaveBufferToSMB (char *filepath, int datasize, bool silent)
|
||||
int
|
||||
LoadBufferFromSMB (char *filepath, bool silent)
|
||||
{
|
||||
ClearSaveBuffer ();
|
||||
return LoadBufferFromSMB((char *)savebuffer, filepath, silent);
|
||||
AllocSaveBuffer();
|
||||
int result = LoadBufferFromSMB((char *)savebuffer, filepath, silent);
|
||||
FreeSaveBuffer();
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -44,6 +44,7 @@ extern "C" {
|
||||
#include "controls.h"
|
||||
|
||||
#include "snes9xGX.h"
|
||||
#include "aram.h"
|
||||
#include "dvd.h"
|
||||
#include "smbop.h"
|
||||
#include "video.h"
|
||||
@ -213,6 +214,10 @@ main ()
|
||||
unsigned int save_flags;
|
||||
int selectedMenu = -1;
|
||||
|
||||
// Initialise video
|
||||
InitGCVideo ();
|
||||
|
||||
// Controllers
|
||||
#ifdef HW_RVL
|
||||
WPAD_Init();
|
||||
// read wiimote accelerometer and IR data
|
||||
@ -220,8 +225,23 @@ main ()
|
||||
WPAD_SetVRes(WPAD_CHAN_ALL,640,480);
|
||||
#endif
|
||||
|
||||
// Initialise video
|
||||
InitGCVideo ();
|
||||
PAD_Init ();
|
||||
|
||||
// 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
|
||||
if (FT_Init ())
|
||||
|
@ -44,8 +44,6 @@ preparesavedata (int method)
|
||||
int offset = 0;
|
||||
int size;
|
||||
|
||||
ClearSaveBuffer ();
|
||||
|
||||
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
||||
{
|
||||
// Copy in save icon
|
||||
@ -154,6 +152,8 @@ LoadSRAM (int method, bool silent)
|
||||
char filepath[1024];
|
||||
int offset = 0;
|
||||
|
||||
AllocSaveBuffer ();
|
||||
|
||||
if(method == METHOD_SD || method == METHOD_USB)
|
||||
{
|
||||
ChangeFATInterface(method, NOTSILENT);
|
||||
@ -179,14 +179,22 @@ LoadSRAM (int method, bool silent)
|
||||
{
|
||||
decodesavedata (method, offset);
|
||||
S9xSoftReset();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// if we reached here, nothing was done!
|
||||
if(!silent)
|
||||
WaitPrompt ((char*) "SRAM file not found");
|
||||
FreeSaveBuffer ();
|
||||
|
||||
return 0;
|
||||
if(offset > 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// if we reached here, nothing was done!
|
||||
if(!silent)
|
||||
WaitPrompt ((char*) "SRAM file not found");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -205,6 +213,8 @@ SaveSRAM (int method, bool silent)
|
||||
int datasize;
|
||||
int offset = 0;
|
||||
|
||||
AllocSaveBuffer ();
|
||||
|
||||
datasize = preparesavedata (method);
|
||||
|
||||
if ( datasize )
|
||||
@ -244,5 +254,7 @@ SaveSRAM (int method, bool silent)
|
||||
if(!silent)
|
||||
WaitPrompt((char *)"No SRAM data to save!");
|
||||
}
|
||||
|
||||
FreeSaveBuffer ();
|
||||
return retval;
|
||||
}
|
||||
|
@ -446,23 +446,8 @@ UpdatePadsCB ()
|
||||
void
|
||||
InitGCVideo ()
|
||||
{
|
||||
int *romptr = (int *) 0x81000000; // injected rom
|
||||
|
||||
// init video
|
||||
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
|
||||
vmode = VIDEO_GetPreferredMode(NULL);
|
||||
|
Loading…
Reference in New Issue
Block a user