VLink  2.0.0
A high-performance communication middleware
memory_pool.h 文件参考

Size-class tiered memory pool with per-tier free lists and runtime statistics. 更多...

#include <cstddef>
#include <cstdint>
#include <memory>
#include <vector>
#include "./macros.h"
memory_pool.h 的引用(Include)关系图:
此图展示该文件直接或间接的被哪些文件引用了:

浏览源代码.

class  vlink::MemoryPool
 Per-tier free-list pool with runtime statistics and oversized passthrough. 更多...
 
struct  vlink::MemoryPool::Tier
 Descriptor for one size class. 更多...
 
struct  vlink::MemoryPool::Config
 Constructor configuration grouping the tier list and the preallocation toggle. 更多...
 
struct  vlink::MemoryPool::TierStats
 Per-tier runtime statistics snapshot. 更多...
 
struct  vlink::MemoryPool::OversizedStats
 Statistics for allocations that bypass the tier free lists. 更多...
 

命名空间

 

详细描述

Size-class tiered memory pool with per-tier free lists and runtime statistics.

MemoryPool dispatches each allocation request to one of a fixed pyramid of size classes. Every tier owns a singly-linked free list of fixed-size blocks plus a vector of upstream chunks; the chunk capacity starts small and doubles geometrically until it reaches the configured blocks_per_chunk. Requests larger than the biggest tier (or with an alignment stricter than alignof(std::max_align_t)) bypass the pool and route directly to ::operator new / ::operator delete.

Tier / bucket source
Source When used
Caller-supplied Config MemoryPool(const Config&) with non-empty tiers
get_default_config() (level 0..9) global_instance(true)
Built-in level-3 balanced pyramid Malformed input fallback or global_instance(false)
Built-in level N row MemoryPool(int level, bool prealloc) ctor
Allocation flow
*  allocate(bytes, align)
*      |
*      v
*  +-----------------+       align > kBlockAlignment?      +---------------------+
*  |      guard      | ----------------------------------> |  oversized bypass   |
*  +-----------------+                                     |  ::operator new     |
*           |  no                                          +---------------------+
*           v
*  +-----------------+   no fit ----> oversized bypass
*  |  find_tier(bs)  |
*  +--------+--------+
*           | tier hit
*           v
*  +-----------------+   free list non-empty -> pop block
*  |  per-tier lock  |
*  +--------+--------+
*           | free list empty
*           v
*  +-----------------+   ::operator new for next chunk
*  | upstream alloc  |   carve into N blocks of tier size
*  +-----------------+
* 
Cleanup primitives
Method What it releases Concurrent traffic?
clear / trim Only fully-free chunks; live blocks preserved Safe
~MemoryPool Every chunk unconditionally Caller must quiesce
Example
void* p = pool.allocate(512);
pool.deallocate(p, 512); // sizes MUST match
pool.trim(); // periodic memory reclaim
注解
Public methods are noexcept and safe for concurrent use. Per-tier locking means traffic across different size classes does not contend. deallocate requires the same bytes value passed to allocate. allocate returns nullptr on upstream OOM and never throws.