Skip to content

Commit

Permalink
do not heap allocate authkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
no-lex committed Dec 17, 2024
1 parent b60005c commit 187ee33
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions game/gameclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,22 @@ namespace game
delete[] desc;
}
};
static std::vector<authkey *> authkeys;
static std::vector<authkey> authkeys;

authkey *findauthkey(const char *desc = "")
{
for(uint i = 0; i < authkeys.size(); i++)
for(authkey &a : authkeys)
{
if(!strcmp(authkeys[i]->desc, desc) && !strcasecmp(authkeys[i]->name, player1->name))
if(!strcmp(a.desc, desc) && !strcasecmp(a.name, player1->name))
{
return authkeys[i];
return &a;
}
}
for(uint i = 0; i < authkeys.size(); i++)
for(authkey &a : authkeys)
{
if(!strcmp(authkeys[i]->desc, desc))
if(!strcmp(a.desc, desc))
{
return authkeys[i];
return &a;
}
}
return nullptr;
Expand All @@ -190,15 +190,14 @@ namespace game
{
for(int i = static_cast<int>(authkeys.size()); --i >=0;) //note reverse iteration
{
if(!strcmp(authkeys[i]->desc, desc) && !strcmp(authkeys[i]->name, name))
if(!strcmp(authkeys[i].desc, desc) && !strcmp(authkeys[i].name, name))
{
delete authkeys[i];
authkeys.erase(authkeys.begin() + i);
}
}
if(name[0] && key[0])
{
authkeys.push_back(new authkey(name, key, desc));
authkeys.emplace_back(name, key, desc);
}
}
bool _icmd_authkey = addcommand("authkey", reinterpret_cast<identfun>(+[] (char *name, char *key, char *desc) { addauthkey(name, key, desc); }), "sss", Id_Command);
Expand All @@ -211,7 +210,7 @@ namespace game
}
for(int i = static_cast<int>(authkeys.size()); --i >=0;) //note reverse iteration
{
if(!strcmp(authkeys[i]->desc, desc) && !strcmp(authkeys[i]->name, name))
if(!strcmp(authkeys[i].desc, desc) && !strcmp(authkeys[i].name, name))
{
return true;
}
Expand Down Expand Up @@ -270,10 +269,9 @@ namespace game
conoutf(Console_Error, "failed to open %s for writing", fname);
return;
}
for(uint i = 0; i < authkeys.size(); i++)
for(const authkey &a : authkeys)
{
authkey *a = authkeys[i];
f->printf("authkey %s %s %s\n", escapestring(a->name), escapestring(a->key), escapestring(a->desc));
f->printf("authkey %s %s %s\n", escapestring(a.name), escapestring(a.key), escapestring(a.desc));
}
conoutf("saved authkeys to %s", fname);
delete f;
Expand Down

0 comments on commit 187ee33

Please sign in to comment.