Skip to content

Commit 5decdd7

Browse files
asomersGleb Pomykalov
authored andcommitted
Eliminate a mem::zeroed() in recvmmsg
1 parent 833369b commit 5decdd7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/sys/socket/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,16 +982,18 @@ pub fn recvmmsg<'a, I>(
982982

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

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

989991
let results: Vec<_> = iter.enumerate().map(|(i, d)| {
990992
let (msg_controllen, mhdr) = unsafe {
991993
pack_mhdr_to_receive(
992994
d.iov.as_ref(),
993995
&mut d.cmsg_buffer,
994-
&mut addresses[i],
996+
addresses[i].as_mut_ptr(),
995997
)
996998
};
997999

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

10181020
Ok(output
10191021
.into_iter()
1020-
.zip(addresses.into_iter())
1022+
.zip(addresses.iter().map(|addr| unsafe{addr.assume_init()}))
10211023
.zip(results.into_iter())
10221024
.map(|((mmsghdr, address), (msg_controllen, cmsg_buffer))| {
10231025
unsafe {

0 commit comments

Comments
 (0)