use crate::resources::window::Window;
#[derive(Clone, Default)]
pub struct Arena {
data: Vec<f32>,
cursor: usize,
}
#[derive(Clone, Default)]
pub struct RuntimeArena {
data: Box<[f32]>,
scratch_start: usize, rt_cursor: usize, }
impl Arena {
pub fn new(capacity_hint: usize) -> Self {
Self {
data: Vec::with_capacity(capacity_hint),
cursor: 0,
}
}
pub fn alloc(&mut self, size: usize) -> Window {
if self.cursor + size > self.data.len() {
self.data.resize(self.cursor + size, 0.0);
}
let start = self.cursor;
self.cursor += size;
Window { start, len: size }
}
pub fn seal(mut self, rt_capacity: usize) -> RuntimeArena {
let total = self.cursor + rt_capacity;
self.data.resize(total, 0.0);
RuntimeArena {
data: self.data.into_boxed_slice(),
scratch_start: self.cursor,
rt_cursor: self.cursor,
}
}
}
impl RuntimeArena {
pub fn rt_alloc(&mut self, len: usize) -> Window {
assert!(
self.rt_cursor + len <= self.data.len(),
"RuntimeArena Capacity Exceeded!: requested {len}, {} remaining",
self.data.len() - self.rt_cursor
);
let start = self.rt_cursor;
self.rt_cursor += len;
Window { start, len }
}
pub fn rt_reset(&mut self) {
self.rt_cursor = self.scratch_start;
}
#[inline(always)]
pub fn slice(&self, w: Window) -> &[f32] {
debug_assert!(w.start + w.len <= self.data.len());
&self.data[w.start..w.start + w.len]
}
#[inline(always)]
pub fn slice_mut(&mut self, w: Window) -> &mut [f32] {
debug_assert!(w.start + w.len <= self.data.len());
&mut self.data[w.start..w.start + w.len]
}
pub fn rt_allocated(&self) -> usize {
self.rt_cursor - self.scratch_start
}
pub fn rt_capacity(&self) -> usize {
self.data.len() - self.rt_cursor
}
pub fn total_capacity(&self) -> usize {
self.data.len()
}
}