From fbd9e6eae64c2cd0f044ef35c75e846b1df4b77a Mon Sep 17 00:00:00 2001 From: Adam Sawicki Date: Fri, 9 May 2025 12:55:22 +0200 Subject: [PATCH] Code improvements based on static analysis See #488 --- include/vk_mem_alloc.h | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/include/vk_mem_alloc.h b/include/vk_mem_alloc.h index b9c5d1c..3878a68 100644 --- a/include/vk_mem_alloc.h +++ b/include/vk_mem_alloc.h @@ -3982,7 +3982,7 @@ VmaBufferImageUsage::VmaBufferImageUsage(const VkBufferCreateInfo &createInfo, // of the VK_KHR_maintenance5 extension. const VkBufferUsageFlags2CreateInfoKHR* const usageFlags2 = VmaPnextChainFind(&createInfo, VK_STRUCTURE_TYPE_BUFFER_USAGE_FLAGS_2_CREATE_INFO_KHR); - if(usageFlags2 != 0) + if(usageFlags2 != VMA_NULL) { this->Value = usageFlags2->usage; return; @@ -10153,7 +10153,6 @@ public: explicit VmaVirtualBlock_T(const VmaVirtualBlockCreateInfo& createInfo); ~VmaVirtualBlock_T(); - VkResult Init() { return VK_SUCCESS; } bool IsEmpty() const { return m_Metadata->IsEmpty(); } void Free(VmaVirtualAllocation allocation) { m_Metadata->Free((VmaAllocHandle)allocation); } void SetAllocationUserData(VmaVirtualAllocation allocation, void* userData) { m_Metadata->SetAllocationUserData((VmaAllocHandle)allocation, userData); } @@ -11412,7 +11411,7 @@ VkResult VmaBlockVector::AllocatePage( { const bool isUpperAddress = (createInfo.flags & VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT) != 0; - VkDeviceSize freeMemory; + VkDeviceSize freeMemory = 0; { const uint32_t heapIndex = m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex); VmaBudget heapBudget = {}; @@ -11993,6 +11992,8 @@ VmaDefragmentationContext_T::VmaDefragmentationContext_T( m_AlgorithmState = vma_new_array(hAllocator, StateExtensive, m_BlockVectorCount); } break; + default: + ; // Do nothing. } } @@ -12089,11 +12090,10 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo size_t currentCount = 0; VkDeviceSize freedBlockSize = 0; - uint32_t vectorIndex; - VmaBlockVector* vector; + uint32_t vectorIndex = 0; + VmaBlockVector* vector = VMA_NULL; if (m_PoolBlockVector != VMA_NULL) { - vectorIndex = 0; vector = m_PoolBlockVector; } else @@ -12178,7 +12178,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo } freedBlockSize *= prevCount - currentCount; - VkDeviceSize dstBlockSize; + VkDeviceSize dstBlockSize = SIZE_MAX; { VmaMutexLockRead lock(vector->GetMutex(), vector->GetAllocator()->m_UseMutex); dstBlockSize = move.dstTmpAllocation->GetBlock()->m_pMetadata->GetSize(); @@ -12354,8 +12354,7 @@ VmaDefragmentationContext_T::CounterStatus VmaDefragmentationContext_T::CheckCou { if (++m_IgnoredAllocs < MAX_ALLOCS_TO_IGNORE) return CounterStatus::Ignore; - else - return CounterStatus::End; + return CounterStatus::End; } m_IgnoredAllocs = 0; @@ -14574,11 +14573,9 @@ VkResult VmaAllocator_T::BindVulkanBuffer( bindBufferMemoryInfo.memoryOffset = memoryOffset; return (*m_VulkanFunctions.vkBindBufferMemory2KHR)(m_hDevice, 1, &bindBufferMemoryInfo); } - else #endif // #if VMA_VULKAN_VERSION >= 1001000 || VMA_BIND_MEMORY2 - { - return VK_ERROR_EXTENSION_NOT_PRESENT; - } + + return VK_ERROR_EXTENSION_NOT_PRESENT; } else { @@ -15543,7 +15540,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForBufferInfo( const VkDevice hDev = allocator->m_hDevice; const VmaVulkanFunctions* funcs = &allocator->GetVulkanFunctions(); - VkResult res; + VkResult res = VK_SUCCESS; #if VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000 if(funcs->vkGetDeviceBufferMemoryRequirements) @@ -15595,7 +15592,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo( const VkDevice hDev = allocator->m_hDevice; const VmaVulkanFunctions* funcs = &allocator->GetVulkanFunctions(); - VkResult res; + VkResult res = VK_SUCCESS; #if VMA_KHR_MAINTENANCE4 || VMA_VULKAN_VERSION >= 1003000 if(funcs->vkGetDeviceImageMemoryRequirements) @@ -16712,6 +16709,11 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateVirtualBlock( VMA_DEBUG_LOG("vmaCreateVirtualBlock"); VMA_DEBUG_GLOBAL_MUTEX_LOCK; *pVirtualBlock = vma_new(pCreateInfo->pAllocationCallbacks, VmaVirtualBlock_T)(*pCreateInfo); + return VK_SUCCESS; + + /* + Code for the future if we ever need a separate Init() method that could fail: + VkResult res = (*pVirtualBlock)->Init(); if(res < 0) { @@ -16719,6 +16721,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateVirtualBlock( *pVirtualBlock = VK_NULL_HANDLE; } return res; + */ } VMA_CALL_PRE void VMA_CALL_POST vmaDestroyVirtualBlock(VmaVirtualBlock VMA_NULLABLE virtualBlock)