Sample/testing app: enable custom CPU allocation callbacks by default

Added allocation counting to make sure there are no memory leaks at the end.
This commit is contained in:
Adam Sawicki 2019-11-18 13:14:11 +01:00
parent b3f5110987
commit 8317ba9d5e

View File

@ -26,6 +26,7 @@
#include "Tests.h"
#include "VmaUsage.h"
#include "Common.h"
#include <atomic>
static const char* const SHADER_PATH1 = "./";
static const char* const SHADER_PATH2 = "../bin/";
@ -37,7 +38,7 @@ static const wchar_t* const APP_TITLE_W = L"Vulkan Memory Allocator Sample 2.3.0
static const bool VSYNC = true;
static const uint32_t COMMAND_BUFFER_COUNT = 2;
static void* const CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA = (void*)(intptr_t)43564544;
static const bool USE_CUSTOM_CPU_ALLOCATION_CALLBACKS = false;
static const bool USE_CUSTOM_CPU_ALLOCATION_CALLBACKS = true;
VkPhysicalDevice g_hPhysicalDevice;
VkDevice g_hDevice;
@ -109,12 +110,19 @@ static VkImage g_hTextureImage;
static VmaAllocation g_hTextureImageAlloc;
static VkImageView g_hTextureImageView;
static std::atomic_uint32_t g_CpuAllocCount;
static void* CustomCpuAllocation(
void* pUserData, size_t size, size_t alignment,
VkSystemAllocationScope allocationScope)
{
assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
return _aligned_malloc(size, alignment);
void* const result = _aligned_malloc(size, alignment);
if(result)
{
++g_CpuAllocCount;
}
return result;
}
static void* CustomCpuReallocation(
@ -122,13 +130,27 @@ static void* CustomCpuReallocation(
VkSystemAllocationScope allocationScope)
{
assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
return _aligned_realloc(pOriginal, size, alignment);
void* const result = _aligned_realloc(pOriginal, size, alignment);
if(pOriginal && !result)
{
--g_CpuAllocCount;
}
else if(!pOriginal && result)
{
++g_CpuAllocCount;
}
return result;
}
static void CustomCpuFree(void* pUserData, void* pMemory)
{
assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
_aligned_free(pMemory);
if(pMemory)
{
const uint32_t oldAllocCount = g_CpuAllocCount.fetch_sub(1);
TEST(oldAllocCount > 0);
_aligned_free(pMemory);
}
}
static const VkAllocationCallbacks g_CpuAllocationCallbacks = {
@ -1836,6 +1858,8 @@ int main()
DrawFrame();
}
TEST(g_CpuAllocCount.load() == 0);
return 0;
}