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

View File

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

View File

@ -566,7 +566,7 @@ namespace Components
if (originalConnect == menu) // Check if we draw the original loadscreen
{
if (Menus::MenuList.find("connect") != Menus::MenuList.end()) // Check if we have a custom loadscreen, to prevent drawing the original one ontop
if (Menus::MenuList.find("connect") != Menus::MenuList.end()) // Check if we have a custom loadscreen, to prevent drawing the original one on top
{
return false;
}

View File

@ -52,18 +52,7 @@ namespace Components
{
if (ModList::CurrentMod < ModList::Mods.size())
{
auto fsGame = Dvar::Var("fs_game");
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);
}
ModList::RunMod(ModList::Mods[ModList::CurrentMod]);
}
}
@ -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::CurrentMod = 0;

View File

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

View File

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

View File

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

View File

@ -15,10 +15,10 @@ namespace Utils
}
~Allocator()
{
this->Free();
this->Clear();
}
void Free()
void Clear()
{
for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i)
{
@ -38,6 +38,28 @@ namespace Utils
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)
{
this->RefMemory[memory] = callback;