mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-05-09 22:14:56 +00:00
ZoneCommon: Extract non ZoneMemory management from ZoneMemory into Utils::MemoryManager class
This commit is contained in:
parent
b67b4ee316
commit
63330ff908
54
src/Utils/Utils/MemoryManager.cpp
Normal file
54
src/Utils/Utils/MemoryManager.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "MemoryManager.h"
|
||||
|
||||
MemoryManager::AllocationInfo::AllocationInfo(IDestructible* data, void* dataPtr)
|
||||
{
|
||||
m_data = data;
|
||||
m_data_ptr = dataPtr;
|
||||
}
|
||||
|
||||
MemoryManager::MemoryManager()
|
||||
= default;
|
||||
|
||||
MemoryManager::~MemoryManager()
|
||||
{
|
||||
for (auto allocation : m_allocations)
|
||||
{
|
||||
free(allocation);
|
||||
}
|
||||
m_allocations.clear();
|
||||
|
||||
for (auto destructible : m_destructible)
|
||||
{
|
||||
delete destructible.m_data;
|
||||
}
|
||||
m_destructible.clear();
|
||||
}
|
||||
|
||||
void* MemoryManager::Alloc(const size_t size)
|
||||
{
|
||||
void* result = malloc(size);
|
||||
m_allocations.push_back(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char* MemoryManager::Dup(const char* str)
|
||||
{
|
||||
char* result = _strdup(str);
|
||||
m_allocations.push_back(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void MemoryManager::Delete(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;
|
||||
}
|
||||
}
|
||||
}
|
61
src/Utils/Utils/MemoryManager.h
Normal file
61
src/Utils/Utils/MemoryManager.h
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
class MemoryManager
|
||||
{
|
||||
class IDestructible
|
||||
{
|
||||
public:
|
||||
virtual ~IDestructible() = default;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class Allocation final : public IDestructible
|
||||
{
|
||||
public:
|
||||
T m_entry;
|
||||
|
||||
template <class... _Valty>
|
||||
explicit Allocation(_Valty&&... _Val)
|
||||
: m_entry(std::forward<_Valty>(_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;
|
||||
|
||||
public:
|
||||
MemoryManager();
|
||||
virtual ~MemoryManager();
|
||||
|
||||
void* Alloc(size_t size);
|
||||
char* Dup(const char* str);
|
||||
|
||||
template <class T, class... _Valty>
|
||||
T* Create(_Valty&&... _Val)
|
||||
{
|
||||
Allocation<T>* allocation = new Allocation<T>(std::forward<_Valty>(_Val)...);
|
||||
m_destructible.emplace_back(allocation, &allocation->m_entry);
|
||||
return &allocation->m_entry;
|
||||
}
|
||||
|
||||
void Delete(void* data);
|
||||
};
|
@ -1,12 +1,14 @@
|
||||
ZoneCommon = {}
|
||||
|
||||
function ZoneCommon:include()
|
||||
Utils:include()
|
||||
includedirs {
|
||||
path.join(ProjectFolder(), "ZoneCommon")
|
||||
}
|
||||
end
|
||||
|
||||
function ZoneCommon:link()
|
||||
Utils:link()
|
||||
links {
|
||||
"ZoneCommon"
|
||||
}
|
||||
|
@ -1,12 +1,5 @@
|
||||
#include "ZoneMemory.h"
|
||||
|
||||
|
||||
ZoneMemory::AllocationInfo::AllocationInfo(IDestructible* data, void* dataPtr)
|
||||
{
|
||||
m_data = data;
|
||||
m_data_ptr = dataPtr;
|
||||
}
|
||||
|
||||
ZoneMemory::ZoneMemory()
|
||||
= default;
|
||||
|
||||
@ -17,50 +10,9 @@ ZoneMemory::~ZoneMemory()
|
||||
delete block;
|
||||
}
|
||||
m_blocks.clear();
|
||||
|
||||
for (auto allocation : m_allocations)
|
||||
{
|
||||
free(allocation);
|
||||
}
|
||||
m_allocations.clear();
|
||||
|
||||
for(auto destructible : m_destructible)
|
||||
{
|
||||
delete destructible.m_data;
|
||||
}
|
||||
m_destructible.clear();
|
||||
}
|
||||
|
||||
void ZoneMemory::AddBlock(XBlock* block)
|
||||
{
|
||||
m_blocks.push_back(block);
|
||||
}
|
||||
|
||||
void* ZoneMemory::Alloc(const size_t size)
|
||||
{
|
||||
void* result = malloc(size);
|
||||
m_allocations.push_back(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char* ZoneMemory::Dup(const char* str)
|
||||
{
|
||||
char* result = _strdup(str);
|
||||
m_allocations.push_back(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ZoneMemory::Delete(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,65 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "Utils/MemoryManager.h"
|
||||
#include "Zone/XBlock.h"
|
||||
#include <vector>
|
||||
|
||||
class ZoneMemory
|
||||
class ZoneMemory : public MemoryManager
|
||||
{
|
||||
class IDestructible
|
||||
{
|
||||
public:
|
||||
virtual ~IDestructible() = default;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class Allocation final : public IDestructible
|
||||
{
|
||||
public:
|
||||
T m_entry;
|
||||
|
||||
template <class... _Valty>
|
||||
explicit Allocation(_Valty&&... _Val)
|
||||
: m_entry(std::forward<_Valty>(_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<XBlock*> m_blocks;
|
||||
std::vector<void*> m_allocations;
|
||||
std::vector<AllocationInfo> m_destructible;
|
||||
|
||||
public:
|
||||
ZoneMemory();
|
||||
~ZoneMemory();
|
||||
~ZoneMemory() override;
|
||||
|
||||
void AddBlock(XBlock* block);
|
||||
|
||||
void* Alloc(size_t size);
|
||||
char* Dup(const char* str);
|
||||
|
||||
template <class T, class... _Valty>
|
||||
T* Create(_Valty&&... _Val)
|
||||
{
|
||||
Allocation<T>* allocation = new Allocation<T>(std::forward<_Valty>(_Val)...);
|
||||
m_destructible.emplace_back(allocation, &allocation->m_entry);
|
||||
return &allocation->m_entry;
|
||||
}
|
||||
|
||||
void Delete(void* data);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user