Skip to content

Commit

Permalink
Merge pull request #4208 from igankevich/const-fn-major-minor
Browse files Browse the repository at this point in the history
Make `makedev`, `major`, `minor` const
  • Loading branch information
tgross35 authored Feb 24, 2025
2 parents 3695157 + 37c3333 commit 989b64a
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 159 deletions.
16 changes: 12 additions & 4 deletions libc-test/src/makedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
#include <sys/sysmacros.h>
#endif

// Since makedev is a macro instead of a function, it isn't available to FFI.
// libc must reimplement it, which is error-prone. This file provides FFI
// access to the actual macro so it can be tested against the Rust
// reimplementation.
// Since makedev, major, minor are macros instead of functions, they aren't
// available to FFI. libc must reimplement them, which is error-prone. This
// file provides FFI access to the actual macros so they can be tested against
// the Rust reimplementation.

dev_t makedev_ffi(unsigned major, unsigned minor) {
return makedev(major, minor);
}

unsigned int major_ffi(dev_t dev) {
return major(dev);
}

unsigned int minor_ffi(dev_t dev) {
return minor(dev);
}
58 changes: 55 additions & 3 deletions libc-test/test/makedev.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
//! Compare libc's makdev function against the actual C macros, for various
//! Compare libc's makedev, major, minor functions against the actual C macros, for various
//! inputs.
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
mod ret {
pub type MajorRetType = libc::major_t;
pub type MinorRetType = libc::minor_t;
}

#[cfg(any(
target_os = "linux",
target_os = "l4re",
target_os = "emscripten",
target_os = "fuchsia",
target_os = "aix",
target_os = "nto",
target_os = "hurd",
target_os = "openbsd",
))]
mod ret {
pub type MajorRetType = libc::c_uint;
pub type MinorRetType = libc::c_uint;
}

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "freebsd",
))]
mod ret {
pub type MajorRetType = libc::c_int;
pub type MinorRetType = libc::c_int;
}

#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos"
))]
mod ret {
pub type MajorRetType = i32;
pub type MinorRetType = i32;
}

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
Expand All @@ -14,13 +58,21 @@
mod t {
use libc::{self, c_uint, dev_t};

use super::ret::*;

extern "C" {
pub fn makedev_ffi(major: c_uint, minor: c_uint) -> dev_t;
pub fn major_ffi(dev: dev_t) -> c_uint;
pub fn minor_ffi(dev: dev_t) -> c_uint;
}

fn compare(major: c_uint, minor: c_uint) {
let expected = unsafe { makedev_ffi(major, minor) };
assert_eq!(libc::makedev(major, minor), expected);
let dev = unsafe { makedev_ffi(major, minor) };
assert_eq!(libc::makedev(major, minor), dev);
let major = unsafe { major_ffi(dev) };
assert_eq!(libc::major(dev), major as MajorRetType);
let minor = unsafe { minor_ffi(dev) };
assert_eq!(libc::minor(dev), minor as MinorRetType);
}

// Every OS should be able to handle 8 bit major and minor numbers
Expand Down
28 changes: 14 additions & 14 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3423,20 +3423,6 @@ f! {
set1.bits == set2.bits
}

pub fn major(dev: crate::dev_t) -> c_uint {
let mut major = 0;
major |= (dev & 0x00000000000fff00) >> 8;
major |= (dev & 0xfffff00000000000) >> 32;
major as c_uint
}

pub fn minor(dev: crate::dev_t) -> c_uint {
let mut minor = 0;
minor |= (dev & 0x00000000000000ff) >> 0;
minor |= (dev & 0x00000ffffff00000) >> 12;
minor as c_uint
}

pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
cmsg.offset(1) as *mut c_uchar
}
Expand Down Expand Up @@ -3519,6 +3505,20 @@ safe_f! {
dev |= (minor & 0xffffff00) << 12;
dev
}

pub {const} fn major(dev: crate::dev_t) -> c_uint {
let mut major = 0;
major |= (dev & 0x00000000000fff00) >> 8;
major |= (dev & 0xfffff00000000000) >> 32;
major as c_uint
}

pub {const} fn minor(dev: crate::dev_t) -> c_uint {
let mut minor = 0;
minor |= (dev & 0x00000000000000ff) >> 0;
minor |= (dev & 0x00000ffffff00000) >> 12;
minor as c_uint
}
}

fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t {
Expand Down
38 changes: 19 additions & 19 deletions src/unix/aix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2545,25 +2545,6 @@ f! {
let fd = fd as usize;
return ((*set).fds_bits[fd / bits] & (1 << (fd % bits))) != 0;
}

pub fn major(dev: crate::dev_t) -> c_uint {
let x = dev >> 16;
x as c_uint
}

pub fn minor(dev: crate::dev_t) -> c_uint {
let y = dev & 0xFFFF;
y as c_uint
}

pub fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
let major = major as crate::dev_t;
let minor = minor as crate::dev_t;
let mut dev = 0;
dev |= major << 16;
dev |= minor;
dev
}
}

safe_f! {
Expand Down Expand Up @@ -2611,6 +2592,25 @@ safe_f! {
pub {const} fn WCOREDUMP(_status: c_int) -> bool {
false
}

pub {const} fn major(dev: crate::dev_t) -> c_uint {
let x = dev >> 16;
x as c_uint
}

pub {const} fn minor(dev: crate::dev_t) -> c_uint {
let y = dev & 0xFFFF;
y as c_uint
}

pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
let major = major as crate::dev_t;
let minor = minor as crate::dev_t;
let mut dev = 0;
dev |= major << 16;
dev |= minor;
dev
}
}

#[link(name = "thread")]
Expand Down
24 changes: 12 additions & 12 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5543,18 +5543,6 @@ f! {
pub {const} fn VM_MAKE_TAG(id: u8) -> u32 {
(id as u32) << 24u32
}

pub fn major(dev: dev_t) -> i32 {
(dev >> 24) & 0xff
}

pub fn minor(dev: dev_t) -> i32 {
dev & 0xffffff
}

pub fn makedev(major: i32, minor: i32) -> dev_t {
(major << 24) | minor
}
}

safe_f! {
Expand All @@ -5577,6 +5565,18 @@ safe_f! {
pub {const} fn WIFSTOPPED(status: c_int) -> bool {
_WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) != 0x13
}

pub {const} fn makedev(major: i32, minor: i32) -> dev_t {
(major << 24) | minor
}

pub {const} fn major(dev: dev_t) -> i32 {
(dev >> 24) & 0xff
}

pub {const} fn minor(dev: dev_t) -> i32 {
dev & 0xffffff
}
}

extern "C" {
Expand Down
16 changes: 8 additions & 8 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,14 +1590,6 @@ f! {
let (idx, offset) = ((cpu >> 6) & 3, cpu & 63);
0 != cpuset.ary[idx] & (1 << offset)
}

pub fn major(dev: crate::dev_t) -> c_int {
((dev >> 8) & 0xff) as c_int
}

pub fn minor(dev: crate::dev_t) -> c_int {
(dev & 0xffff00ff) as c_int
}
}

safe_f! {
Expand All @@ -1613,6 +1605,14 @@ safe_f! {
dev |= minor;
dev
}

pub {const} fn major(dev: crate::dev_t) -> c_int {
((dev >> 8) & 0xff) as c_int
}

pub {const} fn minor(dev: crate::dev_t) -> c_int {
(dev & 0xffff00ff) as c_int
}
}

extern "C" {
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,12 @@ safe_f! {
let minor = minor as crate::dev_t;
(major << 8) | minor
}
}

f! {
pub fn major(dev: crate::dev_t) -> c_int {
pub {const} fn major(dev: crate::dev_t) -> c_int {
((dev >> 8) & 0xff) as c_int
}

pub fn minor(dev: crate::dev_t) -> c_int {
pub {const} fn minor(dev: crate::dev_t) -> c_int {
(dev & 0xffff00ff) as c_int
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,12 @@ safe_f! {
dev |= ((minor & 0xffff00ff) as dev_t) << 0;
dev
}
}

f! {
pub fn major(dev: crate::dev_t) -> c_int {
pub {const} fn major(dev: crate::dev_t) -> c_int {
(((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int
}

pub fn minor(dev: crate::dev_t) -> c_int {
pub {const} fn minor(dev: crate::dev_t) -> c_int {
(((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,12 @@ safe_f! {
dev |= ((minor & 0xffff00ff) as dev_t) << 0;
dev
}
}

f! {
pub fn major(dev: crate::dev_t) -> c_int {
pub {const} fn major(dev: crate::dev_t) -> c_int {
(((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int
}

pub fn minor(dev: crate::dev_t) -> c_int {
pub {const} fn minor(dev: crate::dev_t) -> c_int {
(((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,12 @@ safe_f! {
dev |= ((minor & 0xffff00ff) as dev_t) << 0;
dev
}
}

f! {
pub fn major(dev: crate::dev_t) -> c_int {
pub {const} fn major(dev: crate::dev_t) -> c_int {
(((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int
}

pub fn minor(dev: crate::dev_t) -> c_int {
pub {const} fn minor(dev: crate::dev_t) -> c_int {
(((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/freebsd15/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,12 @@ safe_f! {
dev |= ((minor & 0xffff00ff) as dev_t) << 0;
dev
}
}

f! {
pub fn major(dev: crate::dev_t) -> c_int {
pub {const} fn major(dev: crate::dev_t) -> c_int {
(((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int
}

pub fn minor(dev: crate::dev_t) -> c_int {
pub {const} fn minor(dev: crate::dev_t) -> c_int {
(((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2461,17 +2461,6 @@ f! {
pub fn PROT_MPROTECT_EXTRACT(x: c_int) -> c_int {
(x >> 3) & 0x7
}

pub fn major(dev: crate::dev_t) -> c_int {
(((dev as u32) & 0x000fff00) >> 8) as c_int
}

pub fn minor(dev: crate::dev_t) -> c_int {
let mut res = 0;
res |= ((dev as u32) & 0xfff00000) >> 12;
res |= (dev as u32) & 0x000000ff;
res as c_int
}
}

safe_f! {
Expand Down Expand Up @@ -2500,6 +2489,17 @@ safe_f! {
dev |= minor & 0xff;
dev
}

pub {const} fn major(dev: crate::dev_t) -> c_int {
(((dev as u32) & 0x000fff00) >> 8) as c_int
}

pub {const} fn minor(dev: crate::dev_t) -> c_int {
let mut res = 0;
res |= ((dev as u32) & 0xfff00000) >> 12;
res |= (dev as u32) & 0x000000ff;
res as c_int
}
}

extern "C" {
Expand Down
Loading

0 comments on commit 989b64a

Please sign in to comment.