Update: switch over to all arrays being allocated by my own memory arena. Prevents stack overflow for high entity counts

This commit is contained in:
Rodolfo Barcelli Jo 2026-01-23 16:37:29 +08:00
parent 60b0c7140c
commit 7f0724fe55

48
becs.h
View file

@ -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);