From d348f34df26437e28ea7eb9c2f5af3da873f9e4f Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 10 Oct 2023 01:50:32 -0700 Subject: [PATCH] Make more `Epoll` types available to io_uring too. This enables most of the epoll module without needing "alloc". It doesn't enable `epoll_wait`, because that needs `Vec`, but this does potentially open up a path to having a non-allocating opetion for waiting on an epoll. --- src/backend/libc/event/epoll.rs | 10 +++++++++- src/backend/libc/event/mod.rs | 2 +- src/backend/linux_raw/conv.rs | 2 +- src/backend/linux_raw/event/epoll.rs | 6 ++++++ src/backend/linux_raw/event/mod.rs | 1 - src/backend/linux_raw/event/syscalls.rs | 17 ++++++----------- src/event/mod.rs | 2 +- src/io_uring.rs | 4 +++- 8 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/backend/libc/event/epoll.rs b/src/backend/libc/event/epoll.rs index 9fba85cc6..39363bf25 100644 --- a/src/backend/libc/event/epoll.rs +++ b/src/backend/libc/event/epoll.rs @@ -74,10 +74,13 @@ //! ``` use crate::backend::c; -use crate::backend::conv::{ret, ret_owned_fd, ret_u32}; +#[cfg(feature = "alloc")] +use crate::backend::conv::ret_u32; +use crate::backend::conv::{ret, ret_owned_fd}; use crate::fd::{AsFd, AsRawFd, OwnedFd}; use crate::io; use crate::utils::as_mut_ptr; +#[cfg(feature = "alloc")] use alloc::vec::Vec; use bitflags::bitflags; use core::ffi::c_void; @@ -257,6 +260,8 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> { /// /// For each event of interest, an element is written to `events`. On /// success, this returns the number of written elements. +#[cfg(feature = "alloc")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] pub fn wait(epoll: impl AsFd, event_list: &mut EventVec, timeout: c::c_int) -> io::Result<()> { // SAFETY: We're calling `epoll_wait` via FFI and we know how it // behaves. @@ -395,10 +400,12 @@ struct SixtyFourBitPointer { } /// A vector of `Event`s, plus context for interpreting them. +#[cfg(feature = "alloc")] pub struct EventVec { events: Vec, } +#[cfg(feature = "alloc")] impl EventVec { /// Constructs an `EventVec` from raw pointer, length, and capacity. /// @@ -473,6 +480,7 @@ impl EventVec { } } +#[cfg(feature = "alloc")] impl<'a> IntoIterator for &'a EventVec { type IntoIter = Iter<'a>; type Item = Event; diff --git a/src/backend/libc/event/mod.rs b/src/backend/libc/event/mod.rs index 44e8a090a..6aed4612a 100644 --- a/src/backend/libc/event/mod.rs +++ b/src/backend/libc/event/mod.rs @@ -5,5 +5,5 @@ pub(crate) mod types; #[cfg_attr(windows, path = "windows_syscalls.rs")] pub(crate) mod syscalls; -#[cfg(all(feature = "alloc", linux_kernel))] +#[cfg(linux_kernel)] pub mod epoll; diff --git a/src/backend/linux_raw/conv.rs b/src/backend/linux_raw/conv.rs index f915db140..3a3825266 100644 --- a/src/backend/linux_raw/conv.rs +++ b/src/backend/linux_raw/conv.rs @@ -581,7 +581,7 @@ impl<'a, Num: ArgNumber> From for ArgReg<'a, Num> { } } -#[cfg(all(feature = "alloc", feature = "event"))] +#[cfg(feature = "event")] impl<'a, Num: ArgNumber> From for ArgReg<'a, Num> { #[inline] fn from(flags: crate::event::epoll::CreateFlags) -> Self { diff --git a/src/backend/linux_raw/event/epoll.rs b/src/backend/linux_raw/event/epoll.rs index 9c9c34632..90fde2d83 100644 --- a/src/backend/linux_raw/event/epoll.rs +++ b/src/backend/linux_raw/event/epoll.rs @@ -79,6 +79,7 @@ use crate::backend::c; use crate::backend::event::syscalls; use crate::fd::{AsFd, AsRawFd, OwnedFd}; use crate::io; +#[cfg(feature = "alloc")] use alloc::vec::Vec; use bitflags::bitflags; use core::ffi::c_void; @@ -242,6 +243,8 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> { /// /// For each event of interest, an element is written to `events`. On /// success, this returns the number of written elements. +#[cfg(feature = "alloc")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] #[inline] pub fn wait(epoll: impl AsFd, event_list: &mut EventVec, timeout: c::c_int) -> io::Result<()> { // SAFETY: We're calling `epoll_wait` via FFI and we know how it @@ -371,10 +374,12 @@ struct SixtyFourBitPointer { } /// A vector of `Event`s, plus context for interpreting them. +#[cfg(feature = "alloc")] pub struct EventVec { events: Vec, } +#[cfg(feature = "alloc")] impl EventVec { /// Constructs an `EventVec` from raw pointer, length, and capacity. /// @@ -449,6 +454,7 @@ impl EventVec { } } +#[cfg(feature = "alloc")] impl<'a> IntoIterator for &'a EventVec { type IntoIter = Iter<'a>; type Item = Event; diff --git a/src/backend/linux_raw/event/mod.rs b/src/backend/linux_raw/event/mod.rs index 4148a8c7f..605de2538 100644 --- a/src/backend/linux_raw/event/mod.rs +++ b/src/backend/linux_raw/event/mod.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "alloc")] pub mod epoll; pub(crate) mod poll_fd; pub(crate) mod syscalls; diff --git a/src/backend/linux_raw/event/syscalls.rs b/src/backend/linux_raw/event/syscalls.rs index a8003b004..bd9423a3d 100644 --- a/src/backend/linux_raw/event/syscalls.rs +++ b/src/backend/linux_raw/event/syscalls.rs @@ -6,17 +6,16 @@ #![allow(unsafe_code, clippy::undocumented_unsafe_blocks)] use crate::backend::c; +#[cfg(feature = "alloc")] +use crate::backend::conv::pass_usize; +use crate::backend::conv::{by_ref, raw_fd, ret, zero}; use crate::backend::conv::{c_int, c_uint, ret_owned_fd, ret_usize, slice_mut}; +use crate::event::epoll; use crate::event::{EventfdFlags, PollFd}; +use crate::fd::BorrowedFd; use crate::fd::OwnedFd; use crate::io; -#[cfg(feature = "alloc")] -use { - crate::backend::conv::{by_ref, pass_usize, raw_fd, ret, zero}, - crate::event::epoll, - crate::fd::BorrowedFd, - linux_raw_sys::general::{EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD}, -}; +use linux_raw_sys::general::{EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD}; #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] use { crate::backend::conv::{opt_ref, size_of}, @@ -52,13 +51,11 @@ pub(crate) fn poll(fds: &mut [PollFd<'_>], timeout: c::c_int) -> io::Result io::Result { unsafe { ret_owned_fd(syscall_readonly!(__NR_epoll_create1, flags)) } } -#[cfg(feature = "alloc")] #[inline] pub(crate) unsafe fn epoll_add( epfd: BorrowedFd<'_>, @@ -74,7 +71,6 @@ pub(crate) unsafe fn epoll_add( )) } -#[cfg(feature = "alloc")] #[inline] pub(crate) unsafe fn epoll_mod( epfd: BorrowedFd<'_>, @@ -90,7 +86,6 @@ pub(crate) unsafe fn epoll_mod( )) } -#[cfg(feature = "alloc")] #[inline] pub(crate) unsafe fn epoll_del(epfd: BorrowedFd<'_>, fd: c::c_int) -> io::Result<()> { ret(syscall_readonly!( diff --git a/src/event/mod.rs b/src/event/mod.rs index 03abc9f12..b0b62e0a6 100644 --- a/src/event/mod.rs +++ b/src/event/mod.rs @@ -13,7 +13,7 @@ mod poll; #[cfg(solarish)] pub mod port; -#[cfg(all(feature = "alloc", linux_kernel))] +#[cfg(linux_kernel)] pub use crate::backend::event::epoll; #[cfg(any( linux_kernel, diff --git a/src/io_uring.rs b/src/io_uring.rs index b0e4283a5..eb95678eb 100644 --- a/src/io_uring.rs +++ b/src/io_uring.rs @@ -32,7 +32,9 @@ use core::ptr::{null_mut, write_bytes}; use linux_raw_sys::net; // Export types used in io_uring APIs. -pub use crate::event::epoll::Event as EpollEvent; +pub use crate::event::epoll::{ + Event as EpollEvent, EventData as EpollEventData, EventFlags as EpollEventFlags, +}; pub use crate::fs::{Advice, AtFlags, Mode, OFlags, RenameFlags, ResolveFlags, Statx, StatxFlags}; pub use crate::io::ReadWriteFlags; pub use crate::net::{RecvFlags, SendFlags, SocketFlags};