#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use core::cell::UnsafeCell;
use core::mem::MaybeUninit;
pub struct OwnedArena<T> {
#[cfg(not(feature = "alloc"))]
inner: T,
#[cfg(feature = "alloc")]
inner: Box<T>,
}
impl<T> OwnedArena<T> {
pub fn new(value: T) -> Self {
Self {
#[cfg(not(feature = "alloc"))]
inner: value,
#[cfg(feature = "alloc")]
inner: Box::new(value),
}
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T: Default> Default for OwnedArena<T> {
fn default() -> Self {
Self::new(T::default())
}
}
pub struct ArenaStorage<T> {
val: UnsafeCell<T>,
}
impl<T> ArenaStorage<T> {
pub const fn new(value: T) -> Self {
Self {
val: UnsafeCell::new(value),
}
}
}
unsafe impl<T: Send> Sync for ArenaStorage<T> {}
#[cfg(feature = "ariel-os")]
pub struct SharedArena<T: 'static> {
storage: &'static ArenaStorage<T>,
lock: ariel_os::thread::sync::Lock,
}
#[cfg(feature = "ariel-os")]
impl<T: 'static> SharedArena<T> {
pub const fn new(value: &'static ArenaStorage<T>) -> Self {
Self {
storage: value,
lock: ariel_os::thread::sync::Lock::new(),
}
}
pub fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
self.lock.acquire();
let mut arena = unsafe { &mut *self.storage.val.get() };
let res = f(&mut arena);
self.lock.release();
res
}
}
#[cfg(not(feature = "ariel-os"))]
pub struct SharedArena<T: 'static> {
storage: &'static ArenaStorage<T>,
_dummy_mutex: critical_section::Mutex<core::cell::RefCell<u8>>,
}
#[cfg(not(feature = "ariel-os"))]
impl<T: 'static> SharedArena<T> {
pub const fn new(value: &'static ArenaStorage<T>) -> Self {
Self {
storage: value,
_dummy_mutex: critical_section::Mutex::new(core::cell::RefCell::new(42)),
}
}
pub fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
critical_section::with(|cs| {
let _borrow = self._dummy_mutex.borrow_ref_mut(cs);
let arena: &mut T = unsafe { &mut *self.storage.val.get() };
f(arena)
})
}
}
unsafe impl<T: Send> Send for SharedArena<T> {}
unsafe impl<T: Send> Sync for SharedArena<T> {}