From cf283b6d99971bb933af63fc8106e6af1a573c51 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Mon, 23 Mar 2026 09:55:59 +1000 Subject: [PATCH] revert growing_array_move revision --- src/game/memory.c | 54 ++++++++++++++--------------------------------- src/game/memory.h | 2 +- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/game/memory.c b/src/game/memory.c index da800b996..11b98317e 100644 --- a/src/game/memory.c +++ b/src/game/memory.c @@ -233,52 +233,30 @@ void *growing_array_alloc(struct GrowingArray *array, u32 size) { return NULL; } -// This function can move elements to an overlapping region -// `to` is the destination index in the array before any operations -// Does not expand the array if `to + count` exceeds capacity -bool growing_array_move(struct GrowingArray *array, u32 from, u32 to, u32 count) { - if (!array || !array->buffer) { return false; } - if (count == 0) { return true; } - if (from >= array->count) { return false; } - if (from + count > array->count) { return false; } - if (to > array->count) { return false; } - if (to + count > array->count) { return false; } - if (from == to) { return true; } +void growing_array_move(struct GrowingArray *array, u32 from, u32 to, u32 count) { + if (array && array->buffer && count > 0 && + (to < from || to > from + count) && + (from + count) <= array->count && to <= array->count) { - // Use stack memory for small moves (faster) - // Use heap memory for large moves (dynamic size) - void **temp; - void *stackTemp[64]; - if (count <= 64) { - temp = stackTemp; - } else { - temp = (void **) malloc(sizeof(void *) * count); - if (!temp) { return false; } - } + void **temp = malloc(sizeof(void *) * count); + if (!temp) { return; } - // Copy elements to move to temporary buffer - memcpy(temp, array->buffer + from, sizeof(void *) * count); + // Copy elements to move to temporary buffer + memcpy(temp, array->buffer + from, sizeof(void *) * count); - // Remove copied elements from the array - u32 tailCount = array->count - (from + count); - if (tailCount > 0) { - memmove(array->buffer + from, array->buffer + from + count, sizeof(void *) * tailCount); - } + // Remove copied elements from the array + memmove(array->buffer + from, array->buffer + (from + count), sizeof(void *) * (array->count - (from + count))); - // Make place for the copied elements - u32 numToShift = (array->count - count) - to; - if (numToShift > 0) { - memmove(array->buffer + to + count, array->buffer + to, sizeof(void *) * numToShift); - } + // Make place for the copied elements + // If moving left to right, account for the removed elements + if (to > from) { to -= count; } + memmove(array->buffer + (to + count), array->buffer + to, sizeof(void *) * (array->count - (to + count))); - // Insert copied elements - memcpy(array->buffer + to, temp, sizeof(void *) * count); + // Insert copied elements + memcpy(array->buffer + to, temp, sizeof(void *) * count); - if (count > 64) { free(temp); } - - return true; } void growing_array_free(struct GrowingArray **array) { diff --git a/src/game/memory.h b/src/game/memory.h index 62bd5c7eb..d8f469814 100644 --- a/src/game/memory.h +++ b/src/game/memory.h @@ -77,7 +77,7 @@ void growing_pool_free_pool(struct GrowingPool *pool); struct GrowingArray *growing_array_init(struct GrowingArray *array, u32 capacity, GrowingArrayAllocFunc alloc, GrowingArrayFreeFunc free); void *growing_array_alloc(struct GrowingArray *array, u32 size); -bool growing_array_move(struct GrowingArray *array, u32 from, u32 to, u32 count); +void growing_array_move(struct GrowingArray *array, u32 from, u32 to, u32 count); void growing_array_free(struct GrowingArray **array); void growing_array_debug_print(struct GrowingArray *array, const char *name, s32 x, s32 y);