Skip to main content

BufferPool

Struct BufferPool 

Source
pub struct BufferPool { /* private fields */ }
Expand description

A pool of reusable buffers for zero-copy operations.

BufferPool manages a collection of fixed-size buffers that can be acquired and released. This helps reduce allocation overhead in hot paths like frame decoding.

§Thread Safety

BufferPool is thread-safe and can be shared across threads. Acquired buffers are wrapped in Arc<RwLock<_>> for safe concurrent access.

§Examples

use oximedia_core::alloc::BufferPool;

// Create a pool with 4 buffers of 1MB each
let pool = BufferPool::new(4, 1024 * 1024);

// Acquire a buffer
let buffer = pool.acquire();
assert!(buffer.is_some());

// Write to the buffer
{
    let mut guard = buffer.as_ref().expect("buffer present").write().expect("lock ok");
    guard[0] = 42;
}

// Release it back to the pool
pool.release(buffer.expect("buffer present"));

Implementations§

Source§

impl BufferPool

Source

pub fn new(count: usize, buffer_size: usize) -> Self

Creates a new buffer pool.

§Arguments
  • count - Initial number of buffers to allocate
  • buffer_size - Size of each buffer in bytes
§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::new(8, 4096);
Source

pub fn with_capacity(max_buffers: usize, buffer_size: usize) -> Self

Creates a new buffer pool with a specified maximum capacity.

The pool starts empty and allocates buffers on demand up to max_buffers.

§Arguments
  • max_buffers - Maximum number of buffers the pool can hold
  • buffer_size - Size of each buffer in bytes
§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::with_capacity(16, 8192);
Source

pub fn acquire(&self) -> Option<Arc<RwLock<Vec<u8>>>>

Acquires a buffer from the pool.

Returns None if no buffers are available. Use acquire_or_alloc if you want to allocate a new buffer when the pool is empty.

§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::new(2, 1024);
let buf1 = pool.acquire();
let buf2 = pool.acquire();
let buf3 = pool.acquire(); // Returns None, pool exhausted
assert!(buf1.is_some());
assert!(buf2.is_some());
assert!(buf3.is_none());
Source

pub fn acquire_or_alloc(&self) -> Arc<RwLock<Vec<u8>>>

Acquires a buffer from the pool, allocating a new one if necessary.

If the pool is empty, allocates a new buffer. This is useful when you need a buffer regardless of pool state.

§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::new(0, 1024); // Empty pool
let buffer = pool.acquire_or_alloc();
assert_eq!(buffer.read().expect("lock ok").len(), 1024);
Source

pub fn release(&self, buffer: Arc<RwLock<Vec<u8>>>)

Releases a buffer back to the pool.

The buffer should have been previously acquired from this pool. If the pool is at capacity, the buffer is dropped.

§Arguments
  • buffer - The buffer to return to the pool
§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::new(2, 1024);
let buffer = pool.acquire().expect("buffer available");
// Use the buffer...
pool.release(buffer);
Source

pub fn available(&self) -> usize

Returns the number of buffers currently available in the pool.

§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::new(4, 1024);
assert_eq!(pool.available(), 4);
let _buf = pool.acquire();
assert_eq!(pool.available(), 3);
Source

pub fn buffer_size(&self) -> usize

Returns the size of each buffer in the pool.

§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::new(2, 4096);
assert_eq!(pool.buffer_size(), 4096);
Source

pub fn max_buffers(&self) -> usize

Returns the maximum number of buffers the pool can hold.

§Examples
use oximedia_core::alloc::BufferPool;

let pool = BufferPool::new(8, 1024);
assert_eq!(pool.max_buffers(), 8);

Trait Implementations§

Source§

impl Debug for BufferPool

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for BufferPool

Source§

fn default() -> Self

Returns the “default value” for a type. 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.