From 63330ff908fdc6798b4ec851f9956fb4b5602921 Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 5 Feb 2020 16:52:43 +0100 Subject: [PATCH] ZoneCommon: Extract non ZoneMemory management from ZoneMemory into Utils::MemoryManager class --- src/Utils/Utils/MemoryManager.cpp | 54 ++++++++++++++++++++++++++ src/Utils/Utils/MemoryManager.h | 61 ++++++++++++++++++++++++++++++ src/ZoneCommon.lua | 2 + src/ZoneCommon/Zone/ZoneMemory.cpp | 48 ----------------------- src/ZoneCommon/Zone/ZoneMemory.h | 55 ++------------------------- 5 files changed, 120 insertions(+), 100 deletions(-) create mode 100644 src/Utils/Utils/MemoryManager.cpp create mode 100644 src/Utils/Utils/MemoryManager.h diff --git a/src/Utils/Utils/MemoryManager.cpp b/src/Utils/Utils/MemoryManager.cpp new file mode 100644 index 00000000..802564c0 --- /dev/null +++ b/src/Utils/Utils/MemoryManager.cpp @@ -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; + } + } +} \ No newline at end of file diff --git a/src/Utils/Utils/MemoryManager.h b/src/Utils/Utils/MemoryManager.h new file mode 100644 index 00000000..d3301bfe --- /dev/null +++ b/src/Utils/Utils/MemoryManager.h @@ -0,0 +1,61 @@ +#pragma once + +#include + +class MemoryManager +{ + 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_allocations; + std::vector m_destructible; + +public: + MemoryManager(); + virtual ~MemoryManager(); + + void* Alloc(size_t size); + char* Dup(const char* str); + + 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); +}; \ No newline at end of file diff --git a/src/ZoneCommon.lua b/src/ZoneCommon.lua index 9df310a4..5231d6e6 100644 --- a/src/ZoneCommon.lua +++ b/src/ZoneCommon.lua @@ -1,12 +1,14 @@ ZoneCommon = {} function ZoneCommon:include() + Utils:include() includedirs { path.join(ProjectFolder(), "ZoneCommon") } end function ZoneCommon:link() + Utils:link() links { "ZoneCommon" } diff --git a/src/ZoneCommon/Zone/ZoneMemory.cpp b/src/ZoneCommon/Zone/ZoneMemory.cpp index 8fb8e727..135e9f04 100644 --- a/src/ZoneCommon/Zone/ZoneMemory.cpp +++ b/src/ZoneCommon/Zone/ZoneMemory.cpp @@ -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; - } - } -} diff --git a/src/ZoneCommon/Zone/ZoneMemory.h b/src/ZoneCommon/Zone/ZoneMemory.h index 57720575..7d8eb667 100644 --- a/src/ZoneCommon/Zone/ZoneMemory.h +++ b/src/ZoneCommon/Zone/ZoneMemory.h @@ -1,65 +1,16 @@ #pragma once +#include "Utils/MemoryManager.h" #include "Zone/XBlock.h" #include -class ZoneMemory +class ZoneMemory : public MemoryManager { - 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(); - ~ZoneMemory(); + ~ZoneMemory() override; void AddBlock(XBlock* block); - - void* Alloc(size_t size); - char* Dup(const char* str); - - 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); };