saveload.c
changeset 2414 53ecd2131592
parent 2413 4ba8f2b0da2e
child 2418 8599776539b1
--- a/saveload.c	Sun Sep 11 14:17:21 2005 +0000
+++ b/saveload.c	Sun Sep 11 14:20:46 2005 +0000
@@ -71,6 +71,7 @@
 
 	// these 3 may be used by compressor/decompressors.
 	byte *buf;                           /// pointer to temporary memory to read/write, initialized by SaveLoadFormat->initread/write
+	byte *buf_ori;                       /// pointer to the original memory location of buf, used to free it afterwards
 	uint bufsize;                        /// the size of the temporary memory *buf
 	FILE *fh;                            /// the file from which is read or written to
 
@@ -860,13 +861,13 @@
 static bool InitLZO(void)
 {
 	_sl.bufsize = LZO_SIZE;
-	_sl.buf = (byte*)malloc(LZO_SIZE);
+	_sl.buf = _sl.buf_ori = (byte*)malloc(LZO_SIZE);
 	return true;
 }
 
 static void UninitLZO(void)
 {
-	free(_sl.buf);
+	free(_sl.buf_ori);
 }
 
 //*********************************************
@@ -885,13 +886,13 @@
 static bool InitNoComp(void)
 {
 	_sl.bufsize = LZO_SIZE;
-	_sl.buf = (byte*)malloc(LZO_SIZE);
+	_sl.buf = _sl.buf_ori =(byte*)malloc(LZO_SIZE);
 	return true;
 }
 
 static void UninitNoComp(void)
 {
-	free(_sl.buf);
+	free(_sl.buf_ori);
 }
 
 //********************************************
@@ -969,7 +970,7 @@
 	if (inflateInit(&_z) != Z_OK) return false;
 
 	_sl.bufsize = 4096;
-	_sl.buf = (byte*)malloc(4096 + 4096); // also contains fread buffer
+	_sl.buf = _sl.buf_ori = (byte*)malloc(4096 + 4096); // also contains fread buffer
 	return true;
 }
 
@@ -1001,7 +1002,7 @@
 static void UninitReadZlib(void)
 {
 	inflateEnd(&_z);
-	free(_sl.buf);
+	free(_sl.buf_ori);
 }
 
 static bool InitWriteZlib(void)
@@ -1010,7 +1011,7 @@
 	if (deflateInit(&_z, 6) != Z_OK) return false;
 
 	_sl.bufsize = 4096;
-	_sl.buf = (byte*)malloc(4096); // also contains fread buffer
+	_sl.buf = _sl.buf_ori = (byte*)malloc(4096); // also contains fread buffer
 	return true;
 }
 
@@ -1045,7 +1046,7 @@
 	// flush any pending output.
 	if (_sl.fh) WriteZlibLoop(&_z, NULL, 0, Z_FINISH);
 	deflateEnd(&_z);
-	free(_sl.buf);
+	free(_sl.buf_ori);
 }
 
 #endif /* WITH_ZLIB */
@@ -1291,20 +1292,14 @@
 static void* SaveFileToDisk(void *arg)
 {
 	const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format);
-	/* XXX - backup _sl.buf cause it is used internally by the writer
-	 * and we update it for our own purposes */
-	static byte *tmp = NULL;
 	uint32 hdr[2];
 
 	if (arg != NULL) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_START);
 
-	tmp = _sl.buf;
-
 	/* XXX - Setup setjmp error handler if an error occurs anywhere deep during
 	 * loading/saving execute a longjmp() and continue execution here */
 	if (setjmp(_sl.excpt)) {
 		AbortSaveLoad();
-		_sl.buf = tmp;
 		_sl.excpt_uninit();
 
 		fprintf(stderr, "Save game failed: %s.", _sl.excpt_msg);
@@ -1319,7 +1314,6 @@
 	if (fwrite(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError("file write failed");
 
 	if (!fmt->init_write()) SlError("cannot initialize compressor");
-	tmp = _sl.buf; // XXX - init_write can change _sl.buf, so update it
 
 	{
 		uint i;
@@ -1335,8 +1329,6 @@
 		 * as much data as it is in there */
 		_sl.buf = _ts.save->blocks[i];
 		fmt->writer(_ts.count - (i * count));
-
-		_sl.buf = tmp; // XXX - reset _sl.buf to its original value to let it continue its internal usage
 	}
 
 	fmt->uninit_write();