mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 16:15:43 +00:00
ZoneCommon: Add class object construction and destruction methods to ZoneMemory
This commit is contained in:
parent
297358cdb0
commit
c67911bdbb
@ -1,5 +1,12 @@
|
||||
#include "ZoneMemory.h"
|
||||
|
||||
|
||||
ZoneMemory::AllocationInfo::AllocationInfo(IDestructible* data, void* dataPtr)
|
||||
{
|
||||
m_data = data;
|
||||
m_data_ptr = dataPtr;
|
||||
}
|
||||
|
||||
ZoneMemory::ZoneMemory()
|
||||
= default;
|
||||
|
||||
@ -16,6 +23,12 @@ ZoneMemory::~ZoneMemory()
|
||||
free(allocation);
|
||||
}
|
||||
m_allocations.clear();
|
||||
|
||||
for(auto destructible : m_destructible)
|
||||
{
|
||||
delete destructible.m_data;
|
||||
}
|
||||
m_destructible.clear();
|
||||
}
|
||||
|
||||
void ZoneMemory::AddBlock(XBlock* block)
|
||||
@ -38,3 +51,16 @@ char* ZoneMemory::Dup(const char* str)
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,44 @@
|
||||
|
||||
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<void*> m_allocations;
|
||||
std::vector<AllocationInfo> m_destructible;
|
||||
|
||||
public:
|
||||
ZoneMemory();
|
||||
@ -16,4 +52,14 @@ public:
|
||||
|
||||
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