From d5b5385235d4d9ffc7b4b5c52c88330071793238 Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Sun, 31 Mar 2019 23:38:10 +0200 Subject: [PATCH] Codechange: Switch remaining uses of AutoFreeSmallVector to std::vector. --- src/console_cmds.cpp | 9 ++++----- src/core/smallvec_type.hpp | 32 ++----------------------------- src/game/game_text.cpp | 35 ++++++++++++++++------------------ src/network/core/address.cpp | 11 +++++------ src/network/core/address.h | 2 +- src/network/core/tcp_listen.h | 6 +++--- src/network/network.cpp | 8 ++++---- src/network/network_client.cpp | 9 ++++++++- src/network/network_gui.cpp | 4 ++-- src/network/network_server.cpp | 8 ++++---- src/openttd.cpp | 4 ++-- src/saveload/game_sl.cpp | 8 ++++---- src/saveload/saveload.cpp | 13 ++++++++++--- src/settings.cpp | 24 +++++++++++------------ 14 files changed, 77 insertions(+), 96 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 1d3e9c52c41f6..fb0e1e262b05e 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -553,7 +553,7 @@ DEF_CONSOLE_CMD(ConUnBan) /* Try by IP. */ uint index; for (index = 0; index < _network_ban_list.size(); index++) { - if (strcmp(_network_ban_list[index], argv[1]) == 0) break; + if (strcmp(_network_ban_list[index].c_str(), argv[1]) == 0) break; } /* Try by index. */ @@ -563,9 +563,8 @@ DEF_CONSOLE_CMD(ConUnBan) if (index < _network_ban_list.size()) { char msg[64]; - seprintf(msg, lastof(msg), "Unbanned %s", _network_ban_list[index]); + seprintf(msg, lastof(msg), "Unbanned %s", _network_ban_list[index].c_str()); IConsolePrint(CC_DEFAULT, msg); - free(_network_ban_list[index]); _network_ban_list.erase(_network_ban_list.begin() + index); } else { IConsolePrint(CC_DEFAULT, "Invalid list index or IP not in ban-list."); @@ -585,8 +584,8 @@ DEF_CONSOLE_CMD(ConBanList) IConsolePrint(CC_DEFAULT, "Banlist: "); uint i = 1; - for (char *entry : _network_ban_list) { - IConsolePrintF(CC_DEFAULT, " %d) %s", i, entry); + for (const auto &entry : _network_ban_list) { + IConsolePrintF(CC_DEFAULT, " %d) %s", i, entry.c_str()); } return true; diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp index cbc857c1c060f..830570b8c7f01 100644 --- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -17,6 +17,7 @@ #include #include #include +#include /** * Helper function to append an item to a vector if it is not already contained @@ -70,36 +71,7 @@ T* grow(std::vector& vec, std::size_t num) return vec.data() + pos; } -/** - * Simple vector template class, with automatic free. - * - * @note There are no asserts in the class so you have - * to care about that you grab an item which is - * inside the list. - * - * @param T The type of the items stored, must be a pointer - */ -template -class AutoFreeSmallVector : public std::vector { -public: - ~AutoFreeSmallVector() - { - this->Clear(); - } - - /** - * Remove all items from the list. - */ - inline void Clear() - { - for (T p : *this) { - free(p); - } - - std::vector::clear(); - } -}; -typedef AutoFreeSmallVector StringList; ///< Type for a list of strings. +typedef std::vector StringList; ///< Type for a list of strings. #endif /* SMALLVEC_TYPE_HPP */ diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp index 26498338c194d..944bf82d1961c 100644 --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -111,7 +111,7 @@ std::unique_ptr ReadRawLanguageStrings(const char *file) while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--; buffer[i] = '\0'; - ret->lines.push_back(stredup(buffer, buffer + to_read - 1)); + ret->lines.emplace_back(buffer, buffer + to_read - 1); if (len > to_read) { to_read = 0; @@ -129,8 +129,8 @@ std::unique_ptr ReadRawLanguageStrings(const char *file) /** A reader that simply reads using fopen. */ struct StringListReader : StringReader { - const char * const *p; ///< The current location of the iteration. - const char * const *end; ///< The end of the iteration. + StringList::const_iterator p; ///< The current location of the iteration. + StringList::const_iterator end; ///< The end of the iteration. /** * Create the reader. @@ -140,7 +140,7 @@ struct StringListReader : StringReader { * @param translation Are we reading a translation? */ StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation) : - StringReader(data, strings.language, master, translation), p(strings.lines.data()), end(p + strings.lines.size()) + StringReader(data, strings.language, master, translation), p(strings.lines.begin()), end(strings.lines.end()) { } @@ -148,7 +148,7 @@ struct StringListReader : StringReader { { if (this->p == this->end) return NULL; - strecpy(buffer, *this->p, last); + strecpy(buffer, this->p->c_str(), last); this->p++; return buffer; @@ -157,13 +157,13 @@ struct StringListReader : StringReader { /** Class for writing an encoded language. */ struct TranslationWriter : LanguageWriter { - StringList *strings; ///< The encoded strings. + StringList &strings; ///< The encoded strings. /** * Writer for the encoded data. * @param strings The string table to add the strings to. */ - TranslationWriter(StringList *strings) : strings(strings) + TranslationWriter(StringList &strings) : strings(strings) { } @@ -184,28 +184,25 @@ struct TranslationWriter : LanguageWriter { void Write(const byte *buffer, size_t length) { - char *dest = MallocT(length + 1); - memcpy(dest, buffer, length); - dest[length] = '\0'; - this->strings->push_back(dest); + this->strings.emplace_back((const char *)buffer, length); } }; /** Class for writing the string IDs. */ struct StringNameWriter : HeaderWriter { - StringList *strings; ///< The string names. + StringList &strings; ///< The string names. /** * Writer for the string names. * @param strings The string table to add the strings to. */ - StringNameWriter(StringList *strings) : strings(strings) + StringNameWriter(StringList &strings) : strings(strings) { } void WriteStringID(const char *name, int stringid) { - if (stringid == (int)this->strings->size()) this->strings->push_back(stredup(name)); + if (stringid == (int)this->strings.size()) this->strings.emplace_back(name); } void Finalise(const StringData &data) @@ -314,7 +311,7 @@ void GameStrings::Compile() this->version = data.Version(); - StringNameWriter id_writer(&this->string_names); + StringNameWriter id_writer(this->string_names); id_writer.WriteHeader(data); for (const auto &p : this->raw_strings) { @@ -324,7 +321,7 @@ void GameStrings::Compile() if (_errors != 0) throw std::exception(); this->compiled_strings.emplace_back(new LanguageStrings(p->language)); - TranslationWriter writer(&this->compiled_strings.back()->lines); + TranslationWriter writer(this->compiled_strings.back()->lines); writer.WriteLang(data); } } @@ -340,7 +337,7 @@ GameStrings *_current_data = NULL; const char *GetGameStringPtr(uint id) { if (id >= _current_data->cur_language->lines.size()) return GetStringPtr(STR_UNDEFINED); - return _current_data->cur_language->lines[id]; + return _current_data->cur_language->lines[id].c_str(); } /** @@ -359,8 +356,8 @@ void RegisterGameTranslation(Squirrel *engine) if (SQ_FAILED(sq_get(vm, -2))) return; int idx = 0; - for (const char * const p : _current_data->string_names) { - sq_pushstring(vm, p, -1); + for (const auto &p : _current_data->string_names) { + sq_pushstring(vm, p.c_str(), -1); sq_pushinteger(vm, idx); sq_rawset(vm, -3); idx++; diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index 9522297768384..11b5d753f3573 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -156,7 +156,7 @@ bool NetworkAddress::IsFamily(int family) * @note netmask without /n assumes all bits need to match. * @return true if this IP is within the netmask. */ -bool NetworkAddress::IsInNetmask(char *netmask) +bool NetworkAddress::IsInNetmask(const char *netmask) { /* Resolve it if we didn't do it already */ if (!this->IsResolved()) this->GetAddress(); @@ -166,17 +166,16 @@ bool NetworkAddress::IsInNetmask(char *netmask) NetworkAddress mask_address; /* Check for CIDR separator */ - char *chr_cidr = strchr(netmask, '/'); + const char *chr_cidr = strchr(netmask, '/'); if (chr_cidr != NULL) { int tmp_cidr = atoi(chr_cidr + 1); /* Invalid CIDR, treat as single host */ if (tmp_cidr > 0 || tmp_cidr < cidr) cidr = tmp_cidr; - /* Remove and then replace the / so that NetworkAddress works on the IP portion */ - *chr_cidr = '\0'; - mask_address = NetworkAddress(netmask, 0, this->address.ss_family); - *chr_cidr = '/'; + /* Remove the / so that NetworkAddress works on the IP portion */ + std::string ip_str(netmask, chr_cidr - netmask); + mask_address = NetworkAddress(ip_str.c_str(), 0, this->address.ss_family); } else { mask_address = NetworkAddress(netmask, 0, this->address.ss_family); } diff --git a/src/network/core/address.h b/src/network/core/address.h index 2f26a3d00f209..bd1bab6769749 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -129,7 +129,7 @@ class NetworkAddress { } bool IsFamily(int family); - bool IsInNetmask(char *netmask); + bool IsInNetmask(const char *netmask); /** * Compare the address of this class with the address of another. diff --git a/src/network/core/tcp_listen.h b/src/network/core/tcp_listen.h index 744f8841fd522..5668e48b29ce2 100644 --- a/src/network/core/tcp_listen.h +++ b/src/network/core/tcp_listen.h @@ -54,13 +54,13 @@ class TCPListenHandler { /* Check if the client is banned */ bool banned = false; - for (char *entry : _network_ban_list) { - banned = address.IsInNetmask(entry); + for (const auto &entry : _network_ban_list) { + banned = address.IsInNetmask(entry.c_str()); if (banned) { Packet p(Tban_packet); p.PrepareToSend(); - DEBUG(net, 1, "[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), entry); + DEBUG(net, 1, "[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), entry.c_str()); if (send(s, (const char*)p.buffer, p.size, 0) < 0) { DEBUG(net, 0, "send failed with error %d", GET_LAST_ERROR()); diff --git a/src/network/network.cpp b/src/network/network.cpp index be92623e41db4..ab3884c56eabe 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -632,8 +632,8 @@ void NetworkAddServer(const char *b) */ void GetBindAddresses(NetworkAddressList *addresses, uint16 port) { - for (char *iter : _network_bind_list) { - addresses->emplace_back(iter, port); + for (const auto &iter : _network_bind_list) { + addresses->emplace_back(iter.c_str(), port); } /* No address, so bind to everything. */ @@ -647,10 +647,10 @@ void GetBindAddresses(NetworkAddressList *addresses, uint16 port) * by the function that generates the config file. */ void NetworkRebuildHostList() { - _network_host_list.Clear(); + _network_host_list.clear(); for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) { - if (item->manually) _network_host_list.push_back(stredup(item->address.GetAddressAsString(false))); + if (item->manually) _network_host_list.emplace_back(item->address.GetAddressAsString(false)); } } diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 7b658e8b5a4ae..4bed201b75b19 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -41,7 +41,7 @@ struct PacketReader : LoadFilter { static const size_t CHUNK = 32 * 1024; ///< 32 KiB chunks of memory. - AutoFreeSmallVector blocks; ///< Buffer with blocks of allocated memory. + std::vector blocks; ///< Buffer with blocks of allocated memory. byte *buf; ///< Buffer we're going to write to/read from. byte *bufe; ///< End of the buffer we write to/read from. byte **block; ///< The block we're reading from/writing to. @@ -53,6 +53,13 @@ struct PacketReader : LoadFilter { { } + ~PacketReader() override + { + for (auto p : this->blocks) { + free(p); + } + } + /** * Add a packet to this buffer. * @param p The packet to add. diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 0fd707676b928..fc5822641d95a 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1045,8 +1045,8 @@ void ShowNetworkGameWindow() if (first) { first = false; /* Add all servers from the config file to our list. */ - for (char *iter : _network_host_list) { - NetworkAddServer(iter); + for (const auto &iter : _network_host_list) { + NetworkAddServer(iter.c_str()); } } diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 36dde0f2cf3e8..3ae5176046a1f 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -2095,13 +2095,13 @@ uint NetworkServerKickOrBanIP(const char *ip, bool ban) /* Add address to ban-list */ if (ban) { bool contains = false; - for (char *iter : _network_ban_list) { - if (strcmp(iter, ip) == 0) { + for (const auto &iter : _network_ban_list) { + if (strcmp(iter.c_str(), ip) == 0) { contains = true; break; } } - if (!contains) _network_ban_list.push_back(stredup(ip)); + if (!contains) _network_ban_list.emplace_back(ip); } uint n = 0; @@ -2110,7 +2110,7 @@ uint NetworkServerKickOrBanIP(const char *ip, bool ban) NetworkClientSocket *cs; FOR_ALL_CLIENT_SOCKETS(cs) { if (cs->client_id == CLIENT_ID_SERVER) continue; - if (cs->client_address.IsInNetmask(const_cast(ip))) { + if (cs->client_address.IsInNetmask(ip)) { NetworkServerKickClient(cs->client_id); n++; } diff --git a/src/openttd.cpp b/src/openttd.cpp index 29319236afc70..e70f8ff352bfc 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -444,8 +444,8 @@ struct AfterNewGRFScan : NewGRFScanCallback { if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed; if (dedicated_host != NULL) { - _network_bind_list.Clear(); - _network_bind_list.push_back(stredup(dedicated_host)); + _network_bind_list.clear(); + _network_bind_list.emplace_back(dedicated_host); } if (dedicated_port != 0) _settings_client.network.server_port = dedicated_port; diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp index 626afb17e975d..52e8ac5215194 100644 --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -129,14 +129,14 @@ static const SaveLoad _game_language_string[] = { SLE_END() }; -static void SaveReal_GSTR(LanguageStrings *ls) +static void SaveReal_GSTR(const LanguageStrings *ls) { _game_saveload_string = ls->language; _game_saveload_strings = (uint)ls->lines.size(); SlObject(NULL, _game_language_header); - for (uint i = 0; i < _game_saveload_strings; i++) { - _game_saveload_string = ls->lines[i]; + for (const auto &i : ls->lines) { + _game_saveload_string = i.c_str(); SlObject(NULL, _game_language_string); } } @@ -153,7 +153,7 @@ static void Load_GSTR() std::unique_ptr ls(new LanguageStrings(_game_saveload_string != NULL ? _game_saveload_string : "")); for (uint i = 0; i < _game_saveload_strings; i++) { SlObject(NULL, _game_language_string); - ls->lines.push_back(stredup(_game_saveload_string != NULL ? _game_saveload_string : "")); + ls->lines.emplace_back(_game_saveload_string != NULL ? _game_saveload_string : ""); } _current_data->raw_strings.push_back(std::move(ls)); diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index c97087224b286..bafc40fb1486d 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -125,15 +125,22 @@ struct ReadBuffer { /** Container for dumping the savegame (quickly) to memory. */ struct MemoryDumper { - AutoFreeSmallVector blocks; ///< Buffer with blocks of allocated memory. - byte *buf; ///< Buffer we're going to write to. - byte *bufe; ///< End of the buffer we write to. + std::vector blocks; ///< Buffer with blocks of allocated memory. + byte *buf; ///< Buffer we're going to write to. + byte *bufe; ///< End of the buffer we write to. /** Initialise our variables. */ MemoryDumper() : buf(NULL), bufe(NULL) { } + ~MemoryDumper() + { + for (auto p : this->blocks) { + free(p); + } + } + /** * Write a single byte into the dumper. * @param b The byte to write. diff --git a/src/settings.cpp b/src/settings.cpp index 60cc0141ad6c6..12b865d798310 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -84,7 +84,7 @@ static ErrorList _settings_error_list; ///< Errors while loading minimal setting typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object); -typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list); +typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList &list); static bool IsSignedVarMemType(VarType vt); @@ -718,16 +718,16 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp * @param grpname character string identifying the section-header of the ini file that will be parsed * @param list new list with entries of the given section */ -static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list) +static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList &list) { IniGroup *group = ini->GetGroup(grpname); - if (group == NULL || list == NULL) return; + if (group == NULL) return; - list->Clear(); + list.clear(); for (const IniItem *item = group->item; item != NULL; item = item->next) { - if (item->name != NULL) list->push_back(stredup(item->name)); + if (item->name != NULL) list.emplace_back(item->name); } } @@ -740,15 +740,15 @@ static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *li * @param list pointer to an string(pointer) array that will be used as the * source to be saved into the relevant ini section */ -static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list) +static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList &list) { IniGroup *group = ini->GetGroup(grpname); - if (group == NULL || list == NULL) return; + if (group == NULL) return; group->Clear(); - for (char *iter : *list) { - group->GetItem(iter, true)->SetValue(""); + for (const auto &iter : list) { + group->GetItem(iter.c_str(), true)->SetValue(""); } } @@ -1699,9 +1699,9 @@ static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescP proc(ini, _currency_settings,"currency", &_custom_currency); proc(ini, _company_settings, "company", &_settings_client.company); - proc_list(ini, "server_bind_addresses", &_network_bind_list); - proc_list(ini, "servers", &_network_host_list); - proc_list(ini, "bans", &_network_ban_list); + proc_list(ini, "server_bind_addresses", _network_bind_list); + proc_list(ini, "servers", _network_host_list); + proc_list(ini, "bans", _network_ban_list); } }