Made ComponentPool copyable by making sure string fields are handled properly

This commit is contained in:
2016-02-18 11:37:03 +01:00
parent 0af9edc6e5
commit 97499f1c28
7 changed files with 89 additions and 48 deletions
+18 -3
View File
@@ -66,9 +66,24 @@ public:
, m_LowestAllocatedSlot(m_NumSlots)
{ }
//We may get problems with memory being released
//prematurely, etc. if we allow copies.
MemoryPool(const MemoryPool<T>& other) = delete;
MemoryPool(const MemoryPool<T>& other)
: m_StartAddress(new char[other.m_NumSlots*other.m_Stride])
, m_SlotIsAllocated(other.m_NumSlots, false)
, m_NumSlots(other.m_NumSlots)
, m_Stride(other.m_Stride)
, m_NumAllocatedSlots(0)
, m_CurrentAllocSlot(0)
, m_LowestAllocatedSlot(m_NumSlots)
{
// Copy statically allocated pool
memcpy(m_StartAddress, other.m_StartAddress, m_NumSlots*m_Stride);
// Copy dynamically allocated memory
for (char* otherAddr : other.m_ExtraMemory) {
char* addr = (char*)malloc(m_Stride);
memcpy(addr, otherAddr, m_Stride);
m_ExtraMemory.push_back(addr);
}
}
MemoryPool(const MemoryPool<T>&& other) = delete;
//Free all memory that has been allocated.