Trait vm_memory::guest_memory::GuestAddressSpace
source · pub trait GuestAddressSpace {
type M: GuestMemory;
type T: Clone + Deref<Target = Self::M>;
// Required method
fn memory(&self) -> Self::T;
}
Expand description
GuestAddressSpace
provides a way to retrieve a GuestMemory
object.
The vm-memory crate already provides trivial implementation for
references to GuestMemory
or reference-counted GuestMemory
objects,
but the trait can also be implemented by any other struct in order
to provide temporary access to a snapshot of the memory map.
In order to support generic mutable memory maps, devices (or other things
that access memory) should store the memory as a GuestAddressSpace<M>
.
This example shows that references can also be used as the GuestAddressSpace
implementation, providing a zero-cost abstraction whenever immutable memory
maps are sufficient.
§Examples (uses the backend-mmap
and backend-atomic
features)
pub struct VirtioDevice<AS: GuestAddressSpace> {
mem: Option<AS>,
}
impl<AS: GuestAddressSpace> VirtioDevice<AS> {
fn new() -> Self {
VirtioDevice { mem: None }
}
fn activate(&mut self, mem: AS) {
self.mem = Some(mem)
}
}
fn get_mmap() -> GuestMemoryMmap<()> {
let start_addr = GuestAddress(0x1000);
GuestMemoryMmap::from_ranges(&vec![(start_addr, 0x400)])
.expect("Could not create guest memory")
}
// Using `VirtioDevice` with an immutable GuestMemoryMmap:
let mut for_immutable_mmap = VirtioDevice::<&GuestMemoryMmap<()>>::new();
let mmap = get_mmap();
for_immutable_mmap.activate(&mmap);
let mut another = VirtioDevice::<&GuestMemoryMmap<()>>::new();
another.activate(&mmap);
// Using `VirtioDevice` with a mutable GuestMemoryMmap:
let mut for_mutable_mmap = VirtioDevice::<GuestMemoryAtomic<GuestMemoryMmap<()>>>::new();
let atomic = GuestMemoryAtomic::new(get_mmap());
for_mutable_mmap.activate(atomic.clone());
let mut another = VirtioDevice::<GuestMemoryAtomic<GuestMemoryMmap<()>>>::new();
another.activate(atomic.clone());
// atomic can be modified here...
Required Associated Types§
sourcetype M: GuestMemory
type M: GuestMemory
The type that will be used to access guest memory.