From 486d993c4cdf325a543319330a18bb6957d3d8ec Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 16 Jun 2021 18:31:16 +0800 Subject: [PATCH 1/2] xous: hosted: don't over-allocate memory messages Call `shrink_to_fit()` on the array that contains memory messages. This is beacuse we throw away the `capacity` field, and we want to make sure we can reconstitute the vector at a later stage. Signed-off-by: Sean Cross --- xous-rs/src/arch/hosted.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xous-rs/src/arch/hosted.rs b/xous-rs/src/arch/hosted.rs index 7efe75c4c..add46d0d9 100644 --- a/xous-rs/src/arch/hosted.rs +++ b/xous-rs/src/arch/hosted.rs @@ -687,6 +687,8 @@ fn _xous_syscall_result(ret: &mut Result, thread_id: TID, server_connection: &Se eprintln!("Server shut down: {}", e); std::process::exit(0); } + data.shrink_to_fit(); + assert_eq!(data.len(), data.capacity()); let len = data.len(); let addr = data.as_mut_ptr(); memory_message.buf.addr = crate::MemoryAddress::new(addr as _).unwrap(); From 00e9ead818064a495a7d289e1f4bc104e022bac6 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 16 Jun 2021 18:33:47 +0800 Subject: [PATCH 2/2] xous: definitions: dispose of Vec on hosted When running on Hosted mode, reconstitute the Vec and dispose of it rather than freeing the memory by passing it back to the kernel. This is because the data was previously allocated by way of a vec, and only a reconstituted vec knows how to properly dispose of the data. Signed-off-by: Sean Cross --- xous-rs/src/definitions.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 4b68e486e..8643e97e8 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -584,6 +584,16 @@ impl Drop for MessageEnvelope { .expect("couldn't return memory") } Message::Move(msg) => { + // Reconstitute the vec and drop it right away to ensure it's freed. + #[cfg(any(unix, windows))] + let _v = unsafe { + std::vec::Vec::from_raw_parts( + msg.buf.as_mut_ptr(), + msg.buf.len(), + msg.buf.len(), + ) + }; + #[cfg(not(any(unix, windows)))] crate::syscall::unmap_memory(msg.buf).expect("couldn't free memory message") } _ => (),