2022-01-13 19:27:10 +01:00
|
|
|
use core::mem;
|
|
|
|
|
2021-05-11 03:04:59 +02:00
|
|
|
const SRAM_LOWER: usize = 0x2000_0000;
|
|
|
|
const SRAM_UPPER: usize = 0x3000_0000;
|
|
|
|
|
2022-01-13 19:27:10 +01:00
|
|
|
// TODO: replace transmutes with core::ptr::metadata once it's stable
|
2022-01-19 15:59:06 +01:00
|
|
|
pub(crate) fn slice_ptr_parts<T>(slice: *const [T]) -> (*const T, usize) {
|
2022-01-13 19:27:10 +01:00
|
|
|
unsafe { mem::transmute(slice) }
|
|
|
|
}
|
|
|
|
|
2022-01-19 15:59:06 +01:00
|
|
|
pub(crate) fn slice_ptr_parts_mut<T>(slice: *mut [T]) -> (*mut T, usize) {
|
2022-01-13 19:27:10 +01:00
|
|
|
unsafe { mem::transmute(slice) }
|
|
|
|
}
|
|
|
|
|
2021-05-11 03:04:59 +02:00
|
|
|
/// Does this slice reside entirely within RAM?
|
2022-01-13 19:27:10 +01:00
|
|
|
pub(crate) fn slice_in_ram<T>(slice: *const [T]) -> bool {
|
|
|
|
let (ptr, len) = slice_ptr_parts(slice);
|
2022-01-19 15:59:06 +01:00
|
|
|
let ptr = ptr as usize;
|
2022-01-13 19:27:10 +01:00
|
|
|
ptr >= SRAM_LOWER && (ptr + len * core::mem::size_of::<T>()) < SRAM_UPPER
|
2021-05-11 03:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:51:01 +01:00
|
|
|
/// Return an error if slice is not in RAM. Skips check if slice is zero-length.
|
2021-10-11 01:09:57 +02:00
|
|
|
#[cfg(not(feature = "nrf51"))]
|
2022-01-13 19:27:10 +01:00
|
|
|
pub(crate) fn slice_in_ram_or<T, E>(slice: *const [T], err: E) -> Result<(), E> {
|
2022-02-23 23:30:50 +01:00
|
|
|
let (_, len) = slice_ptr_parts(slice);
|
2022-03-08 16:29:42 +01:00
|
|
|
if len == 0 || slice_in_ram(slice) {
|
2021-05-11 03:04:59 +02:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|