Skip to content

Commit

Permalink
Miscellaneous cleanups.
Browse files Browse the repository at this point in the history
Factor out redundant code, make some functions `const fn`, and add some
documentation comments.
  • Loading branch information
sunfishcode committed Jun 29, 2023
1 parent 739e84e commit 8a1b751
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 88 deletions.
76 changes: 22 additions & 54 deletions src/backend/libc/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,26 +187,28 @@ pub(super) unsafe fn prlimit(
prlimit64(pid, resource, new_limit, old_limit)
}

// 64-bit offsets on 32-bit platforms are passed in endianness-specific
// lo/hi pairs. See src/backend/linux_raw/conv.rs for details.
#[cfg(all(linux_kernel, target_endian = "little", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
(x >> 32) as usize
}
#[cfg(all(linux_kernel, target_endian = "little", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
x as usize
}
#[cfg(all(linux_kernel, target_endian = "big", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
x as usize
}
#[cfg(all(linux_kernel, target_endian = "big", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
(x >> 32) as usize
}

#[cfg(target_os = "android")]
mod readwrite_pv64 {
// 64-bit offsets on 32-bit platforms are passed in endianness-specific
// lo/hi pairs. See src/backend/linux_raw/conv.rs for details.
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
(x >> 32) as usize
}
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
x as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
x as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
(x >> 32) as usize
}
use super::*;

pub(in super::super) unsafe fn preadv64(
fd: libc::c_int,
Expand Down Expand Up @@ -320,24 +322,7 @@ pub(super) use readwrite_pv::{preadv, pwritev};
// glibc added `preadv64v2` and `pwritev64v2` in version 2.26.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
mod readwrite_pv64v2 {
// 64-bit offsets on 32-bit platforms are passed in endianness-specific
// lo/hi pairs. See src/backend/linux_raw/conv.rs for details.
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
(x >> 32) as usize
}
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
x as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
x as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
(x >> 32) as usize
}
use super::*;

pub(in super::super) unsafe fn preadv64v2(
fd: libc::c_int,
Expand Down Expand Up @@ -439,24 +424,7 @@ pub(super) use readwrite_pv64v2::{preadv64v2 as preadv2, pwritev64v2 as pwritev2
all(target_os = "linux", not(target_env = "gnu")),
))]
mod readwrite_pv64v2 {
// 64-bit offsets on 32-bit platforms are passed in endianness-specific
// lo/hi pairs. See src/backend/linux_raw/conv.rs for details.
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
(x >> 32) as usize
}
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
x as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn lo(x: i64) -> usize {
x as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn hi(x: i64) -> usize {
(x >> 32) as usize
}
use super::*;

pub(in super::super) unsafe fn preadv64v2(
fd: libc::c_int,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/libc/event/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ pub union EventData {
impl EventData {
/// Construct a new value containing a `u64`.
#[inline]
pub fn new_u64(value: u64) -> Self {
pub const fn new_u64(value: u64) -> Self {
Self { as_u64: value }
}

/// Construct a new value containing a `*mut c_void`.
#[inline]
pub fn new_ptr(value: *mut c_void) -> Self {
pub const fn new_ptr(value: *mut c_void) -> Self {
Self {
sixty_four_bit_pointer: SixtyFourBitPointer {
pointer: value,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/libc/mm/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub(crate) unsafe fn munlock(addr: *mut c::c_void, length: usize) -> io::Result<
pub(crate) unsafe fn userfaultfd(flags: UserfaultfdFlags) -> io::Result<OwnedFd> {
syscall! {
fn userfaultfd(
flags: libc::c_int
flags: c::c_int
) via SYS_userfaultfd -> c::c_int
}
ret_owned_fd(userfaultfd(bitflags_bits!(flags)))
Expand Down
7 changes: 3 additions & 4 deletions src/backend/libc/pty/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ pub(crate) fn ptsname(fd: BorrowedFd, mut buffer: Vec<u8>) -> io::Result<CString
loop {
// On platforms with `ptsname_r`, use it.
#[cfg(any(target_os = "freebsd", linux_like, target_os = "fuchsia"))]
let r =
unsafe { libc::ptsname_r(borrowed_fd(fd), buffer.as_mut_ptr().cast(), buffer.len()) };
let r = unsafe { c::ptsname_r(borrowed_fd(fd), buffer.as_mut_ptr().cast(), buffer.len()) };

// MacOS 10.13.4 has `ptsname_r`; use it if we have it, otherwise fall
// back to calling the underlying ioctl directly.
Expand All @@ -46,7 +45,7 @@ pub(crate) fn ptsname(fd: BorrowedFd, mut buffer: Vec<u8>) -> io::Result<CString
} else {
// The size declared in the `TIOCPTYGNAME` macro in sys/ttycom.h is 128.
let mut name: [u8; 128] = [0_u8; 128];
match libc::ioctl(borrowed_fd(fd), libc::TIOCPTYGNAME as u64, &mut name) {
match c::ioctl(borrowed_fd(fd), c::TIOCPTYGNAME as u64, &mut name) {
0 => {
let len = CStr::from_ptr(name.as_ptr().cast()).to_bytes().len();
std::ptr::copy_nonoverlapping(name.as_ptr(), buffer.as_mut_ptr(), len + 1);
Expand All @@ -60,7 +59,7 @@ pub(crate) fn ptsname(fd: BorrowedFd, mut buffer: Vec<u8>) -> io::Result<CString
if r == 0 {
return Ok(unsafe { CStr::from_ptr(buffer.as_ptr().cast()).to_owned() });
}
if r != libc::ERANGE {
if r != c::ERANGE {
return Err(io::Errno::from_raw_os_error(r));
}

Expand Down
4 changes: 2 additions & 2 deletions src/backend/linux_raw/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ macro_rules! syscall {
};
}

// Macro to invoke a syscall that always uses direct assembly, rather than the vdso. Useful when
// still finding the vdso.
// Macro to invoke a syscall that always uses direct assembly, rather than the
// vDSO. Useful when still finding the vDSO.
#[allow(unused_macros)]
macro_rules! syscall_always_asm {
($nr:ident) => {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/linux_raw/event/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ pub union EventData {
impl EventData {
/// Construct a new value containing a `u64`.
#[inline]
pub fn new_u64(value: u64) -> Self {
pub const fn new_u64(value: u64) -> Self {
Self { as_u64: value }
}

/// Construct a new value containing a `*mut c_void`.
#[inline]
pub fn new_ptr(value: *mut c_void) -> Self {
pub const fn new_ptr(value: *mut c_void) -> Self {
Self {
sixty_four_bit_pointer: SixtyFourBitPointer {
pointer: value,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/linux_raw/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Dir {
.iter()
.position(|x| *x == b'\0')
.unwrap();
let name = CStr::from_bytes_with_nul(&self.buf[name_start..][..name_len + 1]).unwrap();
let name = CStr::from_bytes_with_nul(&self.buf[name_start..][..=name_len]).unwrap();
let name = name.to_owned();
assert!(name.as_bytes().len() <= self.buf.len() - name_start);

Expand Down
2 changes: 1 addition & 1 deletion src/backend/linux_raw/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ fn statfs_to_statvfs(statfs: StatFs) -> StatVfs {
f_files: statfs.f_files as u64,
f_ffree: statfs.f_ffree as u64,
f_favail: statfs.f_ffree as u64,
f_fsid: f_fsid_val0 as u32 as u64 | ((f_fsid_val1 as u32 as u64) << 32),
f_fsid: u64::from(f_fsid_val0 as u32) | u64::from(f_fsid_val1 as u32) << 32,
f_flag: StatVfsMountFlags::from_bits_retain(statfs.f_flags as u64),
f_namemax: statfs.f_namelen as u64,
}
Expand Down
13 changes: 3 additions & 10 deletions src/backend/linux_raw/param/auxv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::backend::elf::*;
use crate::fd::OwnedFd;
#[cfg(feature = "param")]
use crate::ffi::CStr;
#[cfg(not(target_vendor = "mustang"))]
use crate::fs::{Mode, OFlags};
use crate::utils::{as_ptr, check_raw_pointer};
use alloc::vec::Vec;
Expand Down Expand Up @@ -130,7 +129,6 @@ static PHDR: AtomicPtr<Elf_Phdr> = AtomicPtr::new(null_mut());
static PHNUM: AtomicUsize = AtomicUsize::new(0);
static EXECFN: AtomicPtr<c::c_char> = AtomicPtr::new(null_mut());

#[cfg(not(target_vendor = "mustang"))]
fn pr_get_auxv() -> crate::io::Result<Vec<u8>> {
use super::super::conv::{c_int, pass_usize, ret_usize};
const PR_GET_AUXV: c::c_int = 0x41555856;
Expand Down Expand Up @@ -160,9 +158,9 @@ fn pr_get_auxv() -> crate::io::Result<Vec<u8>> {
return Ok(buffer);
}

/// On non-Mustang platforms, we read the aux vector via the prctl PR_GET_AUXV, with a fallback to
/// /proc/self/auxv for kernels that don't support PR_GET_AUXV.
#[cfg(not(target_vendor = "mustang"))]
/// On non-Mustang platforms, we read the aux vector via the `prctl`
/// `PR_GET_AUXV`, with a fallback to /proc/self/auxv for kernels that don't
/// support `PR_GET_AUXV`.
fn init_auxv() {
match pr_get_auxv() {
Ok(buffer) => {
Expand All @@ -186,11 +184,6 @@ fn init_auxv() {
let _ = init_from_auxv_file(file);
}

#[cfg(target_vendor = "mustang")]
fn init_auxv() {
panic!("mustang should have initialized the auxv values");
}

/// Process auxv entries from the open file `auxv`.
fn init_from_auxv_file(auxv: OwnedFd) -> Option<()> {
let mut buffer = Vec::<u8>::with_capacity(512);
Expand Down
12 changes: 5 additions & 7 deletions src/event/kqueue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! An API for interfacing with `kqueue`.

use crate::fd::{AsFd, AsRawFd, OwnedFd, RawFd};
use crate::fd::{AsFd, OwnedFd, RawFd};
use crate::pid::Pid;
use crate::signal::Signal;
use crate::{backend, io};
Expand All @@ -26,13 +26,11 @@ impl Event {
#[allow(clippy::needless_update)]
pub fn new(filter: EventFilter, flags: EventFlags, udata: isize) -> Event {
let (ident, data, filter, fflags) = match filter {
EventFilter::Read(fd) => (fd.as_raw_fd() as uintptr_t, 0, c::EVFILT_READ, 0),
EventFilter::Write(fd) => (fd.as_raw_fd() as _, 0, c::EVFILT_WRITE, 0),
EventFilter::Read(fd) => (fd as uintptr_t, 0, c::EVFILT_READ, 0),
EventFilter::Write(fd) => (fd as _, 0, c::EVFILT_WRITE, 0),
#[cfg(target_os = "freebsd")]
EventFilter::Empty(fd) => (fd.as_raw_fd() as _, 0, c::EVFILT_EMPTY, 0),
EventFilter::Vnode { vnode, flags } => {
(vnode.as_raw_fd() as _, 0, c::EVFILT_VNODE, flags.bits())
}
EventFilter::Empty(fd) => (fd as _, 0, c::EVFILT_EMPTY, 0),
EventFilter::Vnode { vnode, flags } => (vnode as _, 0, c::EVFILT_VNODE, flags.bits()),
EventFilter::Proc { pid, flags } => {
(Pid::as_raw(Some(pid)) as _, 0, c::EVFILT_PROC, flags.bits())
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/socket_addr_any.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A socket address for any kind of socket.
//!
//! This is similar to [`std::net::SocketAddr`], but also supports Unix-domain
//! socket addresses.
//! socket addresses on Unix.
//!
//! # Safety
//!
Expand Down
3 changes: 2 additions & 1 deletion src/net/socketpair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::fd::OwnedFd;
use crate::net::{AddressFamily, Protocol, SocketFlags, SocketType};
use crate::{backend, io};

/// `socketpair(domain, type_ | accept_flags, protocol)`
/// `socketpair(domain, type_ | accept_flags, protocol)`—Create a pair of
/// sockets that are connected to each other.
///
/// # References
/// - [POSIX]
Expand Down
4 changes: 2 additions & 2 deletions src/termios/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ pub mod speed {
}
}

/// An array indexed by `SpecialCodeIndex` indicating the current values
/// An array indexed by [`SpecialCodeIndex`] indicating the current values
/// of various special control codes.
#[repr(transparent)]
#[derive(Clone, Debug)]
Expand All @@ -968,7 +968,7 @@ impl core::ops::IndexMut<SpecialCodeIndex> for SpecialCodes {
}
}

/// Indices for use with `Termios::special_codes`.
/// Indices for use with [`Termios::special_codes`].
pub struct SpecialCodeIndex(usize);

#[rustfmt::skip]
Expand Down
3 changes: 3 additions & 0 deletions src/timespec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! `Timespec` and related types, which are used by multiple public API
//! modules.

use crate::backend::c;

/// `struct timespec`
Expand Down

0 comments on commit 8a1b751

Please sign in to comment.