va_c_arena is a C99/C11 arena memory management library designed to provide efficient memory allocation and deallocation. This library is structured as a single-header, stb-style implementation for easy integration and cross-platform use.
Note:
- The core arena allocator is fully C99-compliant.
- Aligned allocation (
arena_alloc_aligned) and the provided tests/examples require C11 or newer for features like_Alignas,_Alignof, and their macros (VA_ALIGNAS,VA_ALIGNOF).- Compilation with
-std=c99will fail if you use aligned allocation or the aligned examples/tests.- Compilation with
-std=c11,-std=c17, or-std=c23is fully supported.
- Arena Memory Management: Efficiently allocate and manage memory in blocks.
- Single-Header (stb-style): Just include one header
include/va_arena.hand add#define VA_ARENA_IMPLEMENTATIONin one source file. - Cross-Platform: Works on various operating systems without modification.
- Portable: Uses only standard C (
malloc,realloc,free); no platform-specific APIs likemmaporVirtualAlloc. - Easy to Use API: Simple functions for memory management that are easy to integrate into your projects.
- Unit Tested: Includes tests for all API functions.
- Aligned Allocations: Supports aligned allocations for types with strict alignment requirements (C11 or newer required).
- Expandable Arena: Optionally grow the arena at runtime with
arena_expand.
- C99 compiler for the core arena functionality.
- C11 or newer compiler only for aligned allocations and tests/examples using
VA_ALIGNAS/VA_ALIGNOF. - Standard C library.
#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;
}- Alignment:
arena_allocreturns memory aligned at least toalignof(max_align_t)(C11+) or_Alignof(max_align_t)(C11/C17). For stricter alignment, usearena_alloc_aligned(requires C11 or newer). - Thread Safety: This arena implementation is not thread-safe. If you need to use an arena from multiple threads, you must provide your own synchronization.
If you use arena_expand to grow the arena, all pointers previously returned by arena_alloc or arena_alloc_aligned may become invalid. This is because the underlying memory block may be moved by realloc.
After calling arena_expand, you must not use any pointers previously obtained from the arena.
Example:
void *ptr = arena_alloc(arena, 128);
// ... use ptr ...
arena_expand(arena, 4096);
// DO NOT use ptr here! It may point to invalid memory.Build with make (requires CMake 3.16+):
make # Build all examples and testsOr use CMake directly for IDE integration:
cmake -B build -S .
cmake --build buildA compile_commands.json is generated inside build/ for clangd/LSP support.
make example # Run the basic example
make aligned # Run the aligned allocation example
make test # Run the unit testsmake valgrind # Valgrind leak check on all executables
make test-stds # Build example_aligned under c99, c11, c17, c23make clean # Remove build directory
make help # Show all available targets
CC=clang make # Build with a specific C compiler (default: clang)This project is licensed under the MIT License.