# ccgargantua/arena-allocator architecture

> Architecture diagram of the GitHub repository ccgargantua/arena-allocator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ccgargantua/arena-allocator
- Repository: https://github.com/ccgargantua/arena-allocator

## Overview

#### Purpose and Distribution Model
This repository is a compact C arena allocator library: it obtains a contiguous region once, suballocates via pointer/index advancement, then clears or destroys allocations as a group. The public product is a single header, designed for C89-oriented portability and embedded directly into consumer projects. Core API and implementation: `arena.h`.

#### Consumer Entry Point and Linkage Boundary
A consuming program defines `ARENA_IMPLEMENTATION` in exactly one translation unit before including `arena.h`; all other translation units include the same header normally. This macro gate is the library’s implementation/linkage boundary. Optional compile-time substitution points (`ARENA_MALLOC`, `ARENA_FREE`, `ARENA_MEMCPY`) isolate the allocator from a specific standard-library memory backend. Paths: `arena.h`, `code_examples/example.c`.

#### Arena Lifecycle
`Arena` owns or references a memory region, its capacity, and a current allocation index. Two initialization routes define ownership behavior:
- `arena_init` wraps caller-provided storage, enabling stack/static/custom-managed regions.
- `arena_create` obtains an arena-backed region through the configured malloc-like backend.
- `arena_destroy` releases an owned arena region; `arena_clear` resets the allocation cursor for reuse.
Paths: `arena.h`, `code_examples/example.c`.

#### Allocation and Alignment Path
The hot path is bump allocation: validate request, compute alignment padding, verify remaining capacity, return a segment of `Arena.region`, and advance `Arena.index`. `arena_alloc` uses a configurable default alignment; `arena_alloc_aligned` accepts per-request alignment. Allocation is intentionally bulk-lifetime-oriented: individual allocations are not freed and callers retain bounds/lifetime responsibility. Paths: `arena.h`, `code_examples/example_aligned.c`.

#### Copy and Memory Boundaries
`arena_copy` transfers arena contents into an already-created destination via the configurable memcpy-like backend. The principal boundaries are caller-managed external regions versus heap-created regions, and capacity checks versus unsafe use after a returned pointer: the allocator distributes raw memory but does not enforce object bounds. Paths: `arena.h`, `code_examples/example.c`.

#### Optional Debug Instrumentation
When `ARENA_DEBUG` is defined, the arena additionally tracks allocation count and a linked list of `Arena_Allocation` records. Debug APIs locate a record for a pointer, append allocation metadata, and delete that metadata list during cleanup. This is compile-time optional and changes the observable `Arena` structure/API surface. Paths: `arena.h`, `code_examples/example_debug.c`.

#### Validation and Build Runtime
The library runs as native C, with no application server, database, or network services. The repository’s executable entry point for validation is the test translation unit, backed by vendored `rktest`; the Makefile orchestrates compilation and Valgrind-based leak checking on supported Unix-like environments. Paths: `tests.c`, `rktest.c`, `rktest.h`, `Makefile`.

Main entry points: [`arena.h`](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h) (Arena public API), [`tests.c`](https://github.com/ccgargantua/arena-allocator/blob/main/tests.c) (Allocator tests), [`code_examples/example.c`](https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example.c) (Consumer translation unit), [`code_examples/example_aligned.c`](https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example_aligned.c) (Aligned allocation use)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_consumer["Consumer integration"]
  node_consumer_tu["Consumer translation unit<br/>C program<br/>[example.c]"]
  node_aligned_example["Aligned allocation use<br/>C example<br/>[example_aligned.c]"]
  node_debug_example["Debug instrumentation use<br/>C example<br/>[example_debug.c]"]
end

subgraph group_library["Header-only allocator"]
  node_implementation_gate{{"ARENA_IMPLEMENTATION gate<br/>linkage boundary<br/>[arena.h]"}}
  node_public_api["Arena public API<br/>C89 header API<br/>[arena.h]"]
  node_arena_state["Arena state<br/>allocator state<br/>[arena.h]"]
  node_init_routes["Initialization routes<br/>lifecycle API<br/>[arena.h]"]
  node_memory_backend{{"Memory backend hooks<br/>compile-time abstraction<br/>[arena.h]"}}
  node_bump_allocator["Bump allocation path<br/>allocation engine<br/>[arena.h]"]
  node_bulk_lifecycle["Bulk reset and destroy<br/>lifecycle API<br/>[arena.h]"]
  node_arena_copy["Arena copy<br/>memory transfer API<br/>[arena.h]"]
  node_debug_metadata["Optional debug metadata<br/>conditional instrumentation<br/>[arena.h]"]
end

subgraph group_validation["Native validation"]
  node_tests["Allocator tests<br/>C test translation unit<br/>[tests.c]"]
  node_rktest["rktest<br/>vendored test framework<br/>[rktest.c]"]
  node_makefile["Makefile<br/>build and leak-check runner"]
end

node_consumer_tu -->|"defines macro before include"| node_implementation_gate
node_consumer_tu -->|"includes"| node_public_api
node_aligned_example -->|"uses aligned API"| node_public_api
node_debug_example -.->|"enables debug mode"| node_debug_metadata
node_implementation_gate -->|"emits implementation"| node_public_api
node_public_api -->|"defines"| node_arena_state
node_public_api -->|"exposes"| node_init_routes
node_public_api -->|"exposes alloc and alloc_aligned"| node_bump_allocator
node_init_routes -->|"create allocates through"| node_memory_backend
node_init_routes -->|"initializes"| node_arena_state
node_bump_allocator -->|"reads region and advances index"| node_arena_state
node_public_api -->|"exposes clear and destroy"| node_bulk_lifecycle
node_bulk_lifecycle -->|"destroy frees through"| node_memory_backend
node_public_api -->|"exposes"| node_arena_copy
node_arena_copy -->|"copies through"| node_memory_backend
node_debug_metadata -.->|"extends conditionally"| node_arena_state
node_debug_metadata -.->|"cleanup deletes records"| node_bulk_lifecycle
node_tests -->|"validates"| node_public_api
node_tests -->|"uses"| node_rktest
node_makefile -->|"compiles and runs"| node_tests

click node_consumer_tu "https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example.c"
click node_aligned_example "https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example_aligned.c"
click node_debug_example "https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example_debug.c"
click node_implementation_gate "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_public_api "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_arena_state "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_init_routes "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_memory_backend "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_bump_allocator "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_bulk_lifecycle "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_arena_copy "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_debug_metadata "https://github.com/ccgargantua/arena-allocator/blob/main/arena.h"
click node_tests "https://github.com/ccgargantua/arena-allocator/blob/main/tests.c"
click node_rktest "https://github.com/ccgargantua/arena-allocator/blob/main/rktest.c"
click node_makefile "https://github.com/ccgargantua/arena-allocator/blob/main/Makefile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_consumer_tu,node_aligned_example,node_debug_example toneBlue
class node_implementation_gate,node_public_api,node_arena_state,node_init_routes,node_memory_backend,node_bump_allocator,node_bulk_lifecycle,node_arena_copy,node_debug_metadata toneAmber
class node_tests,node_rktest,node_makefile toneMint
```

## Components

### Consumer integration

- [Consumer translation unit](https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example.c): C program · Defines implementation in exactly one translation unit. · `code_examples/example.c`
- [Aligned allocation use](https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example_aligned.c): C example · `code_examples/example_aligned.c`
- [Debug instrumentation use](https://github.com/ccgargantua/arena-allocator/blob/main/code_examples/example_debug.c): C example · `code_examples/example_debug.c`

### Header-only allocator

- [ARENA_IMPLEMENTATION gate](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): linkage boundary · Emits implementation from the header in one translation unit. · `arena.h`
- [Arena public API](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): C89 header API · Single-header product defining Arena and allocator operations. · `arena.h`
- [Arena state](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): allocator state · Tracks region, capacity, and allocation cursor. · `arena.h`
- [Initialization routes](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): lifecycle API · Wraps caller storage or creates an owned region. · `arena.h`
- [Memory backend hooks](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): compile-time abstraction · ARENA_MALLOC, ARENA_FREE, and ARENA_MEMCPY are substitutable. · `arena.h`
- [Bump allocation path](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): allocation engine · Aligns, checks capacity, returns a region segment, and advances the cursor. · `arena.h`
- [Bulk reset and destroy](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): lifecycle API · Clears the cursor or releases an owned region; no individual frees. · `arena.h`
- [Arena copy](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): memory transfer API · Copies arena contents to an existing destination. · `arena.h`
- [Optional debug metadata](https://github.com/ccgargantua/arena-allocator/blob/main/arena.h): conditional instrumentation · Tracks allocation records and count when ARENA_DEBUG is enabled. · `arena.h`

### Native validation

- [Allocator tests](https://github.com/ccgargantua/arena-allocator/blob/main/tests.c): C test translation unit · Native validation entry point. · `tests.c`
- [rktest](https://github.com/ccgargantua/arena-allocator/blob/main/rktest.c): vendored test framework · `rktest.c`
- [Makefile](https://github.com/ccgargantua/arena-allocator/blob/main/Makefile): build and leak-check runner · Compiles tests and runs Valgrind checks. · `Makefile`

## Connections

- Consumer translation unit → ARENA_IMPLEMENTATION gate: defines macro before include
- Consumer translation unit → Arena public API: includes
- Aligned allocation use → Arena public API: uses aligned API
- Debug instrumentation use → Optional debug metadata: enables debug mode
- ARENA_IMPLEMENTATION gate → Arena public API: emits implementation
- Arena public API → Arena state: defines
- Arena public API → Initialization routes: exposes
- Arena public API → Bump allocation path: exposes alloc and alloc_aligned
- Initialization routes → Memory backend hooks: create allocates through
- Initialization routes → Arena state: initializes
- Bump allocation path → Arena state: reads region and advances index
- Arena public API → Bulk reset and destroy: exposes clear and destroy
- Bulk reset and destroy → Memory backend hooks: destroy frees through
- Arena public API → Arena copy: exposes
- Arena copy → Memory backend hooks: copies through
- Optional debug metadata → Arena state: extends conditionally
- Optional debug metadata → Bulk reset and destroy: cleanup deletes records
- Allocator tests → Arena public API: validates
- Allocator tests → rktest: uses
- Makefile → Allocator tests: compiles and runs
