[Core] Fix self referencing node bug in hashmap pruning

This commit is contained in:
Nic Barker 2026-05-05 15:48:01 +10:00
parent 8c6ab72890
commit 1dc7473d47

20
clay.h
View file

@ -4750,29 +4750,29 @@ Clay_RenderCommandArray Clay_EndLayout(float deltaTime) {
for (int i = 0; i < context->layoutElementsHashMap.capacity; ++i) {
int32_t currentElementIndex = context->layoutElementsHashMap.internalArray[i];
int32_t previousElementIndex = -1;
int32_t listDepth = 0;
while (currentElementIndex != -1) {
Clay_LayoutElementHashMapItem* currentItem = Clay__LayoutElementHashMapItemArray_Get(&context->layoutElementsHashMapInternal, currentElementIndex);
int32_t nextIndex = currentItem->nextIndex;
// Needs to be pruned
if (currentItem->generation <= context->generation) {
// Delete the underlying item and add it to the freelist
Clay__LayoutElementHashMapItemArray_Set(&context->layoutElementsHashMapInternal, currentElementIndex, CLAY__INIT(Clay_LayoutElementHashMapItem) { .nextIndex = -1 });
Clay__int32_tArray_Add(&context->layoutElementsHashMapFreeList, currentElementIndex);
// If it's the very top of the bucket, rewrite the first bucket pointer
if (listDepth == 0) {
if (previousElementIndex == -1) {
Clay__int32_tArray_Set(&context->layoutElementsHashMap, i, nextIndex);
listDepth--;
currentElementIndex = nextIndex;
previousElementIndex = -1;
} else {
// Rewrite previous pointer
Clay_LayoutElementHashMapItem* previousItem = Clay__LayoutElementHashMapItemArray_Get(&context->layoutElementsHashMapInternal, previousElementIndex);
previousItem->nextIndex = nextIndex;
currentElementIndex = nextIndex;
}
// Delete the underlying item and add it to the freelist
Clay__LayoutElementHashMapItemArray_Set(&context->layoutElementsHashMapInternal, currentElementIndex, CLAY__INIT(Clay_LayoutElementHashMapItem) { .nextIndex = -1 });
Clay__int32_tArray_Add(&context->layoutElementsHashMapFreeList, currentElementIndex);
} else {
previousElementIndex = currentElementIndex;
currentElementIndex = nextIndex;
}
previousElementIndex = currentElementIndex;
currentElementIndex = nextIndex;
listDepth++;
}
}