Skip to content

Commit c6a623d

Browse files
Serban Iorgaalxiord
authored andcommitted
[GuestMemory] add example for get_host_address()
Signed-off-by: Serban Iorga <seriorga@amazon.com>
1 parent 9a274b9 commit c6a623d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/guest_memory.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,28 @@ pub trait GuestMemory {
405405
/// Note: the underline guest memory is not protected from memory aliasing, which breaks the
406406
/// rust memory safety model. It's the caller's responsibility to ensure that there's no
407407
/// concurrent accesses to the underline guest memory.
408+
///
409+
/// # Arguments
410+
/// * `guest_addr` - Guest address to convert.
411+
///
412+
/// # Examples
413+
///
414+
/// ```
415+
/// # #[cfg(feature = "backend-mmap")]
416+
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
417+
///
418+
/// # #[cfg(feature = "backend-mmap")]
419+
/// # fn test_get_host_address() -> Result<(), ()> {
420+
/// let start_addr = GuestAddress(0x1000);
421+
/// let mut gm = GuestMemoryMmap::new(&vec![(start_addr, 0x500)]).map_err(|_| ())?;
422+
/// let addr = gm.get_host_address(GuestAddress(0x1200)).unwrap();
423+
/// println!("Host address is {:p}", addr);
424+
/// Ok(())
425+
/// # }
426+
///
427+
/// # #[cfg(feature = "backend-mmap")]
428+
/// test_get_host_address();
429+
/// ```
408430
fn get_host_address(&self, addr: GuestAddress) -> Result<*mut u8> {
409431
self.to_region_addr(addr)
410432
.ok_or_else(|| Error::InvalidGuestAddress(addr))

0 commit comments

Comments
 (0)