pub struct BufferPool {
pub buffers: Vec<PooledBuffer>,
pub next_id: u64,
/* private fields */
}Expand description
A pool of frame buffers identified by integer IDs.
Buffers are acquired by ID and released back to the pool by ID.
When constructed with BufferPool::with_pressure, the pool monitors
its fill ratio and fires registered callbacks on level transitions.
Fields§
§buffers: Vec<PooledBuffer>Managed buffers.
next_id: u64Counter for assigning unique IDs to new buffers.
Implementations§
Source§impl BufferPool
impl BufferPool
Sourcepub fn new(count: usize, buf_size: usize) -> Self
pub fn new(count: usize, buf_size: usize) -> Self
Creates a new BufferPool with count buffers each of buf_size bytes.
All buffers share pool_id = 0 and default alignment of 64.
No pressure thresholds or callbacks are configured.
Sourcepub fn with_pressure(
count: usize,
buf_size: usize,
thresholds: PressureThresholds,
) -> Self
pub fn with_pressure( count: usize, buf_size: usize, thresholds: PressureThresholds, ) -> Self
Creates a BufferPool with memory pressure monitoring enabled.
See PressureThresholds for details on watermark configuration.
Sourcepub fn add_pressure_callback(&mut self, cb: MemoryPressureCallback)
pub fn add_pressure_callback(&mut self, cb: MemoryPressureCallback)
Registers a callback to be invoked whenever the pressure level transitions.
The callback receives the new MemoryPressureLevel as its argument.
Multiple callbacks may be registered and are called in registration order.
Sourcepub fn current_pressure_level(&self) -> MemoryPressureLevel
pub fn current_pressure_level(&self) -> MemoryPressureLevel
Returns the current memory pressure level.
If no thresholds are configured, always returns MemoryPressureLevel::Low.
Sourcepub fn acquire(&mut self) -> Option<u64>
pub fn acquire(&mut self) -> Option<u64>
Acquires an available buffer and returns its ID.
Returns None if no buffer is free.
Triggers pressure notification after the acquisition.
Sourcepub fn release(&mut self, id: u64)
pub fn release(&mut self, id: u64)
Releases the buffer with the given id back to the pool.
If the ID is not found this is a no-op. Triggers pressure notification after the release.
Sourcepub fn shrink_to(&mut self, target_count: usize) -> usize
pub fn shrink_to(&mut self, target_count: usize) -> usize
Removes free (not in-use) buffers until the pool has at most target_count
total buffers.
Only idle buffers are removed; buffers currently in use are never evicted. Returns the number of buffers that were removed.
§Example
use oximedia_core::buffer_pool::BufferPool;
let mut pool = BufferPool::new(8, 64);
let removed = pool.shrink_to(4);
assert_eq!(removed, 4);
assert_eq!(pool.total_count(), 4);Sourcepub fn auto_shrink(&mut self) -> usize
pub fn auto_shrink(&mut self) -> usize
Automatically shrinks the pool when pressure is Low and more than half
of the buffers are idle.
Shrinks down to half of the current total count, rounding up so at least one buffer always remains. Returns the number of buffers removed.
Sourcepub fn available_count(&self) -> usize
pub fn available_count(&self) -> usize
Returns the number of buffers not currently in use.
Sourcepub fn total_count(&self) -> usize
pub fn total_count(&self) -> usize
Returns the total number of buffers managed by this pool.
Sourcepub fn in_use_count(&self) -> usize
pub fn in_use_count(&self) -> usize
Returns the number of buffers currently in use.