Skip to content

Commit e31dd15

Browse files
Improvement in function vmaDefragmentationBegin. Added validation of input arrays.
1 parent a12a7fe commit e31dd15

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/vk_mem_alloc.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,6 +3718,32 @@ static IterT VmaBinaryFindFirstNotLess(IterT beg, IterT end, const KeyT &key, Cm
37183718
return beg + down;
37193719
}
37203720

3721+
/*
3722+
Returns true if all pointers in the array are not-null and unique.
3723+
Warning! O(n^2) complexity. Use only inside VMA_HEAVY_ASSERT.
3724+
T must be pointer type, e.g. VmaAllocation, VmaPool.
3725+
*/
3726+
template<typename T>
3727+
static bool VmaValidatePointerArray(uint32_t count, const T* arr)
3728+
{
3729+
for(uint32_t i = 0; i < count; ++i)
3730+
{
3731+
const T iPtr = arr[i];
3732+
if(iPtr == VMA_NULL)
3733+
{
3734+
return false;
3735+
}
3736+
for(uint32_t j = i + 1; j < count; ++j)
3737+
{
3738+
if(iPtr == arr[j])
3739+
{
3740+
return false;
3741+
}
3742+
}
3743+
}
3744+
return true;
3745+
}
3746+
37213747
////////////////////////////////////////////////////////////////////////////////
37223748
// Memory allocation
37233749

@@ -16022,8 +16048,17 @@ VkResult vmaDefragmentationBegin(
1602216048
VmaDefragmentationContext *pContext)
1602316049
{
1602416050
VMA_ASSERT(allocator && pInfo && pContext);
16051+
16052+
// Degenerate case: Nothing to defragment.
16053+
if(pInfo->allocationCount == 0 && pInfo->poolCount == 0)
16054+
{
16055+
return VK_SUCCESS;
16056+
}
16057+
1602516058
VMA_ASSERT(pInfo->allocationCount == 0 || pInfo->pAllocations != VMA_NULL);
1602616059
VMA_ASSERT(pInfo->poolCount == 0 || pInfo->pPools != VMA_NULL);
16060+
VMA_HEAVY_ASSERT(VmaValidatePointerArray(pInfo->allocationCount, pInfo->pAllocations));
16061+
VMA_HEAVY_ASSERT(VmaValidatePointerArray(pInfo->poolCount, pInfo->pPools));
1602716062

1602816063
VMA_DEBUG_LOG("vmaDefragmentationBegin");
1602916064

0 commit comments

Comments
 (0)