pub struct StratifiedMemoryPool { /* private fields */ }Expand description
Multi-size memory pool with automatic bucket selection.
Instead of having a single buffer size, this pool maintains separate pools for different size classes. Allocations are rounded up to the smallest bucket that fits.
§Example
use ringkernel_core::memory::{StratifiedMemoryPool, SizeBucket};
let pool = StratifiedMemoryPool::new("my_pool");
// Allocate various sizes - each goes to appropriate bucket
let tiny_buf = pool.allocate(100); // Uses Tiny bucket (256B)
let medium_buf = pool.allocate(2000); // Uses Medium bucket (4KB)
// Check statistics
let stats = pool.stats();
println!("Hit rate: {:.1}%", stats.hit_rate() * 100.0);Implementations§
Source§impl StratifiedMemoryPool
impl StratifiedMemoryPool
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Create a new stratified pool with default settings.
Creates pools for all bucket sizes with 16 buffers per bucket.
Sourcepub fn with_capacity(
name: impl Into<String>,
max_buffers_per_bucket: usize,
) -> Self
pub fn with_capacity( name: impl Into<String>, max_buffers_per_bucket: usize, ) -> Self
Create a pool with specified max buffers per bucket.
Sourcepub fn allocate(&self, size: usize) -> StratifiedBuffer<'_>
pub fn allocate(&self, size: usize) -> StratifiedBuffer<'_>
Allocate a buffer of at least the requested size.
The buffer may be larger than requested (rounded up to bucket size).
Sourcepub fn allocate_bucket(&self, bucket: SizeBucket) -> StratifiedBuffer<'_>
pub fn allocate_bucket(&self, bucket: SizeBucket) -> StratifiedBuffer<'_>
Allocate from a specific bucket.
Sourcepub fn max_buffers_per_bucket(&self) -> usize
pub fn max_buffers_per_bucket(&self) -> usize
Get max buffers per bucket.
Sourcepub fn bucket_size(&self, bucket: SizeBucket) -> usize
pub fn bucket_size(&self, bucket: SizeBucket) -> usize
Get current size of a specific bucket pool.
Sourcepub fn total_pooled(&self) -> usize
pub fn total_pooled(&self) -> usize
Get total buffers currently pooled across all buckets.
Sourcepub fn stats(&self) -> StratifiedPoolStats
pub fn stats(&self) -> StratifiedPoolStats
Get statistics snapshot.
Sourcepub fn preallocate(&self, bucket: SizeBucket, count: usize)
pub fn preallocate(&self, bucket: SizeBucket, count: usize)
Pre-allocate buffers for a specific bucket.
Sourcepub fn preallocate_all(&self, count_per_bucket: usize)
pub fn preallocate_all(&self, count_per_bucket: usize)
Pre-allocate buffers for all buckets.