Change VmaPoolAllocator to allow not-POD T type, explicitly call its constructor and destructor
See #71
This commit is contained in:
parent
692ccba14d
commit
c7e5f2e44a
1 changed files with 9 additions and 12 deletions
|
@ -4337,7 +4337,6 @@ class VmaPoolAllocator
|
||||||
public:
|
public:
|
||||||
VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity);
|
VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity);
|
||||||
~VmaPoolAllocator();
|
~VmaPoolAllocator();
|
||||||
void Clear();
|
|
||||||
T* Alloc();
|
T* Alloc();
|
||||||
void Free(T* ptr);
|
void Free(T* ptr);
|
||||||
|
|
||||||
|
@ -4345,7 +4344,7 @@ private:
|
||||||
union Item
|
union Item
|
||||||
{
|
{
|
||||||
uint32_t NextFreeIndex;
|
uint32_t NextFreeIndex;
|
||||||
T Value;
|
char Value[sizeof(T)];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ItemBlock
|
struct ItemBlock
|
||||||
|
@ -4373,12 +4372,6 @@ VmaPoolAllocator<T>::VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCa
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
VmaPoolAllocator<T>::~VmaPoolAllocator()
|
VmaPoolAllocator<T>::~VmaPoolAllocator()
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void VmaPoolAllocator<T>::Clear()
|
|
||||||
{
|
{
|
||||||
for(size_t i = m_ItemBlocks.size(); i--; )
|
for(size_t i = m_ItemBlocks.size(); i--; )
|
||||||
vma_delete_array(m_pAllocationCallbacks, m_ItemBlocks[i].pItems, m_ItemBlocks[i].Capacity);
|
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];
|
Item* const pItem = &block.pItems[block.FirstFreeIndex];
|
||||||
block.FirstFreeIndex = pItem->NextFreeIndex;
|
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();
|
ItemBlock& newBlock = CreateNewBlock();
|
||||||
Item* const pItem = &newBlock.pItems[0];
|
Item* const pItem = &newBlock.pItems[0];
|
||||||
newBlock.FirstFreeIndex = pItem->NextFreeIndex;
|
newBlock.FirstFreeIndex = pItem->NextFreeIndex;
|
||||||
return &pItem->Value;
|
T* result = (T*)&pItem->Value;
|
||||||
|
new(result)T(); // Explicit constructor call.
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -4422,6 +4419,7 @@ void VmaPoolAllocator<T>::Free(T* ptr)
|
||||||
// Check if pItemPtr is in address range of this block.
|
// Check if pItemPtr is in address range of this block.
|
||||||
if((pItemPtr >= block.pItems) && (pItemPtr < block.pItems + block.Capacity))
|
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);
|
const uint32_t index = static_cast<uint32_t>(pItemPtr - block.pItems);
|
||||||
pItemPtr->NextFreeIndex = block.FirstFreeIndex;
|
pItemPtr->NextFreeIndex = block.FirstFreeIndex;
|
||||||
block.FirstFreeIndex = index;
|
block.FirstFreeIndex = index;
|
||||||
|
@ -5050,8 +5048,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This struct cannot have constructor or destructor. It must be POD because it is
|
This struct is allocated using VmaPoolAllocator.
|
||||||
allocated using VmaPoolAllocator.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void Ctor(uint32_t currentFrameIndex, bool userDataString)
|
void Ctor(uint32_t currentFrameIndex, bool userDataString)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue