[Memory] Use global memory allocator when possible

This commit is contained in:
momo5502 2017-04-16 11:47:57 +02:00
parent a177e20bf6
commit d11f5dee1f
9 changed files with 70 additions and 62 deletions

View File

@ -2,7 +2,6 @@
namespace Components
{
Utils::Memory::Allocator Command::MemAllocator;
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMap;
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMapSV;
@ -130,7 +129,7 @@ namespace Components
Game::cmd_function_t* Command::Allocate()
{
return Command::MemAllocator.allocate<Game::cmd_function_t>();
return Loader::GetAlloctor()->allocate<Game::cmd_function_t>();
}
void Command::MainCallback()
@ -265,7 +264,6 @@ namespace Components
Command::~Command()
{
Command::MemAllocator.clear();
Command::FunctionMap.clear();
Command::FunctionMapSV.clear();
}

View File

@ -65,7 +65,6 @@ namespace Components
static Game::cmd_function_t* Find(std::string command);
private:
static Utils::Memory::Allocator MemAllocator;
static std::unordered_map<std::string, Utils::Slot<Callback>> FunctionMap;
static std::unordered_map<std::string, Utils::Slot<Callback>> FunctionMapSV;

View File

@ -4,40 +4,40 @@ namespace Components
{
std::recursive_mutex Localization::LocalizeMutex;
Dvar::Var Localization::UseLocalization;
Utils::Memory::Allocator Localization::MemAllocator;
std::unordered_map<std::string, Game::LocalizeEntry*> Localization::LocalizeMap;
std::unordered_map<std::string, Game::LocalizeEntry*> Localization::TempLocalizeMap;
void Localization::Set(std::string key, std::string value)
{
std::lock_guard<std::recursive_mutex> _(Localization::LocalizeMutex);
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
if (Localization::LocalizeMap.find(key) != Localization::LocalizeMap.end())
{
Game::LocalizeEntry* entry = Localization::LocalizeMap[key];
char* newStaticValue = Localization::MemAllocator.duplicateString(value);
char* newStaticValue = allocator->duplicateString(value);
if (!newStaticValue) return;
if (entry->value) Localization::MemAllocator.free(entry->value);
if (entry->value) allocator->free(entry->value);
entry->value = newStaticValue;
return;
}
Game::LocalizeEntry* entry = Localization::MemAllocator.allocate<Game::LocalizeEntry>();
Game::LocalizeEntry* entry = allocator->allocate<Game::LocalizeEntry>();
if (!entry) return;
entry->name = Localization::MemAllocator.duplicateString(key);
entry->name = allocator->duplicateString(key);
if (!entry->name)
{
Localization::MemAllocator.free(entry);
allocator->free(entry);
return;
}
entry->value = Localization::MemAllocator.duplicateString(value);
entry->value = allocator->duplicateString(value);
if (!entry->value)
{
Localization::MemAllocator.free(entry->name);
Localization::MemAllocator.free(entry);
allocator->free(entry->name);
allocator->free(entry);
return;
}
@ -76,30 +76,31 @@ namespace Components
void Localization::SetTemp(std::string key, std::string value)
{
std::lock_guard<std::recursive_mutex> _(Localization::LocalizeMutex);
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
if (Localization::TempLocalizeMap.find(key) != Localization::TempLocalizeMap.end())
{
Game::LocalizeEntry* entry = Localization::TempLocalizeMap[key];
if(entry->value) Localization::MemAllocator.free(entry->value);
entry->value = Localization::MemAllocator.duplicateString(value);
if(entry->value) allocator->free(entry->value);
entry->value = allocator->duplicateString(value);
}
else
{
Game::LocalizeEntry* entry = Localization::MemAllocator.allocate<Game::LocalizeEntry>();
Game::LocalizeEntry* entry = allocator->allocate<Game::LocalizeEntry>();
if (!entry) return;
entry->name = Localization::MemAllocator.duplicateString(key);
entry->name = allocator->duplicateString(key);
if (!entry->name)
{
Localization::MemAllocator.free(entry);
allocator->free(entry);
return;
}
entry->value = Localization::MemAllocator.duplicateString(value);
entry->value = allocator->duplicateString(value);
if (!entry->value)
{
Localization::MemAllocator.free(entry->name);
Localization::MemAllocator.free(entry);
allocator->free(entry->name);
allocator->free(entry);
return;
}
@ -110,14 +111,15 @@ namespace Components
void Localization::ClearTemp()
{
std::lock_guard<std::recursive_mutex> _(Localization::LocalizeMutex);
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
for (auto i = Localization::TempLocalizeMap.begin(); i != Localization::TempLocalizeMap.end(); ++i)
{
if (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);
if (i->second->name) allocator->free(i->second->name);
if (i->second->value) allocator->free(i->second->value);
allocator->free(i->second);
}
}
@ -289,8 +291,6 @@ namespace Components
Localization::~Localization()
{
Localization::ClearTemp();
Localization::LocalizeMap.clear();
Localization::MemAllocator.clear();
}
}

View File

@ -20,7 +20,6 @@ namespace Components
private:
static std::recursive_mutex LocalizeMutex;
static Utils::Memory::Allocator MemAllocator;
static std::unordered_map<std::string, Game::LocalizeEntry*> LocalizeMap;
static std::unordered_map<std::string, Game::LocalizeEntry*> TempLocalizeMap;
static Dvar::Var UseLocalization;

View File

@ -55,6 +55,8 @@ namespace Components
int Menus::LoadMenuSource(std::string name, std::string buffer)
{
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
int handle = Menus::ReserveSourceHandle();
if (!Menus::IsValidSourceHandle(handle)) return 0; // No free source slot!
@ -68,7 +70,7 @@ namespace Components
script->next = nullptr;
Game::source_t *source = Utils::Memory::Allocate<Game::source_t>();
Game::source_t *source = allocator->allocate<Game::source_t>();
if (!source)
{
Game::FreeMemory(script);
@ -81,7 +83,7 @@ namespace Components
source->defines = nullptr;
source->indentstack = nullptr;
source->skip = 0;
source->definehash = static_cast<Game::define_t**>(Utils::Memory::Allocate(4096));
source->definehash = static_cast<Game::define_t**>(allocator->allocate(4096));
Game::sourceFiles[handle] = source;
@ -112,21 +114,23 @@ namespace Components
Game::menuDef_t* Menus::ParseMenu(int handle)
{
Game::menuDef_t* menu = Utils::Memory::Allocate<Game::menuDef_t>();
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
Game::menuDef_t* menu = allocator->allocate<Game::menuDef_t>();
if (!menu) return nullptr;
menu->items = Utils::Memory::AllocateArray<Game::itemDef_t*>(512);
menu->items = allocator->allocateArray<Game::itemDef_t*>(512);
if (!menu->items)
{
Utils::Memory::Free(menu);
allocator->free(menu);
return nullptr;
}
Game::pc_token_t token;
if (!Game::PC_ReadTokenHandle(handle, &token) || token.string[0] != '{')
{
Utils::Memory::Free(menu->items);
Utils::Memory::Free(menu);
allocator->free(menu->items);
allocator->free(menu);
return nullptr;
}
@ -235,21 +239,23 @@ namespace Components
Game::MenuList* Menus::LoadScriptMenu(const char* menu)
{
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
std::vector<std::pair<bool, Game::menuDef_t*>> menus = Menus::LoadMenu(menu);
if (menus.empty()) return nullptr;
// Allocate new menu list
Game::MenuList* newList = Utils::Memory::Allocate<Game::MenuList>();
Game::MenuList* newList = allocator->allocate<Game::MenuList>();
if (!newList) return nullptr;
newList->menus = Utils::Memory::AllocateArray<Game::menuDef_t*>(menus.size());
newList->menus = allocator->allocateArray<Game::menuDef_t*>(menus.size());
if (!newList->menus)
{
Utils::Memory::Free(newList);
allocator->free(newList);
return nullptr;
}
newList->name = Utils::Memory::DuplicateString(menu);
newList->name = allocator->duplicateString(menu);
newList->menuCount = menus.size();
// Copy new menus
@ -312,6 +318,8 @@ namespace Components
Game::MenuList* Menus::LoadMenuList(Game::MenuList* menuList)
{
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
std::vector<std::pair<bool, Game::menuDef_t*>> menus;
for (int i = 0; i < menuList->menuCount; ++i)
@ -340,18 +348,18 @@ namespace Components
}
// Allocate new menu list
Game::MenuList* newList = Utils::Memory::Allocate<Game::MenuList>();
Game::MenuList* newList = allocator->allocate<Game::MenuList>();
if (!newList) return menuList;
size_t size = menus.size();
newList->menus = Utils::Memory::AllocateArray<Game::menuDef_t*>(size);
newList->menus = allocator->allocateArray<Game::menuDef_t*>(size);
if (!newList->menus)
{
Utils::Memory::Free(newList);
allocator->free(newList);
return menuList;
}
newList->name = Utils::Memory::DuplicateString(menuList->name);
newList->name = allocator->duplicateString(menuList->name);
newList->menuCount = size;
// Copy new menus
@ -368,6 +376,8 @@ namespace Components
void Menus::FreeMenuSource(int handle)
{
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
if (!Menus::IsValidSourceHandle(handle)) return;
Game::source_t *source = Game::sourceFiles[handle];
@ -397,18 +407,20 @@ namespace Components
{
Game::indent_t* indent = source->indentstack;
source->indentstack = source->indentstack->next;
Utils::Memory::Free(indent);
allocator->free(indent);
}
if (source->definehash) Utils::Memory::Free(source->definehash);
if (source->definehash) allocator->free(source->definehash);
Utils::Memory::Free(source);
allocator->free(source);
Game::sourceFiles[handle] = nullptr;
}
void Menus::FreeMenu(Game::menuDef_t* menudef)
{
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
// Do i need to free expressions and strings?
// Or does the game take care of it?
// Seems like it does...
@ -423,30 +435,31 @@ namespace Components
// Game::Menu_FreeItemMemory(menudef->items[i]);
//}
Utils::Memory::Free(menudef->items);
allocator->free(menudef->items);
}
Utils::Memory::Free(menudef);
allocator->free(menudef);
}
void Menus::FreeMenuList(Game::MenuList* menuList)
{
if (!menuList) return;
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
// Keep our compiler happy
Game::MenuList list = { menuList->name, menuList->menuCount, menuList->menus };
if (list.name)
{
Utils::Memory::Free(list.name);
allocator->free(list.name);
}
if (list.menus)
{
Utils::Memory::Free(list.menus);
allocator->free(list.menus);
}
Utils::Memory::Free(menuList);
allocator->free(menuList);
}
void Menus::RemoveMenu(std::string menu)

View File

@ -2,7 +2,6 @@
namespace Components
{
Utils::Memory::Allocator StringTable::MemAllocator;
std::unordered_map<std::string, Game::StringTable*> StringTable::StringTableMap;
int StringTable::Hash(const char* data)
@ -21,6 +20,8 @@ namespace Components
Game::StringTable* StringTable::LoadObject(std::string filename)
{
Utils::Memory::Allocator* allocator = Loader::GetAlloctor();
filename = Utils::String::ToLower(filename);
Game::StringTable* table = nullptr;
@ -30,15 +31,15 @@ namespace Components
{
Utils::CSV parsedTable(rawTable.getBuffer(), false, false);
table = StringTable::MemAllocator.allocate<Game::StringTable>();
table = allocator->allocate<Game::StringTable>();
if (table)
{
table->name = StringTable::MemAllocator.duplicateString(filename);
table->name = allocator->duplicateString(filename);
table->columnCount = parsedTable.getColumns();
table->rowCount = parsedTable.getRows();
table->values = StringTable::MemAllocator.allocateArray<Game::StringTableCell>(table->columnCount * table->rowCount);
table->values = allocator->allocateArray<Game::StringTableCell>(table->columnCount * table->rowCount);
if (!table->values)
{
@ -53,7 +54,7 @@ namespace Components
Game::StringTableCell* cell = &table->values[i * table->columnCount + j];
cell->hash = StringTable::Hash(value.data());
cell->string = StringTable::MemAllocator.duplicateString(value);
cell->string = allocator->duplicateString(value);
//if (!cell->string) cell->string = ""; // We have to assume it allocated successfully
}
}
@ -93,6 +94,5 @@ namespace Components
StringTable::~StringTable()
{
StringTable::StringTableMap.clear();
StringTable::MemAllocator.clear();
}
}

View File

@ -13,7 +13,6 @@ namespace Components
#endif
private:
static Utils::Memory::Allocator MemAllocator;
static std::unordered_map<std::string, Game::StringTable*> StringTableMap;
static int Hash(const char* data);

View File

@ -1190,7 +1190,7 @@ namespace Components
AssertSize(image359, 52);
// Copy to new struct
memcpy(&image359, buffer, sizeof(image359));
std::memcpy(&image359, buffer, sizeof(image359));
// Convert to old struct
Game::GfxImage* image = reinterpret_cast<Game::GfxImage*>(buffer);
@ -1212,7 +1212,7 @@ namespace Components
}
else
{
memcpy(buffer + 28, buffer + (size - 4), 4);
std::memcpy(buffer + 28, buffer + (size - 4), 4);
}
}

View File

@ -125,10 +125,10 @@ namespace Utils
}
private:
std::vector<void*> pool;
std::map<void*, FreeCallback> refMemory;
std::mutex mutex;
std::map<void*, void*> ptrMap;
std::vector<void*> pool;
std::unordered_map<void*, void*> ptrMap;
std::unordered_map<void*, FreeCallback> refMemory;
};
static void* AllocateAlign(size_t length, size_t alignment);