[][src]Trait vm_memory::guest_memory::GuestAddressSpace

pub trait GuestAddressSpace {
    type M: GuestMemory;
    type T: Clone + Deref<Target = Self::M>;
    fn memory(&self) -> Self::T;
}

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. This example shows that references can also be used as the GuestAddressSpace implementation, providing a zero-cost abstraction whenever immutable memory maps are sufficient.


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)
    }
}


// Using `VirtioDevice` with an immutable GuestMemoryMmap:
let mut for_immutable_mmap: VirtioDevice<&GuestMemoryMmap> =
    VirtioDevice::new();
let mmap = get_mmap();
for_immutable_mmap.activate(&mmap);
let mut another: VirtioDevice<&GuestMemoryMmap> =
    VirtioDevice::new();
another.activate(&mmap);

// Using `VirtioDevice` with a mutable GuestMemoryMmap:
let mut for_mutable_mmap: VirtioDevice<GuestMemoryAtomic<GuestMemoryMmap>> =
    VirtioDevice::new();
let atomic = GuestMemoryAtomic::new(get_mmap());
for_mutable_mmap.activate(atomic.clone());
let mut another: VirtioDevice<GuestMemoryAtomic<GuestMemoryMmap>> =
    VirtioDevice::new();
another.activate(atomic.clone());
// atomic can be modified here...

Associated Types

type M: GuestMemory

The type that will be used to access guest memory.

type T: Clone + Deref<Target = Self::M>

A type that provides access to the memory.

Loading content...

Required methods

fn memory(&self) -> Self::T

Return an object (e.g. a reference or guard) that can be used to access memory through this address space. The object provides a consistent snapshot of the memory map.

Loading content...

Implementations on Foreign Types

impl<M: GuestMemory, '_> GuestAddressSpace for &'_ M[src]

type T = Self

type M = M

impl<M: GuestMemory> GuestAddressSpace for Rc<M>[src]

type T = Self

type M = M

impl<M: GuestMemory> GuestAddressSpace for Arc<M>[src]

type T = Self

type M = M

Loading content...

Implementors

Loading content...