diff options
author | Sonny Rao <sonnyrao@chromium.org> | 2018-03-22 22:41:56 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-03-27 18:01:43 -0700 |
commit | 62a4063aa6c28d1f73e93fd0e7da2135d4d46d02 (patch) | |
tree | 123b80b5f47507f0f7f914a4f529ea8bc1215387 /sys_util/src/guest_memory.rs | |
parent | ca3a4822cca166fa341615a2b55cd4df18185b87 (diff) | |
download | crosvm-62a4063aa6c28d1f73e93fd0e7da2135d4d46d02.tar crosvm-62a4063aa6c28d1f73e93fd0e7da2135d4d46d02.tar.gz crosvm-62a4063aa6c28d1f73e93fd0e7da2135d4d46d02.tar.bz2 crosvm-62a4063aa6c28d1f73e93fd0e7da2135d4d46d02.tar.lz crosvm-62a4063aa6c28d1f73e93fd0e7da2135d4d46d02.tar.xz crosvm-62a4063aa6c28d1f73e93fd0e7da2135d4d46d02.tar.zst crosvm-62a4063aa6c28d1f73e93fd0e7da2135d4d46d02.zip |
crosvm: guest_memory: add memory_size method
This is useful to get the total size of memory without having to write something that iterates over the regions explicitly. BUG=chromium:797868 TEST=./build_test passes on all architectures TEST=crosvm runs on caroline Change-Id: Iac9a341b4c41d6462cf731f6267b92a0169578e4 Reviewed-on: https://chromium-review.googlesource.com/977565 Commit-Ready: Sonny Rao <sonnyrao@chromium.org> Tested-by: Sonny Rao <sonnyrao@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'sys_util/src/guest_memory.rs')
-rw-r--r-- | sys_util/src/guest_memory.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/sys_util/src/guest_memory.rs b/sys_util/src/guest_memory.rs index 23c8a2f..31500ca 100644 --- a/sys_util/src/guest_memory.rs +++ b/sys_util/src/guest_memory.rs @@ -106,6 +106,15 @@ impl GuestMemory { .map_or(GuestAddress(0), |region| region_end(region)) } + /// Returns the total size of memory in bytes. + pub fn memory_size(&self) -> u64 + { + self.regions + .iter() + .map(|region| region.mapping.size() as u64) + .sum() + } + /// Returns true if the given address is within the memory range available to the guest. pub fn address_in_range(&self, addr: GuestAddress) -> bool { addr < self.end_addr() @@ -462,6 +471,20 @@ mod tests { assert_eq!(val2, num2); } + #[test] + fn test_memory_size() { + let start_region1 = GuestAddress(0x0); + let size_region1 = 0x1000; + let start_region2 = GuestAddress(0x10000); + let size_region2 = 0x2000; + let gm = GuestMemory::new(&vec![(start_region1, size_region1), + (start_region2, size_region2)]).unwrap(); + + let mem_size = gm.memory_size(); + assert_eq!(mem_size, size_region1 + size_region2); + } + + // Get the base address of the mapping for a GuestAddress. fn get_mapping(mem: &GuestMemory, addr: GuestAddress) -> Result<*const u8> { mem.do_in_region(addr, |mapping, _| Ok(mapping.as_ptr() as *const u8)) |