From 1fc8cd969e2d6151cf8cd2cc14dd0b0bceded786 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 28 Jul 2021 17:34:51 +0800 Subject: [PATCH] add from_raw_parts and to_raw_parts methods this is for sending buffers across thread boundaries to help with argument initializaions --- xous-ipc/src/buffer.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/xous-ipc/src/buffer.rs b/xous-ipc/src/buffer.rs index bf2016f72..5d87260e0 100644 --- a/xous-ipc/src/buffer.rs +++ b/xous-ipc/src/buffer.rs @@ -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 {