diff --git a/src/calls/clock_adjtime.rs b/src/calls/clock_adjtime.rs index a054d936..e04ef26e 100644 --- a/src/calls/clock_adjtime.rs +++ b/src/calls/clock_adjtime.rs @@ -1,4 +1,6 @@ -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// diff --git a/src/calls/clock_getres.rs b/src/calls/clock_getres.rs index 742932ef..543ef51e 100644 --- a/src/calls/clock_getres.rs +++ b/src/calls/clock_getres.rs @@ -4,12 +4,17 @@ /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } diff --git a/src/calls/madvise.rs b/src/calls/madvise.rs index 255afa1d..611202f0 100644 --- a/src/calls/madvise.rs +++ b/src/calls/madvise.rs @@ -5,7 +5,7 @@ /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -15,11 +15,11 @@ /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-aarch64/call.rs b/src/platform/linux-aarch64/call.rs index 45495a53..12ccd198 100644 --- a/src/platform/linux-aarch64/call.rs +++ b/src/platform/linux-aarch64/call.rs @@ -480,7 +480,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -502,13 +504,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -3695,7 +3702,7 @@ pub unsafe fn lsetxattr>( /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -3705,11 +3712,11 @@ pub unsafe fn lsetxattr>( /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-arm/call.rs b/src/platform/linux-arm/call.rs index 133c448f..6573b640 100644 --- a/src/platform/linux-arm/call.rs +++ b/src/platform/linux-arm/call.rs @@ -566,7 +566,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -588,13 +590,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -4229,7 +4236,7 @@ pub unsafe fn lstat64>(filename: P, statbuf: &mut stat64_t) -> Re /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -4239,11 +4246,11 @@ pub unsafe fn lstat64>(filename: P, statbuf: &mut stat64_t) -> Re /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-loongarch64/call.rs b/src/platform/linux-loongarch64/call.rs index a25b5c8d..6d5ce0a1 100644 --- a/src/platform/linux-loongarch64/call.rs +++ b/src/platform/linux-loongarch64/call.rs @@ -437,7 +437,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -459,13 +461,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -3243,7 +3250,7 @@ pub unsafe fn lsetxattr>( /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -3253,11 +3260,11 @@ pub unsafe fn lsetxattr>( /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-mips/call.rs b/src/platform/linux-mips/call.rs index 8fc8badf..318a657c 100644 --- a/src/platform/linux-mips/call.rs +++ b/src/platform/linux-mips/call.rs @@ -605,7 +605,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -627,13 +629,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -4335,7 +4342,7 @@ pub unsafe fn lstat64>(filename: P, statbuf: &mut stat64_t) -> Re /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -4345,11 +4352,11 @@ pub unsafe fn lstat64>(filename: P, statbuf: &mut stat64_t) -> Re /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-mips64/call.rs b/src/platform/linux-mips64/call.rs index bcc37a40..fb75f28b 100644 --- a/src/platform/linux-mips64/call.rs +++ b/src/platform/linux-mips64/call.rs @@ -589,7 +589,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -611,13 +613,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -4129,7 +4136,7 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -4139,11 +4146,11 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-ppc64/call.rs b/src/platform/linux-ppc64/call.rs index 673bef77..018efee9 100644 --- a/src/platform/linux-ppc64/call.rs +++ b/src/platform/linux-ppc64/call.rs @@ -598,7 +598,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -620,13 +622,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -4237,7 +4244,7 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -4247,11 +4254,11 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-riscv64/call.rs b/src/platform/linux-riscv64/call.rs index 2c936d28..6b602053 100644 --- a/src/platform/linux-riscv64/call.rs +++ b/src/platform/linux-riscv64/call.rs @@ -480,7 +480,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -502,13 +504,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -3695,7 +3702,7 @@ pub unsafe fn lsetxattr>( /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -3705,11 +3712,11 @@ pub unsafe fn lsetxattr>( /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-s390x/call.rs b/src/platform/linux-s390x/call.rs index 8fe447d2..b33a039b 100644 --- a/src/platform/linux-s390x/call.rs +++ b/src/platform/linux-s390x/call.rs @@ -494,7 +494,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -516,13 +518,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -4113,7 +4120,7 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -4123,11 +4130,11 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-x86/call.rs b/src/platform/linux-x86/call.rs index 2fc86697..4dee6fd4 100644 --- a/src/platform/linux-x86/call.rs +++ b/src/platform/linux-x86/call.rs @@ -504,7 +504,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -526,13 +528,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -4264,7 +4271,7 @@ pub unsafe fn lstat64>(filename: P, statbuf: &mut stat64_t) -> Re /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -4274,11 +4281,11 @@ pub unsafe fn lstat64>(filename: P, statbuf: &mut stat64_t) -> Re /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) }; diff --git a/src/platform/linux-x86_64/call.rs b/src/platform/linux-x86_64/call.rs index d3645fd3..184ee7d9 100644 --- a/src/platform/linux-x86_64/call.rs +++ b/src/platform/linux-x86_64/call.rs @@ -588,7 +588,9 @@ pub unsafe fn chroot>(filename: P) -> Result<(), Errno> { syscall1(SYS_CHROOT, filename_ptr).map(drop) } -/// Tune kernel clock. Returns clock state on success. +/// Tune kernel clock. +/// +/// Returns clock state on success. /// /// # Examples /// @@ -610,13 +612,18 @@ pub unsafe fn clock_adjtime(which_clock: clockid_t, tx: &mut timex_t) -> Result< /// /// ``` /// let mut tp = nc::timespec_t::default(); -/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, &mut tp) }; +/// let ret = unsafe { nc::clock_getres(nc::CLOCK_BOOTTIME, Some(&mut tp)) }; /// assert!(ret.is_ok()); /// assert!(tp.tv_nsec > 0); /// ``` -pub unsafe fn clock_getres(which_clock: clockid_t, tp: &mut timespec_t) -> Result<(), Errno> { +pub unsafe fn clock_getres( + which_clock: clockid_t, + tp: Option<&mut timespec_t>, +) -> Result<(), Errno> { let which_clock = which_clock as usize; - let tp_ptr = tp as *mut timespec_t as usize; + let tp_ptr = tp.map_or(core::ptr::null_mut::() as usize, |tp| { + tp as *mut timespec_t as usize + }); syscall2(SYS_CLOCK_GETRES, which_clock, tp_ptr).map(drop) } @@ -4177,7 +4184,7 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// ``` /// // Initialize an anonymous mapping with 4 pages. /// let map_length = 4 * nc::PAGE_SIZE; -/// let addr = unsafe { +/// let ret = unsafe { /// nc::mmap( /// 0, /// map_length, @@ -4187,11 +4194,11 @@ pub unsafe fn lstat>(filename: P, statbuf: &mut stat_t) -> Result /// 0, /// ) /// }; -/// assert!(addr.is_ok()); -/// let addr = addr.unwrap(); +/// assert!(ret.is_ok()); +/// let addr = ret.unwrap(); /// -/// // Set the third page readonly. And we will run into SIGSEGV when updating it. -/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) }; +/// // Notify kernel that the third page will be accessed. +/// let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_WILLNEED) }; /// assert!(ret.is_ok()); /// /// let ret = unsafe { nc::munmap(addr, map_length) };