Refactoring: Use constructor and destructor in VmaAllocation_T

VmaPoolAllocator supports that. Needed to only use parameter pack to pass arguments to constructor.
This commit is contained in:
Adam Sawicki 2020-01-31 11:19:32 +01:00
parent 1a63ebc860
commit aaba2109f9

View File

@ -4660,7 +4660,7 @@ class VmaPoolAllocator
public: public:
VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity); VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity);
~VmaPoolAllocator(); ~VmaPoolAllocator();
T* Alloc(); template<typename... Types> T* Alloc(Types... args);
void Free(T* ptr); void Free(T* ptr);
private: private:
@ -4702,7 +4702,7 @@ VmaPoolAllocator<T>::~VmaPoolAllocator()
} }
template<typename T> template<typename T>
T* VmaPoolAllocator<T>::Alloc() template<typename... Types> T* VmaPoolAllocator<T>::Alloc(Types... args)
{ {
for(size_t i = m_ItemBlocks.size(); i--; ) for(size_t i = m_ItemBlocks.size(); i--; )
{ {
@ -4713,7 +4713,7 @@ 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;
T* result = (T*)&pItem->Value; T* result = (T*)&pItem->Value;
new(result)T(); // Explicit constructor call. new(result)T(args...); // Explicit constructor call.
return result; return result;
} }
} }
@ -4723,7 +4723,7 @@ T* VmaPoolAllocator<T>::Alloc()
Item* const pItem = &newBlock.pItems[0]; Item* const pItem = &newBlock.pItems[0];
newBlock.FirstFreeIndex = pItem->NextFreeIndex; newBlock.FirstFreeIndex = pItem->NextFreeIndex;
T* result = (T*)&pItem->Value; T* result = (T*)&pItem->Value;
new(result)T(); // Explicit constructor call. new(result)T(args...); // Explicit constructor call.
return result; return result;
} }
@ -5374,25 +5374,24 @@ public:
This struct is allocated using VmaPoolAllocator. This struct is allocated using VmaPoolAllocator.
*/ */
void Ctor(uint32_t currentFrameIndex, bool userDataString) VmaAllocation_T(uint32_t currentFrameIndex, bool userDataString) :
m_Alignment{1},
m_Size{0},
m_pUserData{VMA_NULL},
m_LastUseFrameIndex{currentFrameIndex},
m_MemoryTypeIndex{0},
m_Type{(uint8_t)ALLOCATION_TYPE_NONE},
m_SuballocationType{(uint8_t)VMA_SUBALLOCATION_TYPE_UNKNOWN},
m_MapCount{0},
m_Flags{userDataString ? (uint8_t)FLAG_USER_DATA_STRING : (uint8_t)0}
{ {
m_Alignment = 1;
m_Size = 0;
m_MemoryTypeIndex = 0;
m_pUserData = VMA_NULL;
m_LastUseFrameIndex = currentFrameIndex;
m_Type = (uint8_t)ALLOCATION_TYPE_NONE;
m_SuballocationType = (uint8_t)VMA_SUBALLOCATION_TYPE_UNKNOWN;
m_MapCount = 0;
m_Flags = userDataString ? (uint8_t)FLAG_USER_DATA_STRING : 0;
#if VMA_STATS_STRING_ENABLED #if VMA_STATS_STRING_ENABLED
m_CreationFrameIndex = currentFrameIndex; m_CreationFrameIndex = currentFrameIndex;
m_BufferImageUsage = 0; m_BufferImageUsage = 0;
#endif #endif
} }
void Dtor() ~VmaAllocation_T()
{ {
VMA_ASSERT((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) == 0 && "Allocation was not unmapped before destruction."); VMA_ASSERT((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) == 0 && "Allocation was not unmapped before destruction.");
@ -7057,7 +7056,7 @@ class VmaAllocationObjectAllocator
public: public:
VmaAllocationObjectAllocator(const VkAllocationCallbacks* pAllocationCallbacks); VmaAllocationObjectAllocator(const VkAllocationCallbacks* pAllocationCallbacks);
VmaAllocation Allocate(); template<typename... Types> VmaAllocation Allocate(Types... args);
void Free(VmaAllocation hAlloc); void Free(VmaAllocation hAlloc);
private: private:
@ -12295,8 +12294,7 @@ VkResult VmaBlockVector::AllocatePage(
&bestRequest)) &bestRequest))
{ {
// Allocate from this pBlock. // Allocate from this pBlock.
*pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(); *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(currentFrameIndex, isUserDataString);
(*pAllocation)->Ctor(currentFrameIndex, isUserDataString);
pBestRequestBlock->m_pMetadata->Alloc(bestRequest, suballocType, size, *pAllocation); pBestRequestBlock->m_pMetadata->Alloc(bestRequest, suballocType, size, *pAllocation);
UpdateHasEmptyBlock(); UpdateHasEmptyBlock();
(*pAllocation)->InitBlockAllocation( (*pAllocation)->InitBlockAllocation(
@ -12500,8 +12498,7 @@ VkResult VmaBlockVector::AllocateFromBlock(
} }
} }
*pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(); *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(currentFrameIndex, isUserDataString);
(*pAllocation)->Ctor(currentFrameIndex, isUserDataString);
pBlock->m_pMetadata->Alloc(currRequest, suballocType, size, *pAllocation); pBlock->m_pMetadata->Alloc(currRequest, suballocType, size, *pAllocation);
UpdateHasEmptyBlock(); UpdateHasEmptyBlock();
(*pAllocation)->InitBlockAllocation( (*pAllocation)->InitBlockAllocation(
@ -14835,10 +14832,10 @@ VmaAllocationObjectAllocator::VmaAllocationObjectAllocator(const VkAllocationCal
{ {
} }
VmaAllocation VmaAllocationObjectAllocator::Allocate() template<typename... Types> VmaAllocation VmaAllocationObjectAllocator::Allocate(Types... args)
{ {
VmaMutexLock mutexLock(m_Mutex); VmaMutexLock mutexLock(m_Mutex);
return m_Allocator.Alloc(); return m_Allocator.Alloc(args...);
} }
void VmaAllocationObjectAllocator::Free(VmaAllocation hAlloc) void VmaAllocationObjectAllocator::Free(VmaAllocation hAlloc)
@ -15413,7 +15410,6 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory(
FreeVulkanMemory(memTypeIndex, currAlloc->GetSize(), hMemory); FreeVulkanMemory(memTypeIndex, currAlloc->GetSize(), hMemory);
m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), currAlloc->GetSize()); m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), currAlloc->GetSize());
currAlloc->SetUserData(this, VMA_NULL); currAlloc->SetUserData(this, VMA_NULL);
currAlloc->Dtor();
m_AllocationObjectAllocator.Free(currAlloc); m_AllocationObjectAllocator.Free(currAlloc);
} }
@ -15459,8 +15455,7 @@ VkResult VmaAllocator_T::AllocateDedicatedMemoryPage(
} }
} }
*pAllocation = m_AllocationObjectAllocator.Allocate(); *pAllocation = m_AllocationObjectAllocator.Allocate(m_CurrentFrameIndex.load(), isUserDataString);
(*pAllocation)->Ctor(m_CurrentFrameIndex.load(), isUserDataString);
(*pAllocation)->InitDedicatedAllocation(memTypeIndex, hMemory, suballocType, pMappedData, size); (*pAllocation)->InitDedicatedAllocation(memTypeIndex, hMemory, suballocType, pMappedData, size);
(*pAllocation)->SetUserData(this, pUserData); (*pAllocation)->SetUserData(this, pUserData);
m_Budget.AddAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), size); m_Budget.AddAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), size);
@ -15734,7 +15729,6 @@ void VmaAllocator_T::FreeMemory(
// Do this regardless of whether the allocation is lost. Lost allocations still account to Budget.AllocationBytes. // Do this regardless of whether the allocation is lost. Lost allocations still account to Budget.AllocationBytes.
m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(allocation->GetMemoryTypeIndex()), allocation->GetSize()); m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(allocation->GetMemoryTypeIndex()), allocation->GetSize());
allocation->SetUserData(this, VMA_NULL); allocation->SetUserData(this, VMA_NULL);
allocation->Dtor();
m_AllocationObjectAllocator.Free(allocation); m_AllocationObjectAllocator.Free(allocation);
} }
} }
@ -16168,8 +16162,7 @@ VkResult VmaAllocator_T::CheckCorruption(uint32_t memoryTypeBits)
void VmaAllocator_T::CreateLostAllocation(VmaAllocation* pAllocation) void VmaAllocator_T::CreateLostAllocation(VmaAllocation* pAllocation)
{ {
*pAllocation = m_AllocationObjectAllocator.Allocate(); *pAllocation = m_AllocationObjectAllocator.Allocate(VMA_FRAME_INDEX_LOST, false);
(*pAllocation)->Ctor(VMA_FRAME_INDEX_LOST, false);
(*pAllocation)->InitLost(); (*pAllocation)->InitLost();
} }