Skip to content

Commit

Permalink
Various fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Sep 24, 2024
1 parent f321b9a commit d56f394
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/backend/linux_raw/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub(crate) type size_t = usize;
pub(crate) use linux_raw_sys::ctypes::*;
pub(crate) use linux_raw_sys::errno::EINVAL;
pub(crate) use linux_raw_sys::general::{__kernel_fd_set as fd_set, __FD_SETSIZE as FD_SETSIZE};
pub(crate) use linux_raw_sys::ioctl::{FIONBIO, FIONREAD};
// Import the kernel's `uid_t` and `gid_t` if they're 32-bit.
#[cfg(not(any(target_arch = "arm", target_arch = "sparc", target_arch = "x86")))]
Expand Down
30 changes: 24 additions & 6 deletions src/event/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! `select` is unsafe due to I/O safety.
#![allow(unsafe_code)]

#[cfg(linux_like)]
#[cfg(any(linux_like, target_os = "wasi"))]
use crate::backend::c;
use crate::fd::RawFd;
use crate::{backend, io};
Expand All @@ -23,7 +23,7 @@ struct FD_SET {
/// The wasi-libc headers call this `__nfds`.
fd_count: usize,
/// The wasi-libc headers call this `__fds`.
fd_array: [i32; libc::FD_SETSIZE],
fd_array: [i32; c::FD_SETSIZE],
}

#[cfg(windows)]
Expand Down Expand Up @@ -233,7 +233,10 @@ pub fn fd_set_num_elements(set_count: usize, nfds: RawFd) -> usize {
pub(crate) fn fd_set_num_elements_for_fd_array(set_count: usize) -> usize {
// Allocate space for an `fd_count` field, plus `set_count` elements
// for the `fd_array` field.
1 + set_count
div_ceil(
align_of::<FD_SET>() + set_count * size_of::<RawFd>(),
size_of::<FdSetElement>(),
)
}

/// `fd_set_num_elements` implementation on platforms with bitvector
Expand All @@ -246,7 +249,6 @@ pub(crate) fn fd_set_num_elements_for_bitvector(nfds: RawFd) -> usize {
div_ceil(nfds, BITS)
}

#[cfg(not(any(windows, target_os = "wasi")))]
fn div_ceil(lhs: usize, rhs: usize) -> usize {
let d = lhs / rhs;
let r = lhs % rhs;
Expand Down Expand Up @@ -333,19 +335,35 @@ impl<'a> Iterator for FdSetIter<'a> {
#[cfg(test)]
mod test {
use super::*;
use core::mem::align_of;
use core::mem::{align_of, size_of};

#[test]
#[cfg(any(windows, target_os = "wasi"))]
fn layouts() {
// The `FdSetElement` array should be suitably aligned.
assert_eq!(align_of::<FdSetElement>(), align_of::<FD_SET>());

// The layout of `FD_SET` should match our layout of a set of the same
// size.
assert_eq!(
fd_set_num_elements_for_fd_array(
memoffset::span_of!(FD_SET, fd_array).len() / size_of::<RawFd>()
) * size_of::<FdSetElement>(),
size_of::<FD_SET>()
);
}

#[test]
#[cfg(any(bsd, linux_kernel))]
fn layouts() {
// The `FdSetElement` array should be suitably aligned.
assert_eq!(align_of::<FdSetElement>(), align_of::<libc::fd_set>());
assert_eq!(align_of::<FdSetElement>(), align_of::<c::fd_set>());

// The layout of `fd_set` should match our layout of a set of the same
// size.
assert_eq!(
fd_set_num_elements_for_bitvector(c::FD_SETSIZE as RawFd) * size_of::<FdSetElement>(),
size_of::<c::fd_set>()
);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ mod signal;
feature = "runtime",
feature = "thread",
feature = "time",
all(feature = "event", any(bsd, linux_kernel, windows)),
all(feature = "event", any(bsd, linux_kernel, windows, target_os = "wasi")),
all(
linux_raw,
not(feature = "use-libc-auxv"),
Expand Down

0 comments on commit d56f394

Please sign in to comment.