int SaveState(const char *file) {
void *f;
- GPUFreeze_t *gpufP;
- SPUFreezeHdr_t *spufH;
- SPUFreeze_t *spufP;
+ GPUFreeze_t *gpufP = NULL;
+ SPUFreezeHdr_t spufH;
+ SPUFreeze_t *spufP = NULL;
+ unsigned char *pMem = NULL;
+ int result = -1;
int Size;
- unsigned char *pMem;
f = SaveFuncs.open(file, "wb");
if (f == NULL) return -1;
SaveFuncs.write(f, (void *)&Config.HLE, sizeof(boolean));
pMem = (unsigned char *)malloc(128 * 96 * 3);
- if (pMem == NULL) return -1;
+ if (pMem == NULL) goto cleanup;
GPU_getScreenPic(pMem);
SaveFuncs.write(f, pMem, 128 * 96 * 3);
free(pMem);
// gpu
gpufP = (GPUFreeze_t *)malloc(sizeof(GPUFreeze_t));
+ if (gpufP == NULL) goto cleanup;
gpufP->ulFreezeVersion = 1;
GPU_freeze(1, gpufP);
SaveFuncs.write(f, gpufP, sizeof(GPUFreeze_t));
- free(gpufP);
+ free(gpufP); gpufP = NULL;
// spu
- spufH = malloc(sizeof(*spufH));
- SPU_freeze(2, (SPUFreeze_t *)spufH, psxRegs.cycle);
- Size = spufH->Size; SaveFuncs.write(f, &Size, 4);
- free(spufH);
+ SPU_freeze(2, (SPUFreeze_t *)&spufH, psxRegs.cycle);
+ Size = spufH.Size; SaveFuncs.write(f, &Size, 4);
spufP = (SPUFreeze_t *) malloc(Size);
+ if (spufP == NULL) goto cleanup;
SPU_freeze(1, spufP, psxRegs.cycle);
SaveFuncs.write(f, spufP, Size);
- free(spufP);
+ free(spufP); spufP = NULL;
sioFreeze(f, 1);
cdrFreeze(f, 1);
mdecFreeze(f, 1);
new_dyna_freeze(f, 1);
+ result = 0;
+cleanup:
SaveFuncs.close(f);
-
- return 0;
+ return result;
}
int LoadState(const char *file) {
void *f;
- GPUFreeze_t *gpufP;
- SPUFreeze_t *spufP;
+ GPUFreeze_t *gpufP = NULL;
+ SPUFreeze_t *spufP = NULL;
int Size;
char header[32];
u32 version;
boolean hle;
+ int result = -1;
f = SaveFuncs.open(file, "rb");
if (f == NULL) return -1;
SaveFuncs.read(f, &hle, sizeof(boolean));
if (strncmp("STv4 PCSX", header, 9) != 0 || version != SaveVersion) {
- SaveFuncs.close(f);
- return -1;
+ SysPrintf("incompatible savestate version %x\n", version);
+ goto cleanup;
}
Config.HLE = hle;
// gpu
gpufP = (GPUFreeze_t *)malloc(sizeof(GPUFreeze_t));
+ if (gpufP == NULL) goto cleanup;
SaveFuncs.read(f, gpufP, sizeof(GPUFreeze_t));
GPU_freeze(0, gpufP);
free(gpufP);
// spu
SaveFuncs.read(f, &Size, 4);
spufP = (SPUFreeze_t *)malloc(Size);
+ if (spufP == NULL) goto cleanup;
SaveFuncs.read(f, spufP, Size);
SPU_freeze(0, spufP, psxRegs.cycle);
free(spufP);
mdecFreeze(f, 0);
new_dyna_freeze(f, 0);
+ result = 0;
+cleanup:
SaveFuncs.close(f);
-
- return 0;
+ return result;
}
int CheckState(const char *file) {