use bumpalo::Bump;
use std::rc::Rc;
#[derive(Debug)]
pub struct TwoBufferBumpAllocator {
buffer_a: Rc<Bump>,
buffer_b: Rc<Bump>,
current_is_a: bool,
max_recursion_depth: usize,
}
impl TwoBufferBumpAllocator {
pub fn new() -> Self {
Self {
buffer_a: Rc::new(Bump::new()),
buffer_b: Rc::new(Bump::new()),
current_is_a: true,
max_recursion_depth: 10000, }
}
pub fn current(&self) -> &Bump {
if self.current_is_a {
&self.buffer_a
} else {
&self.buffer_b
}
}
pub fn next(&self) -> &Bump {
if self.current_is_a {
&self.buffer_b
} else {
&self.buffer_a
}
}
pub fn swap_and_clear(self) -> Self {
let new_current = !self.current_is_a;
Self {
buffer_a: if new_current {
Rc::new(Bump::new())
} else {
self.buffer_a.clone()
},
buffer_b: if new_current {
self.buffer_b.clone()
} else {
Rc::new(Bump::new())
},
current_is_a: new_current,
max_recursion_depth: self.max_recursion_depth,
}
}
pub fn with_max_recursion_depth(mut self, depth: usize) -> Self {
self.max_recursion_depth = depth;
self
}
pub fn max_recursion_depth(&self) -> usize {
self.max_recursion_depth
}
}
impl Default for TwoBufferBumpAllocator {
fn default() -> Self {
Self::new()
}
}