Skip to content

Commit

Permalink
safe_struct: Make FreePnextChain() non-recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyg-lunarg committed Jun 10, 2024
1 parent 741921e commit 07759f0
Show file tree
Hide file tree
Showing 2 changed files with 612 additions and 616 deletions.
34 changes: 16 additions & 18 deletions scripts/generators/safe_struct_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,28 +330,31 @@ def generateUtil(self):
}
void FreePnextChain(const void *pNext) {
if (!pNext) return;
// The pNext parameter is const for convenience, since it is called by code
// for many structures where the pNext field is const.
void *current = const_cast<void*>(pNext);
while (current) {
auto header = reinterpret_cast<VkBaseOutStructure *>(current);
void *next = header->pNext;
// prevent destructors from recursing behind our backs.
header->pNext = nullptr;
auto header = reinterpret_cast<const VkBaseOutStructure *>(pNext);
switch (header->sType) {
// Special-case Loader Instance Struct passed to/from layer in pNext chain
switch (header->sType) {
// Special-case Loader Instance Struct passed to/from layer in pNext chain
case VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO:
FreePnextChain(header->pNext);
delete reinterpret_cast<const VkLayerInstanceCreateInfo *>(pNext);
delete reinterpret_cast<VkLayerInstanceCreateInfo *>(current);
break;
// Special-case Loader Device Struct passed to/from layer in pNext chain
case VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO:
FreePnextChain(header->pNext);
delete reinterpret_cast<const VkLayerDeviceCreateInfo *>(pNext);
delete reinterpret_cast<VkLayerDeviceCreateInfo *>(current);
break;
''')

for struct in [x for x in self.vk.structs.values() if x.extends]:
safe_name = self.convertName(struct.name)
out.extend(guard_helper.add_guard(struct.protect))
out.append(f' case {struct.sType}:\n')
out.append(f' delete reinterpret_cast<const {safe_name} *>(header);\n')
out.append(f' delete reinterpret_cast<{safe_name} *>(header);\n')
out.append(' break;\n')
out.extend(guard_helper.add_guard(None))

Expand All @@ -360,18 +363,13 @@ def generateUtil(self):
// If sType is in custom list, free custom struct memory and clean up
for (auto item : custom_stype_info) {
if (item.first == static_cast<uint32_t>(header->sType)) {
if (header->pNext) {
FreePnextChain(header->pNext);
}
free(const_cast<void *>(pNext));
pNext = nullptr;
free(current);
break;
}
}
if (pNext) {
FreePnextChain(header->pNext);
}
break;
}
current = next;
}
}''')
out.append('// clang-format on\n')
Expand Down
Loading

0 comments on commit 07759f0

Please sign in to comment.