From 5d22f7096ba2b3046cb80baf97282bc3a2953b0d Mon Sep 17 00:00:00 2001 From: auronandace Date: Tue, 31 Mar 2026 11:03:41 +0100 Subject: [PATCH] split out iovec from sys_uio header --- src/header/bits_iovec/cbindgen.toml | 18 +++++++++++++ src/header/bits_iovec/mod.rs | 35 ++++++++++++++++++++++++++ src/header/mod.rs | 1 + src/header/sys_socket/cbindgen.toml | 3 ++- src/header/sys_socket/mod.rs | 2 +- src/header/sys_uio/cbindgen.toml | 3 +++ src/header/sys_uio/mod.rs | 39 ++++------------------------- src/platform/redox/libredox.rs | 3 ++- src/platform/redox/socket.rs | 2 +- 9 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 src/header/bits_iovec/cbindgen.toml create mode 100644 src/header/bits_iovec/mod.rs diff --git a/src/header/bits_iovec/cbindgen.toml b/src/header/bits_iovec/cbindgen.toml new file mode 100644 index 0000000000..c9c51fbb10 --- /dev/null +++ b/src/header/bits_iovec/cbindgen.toml @@ -0,0 +1,18 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_uio.h.html +# +# Split out to avoid including all of sys/uio.h in sys/socket.h. +# +# POSIX headers that require iovec: +# - sys/socket.h +# - sys/uio.h (where it should be defined) +# +# sys/types.h included for c_void and size_t +sys_includes = ["sys/types.h"] +include_guard = "_RELIBC_BITS_IOVEC_H" +language = "C" +style = "Tag" +no_includes = true +cpp_compat = true + +[enum] +prefix_with_name = true diff --git a/src/header/bits_iovec/mod.rs b/src/header/bits_iovec/mod.rs new file mode 100644 index 0000000000..ca1d53c1b5 --- /dev/null +++ b/src/header/bits_iovec/mod.rs @@ -0,0 +1,35 @@ +use alloc::vec::Vec; +use core::slice; + +use crate::platform::types::{c_void, size_t}; + +/// See . +#[repr(C)] +#[derive(Debug, CheckVsLibcCrate)] +pub struct iovec { + pub iov_base: *mut c_void, + pub iov_len: size_t, +} + +impl iovec { + unsafe fn to_slice(&self) -> &mut [u8] { + unsafe { slice::from_raw_parts_mut(self.iov_base.cast::(), self.iov_len) } + } +} + +pub unsafe fn gather(iovs: &[iovec]) -> Vec { + let mut vec = Vec::new(); + for iov in iovs.iter() { + vec.extend_from_slice(unsafe { iov.to_slice() }); + } + vec +} + +pub unsafe fn scatter(iovs: &[iovec], vec: Vec) { + let mut i = 0; + for iov in iovs.iter() { + let slice = unsafe { iov.to_slice() }; + slice.copy_from_slice(&vec[i..][..slice.len()]); + i += slice.len(); + } +} diff --git a/src/header/mod.rs b/src/header/mod.rs index 37b6104264..b3bb9aa36f 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -5,6 +5,7 @@ pub mod _fenv; pub mod arpa_inet; pub mod assert; pub mod bits_arpainet; +pub mod bits_iovec; #[path = "bits_locale-t/mod.rs"] pub mod bits_locale_t; pub mod bits_pthread; diff --git a/src/header/sys_socket/cbindgen.toml b/src/header/sys_socket/cbindgen.toml index a8d0e22a96..53adbc0fe3 100644 --- a/src/header/sys_socket/cbindgen.toml +++ b/src/header/sys_socket/cbindgen.toml @@ -1,6 +1,7 @@ -sys_includes = ["stddef.h", "stdint.h", "sys/types.h", "sys/uio.h"] +sys_includes = ["stddef.h", "stdint.h", "sys/types.h"] include_guard = "_SYS_SOCKET_H" after_includes = """ +#include // for iovec #include // for socklen_t """ trailer = "#include " diff --git a/src/header/sys_socket/mod.rs b/src/header/sys_socket/mod.rs index 63c008d3a5..ace3526c6a 100644 --- a/src/header/sys_socket/mod.rs +++ b/src/header/sys_socket/mod.rs @@ -6,7 +6,7 @@ use core::{mem, ptr}; use crate::{ error::ResultExt, - header::{bits_socklen_t::socklen_t, sys_uio::iovec}, + header::{bits_iovec::iovec, bits_socklen_t::socklen_t}, platform::{ PalSocket, Sys, types::{ diff --git a/src/header/sys_uio/cbindgen.toml b/src/header/sys_uio/cbindgen.toml index 83f0d6f5e2..88a3ca3539 100644 --- a/src/header/sys_uio/cbindgen.toml +++ b/src/header/sys_uio/cbindgen.toml @@ -4,6 +4,9 @@ # - "The header shall define the ssize_t and size_t types as described in ." sys_includes = ["sys/types.h"] include_guard = "_SYS_UIO_H" +after_includes = """ +#include // for iovec +""" language = "C" style = "Tag" no_includes = true diff --git a/src/header/sys_uio/mod.rs b/src/header/sys_uio/mod.rs index 0b99c7b8e8..1e3976ae76 100644 --- a/src/header/sys_uio/mod.rs +++ b/src/header/sys_uio/mod.rs @@ -2,50 +2,21 @@ //! //! See . -use alloc::vec::Vec; use core::slice; use crate::{ - header::{errno, unistd}, + header::{ + bits_iovec::{gather, iovec, scatter}, + errno, unistd, + }, platform::{ self, - types::{c_int, c_void, off_t, size_t, ssize_t}, + types::{c_int, c_void, off_t, ssize_t}, }, }; pub const IOV_MAX: c_int = 1024; -/// See . -#[repr(C)] -#[derive(Debug, CheckVsLibcCrate)] -pub struct iovec { - pub iov_base: *mut c_void, - pub iov_len: size_t, -} - -impl iovec { - unsafe fn to_slice(&self) -> &mut [u8] { - unsafe { slice::from_raw_parts_mut(self.iov_base.cast::(), self.iov_len) } - } -} - -unsafe fn gather(iovs: &[iovec]) -> Vec { - let mut vec = Vec::new(); - for iov in iovs.iter() { - vec.extend_from_slice(unsafe { iov.to_slice() }); - } - vec -} - -unsafe fn scatter(iovs: &[iovec], vec: Vec) { - let mut i = 0; - for iov in iovs.iter() { - let slice = unsafe { iov.to_slice() }; - slice.copy_from_slice(&vec[i..][..slice.len()]); - i += slice.len(); - } -} - /// Non-POSIX, see . #[unsafe(no_mangle)] pub unsafe extern "C" fn preadv( diff --git a/src/platform/redox/libredox.rs b/src/platform/redox/libredox.rs index 591423ed0e..0980024b75 100644 --- a/src/platform/redox/libredox.rs +++ b/src/platform/redox/libredox.rs @@ -12,7 +12,8 @@ use syscall::{ use crate::{ header::{ - bits_time::timespec, errno::EINVAL, signal::sigaction, sys_stat::UTIME_NOW, sys_uio::iovec, + bits_iovec::iovec, bits_time::timespec, errno::EINVAL, signal::sigaction, + sys_stat::UTIME_NOW, }, out::Out, platform::{PalSignal, pal::Pal, types::*}, diff --git a/src/platform/redox/socket.rs b/src/platform/redox/socket.rs index 2a166e14d2..0b534b7254 100644 --- a/src/platform/redox/socket.rs +++ b/src/platform/redox/socket.rs @@ -13,6 +13,7 @@ use crate::{ error::{Errno, Result}, header::{ arpa_inet::inet_aton, + bits_iovec::iovec, bits_socklen_t::socklen_t, errno::{ EAFNOSUPPORT, EDOM, EFAULT, EINVAL, EMSGSIZE, ENOMEM, ENOSYS, ENOTSOCK, EOPNOTSUPP, @@ -25,7 +26,6 @@ use crate::{ CMSG_ALIGN, CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_NXTHDR, CMSG_SPACE, cmsghdr, constants::*, msghdr, sa_family_t, sockaddr, ucred, }, - sys_uio::iovec, sys_un::sockaddr_un, }, };