Merge pull request #346 from Laupetin/refactor/remove-memorymanager-create

refactor: remove MemoryManager create method usages
This commit is contained in:
Jan 2025-01-13 23:26:00 +01:00 committed by GitHub
commit a364e63258
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 13 additions and 89 deletions

View File

@ -63,7 +63,7 @@ namespace
menuLoadQueue.pop_front();
}
auto* menuListAsset = m_memory.Create<MenuList>();
auto* menuListAsset = m_memory.Alloc<MenuList>();
menuListAsset->name = m_memory.Dup(assetName.c_str());
registration.SetAsset(menuListAsset);

View File

@ -15,8 +15,7 @@ void MenuConversionZoneState::Inject(ZoneAssetCreationInjection& inject)
auto* memory = inject.m_zone.GetMemory();
m_zone = &inject.m_zone;
m_supporting_data = memory->Create<ExpressionSupportingData>();
memset(m_supporting_data, 0, sizeof(ExpressionSupportingData));
m_supporting_data = memory->Alloc<ExpressionSupportingData>();
}
Statement_s* MenuConversionZoneState::FindFunction(const std::string& functionName)

View File

@ -63,7 +63,7 @@ namespace
menuLoadQueue.pop_front();
}
auto* menuListAsset = m_memory.Create<MenuList>();
auto* menuListAsset = m_memory.Alloc<MenuList>();
menuListAsset->name = m_memory.Dup(assetName.c_str());
registration.SetAsset(menuListAsset);

View File

@ -15,8 +15,7 @@ void MenuConversionZoneState::Inject(ZoneAssetCreationInjection& inject)
auto* memory = inject.m_zone.GetMemory();
m_zone = &inject.m_zone;
m_supporting_data = memory->Create<ExpressionSupportingData>();
memset(m_supporting_data, 0, sizeof(ExpressionSupportingData));
m_supporting_data = memory->Alloc<ExpressionSupportingData>();
}
Statement_s* MenuConversionZoneState::FindFunction(const std::string& functionName)

View File

@ -26,7 +26,7 @@ namespace string_table
public:
StringTableType* LoadFromStream(const std::string& assetName, MemoryManager& memory, std::istream& stream)
{
auto* stringTable = memory.Create<StringTableType>();
auto* stringTable = memory.Alloc<StringTableType>();
stringTable->name = memory.Dup(assetName.c_str());
std::vector<std::vector<std::string>> csvLines;

View File

@ -3,27 +3,14 @@
#include <cstdlib>
#include <cstring>
MemoryManager::AllocationInfo::AllocationInfo(IDestructible* data, void* dataPtr)
{
m_data = data;
m_data_ptr = dataPtr;
}
MemoryManager::MemoryManager() = default;
MemoryManager::~MemoryManager()
{
for (auto allocation : m_allocations)
{
for (auto* allocation : m_allocations)
free(allocation);
}
m_allocations.clear();
for (auto destructible : m_destructible)
{
delete destructible.m_data;
}
m_destructible.clear();
m_allocations.clear();
}
void* MemoryManager::AllocRaw(const size_t size)
@ -58,16 +45,3 @@ void MemoryManager::Free(const void* data)
}
}
}
void MemoryManager::Delete(const void* data)
{
for (auto iAlloc = m_destructible.begin(); iAlloc != m_destructible.end(); ++iAlloc)
{
if (iAlloc->m_data_ptr == data)
{
delete iAlloc->m_data;
m_destructible.erase(iAlloc);
return;
}
}
}

View File

@ -14,64 +14,16 @@ public:
MemoryManager& operator=(const MemoryManager& other) = delete;
MemoryManager& operator=(MemoryManager&& other) noexcept = default;
void* AllocRaw(size_t size);
void* AllocRaw(std::size_t size);
char* Dup(const char* str);
template<typename T> std::add_pointer_t<T> Alloc(const size_t count = 1u)
template<typename T> std::add_pointer_t<T> Alloc(const std::size_t count = 1u)
{
return static_cast<std::add_pointer_t<T>>(AllocRaw(sizeof(T) * count));
}
template<class T, class... ValType> std::add_pointer_t<T> Create(ValType&&... val)
{
Allocation<T>* allocation = new Allocation<T>(std::forward<ValType>(val)...);
m_destructible.emplace_back(allocation, &allocation->m_entry);
return &allocation->m_entry;
}
void Free(const void* data);
void Delete(const void* data);
protected:
class IDestructible
{
public:
IDestructible() = default;
virtual ~IDestructible() = default;
IDestructible(const IDestructible& other) = default;
IDestructible(IDestructible&& other) noexcept = default;
IDestructible& operator=(const IDestructible& other) = default;
IDestructible& operator=(IDestructible&& other) noexcept = default;
};
template<class T> class Allocation final : public IDestructible
{
public:
T m_entry;
template<class... ValType>
explicit Allocation(ValType&&... val)
: m_entry(std::forward<ValType>(val)...)
{
}
~Allocation() override = default;
Allocation(const Allocation& other) = delete;
Allocation(Allocation&& other) noexcept = delete;
Allocation& operator=(const Allocation& other) = delete;
Allocation& operator=(Allocation&& other) noexcept = delete;
};
class AllocationInfo
{
public:
IDestructible* m_data;
void* m_data_ptr;
AllocationInfo(IDestructible* data, void* dataPtr);
};
std::vector<void*> m_allocations;
std::vector<AllocationInfo> m_destructible;
};

View File

@ -13,7 +13,7 @@ void Actions_XModel::SetModelSurfs(XModelLodInfo* lodInfo, XModelSurfs* modelSur
{
if (modelSurfs)
{
lodInfo->modelSurfs = m_zone->GetMemory()->Create<XModelSurfs>();
lodInfo->modelSurfs = m_zone->GetMemory()->Alloc<XModelSurfs>();
memcpy(lodInfo->modelSurfs, modelSurfs, sizeof(XModelSurfs));
}
}

View File

@ -13,7 +13,7 @@ void Actions_XModel::SetModelSurfs(XModelLodInfo* lodInfo, XModelSurfs* modelSur
{
if (modelSurfs)
{
lodInfo->modelSurfs = m_zone->GetMemory()->Create<XModelSurfs>();
lodInfo->modelSurfs = m_zone->GetMemory()->Alloc<XModelSurfs>();
memcpy(lodInfo->modelSurfs, modelSurfs, sizeof(XModelSurfs));
}
}

View File

@ -9,6 +9,6 @@ class TestMemoryManager : public MemoryManager
public:
[[nodiscard]] size_t GetAllocationCount() const
{
return m_allocations.size() + m_destructible.size();
return m_allocations.size();
}
};

View File

@ -51,7 +51,7 @@ namespace test::game::iw4::menu::parsing::it
Material* AddMaterial(const std::string& name)
{
auto* material = m_zone.GetMemory()->Create<Material>();
auto* material = m_zone.GetMemory()->Alloc<Material>();
material->info.name = m_zone.GetMemory()->Dup(name.c_str());
m_context.AddAsset<AssetMaterial>(name, material);