How komal-ai417/fastalloc works
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: global_heap.cpp Global heap · fast_alloc.cpp API orchestrator · CMakeLists.txt CMake build · os_memory.cpp OS virtual memory
Components 17
Allocator core
Hot-path allocation layers.
- Public C API · Allocation, alignment, realloc, purge, and diagnostics surface.
include/fast_alloc.h - API orchestrator · Implements API semantics and optional global new/delete override.
src/fast_alloc.cpp - Allocator config · Size classes, thresholds, and feature selection.
src/fast_alloc_config.h - Thread-local cache · Batches small-object transfers and flushes at thread teardown.
src/tls_cache.cpp - Global heap · Owns striped arenas, slab availability, and cache refill.
src/global_heap.cpp - Remote-free queues · Cross-thread frees are drained by the destination arena.
- Slab manager · Subdivides mapped spans and tracks slab lifecycle.
src/slab.cpp - Cached page spans · Bounds retained empty spans to reduce mapping cost.
- Debug instrumentation · Debug-only ownership checks, canaries, validation, and reporting.
src/debug_aid.cpp
Platform boundary
OS mapping and portability.
- OS virtual memory · Linux mmap/mremap and Windows virtual-memory primitives.
src/os_memory.cpp - Kernel memory APIs
Verification
Build, test, benchmark, and CI harnesses.
- CMake build · Defines targets, feature flags, and harness integration.
CMakeLists.txt - Google Test suites · Covers API, concurrency, large allocation, OOM, alignment, stress, and debug behavior.
tests/test_main.cpp - Benchmark runner · Runs allocator performance workloads.
benchmarks/bench_main.cpp - Comparison suite · Runs process-isolated allocator comparisons.
benchmarks/benchsuite/bench_suite.cpp - Benchmark comparison
benchmarks/benchsuite/bench_compare.py - CI workflow · Runs platform, compiler, sanitizer, and benchmark jobs.
.github/workflows/ci.yml
Connections 23
- 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