How youngyangyang04/memory-pool works
Project Type and Purpose
• This is a C++ “library/framework” project (not a client–server app) that implements custom memory‐allocation strategies (memory pools) to replace or augment malloc/free, especially in multi‐threaded contexts.
• It ships three versions (v1, v2, v3)—each stored in its own subdirectory—showing an evolution from a simple hash‐map‐based allocator (v1) to a highly optimized, multi‐layered allocator (v2 and v3).
File Structure Analysis
• Top‐level directories:
· v1/ – first‐cut memory‐pool implementation
· v2/ – redesigned architecture with thread‐local caching, central cache, and page cache
· v3/ – further optimizations on v2
• Each version directory follows a CMake‐based organization: include/ (public headers), src/ (implementation), tests/ (unit and performance tests).
• Configuration: CMakeLists.txt in each vX directory drives build and links tests against src.
README Insights
• v1: single MemoryPool class using a hash of size‐buckets + lock‐free free lists
• v2/v3: three interacting layers—ThreadCache, CentralCache, PageCache—plus spin‐locks/atomics
Main Components & Their Relationships
A. Version 1 (v1)
• MemoryPool:
– Buckets indexed by size class
– allocate() & deallocate() methods
– lock‐free free‐list per bucket
• User code → MemoryPool::allocate/deallocate → system calls only on block exhaustion
B. Version 2/3 (v2, v3)
• ThreadCache (per thread): local free‐list for each size class
• CentralCache (global): services batch requests from ThreadCache → avoids contention
• PageCache (global): interfaces to OS (mmap/VirtualAlloc) for large chunks; merges/splits pages
• Synchronization: spin‐locks or atomic flags in CentralCache/PageCache
• Overall call flow:
Thread code
↕ allocate()/deallocate()
ThreadCache
↕ (batch refill/return)
CentralCache
↕ (page request/return)
PageCache
↕ (mmap/munmap)
Operating System
Architectural Patterns & Design Principles
• Layered Caching Pattern: per‐thread → central → OS
• Size‐class segregation: fixed‐size blocks to reduce fragmentation
• Lock minimization: lock‐free structures in v1, spin‐locks/atomics in v2/v3
• Extensibility: add new size classes or plug in custom locking strategies by altering CentralCache/PageCache