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
@ -128,7 +139,7 @@ int autoSaveMethod()
int UpdateDirName(int method) int UpdateDirName(int method)
{ {
int size=0; int size=0;
char *test; char * test;
char temp[1024]; char temp[1024];
// update DVD directory (does not utilize 'currentdir') // update DVD directory (does not utilize 'currentdir')
@ -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

@ -211,11 +211,11 @@ DrawText (int x, int y, char *text)
void void
setfontcolour (u8 r, u8 g, u8 b) setfontcolour (u8 r, u8 g, u8 b)
{ {
u32 fontcolour; u32 fontcolour;
fontcolour = getcolour (r, g, b); fontcolour = getcolour (r, g, b);
fonthi = fontcolour & 0xffff0000; fonthi = fontcolour & 0xffff0000;
fontlo = fontcolour & 0xffff; fontlo = fontcolour & 0xffff;
} }
/**************************************************************************** /****************************************************************************
@ -285,25 +285,25 @@ Credits ()
unsigned int unsigned int
getcolour (u8 r1, u8 g1, u8 b1) getcolour (u8 r1, u8 g1, u8 b1)
{ {
int y1, cb1, cr1, y2, cb2, cr2, cb, cr; int y1, cb1, cr1, y2, cb2, cr2, cb, cr;
u8 r2, g2, b2; u8 r2, g2, b2;
r2 = r1; r2 = r1;
g2 = g1; g2 = g1;
b2 = b1; b2 = b1;
y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000; y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000;
cb1 = (-16874 * r1 - 33126 * g1 + 50000 * b1 + 12800000) / 100000; cb1 = (-16874 * r1 - 33126 * g1 + 50000 * b1 + 12800000) / 100000;
cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000; cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000;
y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000; y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000;
cb2 = (-16874 * r2 - 33126 * g2 + 50000 * b2 + 12800000) / 100000; cb2 = (-16874 * r2 - 33126 * g2 + 50000 * b2 + 12800000) / 100000;
cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000; cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000;
cb = (cb1 + cb2) >> 1; cb = (cb1 + cb2) >> 1;
cr = (cr1 + cr2) >> 1; cr = (cr1 + cr2) >> 1;
return ((y1 << 24) | (cb << 16) | (y2 << 8) | cr); return ((y1 << 24) | (cb << 16) | (y2 << 8) | cr);
} }
/**************************************************************************** /****************************************************************************

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"
@ -167,7 +168,7 @@ emulate ()
//S9xReportControllers (); //S9xReportControllers ();
ConfigRequested = 0; ConfigRequested = 0;
#ifdef _DEBUG_VIDEO #ifdef _DEBUG_VIDEO
// log stuff // log stuff
fprintf(debughandle, "\n\nPlaying ROM: %s", Memory.ROMFilename); fprintf(debughandle, "\n\nPlaying ROM: %s", Memory.ROMFilename);
@ -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 ())
@ -259,7 +279,7 @@ main ()
// Initialize libFAT for SD and USB // Initialize libFAT for SD and USB
fatInitDefault(); fatInitDefault();
#ifdef _DEBUG_VIDEO #ifdef _DEBUG_VIDEO
// log stuff // log stuff
debughandle = fopen ("log.txt", "wb"); debughandle = fopen ("log.txt", "wb");

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,14 +179,22 @@ LoadSRAM (int method, bool silent)
{ {
decodesavedata (method, offset); decodesavedata (method, offset);
S9xSoftReset(); S9xSoftReset();
return 1;
} }
// if we reached here, nothing was done! FreeSaveBuffer ();
if(!silent)
WaitPrompt ((char*) "SRAM file not found");
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 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);
@ -769,7 +754,7 @@ update_video (int width, int height)
// yes its pretty cheap and ugly, but its easy! // yes its pretty cheap and ugly, but its easy!
if (GCSettings.widescreen) if (GCSettings.widescreen)
xscale -= (4.0*yscale)/9; xscale -= (4.0*yscale)/9;
xscale *= zoom_level; xscale *= zoom_level;
yscale *= zoom_level; yscale *= zoom_level;
@ -826,10 +811,10 @@ zoom (float speed)
zoom_level += (speed / -100.0); zoom_level += (speed / -100.0);
else else
zoom_level += (speed / -200.0); zoom_level += (speed / -200.0);
if (zoom_level < 0.5) zoom_level = 0.5; if (zoom_level < 0.5) zoom_level = 0.5;
else if (zoom_level > 10.0) zoom_level = 10.0; else if (zoom_level > 10.0) zoom_level = 10.0;
oldvheight = 0; // update video oldvheight = 0; // update video
} }