changeset 14677:96bf81ef9c9e draft

(svn r19256) -Codechange: use a constructor/destructor for GRFConfig to make sure all members are properly initialized
author yexo <yexo@openttd.org>
date Thu, 25 Feb 2010 20:06:11 +0000
parents 353df593bee0
children 68f4a5f222ef
files src/gfxinit.cpp src/network/core/udp.cpp src/newgrf_config.cpp src/newgrf_config.h src/saveload/newgrf_sl.cpp src/saveload/oldloader_sl.cpp src/settings.cpp
diffstat 7 files changed, 32 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/gfxinit.cpp
+++ b/src/gfxinit.cpp
@@ -178,8 +178,7 @@
 	 * so we have to manually add it, and then remove it later.
 	 */
 	GRFConfig *top = _grfconfig;
-	GRFConfig *master = CallocT<GRFConfig>(1);
-	master->filename = strdup(used_set->files[GFT_EXTRA].filename);
+	GRFConfig *master = new GRFConfig(used_set->files[GFT_EXTRA].filename);
 	FillGRFDetails(master, false);
 	master->windows_paletted = (used_set->palette == PAL_WINDOWS);
 	ClrBit(master->flags, GCF_INIT_ONLY);
@@ -189,7 +188,7 @@
 	LoadNewGRF(SPR_NEWGRFS_BASE, i);
 
 	/* Free and remove the top element. */
-	ClearGRFConfig(&master);
+	delete master;
 	_grfconfig = top;
 }
 
--- a/src/network/core/udp.cpp
+++ b/src/network/core/udp.cpp
@@ -238,7 +238,7 @@
 			if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
 
 			for (i = 0; i < num_grfs; i++) {
-				GRFConfig *c = CallocT<GRFConfig>(1);
+				GRFConfig *c = new GRFConfig();
 				this->Recv_GRFIdentifier(p, &c->ident);
 				this->HandleIncomingNetworkGameInfoGRFConfig(c);
 
--- a/src/newgrf_config.cpp
+++ b/src/newgrf_config.cpp
@@ -20,6 +20,21 @@
 #include "fileio_func.h"
 #include "fios.h"
 
+GRFConfig::GRFConfig(const char *filename)
+{
+	if (filename != NULL) this->filename = strdup(filename);
+}
+
+GRFConfig::~GRFConfig()
+{
+	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
+	if (!HasBit(this->flags, GCF_COPY)) {
+		free(this->filename);
+		free(this->name);
+		free(this->info);
+		delete this->error;
+	}
+}
 
 GRFConfig *_all_grfs;
 GRFConfig *_grfconfig;
@@ -105,27 +120,13 @@
 }
 
 
-void ClearGRFConfig(GRFConfig **config)
-{
-	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
-	if (!HasBit((*config)->flags, GCF_COPY)) {
-		free((*config)->filename);
-		free((*config)->name);
-		free((*config)->info);
-		delete (*config)->error;
-	}
-	free(*config);
-	*config = NULL;
-}
-
-
 /* Clear a GRF Config list */
 void ClearGRFConfigList(GRFConfig **config)
 {
 	GRFConfig *c, *next;
 	for (c = *config; c != NULL; c = next) {
 		next = c->next;
-		ClearGRFConfig(&c);
+		delete c;
 	}
 	*config = NULL;
 }
@@ -138,7 +139,7 @@
  */
 GRFConfig *DuplicateGRFConfig(const GRFConfig *c)
 {
-	GRFConfig *config = MallocT<GRFConfig>(1);
+	GRFConfig *config = new GRFConfig();
 	*config = *c;
 
 	if (c->filename != NULL) config->filename = strdup(c->filename);
@@ -203,7 +204,7 @@
 		if (cur->ident.grfid != list->ident.grfid) continue;
 
 		prev->next = cur->next;
-		ClearGRFConfig(&cur);
+		delete cur;
 		cur = prev; // Just go back one so it continues as normal later on
 	}
 
@@ -320,8 +321,7 @@
 
 bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
 {
-	GRFConfig *c = CallocT<GRFConfig>(1);
-	c->filename = strdup(filename + basepath_length);
+	GRFConfig *c = new GRFConfig(filename + basepath_length);
 
 	bool added = true;
 	if (FillGRFDetails(c, false)) {
@@ -355,7 +355,7 @@
 	if (!added) {
 		/* File couldn't be opened, or is either not a NewGRF or is a
 		 * 'system' NewGRF or it's already known, so forget about it. */
-		ClearGRFConfig(&c);
+		delete c;
 	}
 
 	return added;
--- a/src/newgrf_config.h
+++ b/src/newgrf_config.h
@@ -69,7 +69,10 @@
 };
 
 /** Information about GRF, used in the game and (part of it) in savegames */
-struct GRFConfig {
+struct GRFConfig : ZeroedMemoryAllocator {
+	GRFConfig(const char *filename = NULL);
+	~GRFConfig();
+
 	GRFIdentifier ident; ///< grfid and md5sum to uniquely identify newgrfs
 	char *filename;     ///< Filename - either with or without full path
 	char *name;         ///< NOSAVE: GRF name (Action 0x08)
@@ -99,7 +102,6 @@
 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only);
 void AppendStaticGRFConfigs(GRFConfig **dst);
 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el);
-void ClearGRFConfig(GRFConfig **config);
 void ClearGRFConfigList(GRFConfig **config);
 void ResetGRFConfig(bool defaults);
 GRFListCompatibility IsGoodGRFConfigList();
--- a/src/saveload/newgrf_sl.cpp
+++ b/src/saveload/newgrf_sl.cpp
@@ -44,7 +44,7 @@
 {
 	ClearGRFConfigList(&_grfconfig);
 	while (SlIterateArray() != -1) {
-		GRFConfig *c = CallocT<GRFConfig>(1);
+		GRFConfig *c = new GRFConfig();
 		SlObject(c, _grfconfig_desc);
 		if (CheckSavegameVersion(101)) c->windows_paletted = (_use_palette == PAL_WINDOWS);
 		AppendToGRFConfigList(&_grfconfig, c);
--- a/src/saveload/oldloader_sl.cpp
+++ b/src/saveload/oldloader_sl.cpp
@@ -1519,9 +1519,8 @@
 					uint32 grfid = ReadUint32(ls);
 
 					if (ReadByte(ls) == 1) {
-						GRFConfig *c = CallocT<GRFConfig>(1);
+						GRFConfig *c = new GRFConfig("TTDP game, no information");
 						c->ident.grfid = grfid;
-						c->filename = strdup("TTDP game, no information");
 
 						AppendToGRFConfigList(&_grfconfig, c);
 						DEBUG(oldloader, 3, "TTDPatch game using GRF file with GRFID %0X", BSWAP32(c->ident.grfid));
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1190,8 +1190,7 @@
 	if (group == NULL) return NULL;
 
 	for (item = group->item; item != NULL; item = item->next) {
-		GRFConfig *c = CallocT<GRFConfig>(1);
-		c->filename = strdup(item->name);
+		GRFConfig *c = new GRFConfig(item->name);
 
 		/* Parse parameters */
 		if (!StrEmpty(item->value)) {
@@ -1217,7 +1216,7 @@
 			}
 
 			ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
-			ClearGRFConfig(&c);
+			delete c;
 			continue;
 		}
 
@@ -1231,7 +1230,7 @@
 			}
 		}
 		if (duplicate) {
-			ClearGRFConfig(&c);
+			delete c;
 			continue;
 		}