From 7f0724fe55c9b9d1b149785e118c887356f91bc9 Mon Sep 17 00:00:00 2001 From: Rodolfo Barcelli Jo Date: Fri, 23 Jan 2026 16:37:29 +0800 Subject: [PATCH] Update: switch over to all arrays being allocated by my own memory arena. Prevents stack overflow for high entity counts --- becs.h | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/becs.h b/becs.h index 56da0d0..0fd2011 100644 --- a/becs.h +++ b/becs.h @@ -74,21 +74,38 @@ struct BECS_ComponentPool { size_t unitSize; size_t capacity; size_t count; - uint32_t sparseArray[BECS_MAX_ENTITIES]; + uint32_t *sparseArray; void *denseArray; BECS_Entity *denseEntityMapping; }; +/* +struct BECS_ECS { + BECS_Arena arena; + + BECS_Entity nextEntity; + uint32_t entityCount; + BECS_Signature entitySignatures[BECS_MAX_ENTITIES]; + bool aliveEntities[BECS_MAX_ENTITIES]; + + BECS_Entity freeEntities[BECS_MAX_ENTITIES]; + uint32_t freeEntityCount; + + uint32_t componentTypeCount; + BECS_ComponentPool componentPools[BECS_MAX_COMPONENTS]; +}; +*/ + struct BECS_ECS { BECS_Arena arena; /* Entity Management */ BECS_Entity nextEntity; uint32_t entityCount; - BECS_Signature entitySignatures[BECS_MAX_ENTITIES]; - bool aliveEntities[BECS_MAX_ENTITIES]; + BECS_Signature *entitySignatures; + bool *aliveEntities; - BECS_Entity freeEntities[BECS_MAX_ENTITIES]; + BECS_Entity *freeEntities; uint32_t freeEntityCount; /* Component Management */ @@ -135,6 +152,7 @@ static void *BECS_AllocateMemory(BECS_Arena *arena, size_t size) size_t totalSizeBytes = size; uintptr_t nextAllocOffset = arena->nextAllocation + ((64 - (arena->nextAllocation % 64)) & 63); + assert(nextAllocOffset + totalSizeBytes <= arena->capacity); if (nextAllocOffset + totalSizeBytes <= arena->capacity) { arena->nextAllocation = nextAllocOffset + totalSizeBytes; @@ -160,6 +178,7 @@ static void BECS_ComponentPoolInit pool->denseArray = BECS_AllocateMemory(arena, componentSize * capacity); pool->denseEntityMapping = (BECS_Entity *)BECS_AllocateMemory(arena, sizeof(BECS_Entity) * capacity); + pool->sparseArray = BECS_AllocateMemory(arena, sizeof(uint32_t) * BECS_MAX_ENTITIES); uint32_t i; for (i = 0; i < BECS_MAX_ENTITIES; i++) @@ -239,10 +258,20 @@ size_t BECS_GetMinMemoryArenaSize(size_t *componentSizes, size_t arrayLength) assert(arrayLength <= BECS_MAX_COMPONENTS); size_t totalSize = 0; - totalSize += sizeof(BECS_ECS); - totalSize += sizeof(BECS_ComponentPool) * BECS_MAX_COMPONENTS; + // totalSize += sizeof(BECS_ECS); + // totalSize += sizeof(BECS_ComponentPool) * BECS_MAX_COMPONENTS; + totalSize += sizeof(uint32_t) * BECS_MAX_ENTITIES * BECS_MAX_COMPONENTS; totalSize += sizeof(BECS_Entity) * BECS_MAX_ENTITIES * BECS_MAX_COMPONENTS; + /* Signatures Array */ + totalSize += sizeof(BECS_Signature) * BECS_MAX_ENTITIES; + + /* Alive Array */ + totalSize += sizeof(bool) * BECS_MAX_ENTITIES; + + /* Free List Array */ + totalSize += sizeof(BECS_Entity) * BECS_MAX_ENTITIES; + size_t sumCompSizes = 0; uint32_t i; for (i = 0; i < arrayLength; i++) @@ -253,6 +282,10 @@ size_t BECS_GetMinMemoryArenaSize(size_t *componentSizes, size_t arrayLength) sumCompSizes *= BECS_MAX_ENTITIES; totalSize += sumCompSizes; + /* Conservative Alignment Buffer */ + size_t numAllocations = 3 + 3 * arrayLength; + totalSize += numAllocations * 64; + return totalSize; } @@ -265,6 +298,9 @@ BECS_ECS BECS_InitECS(void *memory, size_t size) BECS_ECS ecs; ecs.arena = BECS_CreateArena(memory, size); + ecs.entitySignatures = (BECS_Signature *)BECS_AllocateMemory(&ecs.arena, sizeof(BECS_Signature) * BECS_MAX_ENTITIES); + ecs.aliveEntities = (bool *)BECS_AllocateMemory(&ecs.arena, sizeof(bool) * BECS_MAX_ENTITIES); + ecs.freeEntities = (BECS_Entity *)BECS_AllocateMemory(&ecs.arena, sizeof(BECS_Entity) * BECS_MAX_ENTITIES); ecs.entityCount = 0; memset(ecs.entitySignatures, 0, sizeof(BECS_Signature) * BECS_MAX_ENTITIES); memset(ecs.aliveEntities, false, sizeof(bool) * BECS_MAX_ENTITIES);