summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2010-02-25 20:06:11 +0000
committeryexo <yexo@openttd.org>2010-02-25 20:06:11 +0000
commit7ff55502f04f9f75b01879ea04518c14d4706046 (patch)
treef48f911cf9861a0053bd35b92bf1e0ece0a5bf58
parenta9c8dbc0a0e1f1fc4434cf0c83ff658ab8d9e3d9 (diff)
downloadopenttd-7ff55502f04f9f75b01879ea04518c14d4706046.tar.xz
(svn r19256) -Codechange: use a constructor/destructor for GRFConfig to make sure all members are properly initialized
-rw-r--r--src/gfxinit.cpp5
-rw-r--r--src/network/core/udp.cpp2
-rw-r--r--src/newgrf_config.cpp40
-rw-r--r--src/newgrf_config.h6
-rw-r--r--src/saveload/newgrf_sl.cpp2
-rw-r--r--src/saveload/oldloader_sl.cpp3
-rw-r--r--src/settings.cpp7
7 files changed, 32 insertions, 33 deletions
diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp
index f2678ae09..6ba87eee1 100644
--- a/src/gfxinit.cpp
+++ b/src/gfxinit.cpp
@@ -178,8 +178,7 @@ static void LoadSpriteTables()
* 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 @@ static void LoadSpriteTables()
LoadNewGRF(SPR_NEWGRFS_BASE, i);
/* Free and remove the top element. */
- ClearGRFConfig(&master);
+ delete master;
_grfconfig = top;
}
diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp
index 4bf0c1e30..e56c839d0 100644
--- a/src/network/core/udp.cpp
+++ b/src/network/core/udp.cpp
@@ -238,7 +238,7 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i
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);
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp
index 31e51d9af..a942279c8 100644
--- 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 @@ bool FillGRFDetails(GRFConfig *config, bool is_static)
}
-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 @@ void ClearGRFConfigList(GRFConfig **config)
*/
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 @@ static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list)
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 @@ public:
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 @@ bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
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;
diff --git a/src/newgrf_config.h b/src/newgrf_config.h
index ec587cc1e..9683340dd 100644
--- a/src/newgrf_config.h
+++ b/src/newgrf_config.h
@@ -69,7 +69,10 @@ struct GRFError : ZeroedMemoryAllocator {
};
/** 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 *GetGRFConfig(uint32 grfid, uint32 mask = 0xFFFFFFFF);
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();
diff --git a/src/saveload/newgrf_sl.cpp b/src/saveload/newgrf_sl.cpp
index 9209be7b8..efeeee923 100644
--- a/src/saveload/newgrf_sl.cpp
+++ b/src/saveload/newgrf_sl.cpp
@@ -44,7 +44,7 @@ static void Load_NGRF()
{
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);
diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp
index 004c98ff6..b2eec5a1a 100644
--- a/src/saveload/oldloader_sl.cpp
+++ b/src/saveload/oldloader_sl.cpp
@@ -1519,9 +1519,8 @@ static bool LoadTTDPatchExtraChunks(LoadgameState *ls, int num)
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));
diff --git a/src/settings.cpp b/src/settings.cpp
index 998cf547f..051a11e11 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1190,8 +1190,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
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 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
}
ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
- ClearGRFConfig(&c);
+ delete c;
continue;
}
@@ -1231,7 +1230,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
}
}
if (duplicate) {
- ClearGRFConfig(&c);
+ delete c;
continue;
}