# HG changeset patch # User yexo # Date 2010-02-25 20:06:11 # Node ID a7ee51a0cfe3e6d2d6ef3e29ea21fa9036db1eff # Parent 896110212413473869558fc9bf2039de36714ad7 (svn r19256) -Codechange: use a constructor/destructor for GRFConfig to make sure all members are properly initialized diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp --- 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 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -238,7 +238,7 @@ void NetworkUDPSocketHandler::Recv_Netwo 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 --- 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, b } -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 **conf */ 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 RemoveDuplicatesFromGRFConfi 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 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 --- 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, ui 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 --- 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 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1519,9 +1519,8 @@ static bool LoadTTDPatchExtraChunks(Load 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 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1190,8 +1190,7 @@ static GRFConfig *GRFLoadConfig(IniFile 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 } ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg); - ClearGRFConfig(&c); + delete c; continue; } @@ -1231,7 +1230,7 @@ static GRFConfig *GRFLoadConfig(IniFile } } if (duplicate) { - ClearGRFConfig(&c); + delete c; continue; }