Skip to content

Commit c050a40

Browse files
authored
Use c:: instead of libc:: and linux_raw_sys:: in more places. (#1307)
This helps unify some code between the two backends.
1 parent d55e619 commit c050a40

10 files changed

Lines changed: 149 additions & 140 deletions

File tree

src/backend/libc/c.rs

Lines changed: 123 additions & 122 deletions
Large diffs are not rendered by default.

src/backend/libc/mm/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,15 @@ bitflags! {
479479
/// created. `MCL_ONFAULT` must be used with either `MCL_CURRENT` or
480480
/// `MCL_FUTURE` or both.
481481
#[cfg(linux_kernel)]
482-
const ONFAULT = bitcast!(libc::MCL_ONFAULT);
482+
const ONFAULT = bitcast!(c::MCL_ONFAULT);
483483
/// Lock all pages which will become mapped into the address space of
484484
/// the process in the future. These could be, for instance, new pages
485485
/// required by a growing heap and stack as well as new memory-mapped
486486
/// files or shared memory regions.
487-
const FUTURE = bitcast!(libc::MCL_FUTURE);
487+
const FUTURE = bitcast!(c::MCL_FUTURE);
488488
/// Lock all pages which are currently mapped into the address space of
489489
/// the process.
490-
const CURRENT = bitcast!(libc::MCL_CURRENT);
490+
const CURRENT = bitcast!(c::MCL_CURRENT);
491491

492492
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
493493
const _ = !0;

src/backend/libc/net/netdevice.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
#[cfg(feature = "alloc")]
66
use crate::alloc::string::String;
7+
use crate::backend::c;
78
use crate::backend::io::syscalls::ioctl;
89
use crate::fd::AsFd;
910
use crate::io;
1011
#[cfg(feature = "alloc")]
11-
use libc::SIOCGIFNAME;
12-
use libc::{__c_anonymous_ifr_ifru, c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX};
12+
use c::SIOCGIFNAME;
13+
use c::{__c_anonymous_ifr_ifru, c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX};
1314

1415
pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
1516
let if_name_bytes = if_name.as_bytes();

src/backend/libc/pty/syscalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(crate) fn ptsname(fd: BorrowedFd<'_>, mut buffer: Vec<u8>) -> io::Result<CSt
6666
if let Some(func) = ptsname_r.get() {
6767
func(borrowed_fd(fd), buffer.as_mut_ptr().cast(), buffer.len())
6868
} else {
69-
libc::ENOSYS
69+
c::ENOSYS
7070
}
7171
};
7272

src/backend/libc/termios/syscalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {
102102
let mut result = MaybeUninit::<Termios>::uninit();
103103

104104
// `result` is a `Termios` which starts with the same layout as
105-
// `libc::termios`, so we can cast the pointer.
105+
// `c::termios`, so we can cast the pointer.
106106
ret(c::tcgetattr(borrowed_fd(fd), result.as_mut_ptr().cast()))?;
107107

108108
Ok(result.assume_init())

src/backend/libc/thread/syscalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ pub(crate) fn setgroups_thread(groups: &[crate::ugid::Gid]) -> io::Result<()> {
665665
#[cfg(any(linux_kernel, target_os = "dragonfly"))]
666666
#[inline]
667667
pub(crate) fn sched_getcpu() -> usize {
668-
let r = unsafe { libc::sched_getcpu() };
668+
let r = unsafe { c::sched_getcpu() };
669669
debug_assert!(r >= 0);
670670
r as usize
671671
}

src/backend/libc/time/types.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ impl From<Itimerspec> for LibcItimerspec {
5050

5151
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
5252
#[cfg(not(fix_y2038))]
53-
pub(crate) fn as_libc_itimerspec_ptr(itimerspec: &Itimerspec) -> *const libc::itimerspec {
53+
pub(crate) fn as_libc_itimerspec_ptr(itimerspec: &Itimerspec) -> *const c::itimerspec {
5454
#[cfg(test)]
5555
{
56-
assert_eq_size!(Itimerspec, libc::itimerspec);
56+
assert_eq_size!(Itimerspec, c::itimerspec);
5757
}
58-
crate::utils::as_ptr(itimerspec).cast::<libc::itimerspec>()
58+
crate::utils::as_ptr(itimerspec).cast::<c::itimerspec>()
5959
}
6060

6161
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
6262
#[cfg(not(fix_y2038))]
6363
pub(crate) fn as_libc_itimerspec_mut_ptr(
6464
itimerspec: &mut core::mem::MaybeUninit<Itimerspec>,
65-
) -> *mut libc::itimerspec {
65+
) -> *mut c::itimerspec {
6666
#[cfg(test)]
6767
{
68-
assert_eq_size!(Itimerspec, libc::itimerspec);
68+
assert_eq_size!(Itimerspec, c::itimerspec);
6969
}
70-
itimerspec.as_mut_ptr().cast::<libc::itimerspec>()
70+
itimerspec.as_mut_ptr().cast::<c::itimerspec>()
7171
}
7272

7373
#[cfg(any(linux_kernel, target_os = "fuchsia"))]

src/backend/linux_raw/c.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ pub(crate) use linux_raw_sys::general::{
3535
#[cfg(test)]
3636
pub(crate) use linux_raw_sys::general::epoll_event;
3737

38+
#[cfg(feature = "mm")]
39+
mod mm {
40+
pub(crate) use linux_raw_sys::general::{MAP_HUGETLB, MAP_HUGE_SHIFT};
41+
}
42+
#[cfg(feature = "mm")]
43+
pub(crate) use mm::*;
44+
3845
#[cfg(any(
3946
feature = "fs",
4047
all(

src/fs/special.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod tests {
5858
assert!(CWD.as_raw_fd() != -1);
5959
#[cfg(linux_kernel)]
6060
#[cfg(feature = "io_uring")]
61-
assert!(CWD.as_raw_fd() != linux_raw_sys::io_uring::IORING_REGISTER_FILES_SKIP);
61+
assert!(CWD.as_raw_fd() != crate::io_uring::IORING_REGISTER_FILES_SKIP.as_raw_fd());
6262
}
6363

6464
#[test]
@@ -68,6 +68,6 @@ mod tests {
6868
assert!(ABS.as_raw_fd() != c::AT_FDCWD);
6969
#[cfg(linux_kernel)]
7070
#[cfg(feature = "io_uring")]
71-
assert!(ABS.as_raw_fd() != linux_raw_sys::io_uring::IORING_REGISTER_FILES_SKIP);
71+
assert!(ABS.as_raw_fd() != crate::io_uring::IORING_REGISTER_FILES_SKIP.as_raw_fd());
7272
}
7373
}

src/mm/mmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl MapFlags {
3434
/// ```
3535
#[cfg(linux_kernel)]
3636
pub const fn hugetlb_with_size_log2(huge_page_size_log2: u32) -> Option<Self> {
37-
use linux_raw_sys::general::{MAP_HUGETLB, MAP_HUGE_SHIFT};
37+
use crate::backend::c;
3838
if 16 <= huge_page_size_log2 && huge_page_size_log2 <= 63 {
39-
let bits = MAP_HUGETLB | (huge_page_size_log2 << MAP_HUGE_SHIFT);
39+
let bits = bitcast!(c::MAP_HUGETLB) | (huge_page_size_log2 << c::MAP_HUGE_SHIFT);
4040
Self::from_bits(bits)
4141
} else {
4242
None

0 commit comments

Comments
 (0)