Skip to content

Commit

Permalink
Codechange: Switch remaining uses of AutoFreeSmallVector to std::vector.
Browse files Browse the repository at this point in the history
  • Loading branch information
michicc committed Mar 31, 2019
1 parent 2e7ce36 commit d5b5385
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 96 deletions.
9 changes: 4 additions & 5 deletions src/console_cmds.cpp
Expand Up @@ -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. */
Expand All @@ -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.");
Expand All @@ -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;
Expand Down
32 changes: 2 additions & 30 deletions src/core/smallvec_type.hpp
Expand Up @@ -17,6 +17,7 @@
#include <vector>
#include <algorithm>
#include <memory>
#include <string>

/**
* Helper function to append an item to a vector if it is not already contained
Expand Down Expand Up @@ -70,36 +71,7 @@ T* grow(std::vector<T>& 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 <typename T>
class AutoFreeSmallVector : public std::vector<T> {
public:
~AutoFreeSmallVector()
{
this->Clear();
}

/**
* Remove all items from the list.
*/
inline void Clear()
{
for (T p : *this) {
free(p);
}

std::vector<T>::clear();
}
};

typedef AutoFreeSmallVector<char*> StringList; ///< Type for a list of strings.
typedef std::vector<std::string> StringList; ///< Type for a list of strings.

#endif /* SMALLVEC_TYPE_HPP */
35 changes: 16 additions & 19 deletions src/game/game_text.cpp
Expand Up @@ -111,7 +111,7 @@ std::unique_ptr<LanguageStrings> 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;
Expand All @@ -129,8 +129,8 @@ std::unique_ptr<LanguageStrings> 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.
Expand All @@ -140,15 +140,15 @@ 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())
{
}

char *ReadLine(char *buffer, const char *last) override
{
if (this->p == this->end) return NULL;

strecpy(buffer, *this->p, last);
strecpy(buffer, this->p->c_str(), last);
this->p++;

return buffer;
Expand All @@ -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)
{
}

Expand All @@ -184,28 +184,25 @@ struct TranslationWriter : LanguageWriter {

void Write(const byte *buffer, size_t length)
{
char *dest = MallocT<char>(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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand All @@ -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();
}

/**
Expand All @@ -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++;
Expand Down
11 changes: 5 additions & 6 deletions src/network/core/address.cpp
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/network/core/address.h
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/network/core/tcp_listen.h
Expand Up @@ -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());
Expand Down
8 changes: 4 additions & 4 deletions src/network/network.cpp
Expand Up @@ -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. */
Expand All @@ -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));
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/network/network_client.cpp
Expand Up @@ -41,7 +41,7 @@
struct PacketReader : LoadFilter {
static const size_t CHUNK = 32 * 1024; ///< 32 KiB chunks of memory.

AutoFreeSmallVector<byte *> blocks; ///< Buffer with blocks of allocated memory.
std::vector<byte *> 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.
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/network/network_gui.cpp
Expand Up @@ -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());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/network/network_server.cpp
Expand Up @@ -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;
Expand All @@ -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<char *>(ip))) {
if (cs->client_address.IsInNetmask(ip)) {
NetworkServerKickClient(cs->client_id);
n++;
}
Expand Down
4 changes: 2 additions & 2 deletions src/openttd.cpp
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions src/saveload/game_sl.cpp
Expand Up @@ -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);
}
}
Expand All @@ -153,7 +153,7 @@ static void Load_GSTR()
std::unique_ptr<LanguageStrings> 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));
Expand Down

0 comments on commit d5b5385

Please sign in to comment.