Projects

Basic C memory arena implementation

Single-header arena allocator in C99/C11 with optional aligned allocations and expansion.

26 July 2025 · Systems Programming

Tech: C99, C11, Memory, Allocator

Highlights

  • Single-header (stb-style) arena allocator in C99/C11
  • Optional aligned allocations plus expansion support
  • Unit tests and examples included

Overview #

va_c_arena is a stb-style, single-header arena allocator. Include va_arena.h, define VA_ARENA_IMPLEMENTATION once, and you get fast bump-pointer allocations with optional alignment and expansion.

Features #

Usage sketch #

Example (Aligned Allocation, C11+) #

#include <stdio.h>
#include <stdalign.h>

#define VA_ARENA_IMPLEMENTATION
#include "va_arena.h"

typedef struct {
    VA_ALIGNAS(32) int id;
    VA_ALIGNAS(32) double position[3];
    VA_ALIGNAS(32) char name[32];
} Entity;

int main(void) {
    Arena *arena = arena_create(4096);
    Entity *entities = (Entity *)arena_alloc_aligned(arena, sizeof(Entity) * 10, 32);
    if (!entities) {
        fprintf(stderr, "Failed to allocate aligned entities\n");
        arena_destroy(&arena);
        return 1;
    }
    // Use entities...
    arena_destroy(&arena);
    return 0;
}

Notes #