Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make makedev, major, minor const #4208

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
12 changes: 9 additions & 3 deletions libc-test/test/makedev.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! 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(
Expand All @@ -16,11 +16,17 @@ mod t {

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 _);
let minor = unsafe { minor_ffi(dev) };
assert_eq!(libc::minor(dev), minor as _);
}
Comment on lines +27 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you name the types here rather than using _?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish I could, but the return type can be c_int, c_uint, major_t, minor_t depending on the platform.


// 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 @@ -3436,20 +3436,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 @@ -3532,6 +3518,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 @@ -2546,25 +2546,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 @@ -2612,6 +2593,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 @@ -5544,18 +5544,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 @@ -5578,6 +5566,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 @@ -1591,14 +1591,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 @@ -1614,6 +1606,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 @@ -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
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 @@ -2460,17 +2460,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 @@ -2499,6 +2488,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
25 changes: 12 additions & 13 deletions src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1949,19 +1949,6 @@ f! {
pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint {
(_ALIGN(mem::size_of::<cmsghdr>()) + _ALIGN(length as usize)) as c_uint
}

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

pub fn minor(dev: crate::dev_t) -> c_uint {
let dev = dev as c_uint;
let mut res = 0;
res |= (dev) & 0xff;
res |= ((dev) & 0xffff0000) >> 8;

res
}
}

safe_f! {
Expand Down Expand Up @@ -1990,6 +1977,18 @@ safe_f! {
dev |= (minor & 0xffff00) << 8;
dev
}

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

pub {const} fn minor(dev: crate::dev_t) -> c_uint {
let dev = dev as c_uint;
let mut res = 0;
res |= (dev) & 0xff;
res |= ((dev) & 0xffff0000) >> 8;
res
}
}

extern "C" {
Expand Down
Loading
Loading