Skip to main content

BufferPool

Struct BufferPool 

Source
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

Implementations§

Source§

impl BufferPool

Source

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

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 buffer
  • None - 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
Source

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
Source

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 from acquire()
§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.

Source

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 from acquire()
§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.

Source

pub fn silence() -> &'static [f32; 64]

Return a zeroed silence buffer (static, no allocation).

Source

pub fn available(&self) -> usize

Source

pub fn capacity(&self) -> usize

Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.