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
impl BufferPool
Sourcepub fn with_capacity(max_buffers: usize, buffer_size: usize) -> Self
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 holdbuffer_size- Size of each buffer in bytes
§Examples
use oximedia_core::alloc::BufferPool;
let pool = BufferPool::with_capacity(16, 8192);Sourcepub fn acquire(&self) -> Option<Arc<RwLock<Vec<u8>>>>
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());Sourcepub fn acquire_or_alloc(&self) -> Arc<RwLock<Vec<u8>>>
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);Sourcepub fn release(&self, buffer: Arc<RwLock<Vec<u8>>>)
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);Sourcepub fn available(&self) -> usize
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);Sourcepub fn buffer_size(&self) -> usize
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);Sourcepub fn max_buffers(&self) -> usize
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);