1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use crate::error::{KernelError, UserspaceError};
use failure::Backtrace;
pub fn check_size_aligned(size: usize, alignment: usize) -> Result<(), KernelError> {
match size % alignment {
0 => Ok(()),
_ => Err(KernelError::InvalidSize { size, backtrace: Backtrace::new() })
}
}
pub fn check_nonzero_length(length: usize) -> Result<(), KernelError> {
if length == 0 {
Err(KernelError::InvalidSize { size: 0, backtrace: Backtrace::new() })
} else {
Ok(())
}
}
pub fn check_lower_than_usize(val: u64, err: UserspaceError) -> Result<(), UserspaceError> {
if (usize::max_value() as u64) < val {
Err(err)
} else {
Ok(())
}
}