add from_raw_parts and to_raw_parts methods

this is for sending buffers across thread boundaries to
help with argument initializaions
This commit is contained in:
bunnie 2021-07-28 17:34:51 +08:00
parent 9f3615fd3d
commit 1fc8cd969e

View file

@ -1,3 +1,5 @@
use core::convert::TryInto;
use rkyv::{ser::Serializer, Fallible};
use xous::{
map_memory, send_message, unmap_memory, Error, MemoryAddress, MemoryFlags, MemoryMessage,
@ -72,6 +74,43 @@ impl<'a> Buffer<'a> {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
// use to serialize a buffer between process-local threads. mainly for spawning new threads with more complex argument structures.
#[allow(dead_code)]
pub unsafe fn to_raw_parts(&self) -> (usize, usize, usize) {
if let Some(offset) = self.offset {
(
self.valid.as_ptr() as usize,
self.valid.len(),
usize::from(offset),
)
} else {
(
self.valid.as_ptr() as usize,
self.valid.len(),
0
)
}
}
// use to serialize a buffer between process-local threads. mainly for spawning new threads with more complex argument structures.
#[allow(dead_code)]
pub unsafe fn from_raw_parts(address: usize, len: usize, offset: usize) -> Self {
let mem = MemoryRange::new(address, len).expect("invalid memory range args");
let off = if offset != 0 {
Some(offset.try_into().unwrap())
} else {
None
};
Buffer {
range: mem,
slice: core::slice::from_raw_parts_mut(mem.as_mut_ptr(), mem.len()),
valid: mem,
offset: off,
should_drop: false,
memory_message: None,
}
}
#[allow(dead_code)]
pub unsafe fn from_memory_message(mem: &'a MemoryMessage) -> Self {
Buffer {