Skip to content

Commit 8317ba9

Browse files
Sample/testing app: enable custom CPU allocation callbacks by default
Added allocation counting to make sure there are no memory leaks at the end.
1 parent b3f5110 commit 8317ba9

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

src/VulkanSample.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "Tests.h"
2727
#include "VmaUsage.h"
2828
#include "Common.h"
29+
#include <atomic>
2930

3031
static const char* const SHADER_PATH1 = "./";
3132
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
3738
static const bool VSYNC = true;
3839
static const uint32_t COMMAND_BUFFER_COUNT = 2;
3940
static 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

4243
VkPhysicalDevice g_hPhysicalDevice;
4344
VkDevice g_hDevice;
@@ -109,26 +110,47 @@ static VkImage g_hTextureImage;
109110
static VmaAllocation g_hTextureImageAlloc;
110111
static VkImageView g_hTextureImageView;
111112

113+
static std::atomic_uint32_t g_CpuAllocCount;
114+
112115
static 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

120128
static 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

128145
static 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

134156
static 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

Comments
 (0)