diff --git a/src/ZoneCommon/Zone/ZoneMemory.cpp b/src/ZoneCommon/Zone/ZoneMemory.cpp index 3361f0a9..8fb8e727 100644 --- a/src/ZoneCommon/Zone/ZoneMemory.cpp +++ b/src/ZoneCommon/Zone/ZoneMemory.cpp @@ -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; + } + } +} diff --git a/src/ZoneCommon/Zone/ZoneMemory.h b/src/ZoneCommon/Zone/ZoneMemory.h index 0b1b3e96..57720575 100644 --- a/src/ZoneCommon/Zone/ZoneMemory.h +++ b/src/ZoneCommon/Zone/ZoneMemory.h @@ -5,8 +5,44 @@ class ZoneMemory { + class IDestructible + { + public: + virtual ~IDestructible() = default; + }; + + template + class Allocation final : public IDestructible + { + public: + T m_entry; + + template + 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 m_blocks; std::vector m_allocations; + std::vector m_destructible; public: ZoneMemory(); @@ -16,4 +52,14 @@ public: void* Alloc(size_t size); char* Dup(const char* str); -}; \ No newline at end of file + + template + T* Create(_Valty&&... _Val) + { + Allocation* allocation = new Allocation(std::forward<_Valty>(_Val)...); + m_destructible.emplace_back(allocation, &allocation->m_entry); + return &allocation->m_entry; + } + + void Delete(void* data); +};