Optimize localized string allocation

This commit is contained in:
momo5502 2016-07-22 12:52:12 +02:00
parent cc74cf4d8d
commit a343c5ac5b
8 changed files with 90 additions and 68 deletions

View File

@ -3,6 +3,7 @@
namespace Components namespace Components
{ {
Dvar::Var Localization::UseLocalization; Dvar::Var Localization::UseLocalization;
Utils::Memory::Allocator Localization::MemAllocator;
std::map<std::string, Game::LocalizedEntry*> Localization::LocalizeMap; std::map<std::string, Game::LocalizedEntry*> Localization::LocalizeMap;
std::map<std::string, Game::LocalizedEntry*> Localization::TempLocalizeMap; std::map<std::string, Game::LocalizedEntry*> Localization::TempLocalizeMap;
@ -12,28 +13,28 @@ namespace Components
{ {
Game::LocalizedEntry* entry = Localization::LocalizeMap[key]; Game::LocalizedEntry* entry = Localization::LocalizeMap[key];
char* newStaticValue = Utils::Memory::DuplicateString(value); char* newStaticValue = Localization::MemAllocator.DuplicateString(value);
if (!newStaticValue) return; if (!newStaticValue) return;
if (entry->value) Utils::Memory::Free(entry->value); if (entry->value) Localization::MemAllocator.Free(entry->value);
entry->value = newStaticValue; entry->value = newStaticValue;
return; return;
} }
Game::LocalizedEntry* entry = Utils::Memory::Allocate<Game::LocalizedEntry>(); Game::LocalizedEntry* entry = Localization::MemAllocator.Allocate<Game::LocalizedEntry>();
if (!entry) return; if (!entry) return;
entry->name = Utils::Memory::DuplicateString(key); entry->name = Localization::MemAllocator.DuplicateString(key);
if (!entry->name) if (!entry->name)
{ {
Utils::Memory::Free(entry); Localization::MemAllocator.Free(entry);
return; return;
} }
entry->value = Utils::Memory::DuplicateString(value); entry->value = Localization::MemAllocator.DuplicateString(value);
if (!entry->value) if (!entry->value)
{ {
Utils::Memory::Free(entry->name); Localization::MemAllocator.Free(entry->name);
Utils::Memory::Free(entry); Localization::MemAllocator.Free(entry);
return; return;
} }
@ -70,26 +71,26 @@ namespace Components
if (Localization::TempLocalizeMap.find(key) != Localization::TempLocalizeMap.end()) if (Localization::TempLocalizeMap.find(key) != Localization::TempLocalizeMap.end())
{ {
Game::LocalizedEntry* entry = Localization::TempLocalizeMap[key]; Game::LocalizedEntry* entry = Localization::TempLocalizeMap[key];
if(entry->value) Utils::Memory::Free(entry->value); if(entry->value) Localization::MemAllocator.Free(entry->value);
entry->value = Utils::Memory::DuplicateString(value); entry->value = Localization::MemAllocator.DuplicateString(value);
} }
else else
{ {
Game::LocalizedEntry* entry = Utils::Memory::Allocate<Game::LocalizedEntry>(); Game::LocalizedEntry* entry = Localization::MemAllocator.Allocate<Game::LocalizedEntry>();
if (!entry) return; if (!entry) return;
entry->name = Utils::Memory::DuplicateString(key); entry->name = Localization::MemAllocator.DuplicateString(key);
if (!entry->name) if (!entry->name)
{ {
Utils::Memory::Free(entry); Localization::MemAllocator.Free(entry);
return; return;
} }
entry->value = Utils::Memory::DuplicateString(value); entry->value = Localization::MemAllocator.DuplicateString(value);
if (!entry->value) if (!entry->value)
{ {
Utils::Memory::Free(entry->name); Localization::MemAllocator.Free(entry->name);
Utils::Memory::Free(entry); Localization::MemAllocator.Free(entry);
return; return;
} }
@ -103,9 +104,9 @@ namespace Components
{ {
if (i->second) if (i->second)
{ {
if (i->second->name) Utils::Memory::Free(i->second->name); if (i->second->name) Localization::MemAllocator.Free(i->second->name);
if (i->second->value) Utils::Memory::Free(i->second->value); if (i->second->value) Localization::MemAllocator.Free(i->second->value);
Utils::Memory::Free(i->second); Localization::MemAllocator.Free(i->second);
} }
} }
@ -186,16 +187,7 @@ namespace Components
{ {
Localization::ClearTemp(); Localization::ClearTemp();
for (auto i = Localization::LocalizeMap.begin(); i != Localization::LocalizeMap.end(); ++i)
{
if (i->second)
{
if (i->second->name) Utils::Memory::Free(i->second->name);
if (i->second->value) Utils::Memory::Free(i->second->value);
Utils::Memory::Free(i->second);
}
}
Localization::LocalizeMap.clear(); Localization::LocalizeMap.clear();
Localization::MemAllocator.Clear();
} }
} }

View File

@ -14,6 +14,7 @@ namespace Components
static void ClearTemp(); static void ClearTemp();
private: private:
static Utils::Memory::Allocator MemAllocator;
static std::map<std::string, Game::LocalizedEntry*> LocalizeMap; static std::map<std::string, Game::LocalizedEntry*> LocalizeMap;
static std::map<std::string, Game::LocalizedEntry*> TempLocalizeMap; static std::map<std::string, Game::LocalizedEntry*> TempLocalizeMap;
static Dvar::Var UseLocalization; static Dvar::Var UseLocalization;

View File

@ -52,18 +52,7 @@ namespace Components
{ {
if (ModList::CurrentMod < ModList::Mods.size()) if (ModList::CurrentMod < ModList::Mods.size())
{ {
auto fsGame = Dvar::Var("fs_game"); ModList::RunMod(ModList::Mods[ModList::CurrentMod]);
fsGame.Set(fmt::sprintf("mods/%s", ModList::Mods[ModList::CurrentMod].data()));
fsGame.Get<Game::dvar_t*>()->pad2[0] = 1;
if (Dvar::Var("cl_modVidRestart").Get<bool>())
{
Command::Execute("vid_restart", false);
}
else
{
Command::Execute("closemenu mods_menu", false);
}
} }
} }
@ -83,6 +72,22 @@ namespace Components
} }
} }
void ModList::RunMod(std::string mod)
{
auto fsGame = Dvar::Var("fs_game");
fsGame.Set(fmt::sprintf("mods/%s", mod.data()));
fsGame.Get<Game::dvar_t*>()->pad2[0] = 1;
if (Dvar::Var("cl_modVidRestart").Get<bool>())
{
Command::Execute("vid_restart", false);
}
else
{
Command::Execute("closemenu mods_menu", false);
}
}
ModList::ModList() ModList::ModList()
{ {
ModList::CurrentMod = 0; ModList::CurrentMod = 0;

View File

@ -7,6 +7,8 @@ namespace Components
~ModList(); ~ModList();
const char* GetName() { return "ModList"; }; const char* GetName() { return "ModList"; };
static void RunMod(std::string mod);
private: private:
static std::vector<std::string> Mods; static std::vector<std::string> Mods;
static unsigned int CurrentMod; static unsigned int CurrentMod;

View File

@ -87,6 +87,6 @@ namespace Components
StringTable::~StringTable() StringTable::~StringTable()
{ {
StringTable::StringTableMap.clear(); StringTable::StringTableMap.clear();
StringTable::MemAllocator.Free(); StringTable::MemAllocator.Clear();
} }
} }

View File

@ -208,6 +208,6 @@ namespace Components
StructuredData::~StructuredData() StructuredData::~StructuredData()
{ {
StructuredData::MemAllocator.Free(); StructuredData::MemAllocator.Clear();
} }
} }

View File

@ -15,10 +15,10 @@ namespace Utils
} }
~Allocator() ~Allocator()
{ {
this->Free(); this->Clear();
} }
void Free() void Clear()
{ {
for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i) for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i)
{ {
@ -38,6 +38,28 @@ namespace Utils
this->Pool.clear(); this->Pool.clear();
} }
void Free(void* data)
{
auto i = this->RefMemory.find(data);
if (i != this->RefMemory.end())
{
i->second(i->first);
this->RefMemory.erase(i);
}
auto j = std::find(this->Pool.begin(), this->Pool.end(), data);
if (j != this->Pool.end())
{
Memory::Free(data);
this->Pool.erase(j);
}
}
void Free(const void* data)
{
this->Free(const_cast<void*>(data));
}
void Reference(void* memory, FreeCallback callback) void Reference(void* memory, FreeCallback callback)
{ {
this->RefMemory[memory] = callback; this->RefMemory[memory] = callback;