From ea5e73e44ace6ccfc39d2d1fa61544dd61882510 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 2 Sep 2015 22:36:24 -0700 Subject: [PATCH] GBA: Clean up savestate code a bit --- src/gba/serialize.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/gba/serialize.c b/src/gba/serialize.c index d04b79a08..8abf2b665 100644 --- a/src/gba/serialize.c +++ b/src/gba/serialize.c @@ -206,27 +206,27 @@ static bool _savePNGState(struct GBA* gba, struct VFile* vf) { if (!state) { return false; } + GBASerialize(gba, state); + uLongf len = compressBound(sizeof(*state)); + void* buffer = malloc(len); + if (!buffer) { + GBADeallocateState(state); + return false; + } + 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); - GBADeallocateState(state); + free(buffer); return false; } - 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)); 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; }