1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 16:36:18 +04:00
rCore/src/util.rs

21 lines
514 B
Rust
Raw Normal View History

2018-04-05 18:36:39 +04:00
pub fn bytes_sum<T>(p: &T) -> u8 {
use core::mem::size_of_val;
let len = size_of_val(p);
let p = p as *const T as *const u8;
(0..len).map(|i| unsafe { &*p.offset(i as isize) })
.fold(0, |a, &b| a.overflowing_add(b).0)
}
///
pub trait Checkable {
fn check(&self) -> bool;
}
/// Scan memory to find the struct
pub unsafe fn find_in_memory<T: Checkable>
(begin: usize, len: usize, step: usize) -> Option<usize> {
(begin .. begin + len).step_by(step)
.find(|&addr| { (&*(addr as *const T)).check() })
}