(svn r10001) -Codechange: Add support for removing dynamically allocated newgrf data
authorbelugas
Thu, 31 May 2007 15:40:36 +0000
changeset 7261 a75c5668b75b
parent 7260 23cd58b6bf51
child 7262 3a366c244930
(svn r10001) -Codechange: Add support for removing dynamically allocated newgrf data
src/industry.h
src/newgrf.cpp
src/table/build_industry.h
--- a/src/industry.h	Thu May 31 15:15:00 2007 +0000
+++ b/src/industry.h	Thu May 31 15:40:36 2007 +0000
@@ -22,6 +22,11 @@
 	INVALID_INDUSTRYTILE   = NUM_INDUSTRYTILES,          ///< one above amount is considered invalid
 };
 
+enum {
+	CLEAN_RANDOMSOUNDS,    ///< Free the dynamically allocated sounds table
+	CLEAN_TILELSAYOUT,     ///< Free the dynamically allocated tile layout structure
+};
+
 enum IndustryLifeType {
 	INDUSTRYLIFE_NOT_CLOSABLE,     ///< Industry can never close
 	INDUSTRYLIFE_PRODUCTION,       ///< Industry can close and change of production
@@ -135,6 +140,7 @@
 	const uint8 *random_sounds;           ///< array of random sounds.
 	/* Newgrf data */
 	uint16 callback_flags;                ///< Flags telling which grf callback is set
+	uint8 cleanup_flag;                   ///< flags indicating which data should be freed upon cleaning up
 	bool enabled;                         ///< entity still avaible (by default true).newgrf can disable it, though
 	struct GRFFileProps grf_prop;         ///< properties related the the grf file
 };
--- a/src/newgrf.cpp	Thu May 31 15:15:00 2007 +0000
+++ b/src/newgrf.cpp	Thu May 31 15:40:36 2007 +0000
@@ -4326,6 +4326,24 @@
 				IndustrySpec *ind = file->industryspec[i];
 
 				if (ind != NULL) {
+					/* We need to remove the sounds array */
+					if (HASBIT(ind->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
+						free((void*)ind->random_sounds);
+					}
+
+					/* We need to remove the tiles layouts */
+					if (HASBIT(ind->cleanup_flag, CLEAN_TILELSAYOUT) && ind->table != NULL) {
+						for (int j = 0; j < ind->num_table; j++) {
+							/* remove the individual layouts */
+							if (ind->table[j] != NULL) {
+								free((IndustryTileTable*)ind->table[j]);
+							}
+						}
+						/* remove the layouts pointers */
+						free((IndustryTileTable**)ind->table);
+						ind->table = NULL;
+					}
+
 					free(ind);
 					ind = NULL;
 				}
--- a/src/table/build_industry.h	Thu May 31 15:15:00 2007 +0000
+++ b/src/table/build_industry.h	Thu May 31 15:40:36 2007 +0000
@@ -1135,7 +1135,7 @@
            c1, c2, c3, proc, p1, r1, p2, r2, m, a1, im1, a2, im2, a3, im3, pr, clim, bev, in, intx, s1, s2, s3) \
 	 {tbl, lengthof(tbl), d, {c1, c2, c3}, proc, {p1, p2}, {r1, r2}, m,            \
 	 {a1, a2, a3}, {{im1, 0}, {im2, 0}, {im3, 0}}, pr, clim, bev, col, in, intx, s1, s2, s3, {ai1, ai2, ai3, ai4}, {ag1, ag2, ag3, ag4}, \
-	 sndc, snd, 0, true, {0, 0, NULL, NULL, INVALID_INDUSTRYTYPE}}
+	 sndc, snd, 0, 0, true, {0, 0, NULL, NULL, INVALID_INDUSTRYTYPE}}
 	/* Format:
 	   tile table                              count and sounds table
 	   cost multiplier                         appear chances(4ingame, 4random)  map colour