diff --git a/src/vk_mem_alloc.h b/src/vk_mem_alloc.h index eece8da..91f5b47 100644 --- a/src/vk_mem_alloc.h +++ b/src/vk_mem_alloc.h @@ -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::VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCa template VmaPoolAllocator::~VmaPoolAllocator() -{ - Clear(); -} - -template -void VmaPoolAllocator::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::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::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 @@ -4422,6 +4419,7 @@ void VmaPoolAllocator::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(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)