use std::{mem, ops};
use o3::buffer::{bytes::bounded::Value, storage::provided};
use crate::driver::route::indices;
mod sealed;
pub(in crate::io::fd) use sealed::Discard;
pub use sealed::creation::{CreatedSlot, Creation};
use crate::{
driver,
driver::{route, route::table, schedule::ready},
io::{
fd::{domain, identity, lease},
socket::install::receive,
},
};
const OWNER_SHIFT: u32 = route::SLOT_BITS;
const UNOWNED: u8 = route::FRAMEWORK;
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct OwnedSlot(PhysicalSlot);
#[derive(Clone, Copy)]
#[repr(C, align(4))]
pub struct OutboundSlot {
index: [u8; 3],
owner: route::Id,
}
#[must_use = "accepted descriptor authority must be delivered or reclaimed"]
#[repr(transparent)]
pub struct Accepted(PhysicalSlot);
impl From<OutboundSlot> for OwnedSlot {
fn from(slot: OutboundSlot) -> Self {
Self(slot.into())
}
}
impl From<&domain::Solo<identity::Identity<'_>>> for OwnedSlot {
fn from(slot: &domain::Solo<identity::Identity<'_>>) -> Self {
Self(slot.physical())
}
}
impl From<OwnedSlot> for PhysicalSlot {
fn from(slot: OwnedSlot) -> Self {
slot.0
}
}
impl OutboundSlot {
pub(in crate::io::fd) fn new(index: indices::SlotIndex, owner: route::Id) -> Self {
Self {
index: index.into_le24(),
owner,
}
}
pub(in crate::io::fd) const fn owner(self) -> route::Id {
self.owner
}
}
impl From<OutboundSlot> for PhysicalSlot {
fn from(slot: OutboundSlot) -> Self {
Self(
(u32::from(slot.owner.raw()) << OWNER_SHIFT)
| u32::from_le_bytes([slot.index[0], slot.index[1], slot.index[2], 0]),
)
}
}
impl Accepted {
pub(crate) fn physical(&self) -> PhysicalSlot {
self.0
}
}
#[repr(transparent)]
pub struct Descriptor<'d> {
pub(in crate::io::fd) lease: lease::Lease<'d, lease::Active>,
}
#[repr(transparent)]
pub struct Datagram<'d>(Descriptor<'d>);
pub struct SocketSlot<'d> {
lease: lease::Lease<'d, lease::Unopened>,
}
impl<'d> Datagram<'d> {
pub(crate) fn new(fd: Descriptor<'d>, _: receive::Capacity<'d>) -> Self {
Self(fd)
}
}
impl From<&Datagram<'_>> for PhysicalSlot {
fn from(fd: &Datagram<'_>) -> Self {
fd.0.slot()
}
}
impl<'d> From<Datagram<'d>> for Descriptor<'d> {
fn from(value: Datagram<'d>) -> Self {
value.0
}
}
impl<'d> ops::Deref for Datagram<'d> {
type Target = Descriptor<'d>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'d> Descriptor<'d> {
pub(crate) fn from_slot(
slot: domain::Solo<identity::Identity<'d>>,
driver: driver::Reference<'d>,
) -> Self {
Self {
lease: lease::Lease::from_slot(slot, driver),
}
}
pub(crate) fn slot(&self) -> PhysicalSlot {
self.lease.slot()
}
pub fn index(&self) -> indices::SlotIndex {
self.lease.slot().into()
}
pub fn driver(&self) -> driver::Reference<'d> {
self.lease.driver()
}
pub fn ready_handle(&self) -> ready::Handle<'d> {
self.lease.ready_handle()
}
}
impl<'d> SocketSlot<'d> {
pub(crate) fn from_claim(
id: identity::Identity<'d, OutboundSlot>,
driver: driver::Reference<'d>,
) -> Self {
Self {
lease: lease::Lease::<lease::Unopened>::from_claim(id, driver),
}
}
pub(crate) fn outbound(&self) -> OutboundSlot {
self.lease.outbound()
}
}
const _: () = {
assert!(mem::size_of::<PhysicalSlot>() == mem::size_of::<u32>());
assert!(mem::size_of::<OwnedSlot>() == mem::size_of::<PhysicalSlot>());
assert!(mem::size_of::<[u8; 3]>() * u8::BITS as usize == route::SLOT_BITS as usize);
assert!(mem::align_of::<[u8; 3]>() == mem::align_of::<u8>());
assert!(mem::size_of::<OutboundSlot>() == mem::size_of::<PhysicalSlot>());
assert!(mem::align_of::<PhysicalSlot>() == mem::align_of::<u32>());
assert!(mem::align_of::<OutboundSlot>() == mem::align_of::<PhysicalSlot>());
assert!(mem::size_of::<Option<PhysicalSlot>>() == mem::size_of::<Option<u32>>());
};
const _: () = assert!(mem::size_of::<Retirement>() == mem::size_of::<u8>());
const _: () = assert!(mem::size_of::<Accepted>() == mem::size_of::<PhysicalSlot>());
const _: () = assert!(
mem::size_of::<Descriptor<'_>>()
<= mem::size_of::<
Value<provided::raw::Slice<&'static mut [u8], provided::raw::Owner<'static, ()>>>,
>()
);
const _: () =
assert!(mem::size_of::<Descriptor<'_>>() == mem::size_of::<lease::Lease<'_, lease::Active>>());
const _: () = {
assert!(mem::size_of::<Datagram<'_>>() == mem::size_of::<Descriptor<'_>>());
assert!(mem::align_of::<Datagram<'_>>() == mem::align_of::<Descriptor<'_>>());
assert!(mem::size_of::<Option<Datagram<'_>>>() == mem::size_of::<Option<Descriptor<'_>>>());
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct PhysicalSlot(u32);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Reclaimed {
Queued(PhysicalSlot),
Quarantined(PhysicalSlot),
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[repr(u8)]
pub enum Retirement {
Close,
Standalone,
}
impl PhysicalSlot {
pub(in crate::io::fd) const fn from_index(index: indices::SlotIndex) -> Self {
Self(((UNOWNED as u32) << OWNER_SHIFT) | index.raw())
}
pub(crate) fn owner(self) -> Option<route::Id> {
self.route().app()
}
pub fn route(self) -> route::Route {
route::Route::from_bits((self.0 >> OWNER_SHIFT) as u8)
}
}
impl From<Accepted> for PhysicalSlot {
fn from(accepted: Accepted) -> Self {
accepted.0
}
}
impl From<PhysicalSlot> for indices::SlotIndex {
fn from(slot: PhysicalSlot) -> Self {
Self::from_field(u64::from(slot.0))
}
}
impl TryFrom<(indices::SlotIndex, table::Capacity)> for Accepted {
type Error = (indices::SlotIndex, table::Capacity);
fn try_from((index, end): (indices::SlotIndex, table::Capacity)) -> Result<Self, Self::Error> {
if end.contains(index) {
Ok(Self(PhysicalSlot::from_index(index)))
} else {
Err((index, end))
}
}
}
impl From<PhysicalSlot> for [u8; 3] {
fn from(slot: PhysicalSlot) -> Self {
indices::SlotIndex::from(slot).into_le24()
}
}
impl From<OutboundSlot> for [u8; 3] {
fn from(slot: OutboundSlot) -> Self {
slot.index
}
}
impl From<PhysicalSlot> for u32 {
fn from(slot: PhysicalSlot) -> Self {
slot.0 & route::SLOT_MASK as u32
}
}