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;
@@ -109,26 +110,47 @@ static VkImage g_hTextureImage;
109110static VmaAllocation g_hTextureImageAlloc;
110111static VkImageView g_hTextureImageView;
111112
113+ static std::atomic_uint32_t g_CpuAllocCount;
114+
112115static void * CustomCpuAllocation (
113116 void * pUserData, size_t size, size_t alignment,
114117 VkSystemAllocationScope allocationScope)
115118{
116119 assert (pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
117- return _aligned_malloc (size, alignment);
120+ void * const result = _aligned_malloc (size, alignment);
121+ if (result)
122+ {
123+ ++g_CpuAllocCount;
124+ }
125+ return result;
118126}
119127
120128static void * CustomCpuReallocation (
121129 void * pUserData, void * pOriginal, size_t size, size_t alignment,
122130 VkSystemAllocationScope allocationScope)
123131{
124132 assert (pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
125- return _aligned_realloc (pOriginal, size, alignment);
133+ void * const result = _aligned_realloc (pOriginal, size, alignment);
134+ if (pOriginal && !result)
135+ {
136+ --g_CpuAllocCount;
137+ }
138+ else if (!pOriginal && result)
139+ {
140+ ++g_CpuAllocCount;
141+ }
142+ return result;
126143}
127144
128145static void CustomCpuFree (void * pUserData, void * pMemory)
129146{
130147 assert (pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
131- _aligned_free (pMemory);
148+ if (pMemory)
149+ {
150+ const uint32_t oldAllocCount = g_CpuAllocCount.fetch_sub (1 );
151+ TEST (oldAllocCount > 0 );
152+ _aligned_free (pMemory);
153+ }
132154}
133155
134156static const VkAllocationCallbacks g_CpuAllocationCallbacks = {
@@ -1836,6 +1858,8 @@ int main()
18361858 DrawFrame ();
18371859 }
18381860
1861+ TEST (g_CpuAllocCount.load () == 0 );
1862+
18391863 return 0 ;
18401864}
18411865
0 commit comments