From ec969f124aa0dadbea22cefe06d7116a94c01562 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 29 Jul 2026 16:54:22 +0200 Subject: [PATCH] refactor(udp): drop `cmsghdr` from the payload unions It was there when the union carried the buffer's alignment, since the buffer holds headers as well as payloads, the C `union { struct cmsghdr hdr; char buf[N]; }` idiom. The alignment now comes from a `usize`, which is at least as strict as any platform's `cmsghdr`, so all the member did was feed `size_of::()` a header that `CMSG_SPACE` already accounts for. No constant moves on any target we build: `in6_pktinfo` is the largest payload everywhere `cmsghdr` is not. A union named for payloads now holds only those. --- noq-udp/src/cmsg/unix.rs | 4 ++-- noq-udp/src/cmsg/windows.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/noq-udp/src/cmsg/unix.rs b/noq-udp/src/cmsg/unix.rs index 386439411..26d28894e 100644 --- a/noq-udp/src/cmsg/unix.rs +++ b/noq-udp/src/cmsg/unix.rs @@ -15,7 +15,6 @@ use crate::imp::IpTosTy; #[repr(C)] #[allow(dead_code)] // the fields are here for their size, nothing reads them pub(crate) union Payload { - hdr: libc::cmsghdr, #[cfg(not(target_os = "netbsd"))] ecn_v4: IpTosTy, ecn_v6: c_int, @@ -75,7 +74,8 @@ pub(crate) const RECV_LEN: usize = 4 * MESSAGE_LEN; #[derive(Copy, Clone)] #[repr(C)] pub(crate) struct ControlBuf { - /// Aligns the buffer like the `size_t` the `CMSG_*` macros round offsets to. + /// Aligns the buffer like the `size_t` the `CMSG_*` macros round offsets to, which + /// covers the headers too: no platform aligns `cmsghdr` more strictly than that. /// Zero sized: `repr(align)` takes a literal, not an expression. _align: [usize; 0], bytes: [MaybeUninit; N], diff --git a/noq-udp/src/cmsg/windows.rs b/noq-udp/src/cmsg/windows.rs index 5eac7e387..3dea5cb07 100644 --- a/noq-udp/src/cmsg/windows.rs +++ b/noq-udp/src/cmsg/windows.rs @@ -18,7 +18,6 @@ use super::{CMsgHdr, Encoder, MsgHdr}; #[repr(C)] #[allow(dead_code)] // the fields are here for their size, nothing reads them pub(crate) union Payload { - hdr: WinSock::CMSGHDR, ecn: c_int, segment_size: u32, pktinfo_v4: WinSock::IN_PKTINFO, @@ -75,7 +74,8 @@ pub(crate) const RECV_LEN: usize = 3 * MESSAGE_LEN; #[derive(Copy, Clone)] #[repr(C)] pub(crate) struct ControlBuf { - /// Aligns the buffer like the `usize` `WSA_CMSGDATA_ALIGN` rounds to. + /// Aligns the buffer like the `usize` `WSA_CMSGDATA_ALIGN` rounds to, which covers + /// the headers too: `CMSGHDR` is a `SIZE_T` and two `INT`s. /// Zero sized: `repr(align)` takes a literal, not an expression. _align: [usize; 0], bytes: [MaybeUninit; N],