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 committed Apr 25, 2020
1 parent cbeb0d2 commit dc331e2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/sys/socket/mod.rs
Expand Up @@ -941,16 +941,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 @@ -976,7 +978,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 dc331e2

Please sign in to comment.