From daf89297fea3a7cd3972199fbdc5e46c1c830e66 Mon Sep 17 00:00:00 2001 From: yexo Date: Thu, 25 Feb 2010 20:06:11 +0000 Subject: [PATCH] (svn r19256) -Codechange: use a constructor/destructor for GRFConfig to make sure all members are properly initialized --- src/gfxinit.cpp | 5 ++--- src/network/core/udp.cpp | 2 +- src/newgrf_config.cpp | 40 +++++++++++++++++------------------ src/newgrf_config.h | 6 ++++-- src/saveload/newgrf_sl.cpp | 2 +- src/saveload/oldloader_sl.cpp | 3 +-- src/settings.cpp | 7 +++--- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index f2678ae093..6ba87eee1f 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(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 4bf0c1e30b..e56c839d03 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(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 31e51d9af2..a942279c8d 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(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(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 ec587cc1ee..9683340dd5 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 9209be7b85..efeeee9236 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(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 004c98ff67..b2eec5a1a7 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(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 998cf547f1..051a11e11a 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(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; }