Skip to content

Commit

Permalink
Eliminate a mem::zeroed() in recvmmsg
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers authored and Gleb Pomykalov committed Apr 26, 2020
1 parent 833369b commit 5decdd7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,16 +982,18 @@ pub fn recvmmsg<'a, I>(

let mut output: Vec<libc::mmsghdr> = Vec::with_capacity(num_messages);

// Addresses should be pre-allocated and never change the address during building
// of the input data for `recvmmsg`
let mut addresses: Vec<sockaddr_storage> = vec![unsafe { mem::zeroed() }; num_messages];
// Addresses should be pre-allocated. pack_mhdr_to_receive will store them
// as raw pointers, so we may not move them. Turn the vec into a boxed
// slice so we won't inadvertently reallocate the vec.
let mut addresses = vec![mem::MaybeUninit::uninit(); num_messages]
.into_boxed_slice();

let results: Vec<_> = iter.enumerate().map(|(i, d)| {
let (msg_controllen, mhdr) = unsafe {
pack_mhdr_to_receive(
d.iov.as_ref(),
&mut d.cmsg_buffer,
&mut addresses[i],
addresses[i].as_mut_ptr(),
)
};

Expand All @@ -1017,7 +1019,7 @@ pub fn recvmmsg<'a, I>(

Ok(output
.into_iter()
.zip(addresses.into_iter())
.zip(addresses.iter().map(|addr| unsafe{addr.assume_init()}))
.zip(results.into_iter())
.map(|((mmsghdr, address), (msg_controllen, cmsg_buffer))| {
unsafe {
Expand Down

0 comments on commit 5decdd7

Please sign in to comment.