use sim_kernel::{Error, Result};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BlockArena {
f32_cells: Vec<f32>,
used_f32: usize,
}
impl BlockArena {
pub fn empty() -> Self {
Self::default()
}
pub fn with_f32_capacity(capacity: usize) -> Self {
Self {
f32_cells: vec![0.0; capacity],
used_f32: 0,
}
}
pub fn reset(&mut self) {
self.used_f32 = 0;
}
pub fn f32_capacity(&self) -> usize {
self.f32_cells.len()
}
pub fn alloc_f32(&mut self, len: usize) -> Result<&mut [f32]> {
let start = self.used_f32;
let end = start
.checked_add(len)
.ok_or_else(|| Error::Eval("audio block arena allocation overflow".to_owned()))?;
if end > self.f32_cells.len() {
return Err(Error::Eval(format!(
"audio block arena exhausted: requested {len} f32 cells with {} remaining",
self.f32_cells.len().saturating_sub(start)
)));
}
self.used_f32 = end;
let cells = &mut self.f32_cells[start..end];
cells.fill(0.0);
Ok(cells)
}
}