Struct virtio_device::mem::DeviceRange
source · pub struct DeviceRange<'a>(/* private fields */);
Expand description
Represents a range of memory as seen from the device.
A DeviceRange
can be accessed through the try_ptr
and
try_mut_ptr
methods. Although these functions are guaranteed to point to
valid memory, due to the requirements on new
, raw pointers are still returned as the
memory may always be being modified in parallel by the guest and so references cannot be safely
created. The onus therefore is on the caller to access this memory in a way that is safe under
concurrent modifications.
Although there may be concurrent modifications, these are only from the guest, and it can be
assumed that a DeviceRange
does not alias any other Rust objects from the heap, stack,
globals etc.
With the requirements on new
users of a DeviceRange
can assume that pointers
returned from try_ptr
and try_mut_ptr
are valid for reads and
writes and are correctly aligned. Further, it can be assumed that ptr.offset(N)
is valid for
any N < len() / size_of::<T>()
. These pointer are only valid as long as the original
DeviceRange
is still alive.
The expected way to get DeviceRange
is through DriverMem::translate
, and it is only
implementations of that trait that are expected to use new
and actually construct
a DeviceRange
.
Implementations§
source§impl<'a> DeviceRange<'a>
impl<'a> DeviceRange<'a>
sourcepub fn split_at(&self, offset: usize) -> Option<(Self, Self)>
pub fn split_at(&self, offset: usize) -> Option<(Self, Self)>
Split the range at offset
producing two new ranges.
Returns None
if offset
is not in the range, otherwise produces the two ranges
[start.. start + offset)
and [start + offset ..end)
.
pub fn len(&self) -> usize
sourcepub unsafe fn new(range: Range<usize>) -> Self
pub unsafe fn new(range: Range<usize>) -> Self
Construct a new DeviceRange
.
§Safety
The provided range must be:
- Valid memory that can be read or written to if it were cast to a pointer.
- Not alias any Rust objects from the heap, stack, globals etc.
- Remain valid for the lifetime
'a
.
sourcepub fn try_mut_ptr<T>(&self) -> Option<*mut T>
pub fn try_mut_ptr<T>(&self) -> Option<*mut T>
Attempt to get a pointer to a mutable T
at the start of the range.
Returns a None
if the range is too small to represent a T
, or if the start of the range
has the wrong alignment. Although there ways to safely perform accesses to unaligned
pointers, as virtio requires all objects to be placed with correct alignment any
misalignment represents a configuration issue.
The caller may assume that if a pointer is returned that it is valid for reads and writes of
an object of size and alignment of T, however no guarantee is made on T being a copy-able
object that can be safely read or written. Further, the returned pointer is valid only as
long as the underlying DeviceRange
is alive.
Trait Implementations§
source§impl<'a> Clone for DeviceRange<'a>
impl<'a> Clone for DeviceRange<'a>
source§fn clone(&self) -> DeviceRange<'a>
fn clone(&self) -> DeviceRange<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for DeviceRange<'a>
impl<'a> Debug for DeviceRange<'a>
source§impl<'a> PartialEq for DeviceRange<'a>
impl<'a> PartialEq for DeviceRange<'a>
source§fn eq(&self, other: &DeviceRange<'a>) -> bool
fn eq(&self, other: &DeviceRange<'a>) -> bool
self
and other
values to be equal, and is used
by ==
.impl<'a> Eq for DeviceRange<'a>
impl<'a> StructuralPartialEq for DeviceRange<'a>
Auto Trait Implementations§
impl<'a> Freeze for DeviceRange<'a>
impl<'a> RefUnwindSafe for DeviceRange<'a>
impl<'a> Send for DeviceRange<'a>
impl<'a> Sync for DeviceRange<'a>
impl<'a> Unpin for DeviceRange<'a>
impl<'a> UnwindSafe for DeviceRange<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)