From 7535eb65e298ab4a2e4173c5ed7cad568b105e35 Mon Sep 17 00:00:00 2001 From: PeterN Date: Wed, 26 Apr 2023 06:45:11 +0100 Subject: [PATCH] Codechange: Use vector instead of mallloc/free for Action 6 data. (#10713) --- src/newgrf.cpp | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 83067fd57f..4df75d940e 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -378,7 +378,7 @@ struct GRFLocation { }; static std::map _grm_sprites; -typedef std::map GRFLineToSpriteOverride; +typedef std::map> GRFLineToSpriteOverride; static GRFLineToSpriteOverride _grf_line_to_action6_sprite_override; /** @@ -6690,34 +6690,30 @@ static void CfgApply(ByteReader *buf) size_t pos = file.GetPos(); uint32 num = file.GetContainerVersion() >= 2 ? file.ReadDword() : file.ReadWord(); uint8 type = file.ReadByte(); - byte *preload_sprite = nullptr; /* Check if the sprite is a pseudo sprite. We can't operate on real sprites. */ - if (type == 0xFF) { - preload_sprite = MallocT(num); - file.ReadBlock(preload_sprite, num); + if (type != 0xFF) { + GrfMsg(2, "CfgApply: Ignoring (next sprite is real, unsupported)"); + + /* Reset the file position to the start of the next sprite */ + file.SeekTo(pos, SEEK_SET); + return; + } + + /* Get (or create) the override for the next sprite. */ + GRFLocation location(_cur.grfconfig->ident.grfid, _cur.nfo_line + 1); + std::vector &preload_sprite = _grf_line_to_action6_sprite_override[location]; + + /* Load new sprite data if it hasn't already been loaded. */ + if (preload_sprite.empty()) { + preload_sprite.resize(num); + file.ReadBlock(preload_sprite.data(), num); } /* Reset the file position to the start of the next sprite */ file.SeekTo(pos, SEEK_SET); - if (type != 0xFF) { - GrfMsg(2, "CfgApply: Ignoring (next sprite is real, unsupported)"); - free(preload_sprite); - return; - } - - GRFLocation location(_cur.grfconfig->ident.grfid, _cur.nfo_line + 1); - GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location); - if (it != _grf_line_to_action6_sprite_override.end()) { - free(preload_sprite); - preload_sprite = _grf_line_to_action6_sprite_override[location]; - } else { - _grf_line_to_action6_sprite_override[location] = preload_sprite; - } - /* Now perform the Action 0x06 on our data. */ - for (;;) { uint i; uint param_num; @@ -9484,7 +9480,7 @@ static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage) _cur.file->ReadBlock(buf, num); } else { /* Use the preloaded sprite data. */ - buf = _grf_line_to_action6_sprite_override[location]; + buf = _grf_line_to_action6_sprite_override[location].data(); GrfMsg(7, "DecodeSpecialSprite: Using preloaded pseudo sprite data"); /* Skip the real (original) content of this action. */ @@ -9820,10 +9816,7 @@ static void AfterLoadGRFs() } _string_to_grf_mapping.clear(); - /* Free the action 6 override sprites. */ - for (GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.begin(); it != _grf_line_to_action6_sprite_override.end(); it++) { - free((*it).second); - } + /* Clear the action 6 override sprites. */ _grf_line_to_action6_sprite_override.clear(); /* Polish cargoes */