summary refs log tree commit diff
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2019-06-19 17:00:29 -0700
committerCommit Bot <commit-bot@chromium.org>2019-06-24 23:59:10 +0000
commitf448721872a35509993742095a1d2e54248394ff (patch)
tree011fda90edb0399bc7ff06ef2f682ff01325f0f8
parentf51787b1c75390ce699719e2b39fea6459d06e76 (diff)
downloadcrosvm-f448721872a35509993742095a1d2e54248394ff.tar
crosvm-f448721872a35509993742095a1d2e54248394ff.tar.gz
crosvm-f448721872a35509993742095a1d2e54248394ff.tar.bz2
crosvm-f448721872a35509993742095a1d2e54248394ff.tar.lz
crosvm-f448721872a35509993742095a1d2e54248394ff.tar.xz
crosvm-f448721872a35509993742095a1d2e54248394ff.tar.zst
crosvm-f448721872a35509993742095a1d2e54248394ff.zip
data_model: add sub_slice method to VolatileSlice
The new method is a copy of the get_slice trait method without the
restriction that the returned VolatileSlice has a lifetime limited to the
VolatileSlice, rather the VolatileSlice's lifetime parameter, which is
longer.

TEST=None
BUG=None

Change-Id: I1578981fcd046ce2d6232b28746c08d912c51b4d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1670548
Tested-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
-rw-r--r--data_model/src/volatile_memory.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/data_model/src/volatile_memory.rs b/data_model/src/volatile_memory.rs
index 6ce230b..66073ac 100644
--- a/data_model/src/volatile_memory.rs
+++ b/data_model/src/volatile_memory.rs
@@ -174,6 +174,21 @@ impl<'a> VolatileSlice<'a> {
         unsafe { Ok(VolatileSlice::new(new_addr as *mut u8, new_size)) }
     }
 
+    /// Similar to `get_slice` but the returned slice outlives this slice.
+    ///
+    /// The returned slice's lifetime is still limited by the underlying data's lifetime.
+    pub fn sub_slice(self, offset: u64, count: u64) -> Result<VolatileSlice<'a>> {
+        let mem_end = calc_offset(offset, count)?;
+        if mem_end > self.size {
+            return Err(Error::OutOfBounds { addr: mem_end });
+        }
+        Ok(VolatileSlice {
+            addr: (self.addr as u64 + offset) as *mut _,
+            size: count,
+            phantom: PhantomData,
+        })
+    }
+
     /// Sets each byte of this slice with the given byte, similar to `memset`.
     ///
     /// The bytes of this slice are accessed in an arbitray order.