Skip to content

Commit a020fb8

Browse files
Add prototype function vmaGetName, vmaSetName, change JSON format and VmaDumpVis.py to use that
1 parent 6ac1d3a commit a020fb8

3 files changed

Lines changed: 119 additions & 18 deletions

File tree

src/Tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,20 @@ static void TestPool_SameSize()
31043104
res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
31053105
TEST(res == VK_SUCCESS);
31063106

3107+
// Test pool name
3108+
{
3109+
static const char* const POOL_NAME = "Pool name";
3110+
vmaSetPoolName(g_hAllocator, pool, POOL_NAME);
3111+
3112+
const char* fetchedPoolName = nullptr;
3113+
vmaGetPoolName(g_hAllocator, pool, &fetchedPoolName);
3114+
TEST(strcmp(fetchedPoolName, POOL_NAME) == 0);
3115+
3116+
SaveAllocatorStatsToFile(L"TEST.json");//DELME
3117+
3118+
vmaSetPoolName(g_hAllocator, pool, nullptr);
3119+
}
3120+
31073121
vmaSetCurrentFrameIndex(g_hAllocator, 1);
31083122

31093123
VmaAllocationCreateInfo allocInfo = {};

src/vk_mem_alloc.h

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,6 +2512,20 @@ Possible return values:
25122512
*/
25132513
VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckPoolCorruption(VmaAllocator allocator, VmaPool pool);
25142514

2515+
/** TODO
2516+
*/
2517+
VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName(
2518+
VmaAllocator allocator,
2519+
VmaPool pool,
2520+
const char** ppName);
2521+
2522+
/* TODO
2523+
*/
2524+
VMA_CALL_PRE void VMA_CALL_POST vmaSetPoolName(
2525+
VmaAllocator allocator,
2526+
VmaPool pool,
2527+
const char* pName);
2528+
25152529
/** \struct VmaAllocation
25162530
\brief Represents single memory allocation.
25172531

@@ -4038,6 +4052,30 @@ static void vma_delete_array(const VkAllocationCallbacks* pAllocationCallbacks,
40384052
}
40394053
}
40404054

4055+
static char* VmaCreateStringCopy(const VkAllocationCallbacks* allocs, const char* srcStr)
4056+
{
4057+
if(srcStr != VMA_NULL)
4058+
{
4059+
const size_t len = strlen(srcStr);
4060+
char* const result = vma_new_array(allocs, char, len + 1);
4061+
memcpy(result, srcStr, len + 1);
4062+
return result;
4063+
}
4064+
else
4065+
{
4066+
return VMA_NULL;
4067+
}
4068+
}
4069+
4070+
static void VmaFreeString(const VkAllocationCallbacks* allocs, char* str)
4071+
{
4072+
if(str != VMA_NULL)
4073+
{
4074+
const size_t len = strlen(str);
4075+
vma_delete_array(allocs, str, len + 1);
4076+
}
4077+
}
4078+
40414079
// STL-compatible allocator.
40424080
template<typename T>
40434081
class VmaStlAllocator
@@ -5994,6 +6032,7 @@ struct VmaBlockVector
59946032

59956033
VkResult CreateMinBlocks();
59966034

6035+
VmaAllocator GetAllocator() const { return m_hAllocator; }
59976036
VmaPool GetParentPool() const { return m_hParentPool; }
59986037
uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; }
59996038
VkDeviceSize GetPreferredBlockSize() const { return m_PreferredBlockSize; }
@@ -6135,12 +6174,16 @@ struct VmaPool_T
61356174
uint32_t GetId() const { return m_Id; }
61366175
void SetId(uint32_t id) { VMA_ASSERT(m_Id == 0); m_Id = id; }
61376176

6177+
const char* GetName() const { return m_Name; }
6178+
void SetName(const char* pName);
6179+
61386180
#if VMA_STATS_STRING_ENABLED
61396181
//void PrintDetailedMap(class VmaStringBuilder& sb);
61406182
#endif
61416183

61426184
private:
61436185
uint32_t m_Id;
6186+
char* m_Name;
61446187
};
61456188

61466189
/*
@@ -7388,11 +7431,7 @@ void VmaAllocation_T::SetUserData(VmaAllocator hAllocator, void* pUserData)
73887431

73897432
if(pUserData != VMA_NULL)
73907433
{
7391-
const char* const newStrSrc = (char*)pUserData;
7392-
const size_t newStrLen = strlen(newStrSrc);
7393-
char* const newStrDst = vma_new_array(hAllocator, char, newStrLen + 1);
7394-
memcpy(newStrDst, newStrSrc, newStrLen + 1);
7395-
m_pUserData = newStrDst;
7434+
m_pUserData = VmaCreateStringCopy(hAllocator->GetAllocationCallbacks(), (const char*)pUserData);
73967435
}
73977436
}
73987437
else
@@ -7595,13 +7634,8 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
75957634
void VmaAllocation_T::FreeUserDataString(VmaAllocator hAllocator)
75967635
{
75977636
VMA_ASSERT(IsUserDataString());
7598-
if(m_pUserData != VMA_NULL)
7599-
{
7600-
char* const oldStr = (char*)m_pUserData;
7601-
const size_t oldStrLen = strlen(oldStr);
7602-
vma_delete_array(hAllocator, oldStr, oldStrLen + 1);
7603-
m_pUserData = VMA_NULL;
7604-
}
7637+
VmaFreeString(hAllocator->GetAllocationCallbacks(), (char*)m_pUserData);
7638+
m_pUserData = VMA_NULL;
76057639
}
76067640

76077641
void VmaAllocation_T::BlockAllocMap()
@@ -11407,14 +11441,30 @@ VmaPool_T::VmaPool_T(
1140711441
true, // isCustomPool
1140811442
createInfo.blockSize != 0, // explicitBlockSize
1140911443
createInfo.flags & VMA_POOL_CREATE_ALGORITHM_MASK), // algorithm
11410-
m_Id(0)
11444+
m_Id(0),
11445+
m_Name(VMA_NULL)
1141111446
{
1141211447
}
1141311448

1141411449
VmaPool_T::~VmaPool_T()
1141511450
{
1141611451
}
1141711452

11453+
void VmaPool_T::SetName(const char* pName)
11454+
{
11455+
const VkAllocationCallbacks* allocs = m_BlockVector.GetAllocator()->GetAllocationCallbacks();
11456+
VmaFreeString(allocs, m_Name);
11457+
11458+
if(pName != VMA_NULL)
11459+
{
11460+
m_Name = VmaCreateStringCopy(allocs, pName);
11461+
}
11462+
else
11463+
{
11464+
m_Name = VMA_NULL;
11465+
}
11466+
}
11467+
1141811468
#if VMA_STATS_STRING_ENABLED
1141911469

1142011470
#endif // #if VMA_STATS_STRING_ENABLED
@@ -15769,6 +15819,15 @@ void VmaAllocator_T::PrintDetailedMap(VmaJsonWriter& json)
1576915819
{
1577015820
json.BeginString();
1577115821
json.ContinueString(m_Pools[poolIndex]->GetId());
15822+
const char* poolName = m_Pools[poolIndex]->GetName();
15823+
if(poolName != VMA_NULL && poolName[0] != '\0')
15824+
{
15825+
json.ContinueString(" ");
15826+
json.ContinueString(poolName);
15827+
}
15828+
else
15829+
{
15830+
}
1577215831
json.EndString();
1577315832

1577415833
m_Pools[poolIndex]->m_BlockVector.PrintDetailedMap(json);
@@ -16213,6 +16272,34 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckPoolCorruption(VmaAllocator allocato
1621316272
return allocator->CheckPoolCorruption(pool);
1621416273
}
1621516274

16275+
VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName(
16276+
VmaAllocator allocator,
16277+
VmaPool pool,
16278+
const char** ppName)
16279+
{
16280+
VMA_ASSERT(allocator && pool);
16281+
16282+
VMA_DEBUG_LOG("vmaGetPoolName");
16283+
16284+
VMA_DEBUG_GLOBAL_MUTEX_LOCK
16285+
16286+
*ppName = pool->GetName();
16287+
}
16288+
16289+
VMA_CALL_PRE void VMA_CALL_POST vmaSetPoolName(
16290+
VmaAllocator allocator,
16291+
VmaPool pool,
16292+
const char* pName)
16293+
{
16294+
VMA_ASSERT(allocator && pool);
16295+
16296+
VMA_DEBUG_LOG("vmaSetPoolName");
16297+
16298+
VMA_DEBUG_GLOBAL_MUTEX_LOCK
16299+
16300+
pool->SetName(pName);
16301+
}
16302+
1621616303
VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory(
1621716304
VmaAllocator allocator,
1621816305
const VkMemoryRequirements* pVkMemoryRequirements,

tools/VmaDumpVis/VmaDumpVis.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ def BytesToStr(iBytes):
189189
ProcessBlock(typeData['DefaultPoolBlocks'], int(sBlockId), objBlock, '')
190190
if 'Pools' in jsonSrc:
191191
objPools = jsonSrc['Pools']
192-
for sPoolId, objPool in objPools.items():
192+
for sPoolName, objPool in objPools.items():
193193
iType = int(objPool['MemoryTypeIndex'])
194194
typeData = GetDataForMemoryType(iType)
195195
objBlocks = objPool['Blocks']
196196
sAlgorithm = objPool.get('Algorithm', '')
197197
dstBlockArray = []
198-
typeData['CustomPools'][int(sPoolId)] = dstBlockArray
198+
typeData['CustomPools'][sPoolName] = dstBlockArray
199199
for sBlockId, objBlock in objBlocks.items():
200200
ProcessBlock(dstBlockArray, int(sBlockId), objBlock, sAlgorithm)
201201

@@ -249,13 +249,13 @@ def BytesToStr(iBytes):
249249
DrawBlock(draw, y, objBlock)
250250
y += MAP_SIZE + IMG_MARGIN
251251
index = 0
252-
for iPoolId, listPool in dictMemType['CustomPools'].items():
252+
for sPoolName, listPool in dictMemType['CustomPools'].items():
253253
for objBlock in listPool:
254254
if 'Algorithm' in objBlock and objBlock['Algorithm']:
255255
sAlgorithm = ' (Algorithm: %s)' % (objBlock['Algorithm'])
256256
else:
257257
sAlgorithm = ''
258-
draw.text((IMG_MARGIN, y), "Custom pool %d%s block %d" % (iPoolId, sAlgorithm, objBlock['ID']), fill=COLOR_TEXT_H2, font=font)
258+
draw.text((IMG_MARGIN, y), "Custom pool \"%s\"%s block %d" % (sPoolName, sAlgorithm, objBlock['ID']), fill=COLOR_TEXT_H2, font=font)
259259
y += FONT_SIZE + IMG_MARGIN
260260
DrawBlock(draw, y, objBlock)
261261
y += MAP_SIZE + IMG_MARGIN
@@ -274,7 +274,7 @@ def BytesToStr(iBytes):
274274
- Fixed key 'Size'. Value is int.
275275
- Fixed key 'Suballocations'. Value is list of tuples as above.
276276
- Fixed key 'CustomPools'. Value is dictionary.
277-
- Key is integer pool ID. Value is list of objects representing memory blocks, each containing dictionary with:
277+
- Key is string with pool name. Value is list of objects representing memory blocks, each containing dictionary with:
278278
- Fixed key 'ID'. Value is int.
279279
- Fixed key 'Size'. Value is int.
280280
- Fixed key 'Algorithm'. Optional. Value is string.

0 commit comments

Comments
 (0)