o3 0.60.0

shared-nothing primitives
Documentation
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,
};

/// Inactive allocation; no item or charge exists outside its private scope.
#[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> {
    /// Allocates the exact arena before its context is constructed.
    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();
            // SAFETY: the complete generative call borrows this inactive allocation;
            // the hidden scope reclaims every issued capability before it returns.
            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, ()> {
    /// Limits distinct accounted slots to `accounts`, capped by this arena's capacity.
    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();
            // SAFETY: this sole generation borrow retains the allocation until
            // the operation ends and the hidden scope reclaims every slot.
            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 {
            // SAFETY: the callback ended all issued access; budgets and the
            // operation's final authority remain alive throughout reclamation.
            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> {
    /// # Safety
    /// The raw Arenas contract retains the root and runs its terminal exactly once.
    pub(in crate::buffer) unsafe fn enter_scope(&mut self) -> (Root<'_, T, C>, impl FnOnce() + '_) {
        // SAFETY: the caller retains this inactive arena and executes the
        // returned terminal after all issued access ends, before releasing it.
        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> {
    /// # Safety
    /// The caller exclusively owns the initialized item after all projections end.
    unsafe fn occupied(&self) -> slot::Occupied<'_, T> {
        // SAFETY: the slot retains this item at its final address for the exclusive loan.
        unsafe { slot::Vacant::new(&mut *self.item.get()).assume_init() }
    }
}