2026-05-10 13:36:52 -07:00
|
|
|
// SPDX-FileCopyrightText: 2025 Erin Catto
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
#include "box3d/base.h"
|
2026-05-10 23:50:16 -07:00
|
|
|
#include "container.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
|
|
#define B3_MAX_STACK_ENTRIES 32
|
|
|
|
|
|
|
|
|
|
typedef struct b3StackEntry
|
|
|
|
|
{
|
|
|
|
|
char* data;
|
|
|
|
|
const char* name;
|
|
|
|
|
int size;
|
|
|
|
|
bool usedMalloc;
|
|
|
|
|
} b3StackEntry;
|
|
|
|
|
|
|
|
|
|
// This is a stack-like arena allocator used for fast per step allocations.
|
|
|
|
|
// You must nest allocate/free pairs. The code will B3_ASSERT
|
|
|
|
|
// if you try to interleave multiple allocate/free pairs.
|
|
|
|
|
// This allocator uses the heap if space is insufficient.
|
|
|
|
|
// I could remove the need to free entries individually.
|
|
|
|
|
typedef struct b3Stack
|
|
|
|
|
{
|
|
|
|
|
char* memory;
|
|
|
|
|
int capacity;
|
|
|
|
|
int index;
|
|
|
|
|
|
|
|
|
|
int allocation;
|
|
|
|
|
int maxAllocation;
|
|
|
|
|
|
|
|
|
|
b3StackEntry entries[B3_MAX_STACK_ENTRIES];
|
|
|
|
|
int entryCount;
|
|
|
|
|
} b3Stack;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
// Heap-allocated fallback block tracked when an arena bump overflows.
|
|
|
|
|
typedef struct b3OverflowBlock
|
|
|
|
|
{
|
|
|
|
|
char* data;
|
|
|
|
|
int size;
|
|
|
|
|
} b3OverflowBlock;
|
|
|
|
|
|
|
|
|
|
b3DeclareArray( b3OverflowBlock );
|
|
|
|
|
|
|
|
|
|
// Shared, heap-allocated state co-owned by every copy of a b3Arena.
|
|
|
|
|
// b3Arena is passed by value so its bump pointer auto-restores on
|
|
|
|
|
// function return, but overflow tracking and watermarks must persist
|
|
|
|
|
// across copies -- hence this pointer-shared block.
|
|
|
|
|
typedef struct b3ArenaSharedState
|
|
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
b3Array( b3OverflowBlock ) overflows;
|
2026-05-10 23:50:16 -07:00
|
|
|
int maxIndex; // high water mark of the bump pointer this step
|
|
|
|
|
int overflowBytes; // total bytes in overflow blocks this step
|
|
|
|
|
int peakDemand; // all-time peak of (maxIndex + overflowBytes), survives sync
|
|
|
|
|
} b3ArenaSharedState;
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
typedef struct b3Arena
|
|
|
|
|
{
|
|
|
|
|
char* memory;
|
|
|
|
|
int capacity;
|
|
|
|
|
int index;
|
2026-05-10 23:50:16 -07:00
|
|
|
b3ArenaSharedState* shared;
|
2026-05-10 13:36:52 -07:00
|
|
|
} b3Arena;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
// 16-byte alignment for SSE2 + typical struct alignment.
|
|
|
|
|
#define B3_ARENA_ALIGNMENT 16
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
b3Stack b3CreateStack( int capacity );
|
|
|
|
|
void b3DestroyStack( b3Stack* stack );
|
|
|
|
|
|
|
|
|
|
void* b3StackAlloc( b3Stack* stack, int size, const char* name );
|
|
|
|
|
void b3StackFree( b3Stack* stack, void* mem );
|
|
|
|
|
|
|
|
|
|
// Grow the stack based on usage
|
|
|
|
|
void b3GrowStack( b3Stack* stack );
|
|
|
|
|
|
|
|
|
|
int b3GetStackCapacity( b3Stack* stack );
|
|
|
|
|
int b3GetStackAllocation( b3Stack* stack );
|
|
|
|
|
int b3GetMaxStackAllocation( b3Stack* stack );
|
|
|
|
|
|
|
|
|
|
b3Arena b3CreateArena( int capacity );
|
|
|
|
|
void b3DestroyArena( b3Arena* arena );
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
// Heap-allocate an overflow block, register it in the shared state, return it.
|
|
|
|
|
void* b3ArenaOverflowAlloc( b3Arena* arena, int size );
|
|
|
|
|
|
|
|
|
|
// Call between simulation steps. Frees this step's overflow blocks and grows
|
|
|
|
|
// the backing capacity if last step's demand (maxIndex + overflowBytes) exceeded it.
|
|
|
|
|
void b3ArenaSync( b3Arena* arena );
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
static inline void* b3Bump( b3Arena* arena, int size )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( size == 0 )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
int aligned = ( arena->index + ( B3_ARENA_ALIGNMENT - 1 ) ) & ~( B3_ARENA_ALIGNMENT - 1 );
|
|
|
|
|
|
|
|
|
|
if ( aligned + size > arena->capacity )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
return b3ArenaOverflowAlloc( arena, size );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
arena->index = aligned + size;
|
|
|
|
|
if ( arena->index > arena->shared->maxIndex )
|
|
|
|
|
{
|
|
|
|
|
arena->shared->maxIndex = arena->index;
|
|
|
|
|
}
|
|
|
|
|
return arena->memory + aligned;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|