Struct vm_memory::volatile_memory::VolatileArrayRef
source · pub struct VolatileArrayRef<'a, T, B = ()> { /* private fields */ }
Expand description
A memory location that supports volatile access to an array of elements of type T
.
§Examples
let mut v = [5u32; 1];
let v_ref = unsafe { VolatileArrayRef::new(&mut v[0] as *mut u32 as *mut u8, v.len()) };
assert_eq!(v[0], 5);
assert_eq!(v_ref.load(0), 5);
v_ref.store(0, 500);
assert_eq!(v[0], 500);
Implementations§
source§impl<'a, T> VolatileArrayRef<'a, T>where
T: ByteValued,
impl<'a, T> VolatileArrayRef<'a, T>where
T: ByteValued,
sourcepub unsafe fn new(addr: *mut u8, nelem: usize) -> Self
pub unsafe fn new(addr: *mut u8, nelem: usize) -> Self
Creates a VolatileArrayRef
to an array of elements of
type T
.
§Safety
To use this safely, the caller must guarantee that the memory at addr
is big enough for
nelem
values of type T
and is available for the duration of the lifetime of the new
VolatileRef
. The caller must also guarantee that all other users of the given chunk of
memory are using volatile accesses.
source§impl<'a, T, B> VolatileArrayRef<'a, T, B>where
T: ByteValued,
B: BitmapSlice,
impl<'a, T, B> VolatileArrayRef<'a, T, B>where
T: ByteValued,
B: BitmapSlice,
sourcepub unsafe fn with_bitmap(addr: *mut u8, nelem: usize, bitmap: B) -> Self
pub unsafe fn with_bitmap(addr: *mut u8, nelem: usize, bitmap: B) -> Self
Creates a VolatileArrayRef
to an array of elements of
type T
, using the provided bitmap
object for dirty page tracking.
§Safety
To use this safely, the caller must guarantee that the memory at addr
is big enough for
nelem
values of type T
and is available for the duration of the lifetime of the new
VolatileRef
. The caller must also guarantee that all other users of the given chunk of
memory are using volatile accesses.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if this array is empty.
§Examples
let v_array = unsafe { VolatileArrayRef::<u32>::new(0 as *mut _, 0) };
assert!(v_array.is_empty());
sourcepub fn element_size(&self) -> usize
pub fn element_size(&self) -> usize
Returns the size of T
.
§Examples
let v_ref = unsafe { VolatileArrayRef::<u32>::new(0 as *mut _, 0) };
assert_eq!(v_ref.element_size(), size_of::<u32>() as usize);
sourcepub fn as_ptr(&self) -> *mut u8
pub fn as_ptr(&self) -> *mut u8
Returns a pointer to the underlying memory. Mutable accesses performed using the resulting pointer are not automatically accounted for by the dirty bitmap tracking functionality.
sourcepub fn to_slice(&self) -> VolatileSlice<'a, B>
pub fn to_slice(&self) -> VolatileSlice<'a, B>
Converts this to a VolatileSlice
with the same size and address.
sourcepub fn ref_at(&self, index: usize) -> VolatileRef<'a, T, B>
pub fn ref_at(&self, index: usize) -> VolatileRef<'a, T, B>
Does a volatile read of the element at index
.
§Panics
Panics if index
is less than the number of elements of the array to which &self
points.
sourcepub fn copy_to(&self, buf: &mut [T]) -> usize
pub fn copy_to(&self, buf: &mut [T]) -> usize
Copies as many elements of type T
as possible from this array to buf
.
Copies self.len()
or buf.len()
times the size of T
bytes, whichever is smaller,
to buf
. The copy happens from smallest to largest address in T
sized chunks
using volatile reads.
§Examples
let mut v = [0u8; 32];
let v_ref = unsafe { VolatileArrayRef::new(&mut v[0] as *mut u8, v.len()) };
let mut buf = [5u8; 16];
v_ref.copy_to(&mut buf[..]);
for &v in &buf[..] {
assert_eq!(v, 0);
}
sourcepub fn copy_to_volatile_slice<S: BitmapSlice>(
&self,
slice: VolatileSlice<'_, S>,
)
pub fn copy_to_volatile_slice<S: BitmapSlice>( &self, slice: VolatileSlice<'_, S>, )
Copies as many bytes as possible from this slice to the provided slice
.
The copies happen in an undefined order.
§Examples
let mut v = [0u8; 32];
let v_ref = unsafe { VolatileArrayRef::<u8>::new(&mut v[0] as *mut u8, v.len()) };
let mut buf = [5u8; 16];
let v_ref2 = unsafe { VolatileArrayRef::<u8>::new(&mut buf[0] as *mut u8, buf.len()) };
v_ref.copy_to_volatile_slice(v_ref2.to_slice());
for &v in &buf[..] {
assert_eq!(v, 0);
}
sourcepub fn copy_from(&self, buf: &[T])
pub fn copy_from(&self, buf: &[T])
Copies as many elements of type T
as possible from buf
to this slice.
Copies self.len()
or buf.len()
times the size of T
bytes, whichever is smaller,
to this slice’s memory. The copy happens from smallest to largest address in
T
sized chunks using volatile writes.
§Examples
let mut v = [0u8; 32];
let v_ref = unsafe { VolatileArrayRef::<u8>::new(&mut v[0] as *mut u8, v.len()) };
let buf = [5u8; 64];
v_ref.copy_from(&buf[..]);
for &val in &v[..] {
assert_eq!(5u8, val);
}
Trait Implementations§
source§impl<'a, T: Clone, B: Clone> Clone for VolatileArrayRef<'a, T, B>
impl<'a, T: Clone, B: Clone> Clone for VolatileArrayRef<'a, T, B>
source§fn clone(&self) -> VolatileArrayRef<'a, T, B>
fn clone(&self) -> VolatileArrayRef<'a, T, B>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a, B: BitmapSlice> From<VolatileSlice<'a, B>> for VolatileArrayRef<'a, u8, B>
impl<'a, B: BitmapSlice> From<VolatileSlice<'a, B>> for VolatileArrayRef<'a, u8, B>
source§fn from(slice: VolatileSlice<'a, B>) -> Self
fn from(slice: VolatileSlice<'a, B>) -> Self
impl<'a, T: Copy, B: Copy> Copy for VolatileArrayRef<'a, T, B>
Auto Trait Implementations§
impl<'a, T, B> Freeze for VolatileArrayRef<'a, T, B>where
B: Freeze,
impl<'a, T, B> RefUnwindSafe for VolatileArrayRef<'a, T, B>where
B: RefUnwindSafe,
T: RefUnwindSafe,
impl<'a, T, B = ()> !Send for VolatileArrayRef<'a, T, B>
impl<'a, T, B = ()> !Sync for VolatileArrayRef<'a, T, B>
impl<'a, T, B> Unpin for VolatileArrayRef<'a, T, B>where
B: Unpin,
impl<'a, T, B> UnwindSafe for VolatileArrayRef<'a, T, B>where
B: UnwindSafe,
T: RefUnwindSafe,
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
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)