arceos_posix_api/
utils.rs1#![allow(dead_code)]
2#![allow(unused_macros)]
3
4use axerrno::{LinuxError, LinuxResult};
5use core::ffi::{CStr, c_char};
6
7pub fn char_ptr_to_str<'a>(str: *const c_char) -> LinuxResult<&'a str> {
9 if str.is_null() {
10 Err(LinuxError::EFAULT)
11 } else {
12 let str = str as *const _;
13 unsafe { CStr::from_ptr(str) }
14 .to_str()
15 .map_err(|_| LinuxError::EINVAL)
16 }
17}
18
19pub fn check_null_ptr<T>(ptr: *const T) -> LinuxResult {
20 if ptr.is_null() {
21 Err(LinuxError::EFAULT)
22 } else {
23 Ok(())
24 }
25}
26
27pub fn check_null_mut_ptr<T>(ptr: *mut T) -> LinuxResult {
28 if ptr.is_null() {
29 Err(LinuxError::EFAULT)
30 } else {
31 Ok(())
32 }
33}
34
35macro_rules! syscall_body {
36 ($fn: ident, $($stmt: tt)*) => {{
37 #[allow(clippy::redundant_closure_call)]
38 let res = (|| -> axerrno::LinuxResult<_> { $($stmt)* })();
39 match res {
40 Ok(_) | Err(axerrno::LinuxError::EAGAIN) => debug!(concat!(stringify!($fn), " => {:?}"), res),
41 Err(_) => info!(concat!(stringify!($fn), " => {:?}"), res),
42 }
43 match res {
44 Ok(v) => v as _,
45 Err(e) => {
46 -e.code() as _
47 }
48 }
49 }};
50}
51
52macro_rules! syscall_body_no_debug {
53 ($($stmt: tt)*) => {{
54 #[allow(clippy::redundant_closure_call)]
55 let res = (|| -> axerrno::LinuxResult<_> { $($stmt)* })();
56 match res {
57 Ok(v) => v as _,
58 Err(e) => {
59 -e.code() as _
60 }
61 }
62 }};
63}