dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use std::{marker, num, ptr};

use o3::{alloc, alloc::tail, base::marker::ThreadBound, depot::arena::linked::chain};

use crate::{
    driver,
    driver::{
        receive::{
            credit,
            credit::{sealed::groups, storage, storage::Entry},
        },
        route::table::conn,
        schedule::ready,
        settings,
    },
};

/// Owner of exact reusable linear retention groups.
pub struct Owner<T: Copy, S: Copy, C> {
    context: C,
    groups: chain::List<storage::Group<C>, Entry<T, S, C>>,
    thread: marker::PhantomData<ThreadBound>,
}

impl<T: Copy, S: Copy, C> Owner<T, S, C> {
    pub const fn new(context: C) -> Self {
        Self {
            context,
            groups: chain::List::new(),
            thread: marker::PhantomData,
        }
    }

    pub fn context(&self) -> &C {
        &self.context
    }

    pub fn try_slots(
        &self,
        capacity: num::NonZero<u32>,
    ) -> Result<storage::Slots<'_, T, S, C>, alloc::Error> {
        let count = capacity.get() as usize;
        let mut selected: Option<tail::raw::View<'_, _, _>> = None;
        for raw in self.groups.iter() {
            if !raw.header().value.available.get() || raw.len() < count {
                continue;
            }
            if selected.is_none_or(|current| raw.len() < current.len()) {
                selected = Some(raw);
            }
            if raw.len() == count {
                break;
            }
        }
        if let Some(block) = selected {
            let slots = storage::Slots { block, capacity };
            let group = &block.header().value;
            group.available.set(false);
            group.context.set(ptr::NonNull::from(&self.context));
            for entry in groups::entries(&slots) {
                entry.state.set(storage::State::Idle);
            }
            group.attachment.set(storage::Attachment::Active);
            return Ok(slots);
        }
        let extent = count.checked_next_power_of_two().ok_or(alloc::Overflow)?;
        let memory = tail::Plan::try_new(extent)?.try_allocate()?;
        let block = memory.fill(
            chain::Node::new(storage::Group::new(&self.context)),
            |node, _index| Entry::new(&node.value),
        );
        Ok(storage::Slots {
            block: self.groups.push(block),
            capacity,
        })
    }
}

impl Owner<ready::Stored, credit::Wake, ready::Arena> {
    pub(in crate::driver) fn try_new(plan: settings::Plan<'_>) -> Result<Self, alloc::Error> {
        Ok(Self::new(ready::Arena::new(plan)?))
    }

    pub(in crate::driver) fn try_storage<'d>(
        &'d self,
        cap: conn::Capacity,
        driver: driver::Invariant<'d>,
    ) -> Result<credit::Storage<'d>, alloc::Error> {
        let entries = self.try_slots(cap.count())?;
        Ok(credit::Storage { entries, driver })
    }
}

const _: () = {
    use std::{mem, num};

    assert!(
        mem::size_of::<Owner<num::NonZero<u64>, num::NonZero<u8>, ()>>() == mem::size_of::<usize>()
    );
};