A lightweight, high-performance, malloc/free) is forbidden or risks memory fragmentation.
This project implements an intrusive free list trick: when a memory block is free, its raw bytes are repurposed to hold a structure pointing to the next free block. No extra bookkeeping overhead or heap structures required.
-
Deterministic Performance: Both allocation (
pool_alloc) and deallocation (pool_free) operate in strict$O(1)$ time complexity. - Zero Fragmentation: Fixed-size chunk architecture completely eliminates external memory fragmentation.
- No Heap Footprint: Utilizes a statically sized flat array allocated on the stack, data section, or BSS segment. Ideal for bare-metal embedded targets.
- Intrusive Free List: Reuses free slots to embed list pointers, keeping metadata overhead entirely within unused memory.
- Compile-time Safety: Built-in static assertions verify that your configurations don't lead to undefined behavior or misaligned structures.
-
Diagnostic Reporting: Includes built-in tracking counters (
blocks_in_use,total_allocs,total_frees) to generate real-time health reports.
Building the Demo To compile the test driver, run:
gcc -Wall -Wextra -std=c11 -o mempool_demo main.c mempool.c