Skip to content

Commit e5e5dd5

Browse files
authored
Use utils::{as_ptr, as_ptr_mut} to reduce as casts. (#654)
This reduces `as` casts, and is generally more concise.
1 parent d989d45 commit e5e5dd5

File tree

6 files changed

+16
-29
lines changed

6 files changed

+16
-29
lines changed

src/backend/libc/io/syscalls.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use libc_errno::errno;
3434
use {
3535
super::super::conv::syscall_ret_owned_fd,
3636
crate::io::{IoSliceRaw, ReadWriteFlags, SpliceFlags},
37-
core::ptr,
37+
crate::utils::optional_as_mut_ptr,
3838
};
3939
#[cfg(bsd)]
4040
use {crate::io::kqueue::Event, crate::utils::as_ptr, core::ptr::null};
@@ -586,13 +586,8 @@ pub fn splice(
586586
len: usize,
587587
flags: SpliceFlags,
588588
) -> io::Result<usize> {
589-
let off_in = off_in
590-
.map(|off| (off as *mut u64).cast())
591-
.unwrap_or(ptr::null_mut());
592-
593-
let off_out = off_out
594-
.map(|off| (off as *mut u64).cast())
595-
.unwrap_or(ptr::null_mut());
589+
let off_in = optional_as_mut_ptr(off_in).cast();
590+
let off_out = optional_as_mut_ptr(off_out).cast();
596591

597592
unsafe {
598593
ret_usize(c::splice(

src/backend/linux_raw/io/syscalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub(crate) fn ext4_ioc_resize_fs(fd: BorrowedFd<'_>, blocks: u64) -> io::Result<
366366
__NR_ioctl,
367367
fd,
368368
c_uint(EXT4_IOC_RESIZE_FS),
369-
&blocks as *const u64
369+
by_ref(&blocks)
370370
))
371371
}
372372
}

src/backend/linux_raw/process/syscalls.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::process::{
2222
Resource, Rlimit, Signal, Sysinfo, Uid, WaitId, WaitOptions, WaitStatus, WaitidOptions,
2323
WaitidStatus,
2424
};
25+
use crate::utils::as_mut_ptr;
2526
use core::convert::TryInto;
2627
use core::mem::MaybeUninit;
2728
use core::num::NonZeroU32;
@@ -224,7 +225,7 @@ pub(crate) fn sched_getaffinity(pid: Option<Pid>, cpuset: &mut RawCpuSet) -> io:
224225
size_of::<RawCpuSet, _>(),
225226
by_mut(&mut cpuset.bits)
226227
))?;
227-
let bytes = (cpuset as *mut RawCpuSet).cast::<u8>();
228+
let bytes = as_mut_ptr(cpuset).cast::<u8>();
228229
let rest = bytes.wrapping_add(size);
229230
// Zero every byte in the cpuset not set by the kernel.
230231
rest.write_bytes(0, core::mem::size_of::<RawCpuSet>() - size);

src/process/prctl.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::fd::{AsRawFd, BorrowedFd};
1818
use crate::ffi::CStr;
1919
use crate::io;
2020
use crate::process::{Pid, RawPid};
21+
use crate::utils::{as_mut_ptr, as_ptr};
2122

2223
//
2324
// Helper functions.
@@ -58,7 +59,7 @@ where
5859
T: TryFrom<P, Error = io::Errno>,
5960
{
6061
let mut value: P = Default::default();
61-
prctl_2args(option, ((&mut value) as *mut P).cast())?;
62+
prctl_2args(option, as_mut_ptr(&mut value).cast())?;
6263
TryFrom::try_from(value)
6364
}
6465

@@ -702,7 +703,7 @@ pub unsafe fn set_auxiliary_vector(auxv: &[*const c_void]) -> io::Result<()> {
702703
#[inline]
703704
pub fn virtual_memory_map_config_struct_size() -> io::Result<usize> {
704705
let mut value: c_uint = 0;
705-
let value_ptr = (&mut value) as *mut c_uint;
706+
let value_ptr = as_mut_ptr(&mut value);
706707
unsafe { prctl_3args(PR_SET_MM, PR_SET_MM_MAP_SIZE as *mut _, value_ptr.cast())? };
707708
Ok(value as usize)
708709
}
@@ -761,7 +762,7 @@ pub unsafe fn configure_virtual_memory_map(config: &PrctlMmMap) -> io::Result<()
761762
syscalls::prctl(
762763
PR_SET_MM,
763764
PR_SET_MM_MAP as *mut _,
764-
config as *const PrctlMmMap as *mut _,
765+
as_ptr(config) as *mut _,
765766
size_of::<PrctlMmMap>() as *mut _,
766767
null_mut(),
767768
)

src/process/procctl.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::backend::process::syscalls;
1717
use crate::backend::process::types::{RawId, Signal};
1818
use crate::io;
1919
use crate::process::{Pid, RawPid};
20+
use crate::utils::{as_mut_ptr, as_ptr};
2021

2122
//
2223
// Helper functions.
@@ -61,7 +62,7 @@ pub(crate) unsafe fn procctl_set<P>(
6162
process: ProcSelector,
6263
data: &P,
6364
) -> io::Result<()> {
64-
procctl(option, process, (data as *const P as *mut P).cast())
65+
procctl(option, process, (as_ptr(data) as *mut P).cast())
6566
}
6667

6768
#[inline]
@@ -338,13 +339,7 @@ pub fn get_reaper_pids(process: ProcSelector) -> io::Result<Vec<PidInfo>> {
338339
rp_pad0: [0; 15],
339340
rp_pids: pids.as_mut_slice().as_mut_ptr(),
340341
};
341-
unsafe {
342-
procctl(
343-
PROC_REAP_GETPIDS,
344-
process,
345-
(&mut pinfo as *mut procctl_reaper_pids).cast(),
346-
)?
347-
};
342+
unsafe { procctl(PROC_REAP_GETPIDS, process, as_mut_ptr(&mut pinfo).cast())? };
348343
let mut result = Vec::new();
349344
for raw in pids.into_iter() {
350345
let flags = PidInfoFlags::from_bits_truncate(raw.pi_flags);
@@ -412,13 +407,7 @@ pub fn reaper_kill(
412407
rk_fpid: 0,
413408
rk_pad0: [0; 15],
414409
};
415-
unsafe {
416-
procctl(
417-
PROC_REAP_KILL,
418-
process,
419-
(&mut req as *mut procctl_reaper_kill).cast(),
420-
)?
421-
};
410+
unsafe { procctl(PROC_REAP_KILL, process, as_mut_ptr(&mut req).cast())? };
422411
Ok(KillResult {
423412
killed: req.rk_killed as _,
424413
first_failed: if req.rk_fpid == -1 {

src/thread/prctl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::process::{
2626
prctl_1arg, prctl_2args, prctl_3args, prctl_get_at_arg2_optional, Pid,
2727
PointerAuthenticationKeys,
2828
};
29+
use crate::utils::as_ptr;
2930

3031
//
3132
// PR_GET_KEEPCAPS/PR_SET_KEEPCAPS
@@ -854,7 +855,7 @@ pub unsafe fn enable_syscall_user_dispatch(
854855
PR_SYS_DISPATCH_ON as *mut _,
855856
always_allowed_region.as_ptr() as *mut _,
856857
always_allowed_region.len() as *mut _,
857-
fast_switch_flag as *const AtomicU8 as *mut _,
858+
as_ptr(fast_switch_flag) as *mut _,
858859
)
859860
.map(|_r| ())
860861
}

0 commit comments

Comments
 (0)