# komal-ai417/fastalloc architecture

> Architecture diagram of the GitHub repository komal-ai417/fastalloc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/komal-ai417/fastalloc
- Repository: https://github.com/komal-ai417/fastalloc

## Overview

#### Purpose and Public Entry Points
FastAlloc is a C++17, Windows/Linux memory allocator intended to replace `malloc`/`free` for low-latency concurrent workloads. Its public C-style allocation, aligned allocation, realloc, purge, and diagnostics surface is declared in `include/fast_alloc.h`; orchestration and optional global `new`/`delete` override live in `src/fast_alloc.cpp`. Build targets, feature flags, and test/benchmark integration are defined by `CMakeLists.txt`.

#### Allocation Routing
`src/fast_alloc.cpp` classifies requests by size and routes small/medium allocations through thread-local caches and heap-managed slabs; large allocations take a direct OS-backed path. It also owns API-level semantics such as calloc, realloc, alignment, OOM handling, and purge. Internal constants, size-class thresholds, and compile-time feature selection are centralized in `src/fast_alloc_config.h`.

#### Thread-Local Fast Path
`src/tls_cache.h` and `src/tls_cache.cpp` implement per-thread allocation caches using native TLS facilities. Same-thread small-object alloc/free operations are intended to avoid global locking, batching transfers to the shared heap when local bins are empty or overfull. Thread teardown flushes retained state back to the central allocator, creating a lifecycle boundary between thread-private state and shared ownership.

#### Shared Heap and Cross-Thread Return
`src/global_heap.h` and `src/global_heap.cpp` provide the shared allocator layer, partitioned into arenas/stripes protected by contention-aware synchronization. They manage slab availability, refill/eviction from TLS caches, and pending remote frees. Cross-thread deallocation is handed back through lock-free MPSC pending queues: producers enqueue without taking the destination arena lock, while the owning consumer drains work during heap activity.

#### Slab and Size-Class Management
`src/slab.h` and `src/slab.cpp` represent mapped spans subdivided into fixed-size blocks for small and medium classes. This layer tracks block allocation state, slab ownership, and transitions between active, reusable, and empty slabs. Empty slabs can be returned to the OS outside critical locking paths; the heap also maintains bounded cached page spans to balance mapping cost against retained memory.

#### OS Memory Boundary
`src/os_memory.h` and `src/os_memory.cpp` isolate platform virtual-memory operations, including Linux `mmap`/`mremap` and Windows allocation primitives. Large allocations and slab backing memory cross this boundary. Mapping and unmapping are deliberately performed outside global heap locks, so slow kernel calls do not serialize allocator metadata access. This is the principal portability boundary.

#### Debug/Instrumentation Mode
`src/debug_aid.h` and `src/debug_aid.cpp` supply `FASTALLOC_DEBUG` instrumentation around allocation ownership and integrity: canaries/red zones, poisoning, allocation registry, validation, leak/statistics reporting, and logging. Release builds compile this layer out, preserving the allocation-path split between performance and safety builds.

#### Verification and Performance Harnesses
Google Test exercises API, concurrency, large allocation, OOM, alignment, stress, and debug behavior from `tests/test_main.cpp` and focused suites such as `tests/test_concurrency.cpp`. Benchmarks use Google Benchmark plus process-isolated allocator comparisons: `benchmarks/bench_main.cpp`, `benchmarks/benchsuite/bench_suite.cpp`, and `benchmarks/benchsuite/bench_compare.py`. CI in `.github/workflows/ci.yml` runs platform/compiler/sanitizer configurations and benchmark jobs.

Main entry points: [`src/global_heap.cpp`](https://github.com/komal-ai417/fastalloc/blob/main/src/global_heap.cpp) (Global heap), [`src/fast_alloc.cpp`](https://github.com/komal-ai417/fastalloc/blob/main/src/fast_alloc.cpp) (API orchestrator), [`CMakeLists.txt`](https://github.com/komal-ai417/fastalloc/blob/main/CMakeLists.txt) (CMake build), [`src/os_memory.cpp`](https://github.com/komal-ai417/fastalloc/blob/main/src/os_memory.cpp) (OS virtual memory)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Allocator core"]
  node_public_api["Public C API<br/>API header<br/>[fast_alloc.h]"]
  node_api_orchestrator["API orchestrator<br/>allocation router<br/>[fast_alloc.cpp]"]
  node_config["Allocator config<br/>compile-time policy"]
  node_tls_cache["Thread-local cache<br/>TLS fast path<br/>[tls_cache.cpp]"]
  node_global_heap["Global heap<br/>shared arenas<br/>[global_heap.cpp]"]
  node_remote_queue["Remote-free queues<br/>lock-free MPSC"]
  node_slab["Slab manager<br/>span allocator<br/>[slab.cpp]"]
  node_span_cache[("Cached page spans<br/>memory retention")]
  node_debug_aid["Debug instrumentation<br/>conditional safety layer<br/>[debug_aid.cpp]"]
end

subgraph group_platform["Platform boundary"]
  node_os_memory["OS virtual memory<br/>platform adapter<br/>[os_memory.cpp]"]
  node_kernel_memory{{"Kernel memory APIs<br/>OS services"}}
end

subgraph group_quality["Verification"]
  node_cmake["CMake build<br/>build definition<br/>[CMakeLists.txt]"]
  node_tests["Google Test suites<br/>verification harness<br/>[test_main.cpp]"]
  node_bench_runner["Benchmark runner<br/>Google Benchmark<br/>[bench_main.cpp]"]
  node_bench_suite["Comparison suite<br/>isolated benchmark suite<br/>[bench_suite.cpp]"]
  node_bench_compare["Benchmark comparison<br/>comparison script<br/>[bench_compare.py]"]
  node_ci["CI workflow<br/>automation pipeline<br/>[ci.yml]"]
end

node_public_api -->|"declares"| node_api_orchestrator
node_api_orchestrator -->|"uses policy"| node_config
node_api_orchestrator -->|"small/medium path"| node_tls_cache
node_api_orchestrator -->|"shared and large routing"| node_global_heap
node_api_orchestrator -->|"large allocations"| node_os_memory
node_api_orchestrator -.->|"debug builds"| node_debug_aid
node_tls_cache -->|"refill, overflow, teardown flush"| node_global_heap
node_global_heap -->|"owns pending frees"| node_remote_queue
node_remote_queue -->|"arena drains"| node_global_heap
node_global_heap -->|"manages slabs"| node_slab
node_global_heap -->|"retains empty spans"| node_span_cache
node_slab -->|"map/unmap backing spans"| node_os_memory
node_span_cache -->|"evict outside locks"| node_os_memory
node_os_memory -->|"platform calls"| node_kernel_memory
node_debug_aid -.->|"validates ownership"| node_global_heap
node_cmake -->|"builds library"| node_public_api
node_cmake -->|"integrates"| node_tests
node_cmake -->|"integrates"| node_bench_runner
node_bench_runner -->|"runs workloads"| node_bench_suite
node_bench_suite -->|"comparison results"| node_bench_compare
node_ci -->|"builds configurations"| node_cmake
node_ci -->|"runs tests/sanitizers"| node_tests
node_ci -->|"runs benchmark jobs"| node_bench_runner

click node_public_api "https://github.com/komal-ai417/fastalloc/blob/main/include/fast_alloc.h"
click node_api_orchestrator "https://github.com/komal-ai417/fastalloc/blob/main/src/fast_alloc.cpp"
click node_config "https://github.com/komal-ai417/fastalloc/blob/main/src/fast_alloc_config.h"
click node_tls_cache "https://github.com/komal-ai417/fastalloc/blob/main/src/tls_cache.cpp"
click node_global_heap "https://github.com/komal-ai417/fastalloc/blob/main/src/global_heap.cpp"
click node_slab "https://github.com/komal-ai417/fastalloc/blob/main/src/slab.cpp"
click node_debug_aid "https://github.com/komal-ai417/fastalloc/blob/main/src/debug_aid.cpp"
click node_os_memory "https://github.com/komal-ai417/fastalloc/blob/main/src/os_memory.cpp"
click node_cmake "https://github.com/komal-ai417/fastalloc/blob/main/CMakeLists.txt"
click node_tests "https://github.com/komal-ai417/fastalloc/blob/main/tests/test_main.cpp"
click node_bench_runner "https://github.com/komal-ai417/fastalloc/blob/main/benchmarks/bench_main.cpp"
click node_bench_suite "https://github.com/komal-ai417/fastalloc/blob/main/benchmarks/benchsuite/bench_suite.cpp"
click node_bench_compare "https://github.com/komal-ai417/fastalloc/blob/main/benchmarks/benchsuite/bench_compare.py"
click node_ci "https://github.com/komal-ai417/fastalloc/blob/main/.github/workflows/ci.yml"

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_public_api,node_api_orchestrator,node_config,node_tls_cache,node_global_heap,node_remote_queue,node_slab,node_span_cache,node_debug_aid toneBlue
class node_os_memory,node_kernel_memory toneAmber
class node_cmake,node_tests,node_bench_runner,node_bench_suite,node_bench_compare,node_ci toneMint
```

## Components

### Allocator core

Hot-path allocation layers.

- [Public C API](https://github.com/komal-ai417/fastalloc/blob/main/include/fast_alloc.h): API header · Allocation, alignment, realloc, purge, and diagnostics surface. · `include/fast_alloc.h`
- [API orchestrator](https://github.com/komal-ai417/fastalloc/blob/main/src/fast_alloc.cpp): allocation router · Implements API semantics and optional global new/delete override. · `src/fast_alloc.cpp`
- [Allocator config](https://github.com/komal-ai417/fastalloc/blob/main/src/fast_alloc_config.h): compile-time policy · Size classes, thresholds, and feature selection. · `src/fast_alloc_config.h`
- [Thread-local cache](https://github.com/komal-ai417/fastalloc/blob/main/src/tls_cache.cpp): TLS fast path · Batches small-object transfers and flushes at thread teardown. · `src/tls_cache.cpp`
- [Global heap](https://github.com/komal-ai417/fastalloc/blob/main/src/global_heap.cpp): shared arenas · Owns striped arenas, slab availability, and cache refill. · `src/global_heap.cpp`
- **Remote-free queues**: lock-free MPSC · Cross-thread frees are drained by the destination arena.
- [Slab manager](https://github.com/komal-ai417/fastalloc/blob/main/src/slab.cpp): span allocator · Subdivides mapped spans and tracks slab lifecycle. · `src/slab.cpp`
- **Cached page spans**: memory retention · Bounds retained empty spans to reduce mapping cost.
- [Debug instrumentation](https://github.com/komal-ai417/fastalloc/blob/main/src/debug_aid.cpp): conditional safety layer · Debug-only ownership checks, canaries, validation, and reporting. · `src/debug_aid.cpp`

### Platform boundary

OS mapping and portability.

- [OS virtual memory](https://github.com/komal-ai417/fastalloc/blob/main/src/os_memory.cpp): platform adapter · Linux mmap/mremap and Windows virtual-memory primitives. · `src/os_memory.cpp`
- **Kernel memory APIs**: OS services

### Verification

Build, test, benchmark, and CI harnesses.

- [CMake build](https://github.com/komal-ai417/fastalloc/blob/main/CMakeLists.txt): build definition · Defines targets, feature flags, and harness integration. · `CMakeLists.txt`
- [Google Test suites](https://github.com/komal-ai417/fastalloc/blob/main/tests/test_main.cpp): verification harness · Covers API, concurrency, large allocation, OOM, alignment, stress, and debug behavior. · `tests/test_main.cpp`
- [Benchmark runner](https://github.com/komal-ai417/fastalloc/blob/main/benchmarks/bench_main.cpp): Google Benchmark · Runs allocator performance workloads. · `benchmarks/bench_main.cpp`
- [Comparison suite](https://github.com/komal-ai417/fastalloc/blob/main/benchmarks/benchsuite/bench_suite.cpp): isolated benchmark suite · Runs process-isolated allocator comparisons. · `benchmarks/benchsuite/bench_suite.cpp`
- [Benchmark comparison](https://github.com/komal-ai417/fastalloc/blob/main/benchmarks/benchsuite/bench_compare.py): comparison script · `benchmarks/benchsuite/bench_compare.py`
- [CI workflow](https://github.com/komal-ai417/fastalloc/blob/main/.github/workflows/ci.yml): automation pipeline · Runs platform, compiler, sanitizer, and benchmark jobs. · `.github/workflows/ci.yml`

## Connections

- Public C API → API orchestrator: declares
- API orchestrator → Allocator config: uses policy
- API orchestrator → Thread-local cache: small/medium path
- API orchestrator → Global heap: shared and large routing
- API orchestrator → OS virtual memory: large allocations
- API orchestrator → Debug instrumentation: debug builds
- Thread-local cache → Global heap: refill, overflow, teardown flush
- Global heap → Remote-free queues: owns pending frees
- Remote-free queues → Global heap: arena drains
- Global heap → Slab manager: manages slabs
- Global heap → Cached page spans: retains empty spans
- Slab manager → OS virtual memory: map/unmap backing spans
- Cached page spans → OS virtual memory: evict outside locks
- OS virtual memory → Kernel memory APIs: platform calls
- Debug instrumentation → Global heap: validates ownership
- CMake build → Public C API: builds library
- CMake build → Google Test suites: integrates
- CMake build → Benchmark runner: integrates
- Benchmark runner → Comparison suite: runs workloads
- Comparison suite → Benchmark comparison: comparison results
- CI workflow → CMake build: builds configurations
- CI workflow → Google Test suites: runs tests/sanitizers
- CI workflow → Benchmark runner: runs benchmark jobs
