use crate::Arena;
use std::alloc::{Allocator, GlobalAlloc};
use std::collections::LinkedList;
use std::sync::{Arc, LazyLock, Mutex, RwLock};
pub type SafeArena = Arc<RwLock<Arena>>;
type Sand = LinkedList<Arena>;
type DuneLock = Arc<Mutex<u8>>;
type ArrakisDunes = LazyLock<Sand>;
type ArrakisLock = LazyLock<DuneLock>;
static mut ARRAKIS: ArrakisDunes = ArrakisDunes::new(|| Sand::default());
static mut LOCK: ArrakisLock = ArrakisLock::new(|| Arc::new(Mutex::new(0)));
pub fn dune_allocate(chunk_size: usize) -> &'static Arena {
unsafe {
let _unused = LOCK.lock().unwrap();
ARRAKIS.push_back_mut(Arena::with_capacity(chunk_size))
}
}
pub fn dune_deallocate(arena: &'static Arena) {
let address = arena.address();
unsafe {
let _unused = LOCK.lock().unwrap();
ARRAKIS.retain(|e| e.address() != address);
}
}
#[macro_export]
macro_rules! rumtk_dune_new {
( $capacity:expr ) => {{
use $crate::dune::dune_allocate;
dune_allocate($capacity)
}};
}
#[macro_export]
macro_rules! rumtk_dune_free {
( $arena:expr ) => {{
use $crate::dune::dune_deallocate;
dune_deallocate($arena)
}};
}