LibArena
Lightweight C Memory Arena Library
$
./arena_demo --showcase
use_cases.c
Basic Usage
Compiler/Parser
Game Engine
HTTP Server
Simulate Basic Demo
// Basic arena allocation pattern arena_t *arena = arena_create(10); // Allocate various data structures char *name = arena_alloc(arena, 64); int *numbers = arena_alloc(arena, sizeof(int) * 100); struct Node *tree = arena_alloc(arena, sizeof(struct Node)); // Use the allocated memory... strcpy(name, "LibArena Demo"); for (int i = 0; i < 100; i++) { numbers[i] = i * i; } // One call frees everything arena_destroy(arena);
// Compiler/Parser use case typedef struct { char *lexeme; TokenType type; int line; } Token; arena_t *parse_arena = arena_create(1000); // Parse phase - allocate AST nodes while (has_tokens()) { Token *tok = arena_alloc(parse_arena, sizeof(Token)); ASTNode *node = arena_alloc(parse_arena, sizeof(ASTNode)); // Build syntax tree... parse_expression(tok, node); } // Generate code from AST... generate_code(ast_root); // Free entire parse tree at once arena_destroy(parse_arena);
// Game engine frame allocator arena_t *frame_arena = arena_create(500); void game_frame() { // Checkpoint for temporary allocations size_t frame_start = arena_snapshot(frame_arena); // Allocate temporary game objects Entity *enemies = arena_alloc(frame_arena, sizeof(Entity) * enemy_count); Particle *particles = arena_alloc(frame_arena, sizeof(Particle) * 1000); RenderCommand *commands = arena_alloc(frame_arena, sizeof(RenderCommand) * 50); // Process frame... update_enemies(enemies, enemy_count); simulate_particles(particles); render_scene(commands); // Free all temporary allocations arena_restore(frame_arena, frame_start); }
// HTTP server request handling void handle_request(int client_fd) { // Create per-request arena arena_t *request_arena = arena_create(50); // Allocate request data structures HttpRequest *req = arena_alloc(request_arena, sizeof(HttpRequest)); char *headers = arena_alloc(request_arena, 4096); char *body = arena_alloc(request_arena, 8192); // Register cleanup for file handles FILE *log = fopen("access.log", "a"); arena_alloc(request_arena, sizeof(FILE)); arena_set_destructor(request_arena, log, (void(*)(void*))fclose); // Process request... parse_http_request(client_fd, req, headers, body); handle_route(req); // Automatic cleanup of all request data arena_destroy(request_arena); }
memory_simulator.c
Interactive Arena Memory Simulator
$
arena_visualize
--interactive
alloc(32)
alloc(128)
alloc + finalizer
realloc()
snapshot()
restore()
free_selected()
destroy()
stress_test()
fragmentation_demo()
Arena Layout (Click blocks for details):
Header:
META
SIZE
CAP
Blocks:
Tracking:
PTR
PTR
FIN
Arena: 0/16 blocks allocated | 0 checkpoints | 0 finalizers
Click on a memory block to see details...
# Allocation log will appear here...
performance_comparison.c
Benchmark Results
Allocation Speed (ops/sec)
malloc(): 1.2M ops/sec | arena_alloc(): 15.8M ops/sec
Memory Overhead
80% less overhead vs individual malloc calls
Cache Performance
Better locality = fewer cache misses
advanced_features.c
Advanced Memory Management
// Safe reallocation within arena
char
*buffer =
arena_alloc
(arena,
100
); buffer =
arena_realloc
(arena, buffer,
500
);
// Custom finalizers for resources
void
close_socket
(
void
*data) {
close
(*(
int
*)data); }
int
*socket_fd =
arena_alloc
(arena,
sizeof
(
int
));
arena_set_destructor
(arena, socket_fd, close_socket);
// Scoped temporary allocations
size_t
checkpoint =
arena_snapshot
(arena);
process_temporary_data
(arena);
arena_restore
(arena, checkpoint);
build_and_install.sh
$
git clone
https://github.com/MliliGenes/libArena.git
Cloning into 'libArena'...
remote: Enumerating objects: 42, done.
$
cd
libArena &&
make
gcc -Wall -Wextra -std=c99 -c src/*.c
ar rcs libarena.a *.o
✓ Built libarena.a
$
gcc
example.c
-L. -larena -I./include
$
./example
Arena created with capacity: 10
Allocated: Hello, World
Finalizer called for: World
Arena destroyed - 0 leaks detected
$
explore_libarena
📁 GitHub Repository
📄 API Reference
📦 Releases