use std::{alloc::Layout, mem, num, ptr};
use dope_sys::{
Errno,
io::{Close, Retry},
kernel,
memory::{Error, Failure},
};
use o3::{base::marker::ThreadBound, mem::Rejected};
use crate::Backend;
#[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> {
use kernel::raw::Status;
let invalid = || {
Open::Failure(Error::Length {
element: mem::size_of::<T>(),
count: len,
})
};
let layout = Layout::array::<T>(len).map_err(|_| invalid())?;
let bytes = num::NonZero::new(layout.size()).ok_or_else(invalid)?;
let element = layout.align();
let page = 4096;
if element > page {
return Err(Open::Failure(Error::Alignment {
element,
supported: page,
}));
}
let raw = crate::raw::try_map::<{ libc::MAP_NORESERVE }>(bytes)
.map_err(|cause| Open::Failure(Error::Map { bytes, cause }))?;
let Some(ptr) = ptr::NonNull::new(raw.cast()) else {
return Err(Open::close(
Error::Null { bytes },
Region {
ptr: raw,
bytes,
thread: ThreadBound::NEW,
},
));
};
let mapping = Self {
ptr,
bytes,
thread: ThreadBound::NEW,
};
let result = unsafe {
libc::madvise(
mapping.ptr.as_ptr().cast(),
mapping.bytes.get(),
libc::MADV_POPULATE_WRITE,
)
};
match unsafe { Status::new(result).try_zero::<Backend, dope_sys::Errno>() } {
Ok(()) => Ok(mapping),
Err(cause) => Err(Open::close(
Error::Populate {
bytes: mapping.bytes,
cause,
},
mapping.into_region(),
)),
}
}
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 Close for Region {
type State = Unmap;
fn try_close(self) -> Result<(), Self::State> {
let Self { ptr, bytes, thread } = self;
match unsafe { crate::raw::try_unmap(ptr, bytes) } {
Ok(()) => Ok(()),
Err(cause) => Err(Unmap {
region: Self { ptr, bytes, thread },
cause,
}),
}
}
}
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<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>());
};