use core::alloc::Layout;
use core::ops::Deref;
use core::sync::atomic::{AtomicBool, AtomicPtr, AtomicU32, AtomicU8};
use serde::{Deserialize, Serialize};
#[repr(C)]
pub struct BoxBytes<const N: usize> {
capacity: u32,
len: u32,
payload: [u8; N],
}
impl<const N: usize> Deref for BoxBytes<N> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.payload.as_slice()
}
}
impl BoxBytes<0> {
pub fn deref_dyn(&self) -> &[u8] {
unsafe { core::slice::from_raw_parts(self.payload.as_ptr(), self.len as usize) }
}
pub fn layout(&self) -> Layout {
let me = Layout::new::<BoxBytes<0>>();
let me_align = me.align();
let align_add = me_align - 1;
let to_add = ((self.capacity as usize + align_add) / me_align) * me_align;
unsafe { Layout::from_size_align_unchecked(me.size() + to_add, me.align()) }
}
}
impl<const N: usize> From<&'static BoxBytes<N>> for SysCallBoxBytes {
fn from(other: &'static BoxBytes<N>) -> Self {
Self {
bb_ptr: (other as *const BoxBytes<N>) as usize as u32,
}
}
}
#[derive(Serialize, Deserialize)]
pub struct SysCallBoxBytes {
bb_ptr: u32,
}
#[repr(C)]
pub struct FutureBytes {
refcnt: AtomicU32,
payload: AtomicPtr<BoxBytes<0>>,
status: AtomicU8,
ex_taken: AtomicBool,
}
pub mod status {
pub const KERNEL_ACCESS: u8 = 0;
pub const USERSPACE_ACCESS: u8 = 1;
pub const COMPLETED: u8 = 2;
pub const ERROR: u8 = 3;
pub const INVALID: u8 = 4;
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SysCallFutureBytes {
fb_ptr: u32,
}
impl From<&'static FutureBytes> for SysCallFutureBytes {
fn from(other: &'static FutureBytes) -> Self {
Self {
fb_ptr: (other as *const FutureBytes) as usize as u32,
}
}
}