Skip to content

About

LinearPrisma is a single header file high performance linear allocator, this was developed to be used in my game engine.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LinearPrisma

Single-header linear (bump) allocator designed for performance-critical sections of game engines and real-time systems.

LinearPrisma.h implements a strict linear allocator with explicit support for cache-line layout, SIMD vectorization requirements, zero-overhead alignment, temporary scopes, and safe heap fallback. It was written specifically for my game engine. And as requirement grows ill keep on expanding and improving it.

Integration

Just drop or copy LinearPrisma.h into the project. No build system changes required.

#include "LinearPrisma.h"

Static buffer (recommended hot path)

static LP_FORCED_ALIGN uint8_t hot_arena[8 * 1024 * 1024];   // 8 MiB, compiler/BSS guarantees alignment

LinearPrisma arena;
LP_init(&arena, hot_arena, sizeof(hot_arena));

Optional heap fallback

LP_bind_dynamic(&arena, malloc, free, NULL);
// or your own allocator pair

Core Usage Patterns

Structure-of-Arrays with cache-line split (taken directly from example.c)

typedef struct AE_TranslationLayer {
    alignas(64)               // exactly one cache line of hot data
    float camx, camy, cam_zoom;
    float playerx, playery;
    uint32_t player_state, player_tick, player_effects;
    size_t special_count, special_capacity;
    size_t entity_count, entity_capacity;

    // next cache line begins automatically
    uint32_t *__restrict special_id;
    float    *__restrict specialx, *__restrict specialy;
    // ... additional SoA pointers
} AE_TranslationLayer;

AE_TranslationLayer* layer = LP_PUSH(arena, AE_TranslationLayer);   // alignof = 64, zero-overhead
memset(layer, 0, sizeof(*layer));

layer->special_id = LP_PUSH_ARRAY_ALIGNED(arena, uint32_t, 10000, 32);
layer->specialx   = LP_PUSH_ARRAY_ALIGNED(arena, float,    10000, 32);
// all arrays are 32-byte aligned → safe for AVX2 vector loads

Temporary scratch scope

LP_Temp scratch = LP_temp_begin(&arena);
// allocate any amount (static or heap fallback)
LP_alloc_aligned(&arena, 4096, 64);
LP_temp_end(scratch);   // offsets restored + any temporary heap blocks freed

Resize last allocation

float* positions = LP_PUSH_ARRAY_ALIGNED(&arena, float, 8192, 32);
if (LP_resize_last(&arena, positions, 16384)) {
    // grew in place, no copy, no new alignment work
}

Full API

Initialization

  • void LP_init(LinearPrisma* a, void* buffer, size_t length) – aligns buffer to 64 bytes, stores usable length.
  • void LP_bind_dynamic(LinearPrisma* a, LP_Malloc_Func, LP_Free_Func, void* user_data)

Allocation

  • void* LP_alloc_aligned(LinearPrisma* a, size_t size, size_t alignment) – core routine (power-of-two only).
  • void* LP_alloc(LinearPrisma* a, size_t size) – defaults to 16 bytes.
  • void* LP_alloc_zero(LinearPrisma* a, size_t size)
  • #define LP_PUSH(a, T) – alignof(T)
  • #define LP_PUSH_ARRAY(a, T, n) – alignof(T)
  • #define LP_PUSH_ARRAY_ALIGNED(a, T, n, align)

Special operations

  • int LP_resize_last(LinearPrisma* a, void* ptr, size_t new_size)
  • LP_Temp LP_temp_begin(LinearPrisma* a)
  • void LP_temp_end(LP_Temp t)

Lifetime

  • void LP_reset(LinearPrisma* a) – frees all fallback nodes and rewinds offsets.
  • void LP_destroy(LinearPrisma* a) – alias of reset (for semantic clarity when arena lifetime ends).

Validation

test.c exercises:

  • All power-of-two alignments (1–64) and offset advancement.
  • Fallback path with deliberately misaligned malloc returning correctly aligned payload.
  • Temporary scope rollback of both static offsets and heap nodes.
  • LP_init on deliberately unaligned input buffer (verifies lost-bytes calculation).

example.c shows an example production-style cache-line SoA initialization that ships in the author’s engine.

Building examples

gcc -O3 -march=native example.c -o example
gcc -O3 -march=native test.c -o test && ./test

About

LinearPrisma is a single header file high performance linear allocator, this was developed to be used in my game engine.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages