Change VmaPoolAllocator to allow not-POD T type, explicitly call its constructor and destructor

See #71
This commit is contained in:
Adam Sawicki 2019-07-25 17:23:51 +02:00
parent 692ccba14d
commit c7e5f2e44a

View File

@ -4337,7 +4337,6 @@ class VmaPoolAllocator
public:
VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity);
~VmaPoolAllocator();
void Clear();
T* Alloc();
void Free(T* ptr);
@ -4345,7 +4344,7 @@ private:
union Item
{
uint32_t NextFreeIndex;
T Value;
char Value[sizeof(T)];
};
struct ItemBlock
@ -4373,12 +4372,6 @@ VmaPoolAllocator<T>::VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCa
template<typename T>
VmaPoolAllocator<T>::~VmaPoolAllocator()
{
Clear();
}
template<typename T>
void VmaPoolAllocator<T>::Clear()
{
for(size_t i = m_ItemBlocks.size(); i--; )
vma_delete_array(m_pAllocationCallbacks, m_ItemBlocks[i].pItems, m_ItemBlocks[i].Capacity);
@ -4396,7 +4389,9 @@ T* VmaPoolAllocator<T>::Alloc()
{
Item* const pItem = &block.pItems[block.FirstFreeIndex];
block.FirstFreeIndex = pItem->NextFreeIndex;
return &pItem->Value;
T* result = (T*)&pItem->Value;
new(result)T(); // Explicit constructor call.
return result;
}
}
@ -4404,7 +4399,9 @@ T* VmaPoolAllocator<T>::Alloc()
ItemBlock& newBlock = CreateNewBlock();
Item* const pItem = &newBlock.pItems[0];
newBlock.FirstFreeIndex = pItem->NextFreeIndex;
return &pItem->Value;
T* result = (T*)&pItem->Value;
new(result)T(); // Explicit constructor call.
return result;
}
template<typename T>
@ -4422,6 +4419,7 @@ void VmaPoolAllocator<T>::Free(T* ptr)
// Check if pItemPtr is in address range of this block.
if((pItemPtr >= block.pItems) && (pItemPtr < block.pItems + block.Capacity))
{
ptr->~T(); // Explicit destructor call.
const uint32_t index = static_cast<uint32_t>(pItemPtr - block.pItems);
pItemPtr->NextFreeIndex = block.FirstFreeIndex;
block.FirstFreeIndex = index;
@ -5050,8 +5048,7 @@ public:
};
/*
This struct cannot have constructor or destructor. It must be POD because it is
allocated using VmaPoolAllocator.
This struct is allocated using VmaPoolAllocator.
*/
void Ctor(uint32_t currentFrameIndex, bool userDataString)