mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-05-09 22:14:56 +00:00
refactor: change memory manager API to use a type template
This commit is contained in:
parent
7d920e1060
commit
0845cccd12
@ -26,9 +26,9 @@ MemoryManager::~MemoryManager()
|
||||
m_destructible.clear();
|
||||
}
|
||||
|
||||
void* MemoryManager::Alloc(const size_t size)
|
||||
void* MemoryManager::AllocRaw(const size_t size)
|
||||
{
|
||||
void* result = malloc(size);
|
||||
void* result = calloc(size, 1u);
|
||||
m_allocations.push_back(result);
|
||||
|
||||
return result;
|
||||
@ -46,7 +46,7 @@ char* MemoryManager::Dup(const char* str)
|
||||
return result;
|
||||
}
|
||||
|
||||
void MemoryManager::Free(void* data)
|
||||
void MemoryManager::Free(const void* data)
|
||||
{
|
||||
for (auto iAlloc = m_allocations.begin(); iAlloc != m_allocations.end(); ++iAlloc)
|
||||
{
|
||||
@ -59,7 +59,7 @@ void MemoryManager::Free(void* data)
|
||||
}
|
||||
}
|
||||
|
||||
void MemoryManager::Delete(void* data)
|
||||
void MemoryManager::Delete(const void* data)
|
||||
{
|
||||
for (auto iAlloc = m_destructible.begin(); iAlloc != m_destructible.end(); ++iAlloc)
|
||||
{
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
class MemoryManager
|
||||
@ -8,7 +9,12 @@ class MemoryManager
|
||||
class IDestructible
|
||||
{
|
||||
public:
|
||||
IDestructible() = default;
|
||||
virtual ~IDestructible() = default;
|
||||
IDestructible(const IDestructible& other) = default;
|
||||
IDestructible(IDestructible&& other) noexcept = default;
|
||||
IDestructible& operator=(const IDestructible& other) = default;
|
||||
IDestructible& operator=(IDestructible&& other) noexcept = default;
|
||||
};
|
||||
|
||||
template<class T> class Allocation final : public IDestructible
|
||||
@ -16,9 +22,9 @@ class MemoryManager
|
||||
public:
|
||||
T m_entry;
|
||||
|
||||
template<class... _Valty>
|
||||
explicit Allocation(_Valty&&... _Val)
|
||||
: m_entry(std::forward<_Valty>(_Val)...)
|
||||
template<class... ValType>
|
||||
explicit Allocation(ValType&&... val)
|
||||
: m_entry(std::forward<ValType>(val)...)
|
||||
{
|
||||
}
|
||||
|
||||
@ -45,17 +51,26 @@ class MemoryManager
|
||||
public:
|
||||
MemoryManager();
|
||||
virtual ~MemoryManager();
|
||||
MemoryManager(const MemoryManager& other) = delete;
|
||||
MemoryManager(MemoryManager&& other) noexcept = default;
|
||||
MemoryManager& operator=(const MemoryManager& other) = delete;
|
||||
MemoryManager& operator=(MemoryManager&& other) noexcept = default;
|
||||
|
||||
void* Alloc(size_t size);
|
||||
void* AllocRaw(size_t size);
|
||||
char* Dup(const char* str);
|
||||
|
||||
template<class T, class... _Valty> T* Create(_Valty&&... _Val)
|
||||
template<typename T> std::add_pointer_t<T> Alloc(const size_t count = 1u)
|
||||
{
|
||||
Allocation<T>* allocation = new Allocation<T>(std::forward<_Valty>(_Val)...);
|
||||
return static_cast<std::add_pointer_t<T>>(AllocRaw(sizeof(T) * count));
|
||||
}
|
||||
|
||||
template<class T, class... ValType> std::add_pointer_t<T> Create(ValType&&... val)
|
||||
{
|
||||
Allocation<T>* allocation = new Allocation<T>(std::forward<ValType>(val)...);
|
||||
m_destructible.emplace_back(allocation, &allocation->m_entry);
|
||||
return &allocation->m_entry;
|
||||
}
|
||||
|
||||
void Free(void* data);
|
||||
void Delete(void* data);
|
||||
void Free(const void* data);
|
||||
void Delete(const void* data);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user