Skip to content

Commit

Permalink
Make more Epoll types available to io_uring too.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sunfishcode committed Oct 10, 2023
1 parent e2fcae9 commit b756598
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/backend/libc/event/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -395,10 +400,12 @@ struct SixtyFourBitPointer {
}

/// A vector of `Event`s, plus context for interpreting them.
#[cfg(feature = "alloc")]
pub struct EventVec {
events: Vec<Event>,
}

#[cfg(feature = "alloc")]
impl EventVec {
/// Constructs an `EventVec` from raw pointer, length, and capacity.
///
Expand Down Expand Up @@ -473,6 +480,7 @@ impl EventVec {
}
}

#[cfg(feature = "alloc")]
impl<'a> IntoIterator for &'a EventVec {
type IntoIter = Iter<'a>;
type Item = Event;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/libc/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/backend/linux_raw/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl<'a, Num: ArgNumber> From<crate::event::EventfdFlags> for ArgReg<'a, Num> {
}
}

#[cfg(all(feature = "alloc", feature = "event"))]
#[cfg(feature = "event")]
impl<'a, Num: ArgNumber> From<crate::event::epoll::CreateFlags> for ArgReg<'a, Num> {
#[inline]
fn from(flags: crate::event::epoll::CreateFlags) -> Self {
Expand Down
6 changes: 6 additions & 0 deletions src/backend/linux_raw/event/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -371,10 +374,12 @@ struct SixtyFourBitPointer {
}

/// A vector of `Event`s, plus context for interpreting them.
#[cfg(feature = "alloc")]
pub struct EventVec {
events: Vec<Event>,
}

#[cfg(feature = "alloc")]
impl EventVec {
/// Constructs an `EventVec` from raw pointer, length, and capacity.
///
Expand Down Expand Up @@ -449,6 +454,7 @@ impl EventVec {
}
}

#[cfg(feature = "alloc")]
impl<'a> IntoIterator for &'a EventVec {
type IntoIter = Iter<'a>;
type Item = Event;
Expand Down
1 change: 0 additions & 1 deletion src/backend/linux_raw/event/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature = "alloc")]
pub mod epoll;
pub(crate) mod poll_fd;
pub(crate) mod syscalls;
Expand Down
20 changes: 7 additions & 13 deletions src/backend/linux_raw/event/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
#![allow(unsafe_code, clippy::undocumented_unsafe_blocks)]

use crate::backend::c;
use crate::backend::conv::{c_int, c_uint, ret_owned_fd, ret_usize, slice_mut};
use crate::event::{EventfdFlags, PollFd};
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 crate::backend::conv::pass_usize;
use crate::backend::conv::{
by_ref, c_int, c_uint, raw_fd, ret, ret_owned_fd, ret_usize, slice_mut, zero,
};
use crate::event::{epoll, EventfdFlags, PollFd};
use crate::fd::{BorrowedFd, OwnedFd};
use crate::io;
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},
Expand Down Expand Up @@ -52,13 +50,11 @@ pub(crate) fn poll(fds: &mut [PollFd<'_>], timeout: c::c_int) -> io::Result<usiz
}
}

#[cfg(feature = "alloc")]
#[inline]
pub(crate) fn epoll_create(flags: epoll::CreateFlags) -> io::Result<OwnedFd> {
unsafe { ret_owned_fd(syscall_readonly!(__NR_epoll_create1, flags)) }
}

#[cfg(feature = "alloc")]
#[inline]
pub(crate) unsafe fn epoll_add(
epfd: BorrowedFd<'_>,
Expand All @@ -74,7 +70,6 @@ pub(crate) unsafe fn epoll_add(
))
}

#[cfg(feature = "alloc")]
#[inline]
pub(crate) unsafe fn epoll_mod(
epfd: BorrowedFd<'_>,
Expand All @@ -90,7 +85,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!(
Expand Down
2 changes: 1 addition & 1 deletion src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/io_uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit b756598

Please sign in to comment.