Skip to content

Commit

Permalink
Auto merge of #2132 - JohnTitor:cmsg-space, r=Amanieu
Browse files Browse the repository at this point in the history
Constify `CMSG_SPACE` for all the targets

`CMSG_SPACE` is a const fn for Linux but it isn't for other targets. This constifies it on all the targets for consistency.

Fixes #2087
  • Loading branch information
bors committed Mar 30, 2021
2 parents 8df930f + e187543 commit f2e7721
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3256,12 +3256,12 @@ f! {
}
}

pub fn CMSG_ALIGN(len: ::size_t) -> ::size_t {
pub {const} fn CMSG_ALIGN(len: ::size_t) -> ::size_t {
(len + ::mem::size_of::<::size_t>() - 1)
& !(::mem::size_of::<::size_t>() - 1)
}

pub fn CMSG_SPACE(len: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(len: ::c_uint) -> ::c_uint {
(CMSG_ALIGN(len as ::size_t) + CMSG_ALIGN(::mem::size_of::<cmsghdr>()))
as ::c_uint
}
Expand Down
11 changes: 8 additions & 3 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3366,7 +3366,12 @@ pub const MNT_WAIT: ::c_int = 1;
pub const MNT_NOWAIT: ::c_int = 2;

cfg_if! {
if #[cfg(libc_const_size_of)] {
if #[cfg(libc_const_extern_fn)] {
const fn __DARWIN_ALIGN32(p: usize) -> usize {
const __DARWIN_ALIGNBYTES32: usize = ::mem::size_of::<u32>() - 1;
p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32
}
} else if #[cfg(libc_const_size_of)] {
fn __DARWIN_ALIGN32(p: usize) -> usize {
const __DARWIN_ALIGNBYTES32: usize = ::mem::size_of::<u32>() - 1;
p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32
Expand All @@ -3388,7 +3393,7 @@ f! {
let cmsg_len = (*cmsg).cmsg_len as usize;
let next = cmsg as usize + __DARWIN_ALIGN32(cmsg_len as usize);
let max = (*mhdr).msg_control as usize
+ (*mhdr).msg_controllen as usize;
+ (*mhdr).msg_controllen as usize;
if next + __DARWIN_ALIGN32(::mem::size_of::<::cmsghdr>()) > max {
0 as *mut ::cmsghdr
} else {
Expand All @@ -3401,7 +3406,7 @@ f! {
.offset(__DARWIN_ALIGN32(::mem::size_of::<::cmsghdr>()) as isize)
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(__DARWIN_ALIGN32(::mem::size_of::<::cmsghdr>())
+ __DARWIN_ALIGN32(length as usize))
as ::c_uint
Expand Down
8 changes: 5 additions & 3 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,10 @@ pub const SF_XLINK: ::c_ulong = 0x01000000;
pub const UTIME_OMIT: c_long = -2;
pub const UTIME_NOW: c_long = -1;

fn _CMSG_ALIGN(n: usize) -> usize {
(n + 3) & !3
const_fn! {
{const} fn _CMSG_ALIGN(n: usize) -> usize {
(n + 3) & !3
}
}

f! {
Expand Down Expand Up @@ -1050,7 +1052,7 @@ f! {
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(_CMSG_ALIGN(::mem::size_of::<::cmsghdr>()) +
_CMSG_ALIGN(length as usize)) as ::c_uint
}
Expand Down
8 changes: 5 additions & 3 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,8 +1216,10 @@ pub const F_READAHEAD: ::c_int = 15;
pub const F_RDAHEAD: ::c_int = 16;
pub const F_DUP2FD_CLOEXEC: ::c_int = 18;

fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
const_fn! {
{const} fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
}
}

f! {
Expand Down Expand Up @@ -1248,7 +1250,7 @@ f! {
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(_ALIGN(::mem::size_of::<::cmsghdr>()) + _ALIGN(length as usize))
as ::c_uint
}
Expand Down
8 changes: 5 additions & 3 deletions src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,8 +1748,10 @@ pub const SF_SNAPSHOT: ::c_ulong = 0x00200000;
pub const SF_LOG: ::c_ulong = 0x00400000;
pub const SF_SNAPINVAL: ::c_ulong = 0x00800000;

fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
const_fn! {
{const} fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
}
}

f! {
Expand Down Expand Up @@ -1780,7 +1782,7 @@ f! {
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(_ALIGN(::mem::size_of::<::cmsghdr>()) + _ALIGN(length as usize))
as ::c_uint
}
Expand Down
8 changes: 5 additions & 3 deletions src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,8 +1397,10 @@ pub const PTRACE_FORK: ::c_int = 0x0002;

pub const WCONTINUED: ::c_int = 8;

fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
const_fn! {
{const} fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
}
}

f! {
Expand Down Expand Up @@ -1429,7 +1431,7 @@ f! {
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(_ALIGN(::mem::size_of::<::cmsghdr>()) + _ALIGN(length as usize))
as ::c_uint
}
Expand Down
14 changes: 8 additions & 6 deletions src/unix/solarish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,12 +2103,14 @@ const _CMSG_HDR_ALIGNMENT: usize = 4;

const _CMSG_DATA_ALIGNMENT: usize = ::mem::size_of::<::c_int>();

fn _CMSG_HDR_ALIGN(p: usize) -> usize {
(p + _CMSG_HDR_ALIGNMENT - 1) & !(_CMSG_HDR_ALIGNMENT - 1)
}
const_fn! {
{const} fn _CMSG_HDR_ALIGN(p: usize) -> usize {
(p + _CMSG_HDR_ALIGNMENT - 1) & !(_CMSG_HDR_ALIGNMENT - 1)
}

fn _CMSG_DATA_ALIGN(p: usize) -> usize {
(p + _CMSG_DATA_ALIGNMENT - 1) & !(_CMSG_DATA_ALIGNMENT - 1)
{const} fn _CMSG_DATA_ALIGN(p: usize) -> usize {
(p + _CMSG_DATA_ALIGNMENT - 1) & !(_CMSG_DATA_ALIGNMENT - 1)
}
}

f! {
Expand Down Expand Up @@ -2146,7 +2148,7 @@ f! {
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
_CMSG_HDR_ALIGN(::mem::size_of::<::cmsghdr>() as usize
+ length as usize) as ::c_uint
}
Expand Down
4 changes: 2 additions & 2 deletions src/vxworks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ impl ::Clone for fpos_t {
}

f! {
pub fn CMSG_ALIGN(len: usize) -> usize {
pub {const} fn CMSG_ALIGN(len: usize) -> usize {
len + ::mem::size_of::<usize>() - 1 & !(::mem::size_of::<usize>() - 1)
}

Expand Down Expand Up @@ -1071,7 +1071,7 @@ f! {
.offset(CMSG_ALIGN(::mem::size_of::<::cmsghdr>()) as isize)
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
pub {const} fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(CMSG_ALIGN(length as usize) + CMSG_ALIGN(::mem::size_of::<cmsghdr>()))
as ::c_uint
}
Expand Down

0 comments on commit f2e7721

Please sign in to comment.