use crate::{buffer, buffer::storage::provided::arena};
pub(in crate::buffer::storage::provided) mod account;
pub(in crate::buffer::storage::provided) mod owner;
pub(in crate::buffer::storage::provided) mod root;
pub(in crate::buffer::storage::provided) mod vacant;
pub(in crate::buffer) mod entry;
pub mod native;
mod nodes;
use std::{cell, marker, mem, num, pin, ptr, slice};
pub(in crate::buffer::storage::provided) use entry::Entry;
pub use entry::Layout;
pub(in crate::buffer::storage::provided) use nodes::{Empty, Ready};
use root::Root;
pub(in crate::buffer::storage::provided) use root::State;
type Link<'d, L> = ptr::NonNull<Entry<'d, L>>;
use crate::{
alloc,
alloc::tail,
base::cell::{brand, region},
buffer::storage::provided::fixed,
mem::slot,
};
#[repr(transparent)]
pub struct Arena<T, C = (), L: Layout = cell::Cell<u32>> {
entries: tail::Block<Header<C, L>, mem::MaybeUninit<Slot<'static, T, L>>, num::NonZero<u32>>,
}
#[repr(C)]
struct Header<C, L: Layout, S = mem::MaybeUninit<State<'static, L>>> {
state: S,
context: C,
layout: marker::PhantomData<L>,
}
pub(in crate::buffer::storage::provided) struct Scope<'d, T, L: Layout> {
slots: slice::Iter<'d, arena::Slot<'d, T, L>>,
}
impl<T, C, L: Layout> Arena<T, C, L> {
pub fn try_allocate(
entries: num::NonZero<u32>,
) -> Result<impl FnOnce(C) -> Self, alloc::Error> {
let memory = tail::Plan::try_new(entries)?.try_allocate()?;
Ok(move |context| Self::initialize(memory, context))
}
fn initialize(
memory: tail::Uninit<Header<C, L>, Slot<'static, T, L>, num::NonZero<u32>>,
context: C,
) -> Self {
let header = Header {
state: mem::MaybeUninit::uninit(),
context,
layout: marker::PhantomData,
};
Self {
entries: memory.init(header),
}
}
pub fn context(&self) -> &C {
&tail::View::new(&self.entries).header().context
}
pub fn capacity(&self) -> num::NonZero<u32> {
self.entries.extent()
}
pub fn into_context(self) -> C {
self.entries.into_header().context
}
fn scope_in<S, R>(
&mut self,
state: pin::Pin<&mut S>,
resident: L::Shared,
run: impl for<'d> FnOnce(
region::Token<region::Identity<'d>>,
Root<'d, T, C, L>,
pin::Pin<&'d mut S>,
) -> R,
) -> R {
let mut input = (self, state);
region::Token::scope_pinned(pin::Pin::new(&mut input), |token, input| {
let (arena, state) = input.get_mut();
let (root, scope) = unsafe { arena.enter(arena.capacity().get(), resident) };
let result = run(token, root, state.as_mut());
scope.finish(drop);
result
})
}
}
impl<T, C: buffer::raw::Fixed<T>> Arena<T, C, ()> {
pub fn run<S, O: fixed::Operation<S, T, C>>(
&mut self,
state: pin::Pin<&mut S>,
accounts: u32,
terminal: &mut O::Terminal,
op: O,
) -> O::Output {
let resident = self.context().resident();
let mut input = (self, state, terminal);
brand::scope_pinned(pin::Pin::new(&mut input), |token, regions, input| {
let (arena, state, terminal) = input.get_mut();
let (arena, scope) = unsafe { arena.enter(accounts, resident) };
let terminal = fixed::Loan {
target: &mut **terminal,
brand: regions.0.proof(),
};
let (output, mut end) = op.run(token, regions, arena, state.as_mut(), terminal);
scope.finish(|item| end.release(item.into_inner()));
output
})
}
}
impl<'d, T, L: Layout> Scope<'d, T, L> {
fn finish(self, mut release: impl FnMut(slot::Occupied<'d, T>)) {
for slot in self.slots {
if let Some(item) = unsafe { arena::Entry::revoke(slot) } {
release(item);
}
}
}
}
const _: () = {
assert!(!mem::needs_drop::<State<'_, ()>>());
assert!(size_of::<Arena<(), (), ()>>() == size_of::<usize>());
assert!(size_of::<Root<'_, (), (), ()>>() == size_of::<usize>());
};
impl<T, C> Arena<T, C> {
pub(in crate::buffer) unsafe fn enter_scope(&mut self) -> (Root<'_, T, C>, impl FnOnce() + '_) {
let (root, scope) = unsafe { self.enter(self.capacity().get(), ()) };
(root, move || scope.finish(drop))
}
pub fn scope<S, R>(
&mut self,
state: pin::Pin<&mut S>,
run: impl for<'d> FnOnce(
region::Token<region::Identity<'d>>,
Root<'d, T, C>,
pin::Pin<&'d mut S>,
) -> R,
) -> R {
self.scope_in(state, (), run)
}
}
impl<T, C: buffer::raw::Fixed<T>> Arena<T, C, ()> {
pub fn scope<S, R>(
&mut self,
state: pin::Pin<&mut S>,
run: impl for<'d> FnOnce(
region::Token<region::Identity<'d>>,
Root<'d, T, C, ()>,
pin::Pin<&'d mut S>,
) -> R,
) -> R {
let resident = self.context().resident();
self.scope_in(state, resident, run)
}
}
#[repr(C, align(8))]
pub(in crate::buffer::storage::provided) struct Slot<'d, T, L: Layout> {
entry: arena::Entry<'d, L>,
item: cell::UnsafeCell<mem::MaybeUninit<T>>,
}
impl<T> Slot<'_, T, cell::Cell<u32>> {
pub(in crate::buffer::storage::provided) fn set_resident(&self, resident: u32) {
self.entry.resident.set(mem::MaybeUninit::new(resident));
}
}
impl<T, L: Layout> Slot<'_, T, L> {
unsafe fn occupied(&self) -> slot::Occupied<'_, T> {
unsafe { slot::Vacant::new(&mut *self.item.get()).assume_init() }
}
}