ZoneCommon: Add class object construction and destruction methods to ZoneMemory

This commit is contained in:
Jan 2020-01-04 15:13:44 +01:00
parent 297358cdb0
commit c67911bdbb
2 changed files with 73 additions and 1 deletions

View File

@ -1,5 +1,12 @@
#include "ZoneMemory.h" #include "ZoneMemory.h"
ZoneMemory::AllocationInfo::AllocationInfo(IDestructible* data, void* dataPtr)
{
m_data = data;
m_data_ptr = dataPtr;
}
ZoneMemory::ZoneMemory() ZoneMemory::ZoneMemory()
= default; = default;
@ -16,6 +23,12 @@ ZoneMemory::~ZoneMemory()
free(allocation); free(allocation);
} }
m_allocations.clear(); m_allocations.clear();
for(auto destructible : m_destructible)
{
delete destructible.m_data;
}
m_destructible.clear();
} }
void ZoneMemory::AddBlock(XBlock* block) void ZoneMemory::AddBlock(XBlock* block)
@ -38,3 +51,16 @@ char* ZoneMemory::Dup(const char* str)
return 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;
}
}
}

View File

@ -5,8 +5,44 @@
class ZoneMemory class ZoneMemory
{ {
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<XBlock*> m_blocks;
std::vector<void*> m_allocations; std::vector<void*> m_allocations;
std::vector<AllocationInfo> m_destructible;
public: public:
ZoneMemory(); ZoneMemory();
@ -16,4 +52,14 @@ public:
void* Alloc(size_t size); void* Alloc(size_t size);
char* Dup(const char* str); 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);
};