Skip to content

Commit

Permalink
recvmsg: take slice for cmsg_buffer (#2524)
Browse files Browse the repository at this point in the history
Instead of a Vec, to avoid forcing an allocation.

`cmsg_space!` now creates a zero initialized vec
(instead of a zero-len vec with capacity):
this way callsites do not need to be changed.

Fixes #2523

Co-authored-by: Benedek Thaler <[email protected]>
  • Loading branch information
erenon and Benedek Thaler authored Oct 22, 2024
1 parent c7e35cd commit b91bf39
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/2524.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recvmsg: take slice for cmsg_buffer instead of Vec
6 changes: 3 additions & 3 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ macro_rules! cmsg_space {
( $( $x:ty ),* ) => {
{
let space = 0 $(+ $crate::sys::socket::cmsg_space::<$x>())*;
Vec::<u8>::with_capacity(space)
vec![0u8; space]
}
}
}
Expand Down Expand Up @@ -2081,15 +2081,15 @@ fn pack_mhdr_to_send<'a, I, C, S>(
/// # References
/// [recvmsg(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html)
pub fn recvmsg<'a, 'outer, 'inner, S>(fd: RawFd, iov: &'outer mut [IoSliceMut<'inner>],
mut cmsg_buffer: Option<&'a mut Vec<u8>>,
mut cmsg_buffer: Option<&'a mut [u8]>,
flags: MsgFlags) -> Result<RecvMsg<'a, 'outer, S>>
where S: SockaddrLike + 'a,
'inner: 'outer
{
let mut address = mem::MaybeUninit::uninit();

let (msg_control, msg_controllen) = cmsg_buffer.as_mut()
.map(|v| (v.as_mut_ptr(), v.capacity()))
.map(|v| (v.as_mut_ptr(), v.len()))
.unwrap_or((ptr::null_mut(), 0));
let mut mhdr = unsafe {
pack_mhdr_to_receive(iov.as_mut().as_mut_ptr(), iov.len(), msg_control, msg_controllen, address.as_mut_ptr())
Expand Down

0 comments on commit b91bf39

Please sign in to comment.