Skip to content

Commit 4e54cd1

Browse files
committed
rust: error: Introduce ptr_to_result
Some of the kernel functions use a part of the pointer value range to carry the error information, introduce `ptr_to_result` to decipher. Signed-off-by: Boqun Feng <[email protected]>
1 parent 7082b20 commit 4e54cd1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

rust/kernel/error.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,21 @@ impl From<AllocError> for Error {
105105
Error::ENOMEM
106106
}
107107
}
108+
109+
/// Convert a kernel pointer to [`KernelResult`]
110+
///
111+
/// # Pointer value range
112+
///
113+
/// (According to include/linux/err.h)
114+
/// [0, .., `core::usize::MAX - bindings::MAX_ERRNO`) is the range for normal values of pointer,
115+
/// [`core::unsize::MAX - bindings::MAX_ERRNO`,..,`core::usize::MAX] is the range for error value
116+
/// stored in pointer.
117+
pub fn ptr_to_result<T>(ptr: *mut T) -> KernelResult<*mut T> {
118+
let value = ptr as usize;
119+
120+
if value >= core::usize::MAX - bindings::MAX_ERRNO as usize {
121+
Err(Error::from_kernel_errno(value as c_types::c_int))
122+
} else {
123+
Ok(ptr)
124+
}
125+
}

0 commit comments

Comments
 (0)