From 85d55ca8f2539a5f766aa375b011163832d5a592 Mon Sep 17 00:00:00 2001 From: johannst Date: Sun, 30 May 2021 01:59:03 +0200 Subject: usermem: add load fn --- src/lib.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6793272..83532c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,7 +63,7 @@ impl UserMem { } /// Allocate a zero-initialized memory region of `len` bytes and initialize the first bytes - /// with `init_len`. + /// with `init_from`. /// /// # Panics /// @@ -72,9 +72,21 @@ impl UserMem { assert!(len >= init_from.len()); let mut m = UserMem::new(len)?; - m.as_mut()[..init_from.len()].copy_from_slice(init_from); + m.load(PhysAddr(0), init_from); Ok(m) } + + /// Load the bytes stored in `data` into memory at physical address `addr`. + /// + /// # Panics + /// + /// Panics if `addr + data.len` is larger than the memory size `len`. + pub fn load(&mut self, addr: PhysAddr, data: &[u8]) { + assert!(self.len >= addr.0 as usize + data.len()); + + let addr = addr.0 as usize; + self.as_mut()[addr..addr + data.len()].copy_from_slice(data); + } } impl ops::Drop for UserMem { -- cgit v1.2.3