2626#include " Tests.h"
2727#include " VmaUsage.h"
2828#include " Common.h"
29+ #include < atomic>
2930
3031static const char * const SHADER_PATH1 = " ./" ;
3132static 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
3738static const bool VSYNC = true ;
3839static const uint32_t COMMAND_BUFFER_COUNT = 2 ;
3940static void * const CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA = (void *)(intptr_t )43564544 ;
40- static const bool USE_CUSTOM_CPU_ALLOCATION_CALLBACKS = false ;
41+ static const bool USE_CUSTOM_CPU_ALLOCATION_CALLBACKS = true ;
4142
4243VkPhysicalDevice g_hPhysicalDevice;
4344VkDevice g_hDevice;
@@ -111,26 +112,47 @@ static VkImage g_hTextureImage;
111112static VmaAllocation g_hTextureImageAlloc;
112113static VkImageView g_hTextureImageView;
113114
115+ static std::atomic_uint32_t g_CpuAllocCount;
116+
114117static void * CustomCpuAllocation (
115118 void * pUserData, size_t size, size_t alignment,
116119 VkSystemAllocationScope allocationScope)
117120{
118121 assert (pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
119- return _aligned_malloc (size, alignment);
122+ void * const result = _aligned_malloc (size, alignment);
123+ if (result)
124+ {
125+ ++g_CpuAllocCount;
126+ }
127+ return result;
120128}
121129
122130static void * CustomCpuReallocation (
123131 void * pUserData, void * pOriginal, size_t size, size_t alignment,
124132 VkSystemAllocationScope allocationScope)
125133{
126134 assert (pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
127- return _aligned_realloc (pOriginal, size, alignment);
135+ void * const result = _aligned_realloc (pOriginal, size, alignment);
136+ if (pOriginal && !result)
137+ {
138+ --g_CpuAllocCount;
139+ }
140+ else if (!pOriginal && result)
141+ {
142+ ++g_CpuAllocCount;
143+ }
144+ return result;
128145}
129146
130147static void CustomCpuFree (void * pUserData, void * pMemory)
131148{
132149 assert (pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
133- _aligned_free (pMemory);
150+ if (pMemory)
151+ {
152+ const uint32_t oldAllocCount = g_CpuAllocCount.fetch_sub (1 );
153+ TEST (oldAllocCount > 0 );
154+ _aligned_free (pMemory);
155+ }
134156}
135157
136158static const VkAllocationCallbacks g_CpuAllocationCallbacks = {
@@ -1873,6 +1895,8 @@ int main()
18731895 DrawFrame ();
18741896 }
18751897
1898+ TEST (g_CpuAllocCount.load () == 0 );
1899+
18761900 return 0 ;
18771901}
18781902
0 commit comments