|
| 1 | +//! This module holds abstractions that enable tracking the areas dirtied by writes of a specified |
| 2 | +//! length to a given offset. In particular, this is used to track write accesses within a |
| 3 | +//! `GuestMemoryRegion` object, and the resulting bitmaps can then be aggregated to build the |
| 4 | +//! global view for an entire `GuestMemory` object. |
| 5 | +
|
| 6 | +use std::fmt::Debug; |
| 7 | + |
| 8 | +use crate::{GuestMemory, GuestMemoryRegion}; |
| 9 | + |
| 10 | +/// Trait implemented by types that support creating `BitmapSlice` objects. |
| 11 | +pub trait WithBitmapSlice<'a> { |
| 12 | + /// Type of the bitmap slice. |
| 13 | + type S: BitmapSlice; |
| 14 | +} |
| 15 | + |
| 16 | +/// Trait used to represent that a `BitmapSlice` is a `Bitmap` itself, but also satisfies the |
| 17 | +/// restriction that slices created from it have the same type as `Self`. |
| 18 | +pub trait BitmapSlice: |
| 19 | + Bitmap + Copy + Clone + Debug + for<'a> WithBitmapSlice<'a, S = Self> |
| 20 | +{ |
| 21 | +} |
| 22 | + |
| 23 | +/// Common bitmap operations. Using HRTBs to effectively define an associated type that |
| 24 | +/// has a lifetime parameter, without tagging the `Bitmap` trait with a lifetime as well. |
| 25 | +/// |
| 26 | +/// Using an associated type allows implementing the `Bitmap` and `BitmapSlice` functionality |
| 27 | +/// as a zero-cost abstraction, when providing trivial implementations such as the one |
| 28 | +/// defined for `()`. |
| 29 | +// This is just an initial proposal; we'll add other methods if they need to be part of the main |
| 30 | +// interface as well. |
| 31 | +pub trait Bitmap: for<'a> WithBitmapSlice<'a> { |
| 32 | + /// Mark the memory range specified by the given `offset` and `len` as dirtied. |
| 33 | + fn mark_dirty(&self, offset: usize, len: usize); |
| 34 | + |
| 35 | + /// Return a `Self::S::Slice` of the current bitmap, starting at the specified `offset`. |
| 36 | + fn slice_at(&self, offset: usize) -> <Self as WithBitmapSlice>::S; |
| 37 | +} |
| 38 | + |
| 39 | +/// A no-op `Bitmap` implementation that can be provided for backends that do not actually |
| 40 | +/// require the tracking functionality. |
| 41 | +
|
| 42 | +impl<'a> WithBitmapSlice<'a> for () { |
| 43 | + type S = Self; |
| 44 | +} |
| 45 | + |
| 46 | +impl BitmapSlice for () {} |
| 47 | + |
| 48 | +impl Bitmap for () { |
| 49 | + fn mark_dirty(&self, _offset: usize, _len: usize) {} |
| 50 | + |
| 51 | + fn slice_at(&self, _offset: usize) -> Self {} |
| 52 | +} |
| 53 | + |
| 54 | +/// A `Bitmap` and `BitmapSlice` implementation for `Option<B>`. |
| 55 | +
|
| 56 | +impl<'a, B> WithBitmapSlice<'a> for Option<B> |
| 57 | +where |
| 58 | + B: WithBitmapSlice<'a>, |
| 59 | +{ |
| 60 | + type S = Option<B::S>; |
| 61 | +} |
| 62 | + |
| 63 | +impl<B: BitmapSlice> BitmapSlice for Option<B> {} |
| 64 | + |
| 65 | +impl<B: Bitmap> Bitmap for Option<B> { |
| 66 | + fn mark_dirty(&self, offset: usize, len: usize) { |
| 67 | + if let Some(inner) = self { |
| 68 | + inner.mark_dirty(offset, len) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + fn slice_at(&self, offset: usize) -> Option<<B as WithBitmapSlice>::S> { |
| 73 | + if let Some(inner) = self { |
| 74 | + return Some(inner.slice_at(offset)); |
| 75 | + } |
| 76 | + None |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Helper type alias for referring to the `BitmapSlice` concrete type associated with |
| 81 | +/// an object `B: WithBitmapSlice<'a>`. |
| 82 | +pub type BS<'a, B> = <B as WithBitmapSlice<'a>>::S; |
| 83 | + |
| 84 | +/// Helper type alias for referring to the `BitmapSlice` concrete type associated with |
| 85 | +/// the memory regions of an object `M: GuestMemory`. |
| 86 | +pub type MS<'a, M> = BS<'a, <<M as GuestMemory>::R as GuestMemoryRegion>::B>; |
0 commit comments