sim_lib_audio_graph_core/
arena.rs1use sim_kernel::{Error, Result};
2
3#[derive(Clone, Debug, Default, PartialEq)]
9pub struct BlockArena {
10 f32_cells: Vec<f32>,
11 used_f32: usize,
12}
13
14impl BlockArena {
15 pub fn empty() -> Self {
17 Self::default()
18 }
19
20 pub fn with_f32_capacity(capacity: usize) -> Self {
22 Self {
23 f32_cells: vec![0.0; capacity],
24 used_f32: 0,
25 }
26 }
27
28 pub fn reset(&mut self) {
30 self.used_f32 = 0;
31 }
32
33 pub fn f32_capacity(&self) -> usize {
35 self.f32_cells.len()
36 }
37
38 pub fn alloc_f32(&mut self, len: usize) -> Result<&mut [f32]> {
42 let start = self.used_f32;
43 let end = start
44 .checked_add(len)
45 .ok_or_else(|| Error::Eval("audio block arena allocation overflow".to_owned()))?;
46 if end > self.f32_cells.len() {
47 return Err(Error::Eval(format!(
48 "audio block arena exhausted: requested {len} f32 cells with {} remaining",
49 self.f32_cells.len().saturating_sub(start)
50 )));
51 }
52 self.used_f32 = end;
53 let cells = &mut self.f32_cells[start..end];
54 cells.fill(0.0);
55 Ok(cells)
56 }
57}