use std::{alloc::Layout, mem, num, ptr};
use dope_sys::{
Errno,
io::{Close, Retry},
memory::{Error, Failure},
};
use o3::{base::marker::ThreadBound, mem::Rejected};
mod ffi;
#[derive(Debug)]
pub struct Buffer<T> {
ptr: ptr::NonNull<T>,
bytes: num::NonZero<usize>,
thread: ThreadBound,
}
#[derive(Debug)]
pub struct Unmap {
region: Region,
cause: Errno,
}
#[derive(Debug)]
pub struct Region {
ptr: *mut libc::c_void,
bytes: num::NonZero<usize>,
thread: ThreadBound,
}
#[derive(Debug)]
pub(crate) enum Open {
Failure(Error),
Cleanup { primary: Error, current: Unmap },
}
impl Open {
fn close(primary: Error, region: Region) -> Self {
match region.try_close() {
Ok(()) => Self::Failure(primary),
Err(current) => Self::Cleanup { primary, current },
}
}
}
impl<T> Buffer<T> {
pub(crate) fn allocate(len: usize) -> Result<Self, Open> {
let bytes = Layout::array::<T>(len)
.ok()
.and_then(|layout| num::NonZero::new(layout.size()))
.ok_or(Open::Failure(Error::Length {
element: mem::size_of::<T>(),
count: len,
}))?;
let element = mem::align_of::<T>();
let page = 4096;
if element > page {
return Err(Open::Failure(Error::Alignment {
element,
supported: page,
}));
}
let region = Region::reserve::<{ libc::MAP_NORESERVE }>(bytes)
.map_err(|cause| Open::Failure(Error::Map { bytes, cause }))?;
let mut mapping = match region.admit() {
Ok(mapping) => mapping,
Err(null) => return Err(Open::close(Error::Null { bytes }, null)),
};
match ffi::populate(&mut mapping) {
Ok(()) => Ok(mapping),
Err(cause) => Err(Open::close(
Error::Populate { bytes, cause },
mapping.into_region(),
)),
}
}
pub(in crate::reactor) fn base(&self) -> ptr::NonNull<T> {
self.ptr
}
pub(in crate::reactor) fn bytes(&self) -> num::NonZero<usize> {
self.bytes
}
pub(crate) fn as_ptr(&self) -> *const T {
self.ptr.as_ptr()
}
pub(crate) fn as_mut_ptr(&mut self) -> *mut T {
self.ptr.as_ptr()
}
pub(super) fn into_region(self) -> Region {
let Self { ptr, bytes, thread } = self;
Region {
ptr: ptr.as_ptr().cast(),
bytes,
thread,
}
}
}
impl Region {
pub(in crate::reactor) fn reserve<const FLAGS: libc::c_int>(
bytes: num::NonZero<usize>,
) -> Result<Self, Errno> {
Ok(Self {
ptr: ffi::map::<FLAGS>(bytes)?,
bytes,
thread: ThreadBound::NEW,
})
}
pub(in crate::reactor) fn admit<T>(self) -> Result<Buffer<T>, Self> {
match ptr::NonNull::new(self.ptr.cast()) {
Some(ptr) => Ok(Buffer {
ptr,
bytes: self.bytes,
thread: self.thread,
}),
None => Err(self),
}
}
}
impl Close for Region {
type State = Unmap;
fn try_close(self) -> Result<(), Self::State> {
ffi::release(self)
}
}
impl Failure for Unmap {
fn failure(&self) -> Error {
Error::Unmap {
address: self.region.ptr,
bytes: self.region.bytes,
cause: self.cause,
}
}
}
impl Retry for Unmap {
type Failure = Error;
type Owner = Region;
fn recover(self) -> Rejected<Region, Error> {
let failure = self.failure();
Rejected::new(self.region, failure)
}
}
impl From<&Unmap> for crate::Unmap {
fn from(failed: &Unmap) -> Self {
Self::new(failed.region.ptr, failed.region.bytes, failed.cause.into())
}
}
impl From<Unmap> for Region {
fn from(failed: Unmap) -> Self {
failed.region
}
}
impl<T> Close for Buffer<T> {
type State = Unmap;
fn try_close(self) -> Result<(), Self::State> {
self.into_region().try_close()
}
}
const _: () = {
assert!(mem::size_of::<Buffer<u8>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<Unmap>() == 3 * mem::size_of::<usize>());
};