|
VLink
2.0.0
A high-performance communication middleware
|
Per-tier free-list pool with runtime statistics and oversized passthrough. More...
#include <memory_pool.h>
Classes | |
| struct | Config |
| Constructor configuration grouping the tier list and the preallocation toggle. More... | |
| struct | OversizedStats |
| Statistics for allocations that bypass the tier free lists. More... | |
| struct | Tier |
| Descriptor for one size class. More... | |
| struct | TierStats |
| Per-tier runtime statistics snapshot. More... | |
Public Member Functions | |
| MemoryPool () | |
| Constructs an empty pool in bypass mode. More... | |
| MemoryPool (int level, bool prealloc=false) | |
Constructs a tiered pool using the built-in pyramid for level. More... | |
| MemoryPool (const Config &config) | |
| Constructs a tiered pool with an explicit configuration. More... | |
| ~MemoryPool () | |
| Releases every owned chunk unconditionally. More... | |
| void * | allocate (size_t bytes, size_t alignment=kBlockAlignment) noexcept |
Allocates bytes of memory from the appropriate tier. More... | |
| void | deallocate (void *p, size_t bytes, size_t alignment=kBlockAlignment) noexcept |
Returns a block previously allocated through allocate to the pool. More... | |
| size_t | get_tier_count () const noexcept |
| Returns the number of live managed tiers. More... | |
| std::vector< TierStats > | get_stats () const noexcept |
| Returns a snapshot of per-tier statistics. More... | |
| OversizedStats | get_oversized_stats () const noexcept |
| Returns a snapshot of the oversized-passthrough statistics. More... | |
| void | reset_stats () noexcept |
| Resets per-call statistics counters to zero. More... | |
| void | clear () noexcept |
| Releases only fully-free chunks; preserves chunks still backing live allocations. More... | |
| void | trim () noexcept |
Alias of clear with a name suited to periodic-trim phrasing. More... | |
Static Public Member Functions | |
| static Config | get_default_config () |
Returns the default tier pyramid using VLINK_MEMORY_LEVEL / VLINK_MEMORY_PREALLOC. More... | |
| static MemoryPool & | global_instance (bool use_env_level=true) |
Returns the process-wide shared MemoryPool instance. More... | |
Static Public Attributes | |
| static constexpr size_t | kBlockAlignment = alignof(std::max_align_t) |
| Default block alignment for every pooled allocation. More... | |
Per-tier free-list pool with runtime statistics and oversized passthrough.
Thread-safe. Each tier owns its own spin lock so allocate, deallocate and clear on different size classes never contend. Bypass mode – selected by an empty tier list – routes every request through the global allocator without any pooling.
| vlink::MemoryPool::MemoryPool | ( | ) |
Constructs an empty pool in bypass mode.
Equivalent to MemoryPool(Config{}): every request hits ::operator new / delete.
|
explicit |
Constructs a tiered pool using the built-in pyramid for level.
Out-of-range values are clamped to [0, 9] and a warning is logged. Level 0 yields bypass mode; the prealloc flag is ignored in that case. Level 9 fully saturates to roughly 656 MiB of resident memory.
| level | Built-in level in [0, 9]. |
| prealloc | When true, fills every tier to its quota on construction (best effort). Default: false. |
|
explicit |
Constructs a tiered pool with an explicit configuration.
Empty config.tiers selects bypass mode. Sentinel entries with blocks_per_chunk == 0 declare a size ceiling but are stripped at construction; an all-sentinel list is therefore equivalent to bypass mode. Malformed input (non-monotonic ordering, duplicate max_size, zero max_size or max_size below the minimum block size) logs an error and silently falls back to the level-3 default pyramid. std::bad_alloc may propagate from internal vector growth.
| config | Tier descriptors and preallocation flag. |
| vlink::MemoryPool::~MemoryPool | ( | ) |
Releases every owned chunk unconditionally.
allocate, deallocate, clear or trim on this instance. Use clear for a non-destructive trim.
|
noexcept |
Allocates bytes of memory from the appropriate tier.
Routes to the first tier whose max_size is >= bytes. Requests larger than the biggest tier or with alignment > kBlockAlignment bypass the pool. bytes == 0 routes to the smallest tier and still returns a unique non-null pointer that must be passed back to deallocate with the same 0 size. Never throws.
| bytes | Requested size. |
| alignment | Required alignment (power of two). Default: kBlockAlignment. |
nullptr on upstream OOM.
|
noexcept |
Releases only fully-free chunks; preserves chunks still backing live allocations.
For each tier the free list is grouped by owning chunk; chunks whose free-node count equals their block capacity are released, others stay intact. chunk_count is decremented by the number of released chunks. Safe to call concurrently with allocate and deallocate. Per-tier work is O(C log C + F log C) under the spin lock.
|
noexcept |
Returns a block previously allocated through allocate to the pool.
| p | Pointer returned by allocate. nullptr is a no-op. |
| bytes | Original size passed to allocate; MUST match. |
| alignment | Original alignment passed to allocate. |
|
static |
Returns the default tier pyramid using VLINK_MEMORY_LEVEL / VLINK_MEMORY_PREALLOC.
Each level 0..9 maps to a hand-coded row of {max_size, blocks_per_chunk} pairs. Level 0 is bypass mode. Levels 1..9 return 19 entries covering 32 B to 16 MiB; the 1 MiB / 4 MiB / 8 MiB / 16 MiB ceilings activate at level 2 / 4 / 5 / 6 and are sentinels below those thresholds. Within each level the 32 B head tier carries twice the blocks_per_chunk of the 64 B tier to absorb high-density tiny allocations.
VLINK_MEMORY_PREALLOC controls the prealloc flag; only the literal value "1" enables preallocation.
Config ready to pass to the constructor.
|
noexcept |
Returns a snapshot of the oversized-passthrough statistics.
Counters are loaded with relaxed ordering and are not a globally atomic snapshot.
|
noexcept |
Returns a snapshot of per-tier statistics.
|
noexcept |
Returns the number of live managed tiers.
0 indicates bypass mode. Sentinel entries are stripped at construction so the count reflects only tiers backed by a live free list.
|
static |
Returns the process-wide shared MemoryPool instance.
Lazy Meyers singleton. Only the first call decides the configuration; subsequent calls return the same instance and ignore the argument.
use_env_level takes on the first call is baked in for the rest of the program's lifetime.| use_env_level | true (default): use get_default_config and honour the environment. false: use the built-in level-3 pyramid. |
|
noexcept |
Resets per-call statistics counters to zero.
Clears hit_count and deallocate_count on every tier and the oversized_* counters. Physical / lifetime state (chunk_count, upstream_alloc_count, upstream_alloc_bytes) is preserved.
|
noexcept |
Alias of clear with a name suited to periodic-trim phrasing.
Behaviour, complexity and thread-safety are identical to clear.
|
staticconstexpr |
Default block alignment for every pooled allocation.
Equal to alignof(std::max_align_t). Stricter alignments bypass the pool and request memory directly through ::operator new with std::align_val_t.