Skip to main content

bump_stack/
bump_stack.rs

1use std::sync::OnceLock;
2use std::sync::Mutex;
3use zigzag_alloc::alloc::bump::BumpAllocator;
4use zigzag_alloc::collections::ExVec;
5
6static BUMP: OnceLock<Mutex<BumpAllocator>> = OnceLock::new();
7
8fn get_bump() -> &'static Mutex<BumpAllocator> {
9    BUMP.get_or_init(|| {
10        let buffer = Box::leak(vec![0u8; 1024].into_boxed_slice());
11        Mutex::new(BumpAllocator::new(buffer))
12    })
13}
14
15fn main() {
16    let bump_mutex = get_bump();
17    let mut bump = bump_mutex.lock().unwrap();
18
19    {
20        let mut stack_vec = ExVec::new(&*bump);
21        
22        stack_vec.push(10);
23        stack_vec.push(20);
24        stack_vec.push(30);
25
26        println!("Bump usage: {}/1024", bump.used());
27    } 
28
29    bump.reset();
30    
31    println!("Bump reset. Usage: {}", bump.used());
32}