Skip to content

Commit c00bb56

Browse files
necauquanicholasbishop
authored andcommitted
Improve Handle buffer handling code
1 parent b7e2fff commit c00bb56

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
2626
#![cfg_attr(
2727
feature = "exts",
28-
feature(allocator_api, alloc_layout_extra, vec_into_raw_parts)
28+
feature(allocator_api, alloc_layout_extra, vec_spare_capacity)
2929
)]
3030
#![feature(auto_traits)]
3131
#![feature(control_flow_enum)]

src/table/boot.rs

+9-23
Original file line numberDiff line numberDiff line change
@@ -744,31 +744,17 @@ impl BootServices {
744744
// Determine how much we need to allocate.
745745
let (status1, buffer_size) = self.locate_handle(search_type, None)?.split();
746746

747-
// Allocate a large enough buffer.
748-
let mut buffer = Vec::new();
749-
buffer.resize_with(buffer_size, MaybeUninit::uninit);
747+
// Allocate a large enough buffer without pointless initialization.
748+
let mut handles = Vec::with_capacity(buffer_size);
749+
let buffer = handles.spare_capacity_mut();
750750

751751
// Perform the search.
752-
let (status2, buffer_size) = self.locate_handle(search_type, Some(&mut buffer))?.split();
753-
754-
// Ensure that the buffer's length matches the number of handles
755-
// that were actually filled in. Calling `truncate` only has an
756-
// effect if the new length is smaller than the vec's currently
757-
// length, and that is sufficient here since if `buffer` is
758-
// smaller than the amount of data `locate_handle` wants to
759-
// return then `find_handles` will end up returning
760-
// `Status::BUFFER_TOO_SMALL` and `buffer` will be dropped.
761-
buffer.truncate(buffer_size);
762-
763-
// Convert the buffer from MaybeUninits to Handles.
764-
// The raw parts roundtrip with a pointer cast is better than just
765-
// transmuting vectors per mem::transmute docs.
766-
// Transmuting MaybeUninit<T> to T is also correct, if we are sure that
767-
// it is initialized, which it is, unless UEFI is broken
768-
let handles = unsafe {
769-
let (ptr, len, cap) = buffer.into_raw_parts();
770-
Vec::from_raw_parts(ptr as *mut Handle, len, cap)
771-
};
752+
let (status2, buffer_size) = self.locate_handle(search_type, Some(buffer))?.split();
753+
754+
// Mark the returned number of elements as initialized.
755+
unsafe {
756+
handles.set_len(buffer_size);
757+
}
772758

773759
// Emit output, with warnings
774760
status1

0 commit comments

Comments
 (0)