From 1bd8be2393525ec6cbec00b71fee3e37f06459ec Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 22 Sep 2024 23:58:11 +0800 Subject: [PATCH] cleanup `replace`, and make into_inner/from_inner methods the into/from inner methods are useful for using the Buffer abstraction as a way to invoke rkyv on a structure to serialize it and then manipulate the serialized data directly --- xous-ipc/src/buffer.rs | 61 +++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/xous-ipc/src/buffer.rs b/xous-ipc/src/buffer.rs index 9954013da..d9ce47a59 100644 --- a/xous-ipc/src/buffer.rs +++ b/xous-ipc/src/buffer.rs @@ -43,8 +43,8 @@ impl<'buf> Buffer<'buf> { } } - // use a volatile write to ensure a clear operation is not optimized out - // for ensuring that a buffer is cleared, e.g. at the exit of a function + /// use a volatile write to ensure a clear operation is not optimized out + /// for ensuring that a buffer is cleared, e.g. at the exit of a function pub fn volatile_clear(&mut self) { let b = self.slice.as_mut_ptr(); for i in 0..self.slice.len() { @@ -58,15 +58,15 @@ impl<'buf> Buffer<'buf> { 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. + /// 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) { (self.pages.as_ptr() as usize, self.pages.len(), self.used) } - // use to serialize a buffer between process-local threads. mainly for spawning new threads with more - // complex argument structures. + /// 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"); @@ -79,6 +79,33 @@ impl<'buf> Buffer<'buf> { } } + /// Consume the buffer and return the underlying storage. Used for situations where we just want to + /// serialize into a buffer and then do something manually with the serialized data. + /// + /// Fails if the buffer was converted from a memory message -- the Drop semantics + /// of the memory message would cause problems with this conversion. + pub fn into_inner(mut self) -> core::result::Result<(MemoryRange, usize), Error> { + if self.memory_message.is_none() { + self.should_drop = false; + Ok((self.pages, self.used)) + } else { + Err(Error::ShareViolation) + } + } + + /// Inverse of into_inner(). Used to re-cycle pages back into a Buffer so we don't have + /// to re-allocate data. Only safe if the `pages` matches the criteria for mapped memory + /// pages in Xous: page-aligned, with lengths that are a multiple of a whole page size. + pub unsafe fn from_inner(pages: MemoryRange, used: usize) -> Self { + Buffer { + pages, + slice: core::slice::from_raw_parts_mut(pages.as_mut_ptr(), pages.len()), + used, + should_drop: false, + memory_message: None, + } + } + #[allow(dead_code)] pub unsafe fn from_memory_message(mem: &'buf MemoryMessage) -> Self { Buffer { @@ -237,24 +264,10 @@ impl<'buf> Buffer<'buf> { } } - // We must have a `memory_message` to update in order for this to work. - // Otherwise, we risk having the pointer go to somewhere invalid. - if self.memory_message.is_none() { - // Create this message using `from_memory_message_mut()` instead of - // `from_memory_message()`. - Err("couldn't serialize because buffer wasn't mutable")?; - } - // Unsafe Warning: Create a copy of the backing slice to hand to the deserializer. - // This is required because the deserializer consumes the buffer and returns it - // later as part of `.into_inner()`. - // The "correct" way to do this would be to implement `rkyv::Serializer` an `rkyv::Fallible` - // for ourselves. - let copied_slice = - unsafe { core::slice::from_raw_parts_mut(self.slice.as_mut_ptr(), self.slice.len()) }; let mut scratch = [MaybeUninit::::uninit(); 256]; let wrap = Wrap(&src, PhantomData::); - let writer = RkyvBuffer::from(&mut copied_slice[..]); + let writer = RkyvBuffer::from(&mut self.slice[..]); let alloc = SubAllocator::new(&mut scratch); let serbuf = rkyv::api::low::to_bytes_in_with_alloc::<_, _, Panic>(&wrap, writer, alloc).unwrap(); @@ -295,17 +308,17 @@ impl<'buf> Buffer<'buf> { Ok(r) } - /// A representation identical to the original, but reequires copying to the stack. More expensive so uses + /// A representation identical to the original, but requires copying to the stack. More expensive so uses /// "to_" prefix. #[allow(dead_code)] - pub fn to_original(&self) -> core::result::Result + pub fn to_original(&self) -> core::result::Result where T: rkyv::Archive, U: Portable, ::Archived: Deserialize>, { let r = unsafe { rkyv::access_unchecked::(&self.slice[..self.used]) }; - Ok(rkyv::deserialize::(r).unwrap()) + rkyv::deserialize::(r).map_err(|_| Error::InternalError) } pub fn used(&self) -> usize { self.used }