Skip to main content

BufferPool

Struct BufferPool 

Source
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: u64

Counter for assigning unique IDs to new buffers.

Implementations§

Source§

impl BufferPool

Source

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.

Source

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.

Source

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.

Source

pub fn current_pressure_level(&self) -> MemoryPressureLevel

Returns the current memory pressure level.

If no thresholds are configured, always returns MemoryPressureLevel::Low.

Source

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.

Source

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.

Source

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);
Source

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.

Source

pub fn available_count(&self) -> usize

Returns the number of buffers not currently in use.

Source

pub fn total_count(&self) -> usize

Returns the total number of buffers managed by this pool.

Source

pub fn in_use_count(&self) -> usize

Returns the number of buffers currently in use.

Trait Implementations§

Source§

impl Debug for BufferPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.