2020-07-12 03:18:26 +00:00
|
|
|
//! A Carton is an object that wraps another object for shipping across the kernel
|
|
|
|
|
//! boundary. Structs that are stored in Cartons can be sent as messages.
|
|
|
|
|
extern crate alloc;
|
|
|
|
|
use alloc::alloc::{alloc, dealloc, Layout};
|
|
|
|
|
|
2020-07-14 07:10:25 +00:00
|
|
|
use crate::{MemoryMessage, MemoryRange, CID, Message, Error};
|
2020-07-12 03:18:26 +00:00
|
|
|
|
2020-07-14 07:10:25 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Carton<'a> {
|
|
|
|
|
range: MemoryRange,
|
|
|
|
|
slice: &'a [u8],
|
2020-07-12 03:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-14 07:10:25 +00:00
|
|
|
impl<'a> Carton<'a> {
|
2020-07-12 03:18:26 +00:00
|
|
|
pub fn from_bytes(bytes: &[u8]) -> Self {
|
|
|
|
|
let src_mem = bytes.as_ptr();
|
2020-07-14 07:10:25 +00:00
|
|
|
let size = bytes.len();
|
|
|
|
|
let layout = Layout::from_size_align(size, 4096).unwrap();
|
2020-07-12 03:18:26 +00:00
|
|
|
let new_mem = unsafe {
|
|
|
|
|
let new_mem = alloc(layout);
|
2020-07-14 07:10:25 +00:00
|
|
|
core::ptr::copy(src_mem, new_mem, size);
|
2020-07-12 03:18:26 +00:00
|
|
|
new_mem
|
|
|
|
|
};
|
|
|
|
|
Carton {
|
2020-07-14 07:10:25 +00:00
|
|
|
range: MemoryRange::new(new_mem as usize, size),
|
|
|
|
|
slice: unsafe { core::slice::from_raw_parts_mut(new_mem, size) },
|
2020-07-12 03:18:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn into_message(self, id: usize) -> MemoryMessage {
|
|
|
|
|
MemoryMessage {
|
|
|
|
|
id,
|
2020-07-14 07:10:25 +00:00
|
|
|
buf: self.range,
|
2020-07-12 03:18:26 +00:00
|
|
|
offset: None,
|
|
|
|
|
valid: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-14 07:10:25 +00:00
|
|
|
|
|
|
|
|
/// Perform an immutable lend of this Carton to the specified server.
|
|
|
|
|
/// This function will block until the server returns.
|
|
|
|
|
pub fn lend(&self, connection: CID, id: usize) -> Result<(), Error> {
|
|
|
|
|
let msg = MemoryMessage {
|
|
|
|
|
id,
|
|
|
|
|
buf: self.range,
|
|
|
|
|
offset: None,
|
|
|
|
|
valid: None,
|
|
|
|
|
};
|
|
|
|
|
crate::send_message(connection, Message::Borrow(msg))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Perform a mutable lend of this Carton to the server.
|
|
|
|
|
pub fn lend_mut(&mut self, connection: CID, id: usize) -> Result<(), Error> {
|
|
|
|
|
let msg = MemoryMessage {
|
|
|
|
|
id,
|
|
|
|
|
buf: self.range,
|
|
|
|
|
offset: None,
|
|
|
|
|
valid: None,
|
|
|
|
|
};
|
|
|
|
|
crate::send_message(connection, Message::MutableBorrow(msg))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> AsRef<MemoryRange> for Carton<'a> {
|
|
|
|
|
fn as_ref(&self) -> &MemoryRange {
|
|
|
|
|
&self.range
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> AsRef<[u8]> for Carton<'a> {
|
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
|
&self.slice
|
|
|
|
|
}
|
2020-07-12 03:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-14 07:10:25 +00:00
|
|
|
impl<'a> Drop for Carton<'a> {
|
2020-07-12 03:18:26 +00:00
|
|
|
fn drop(&mut self) {
|
2020-07-14 07:10:25 +00:00
|
|
|
let layout = Layout::from_size_align(self.range.len(), 4096).unwrap();
|
|
|
|
|
let ptr = self.range.as_mut_ptr();
|
2020-07-12 03:18:26 +00:00
|
|
|
unsafe { dealloc(ptr, layout) };
|
|
|
|
|
}
|
|
|
|
|
}
|