pub struct BufferPool { /* private fields */ }Expand description
Pre-allocated audio buffer pool.
Manages a fixed pool of audio buffers (64 samples each) with O(1) acquire/release. All memory is allocated at startup - the RT thread only borrows slices with zero allocation overhead.
§Design
- Structure-of-Arrays: Flat
Vec<f32>storage for cache efficiency - Free list: Stack of available buffer IDs
- Pre-zeroed: Buffers are zeroed on acquire
- Real-time safe: No allocation, no locks, bounded time
§Example
use aether_core::buffer_pool::BufferPool;
use aether_core::BUFFER_SIZE;
let mut pool = BufferPool::new(100);
// Acquire buffers
let buf1 = pool.acquire().unwrap();
let buf2 = pool.acquire().unwrap();
// Use buffers
{
let data = pool.get_mut(buf1);
data[0] = 1.0;
}
// Release buffers
pool.release(buf1);
pool.release(buf2);
// Buffers are recycled
let buf3 = pool.acquire().unwrap();§Capacity
The pool has a fixed capacity set at creation. When exhausted,
acquire() returns None. Released buffers are immediately available
for reuse.
§See Also
BufferId- Opaque buffer handle
Implementations§
Source§impl BufferPool
impl BufferPool
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new buffer pool with the specified capacity.
Pre-allocates all buffers upfront. No further allocation occurs during audio processing.
§Arguments
capacity- Number of buffers to pre-allocate
§Example
use aether_core::buffer_pool::BufferPool;
let pool = BufferPool::new(100);
assert_eq!(pool.capacity(), 100);
assert_eq!(pool.available(), 100);Sourcepub fn acquire(&mut self) -> Option<BufferId>
pub fn acquire(&mut self) -> Option<BufferId>
Acquire a zeroed buffer. O(1). Returns None if pool is exhausted.
Pops a buffer ID from the free list and zeros the buffer before returning it. The buffer is guaranteed to contain all zeros.
§Returns
Some(BufferId)- Handle to an available bufferNone- Pool is exhausted (all buffers in use)
§Example
use aether_core::buffer_pool::BufferPool;
let mut pool = BufferPool::new(10);
let buf = pool.acquire().unwrap();
let data = pool.get(buf);
assert_eq!(data[0], 0.0); // Guaranteed zero§Performance
- Time: O(1)
- Zeros 64 samples (256 bytes)
- Real-time safe
Sourcepub fn release(&mut self, id: BufferId)
pub fn release(&mut self, id: BufferId)
Release a buffer back to the pool. O(1).
Returns the buffer to the free list for reuse. The buffer’s contents
are not cleared until the next acquire().
§Arguments
id- Buffer ID to release
§Example
use aether_core::buffer_pool::BufferPool;
let mut pool = BufferPool::new(10);
let buf = pool.acquire().unwrap();
assert_eq!(pool.available(), 9);
pool.release(buf);
assert_eq!(pool.available(), 10);§Performance
- Time: O(1)
- No zeroing (deferred to acquire)
- Real-time safe
Sourcepub fn get(&self, id: BufferId) -> &[f32; 64]
pub fn get(&self, id: BufferId) -> &[f32; 64]
Get a read-only slice for a buffer.
Returns a reference to the 64-sample buffer identified by id.
This is a zero-cost operation - just pointer arithmetic.
§Arguments
id- Buffer ID fromacquire()
§Returns
A reference to a 64-sample audio buffer.
§Example
use aether_core::buffer_pool::BufferPool;
let mut pool = BufferPool::new(10);
let buf = pool.acquire().unwrap();
// Write to buffer
pool.get_mut(buf)[0] = 1.0;
// Read from buffer
let data = pool.get(buf);
assert_eq!(data[0], 1.0);§Performance
- Time: O(1) - inline pointer arithmetic
- No bounds checking in release builds
- Real-time safe
§Panics
Panics in debug builds if id is out of range.
Sourcepub fn get_mut(&mut self, id: BufferId) -> &mut [f32; 64]
pub fn get_mut(&mut self, id: BufferId) -> &mut [f32; 64]
Get a mutable slice for a buffer.
Returns a mutable reference to the 64-sample buffer identified by id.
Use this to write audio data into the buffer.
§Arguments
id- Buffer ID fromacquire()
§Returns
A mutable reference to a 64-sample audio buffer.
§Example
use aether_core::buffer_pool::BufferPool;
let mut pool = BufferPool::new(10);
let buf = pool.acquire().unwrap();
// Fill buffer with sine wave
let data = pool.get_mut(buf);
for (i, sample) in data.iter_mut().enumerate() {
let phase = i as f32 / 64.0;
*sample = (phase * std::f32::consts::TAU).sin();
}§Performance
- Time: O(1) - inline pointer arithmetic
- No bounds checking in release builds
- Real-time safe
§Panics
Panics in debug builds if id is out of range.
pub fn available(&self) -> usize
pub fn capacity(&self) -> usize
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BufferPool
impl RefUnwindSafe for BufferPool
impl Send for BufferPool
impl Sync for BufferPool
impl Unpin for BufferPool
impl UnsafeUnpin for BufferPool
impl UnwindSafe for BufferPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more