all repos — mgba @ ea5e73e44ace6ccfc39d2d1fa61544dd61882510

mGBA Game Boy Advance Emulator

GBA: Clean up savestate code a bit
Jeffrey Pfau jeffrey@endrift.com
Wed, 02 Sep 2015 22:36:24 -0700
commit

ea5e73e44ace6ccfc39d2d1fa61544dd61882510

parent

1582a25ca9559eed9cb102523751ec6cf6a5b17e

1 files changed, 13 insertions(+), 13 deletions(-)

jump to
M src/gba/serialize.csrc/gba/serialize.c

@@ -206,27 +206,27 @@ struct GBASerializedState* state = GBAAllocateState();

if (!state) { return false; } - png_structp png = PNGWriteOpen(vf); - png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS); - if (!png || !info) { - PNGWriteClose(png, info); - GBADeallocateState(state); - return false; - } + GBASerialize(gba, state); uLongf len = compressBound(sizeof(*state)); void* buffer = malloc(len); if (!buffer) { - PNGWriteClose(png, info); GBADeallocateState(state); return false; } - GBASerialize(gba, state); compress(buffer, &len, (const Bytef*) state, sizeof(*state)); + GBADeallocateState(state); + + png_structp png = PNGWriteOpen(vf); + png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS); + if (!png || !info) { + PNGWriteClose(png, info); + free(buffer); + return false; + } PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels); PNGWriteCustomChunk(png, "gbAs", len, buffer); PNGWriteClose(png, info); free(buffer); - GBADeallocateState(state); return true; }

@@ -234,14 +234,14 @@ static int _loadPNGChunkHandler(png_structp png, png_unknown_chunkp chunk) {

if (strcmp((const char*) chunk->name, "gbAs") != 0) { return 0; } - struct GBASerializedState* state = malloc(sizeof(*state)); + struct GBASerializedState* state = GBAAllocateState(); uLongf len = sizeof(*state); uncompress((Bytef*) state, &len, chunk->data, chunk->size); if (!GBADeserialize(png_get_user_chunk_ptr(png), state)) { - free(state); + GBADeallocateState(state); longjmp(png_jmpbuf(png), 1); } - free(state); + GBADeallocateState(state); return 1; }