mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
xous: add as_slice() and as_mut_slice() to MemoryRange
Add functions to obtain various slices from a `MemoryRange`. These functions are safe because we know that the memory in the range is valid, and we know the size of the type. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
977a175bb5
commit
023918082b
1 changed files with 34 additions and 0 deletions
|
|
@ -637,6 +637,40 @@ impl MemoryRange {
|
|||
pub fn as_mut_ptr(&self) -> *mut u8 {
|
||||
self.addr.get() as *mut u8
|
||||
}
|
||||
|
||||
/// Return this memory as a slice of values. The resulting slice
|
||||
/// will cover the maximum number of elements given the size of `T`.
|
||||
/// For example, if the allocation is 4096 bytes, then the resulting
|
||||
/// `&[u8]` would have 4096 elements, `&[u16]` would have 2048, and
|
||||
/// `&[u32]` would have 1024. Values are rounded down.
|
||||
pub fn as_slice<T>(&self) -> &[T] {
|
||||
// This is safe because the pointer and length are guaranteed to
|
||||
// be valid, as long as the user hasn't already called `as_ptr()`
|
||||
// and done something unsound with the resulting pointer.
|
||||
unsafe {
|
||||
core::slice::from_raw_parts(
|
||||
self.as_ptr() as *const T,
|
||||
self.len() / core::mem::size_of::<T>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return this memory as a slice of mutable values. The resulting slice
|
||||
/// will cover the maximum number of elements given the size of `T`.
|
||||
/// For example, if the allocation is 4096 bytes, then the resulting
|
||||
/// `&[u8]` would have 4096 elements, `&[u16]` would have 2048, and
|
||||
/// `&[u32]` would have 1024. Values are rounded down.
|
||||
pub fn as_slice_mut<T>(&self) -> &mut [T] {
|
||||
// This is safe because the pointer and length are guaranteed to
|
||||
// be valid, as long as the user hasn't already called `as_ptr()`
|
||||
// and done something unsound with the resulting pointer.
|
||||
unsafe {
|
||||
core::slice::from_raw_parts_mut(
|
||||
self.as_mut_ptr() as *mut T,
|
||||
self.len() / core::mem::size_of::<T>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Which memory region the operation should affect.
|
||||
|
|
|
|||
Loading…
Reference in a new issue