44
55use crate :: data_types:: Align ;
66use crate :: { Error , Result , ResultExt , Status } ;
7+ use :: alloc:: alloc:: { alloc, dealloc} ;
78use :: alloc:: boxed:: Box ;
89use core:: alloc:: Layout ;
910use core:: fmt:: Debug ;
1011use core:: slice;
1112
12- #[ cfg( not( feature = "unstable" ) ) ]
13- use :: alloc:: alloc:: { alloc, dealloc} ;
14-
15- #[ cfg( feature = "unstable" ) ]
16- use { core:: alloc:: Allocator , core:: ptr:: NonNull } ;
17-
1813/// Helper to return owned versions of certain UEFI data structures on the heap in a [`Box`]. This
1914/// function is intended to wrap low-level UEFI functions of this crate that
2015/// - can consume an empty buffer without a panic to get the required buffer size in the errors
@@ -23,31 +18,14 @@ use {core::alloc::Allocator, core::ptr::NonNull};
2318/// buffer size is sufficient, and
2419/// - return a mutable typed reference that points to the same memory as the input buffer on
2520/// success.
26- ///
27- /// # Feature `unstable` / `allocator_api`
28- /// By default, this function works with the allocator that is set as
29- /// `#[global_allocator]`. This might be UEFI allocator but depends on your
30- /// use case and how you set up the environment.
31- ///
32- /// If you activate the `unstable`-feature, all allocations uses the provided
33- /// allocator (via `allocator_api`) instead. In that case, the function takes an
34- /// additional parameter describing the specific [`Allocator`]. You can use
35- /// [`alloc::alloc::Global`] which defaults to the `#[global_allocator]`.
36- ///
37- /// [`Allocator`]: https://doc.rust-lang.org/alloc/alloc/trait.Allocator.html
38- /// [`alloc::alloc::Global`]: https://doc.rust-lang.org/alloc/alloc/struct.Global.html
3921pub ( crate ) fn make_boxed <
4022 ' a ,
4123 // The UEFI data structure.
4224 Data : Align + ?Sized + Debug + ' a ,
4325 F : FnMut ( & ' a mut [ u8 ] ) -> Result < & ' a mut Data , Option < usize > > ,
44- #[ cfg( feature = "unstable" ) ] A : Allocator ,
4526> (
4627 // A function to read the UEFI data structure into a provided buffer.
4728 mut fetch_data_fn : F ,
48- #[ cfg( feature = "unstable" ) ]
49- // Allocator of the `allocator_api` feature. You can use `Global` as default.
50- allocator : A ,
5129) -> Result < Box < Data > > {
5230 let required_size = match fetch_data_fn ( & mut [ ] ) . map_err ( Error :: split) {
5331 // This is the expected case: the empty buffer passed in is too
@@ -70,21 +48,13 @@ pub(crate) fn make_boxed<
7048
7149 // Allocate the buffer on the heap.
7250 let heap_buf: * mut u8 = {
73- #[ cfg( not( feature = "unstable" ) ) ]
7451 {
7552 let ptr = unsafe { alloc ( layout) } ;
7653 if ptr. is_null ( ) {
7754 return Err ( Status :: OUT_OF_RESOURCES . into ( ) ) ;
7855 }
7956 ptr
8057 }
81-
82- #[ cfg( feature = "unstable" ) ]
83- allocator
84- . allocate ( layout)
85- . map_err ( |_| <Status as Into < Error > >:: into ( Status :: OUT_OF_RESOURCES ) ) ?
86- . as_ptr ( )
87- . cast :: < u8 > ( )
8858 } ;
8959
9060 // Read the data into the provided buffer.
@@ -97,29 +67,19 @@ pub(crate) fn make_boxed<
9767 let data: & mut Data = match data {
9868 Ok ( data) => data,
9969 Err ( err) => {
100- #[ cfg( not( feature = "unstable" ) ) ]
101- unsafe {
102- dealloc ( heap_buf, layout)
103- } ;
104- #[ cfg( feature = "unstable" ) ]
105- unsafe {
106- allocator. deallocate ( NonNull :: new ( heap_buf) . unwrap ( ) , layout)
107- }
70+ unsafe { dealloc ( heap_buf, layout) } ;
10871 return Err ( err) ;
10972 }
11073 } ;
11174
11275 let data = unsafe { Box :: from_raw ( data) } ;
113-
11476 Ok ( data)
11577}
11678
11779#[ cfg( test) ]
11880mod tests {
11981 use super :: * ;
12082 use crate :: { ResultExt , StatusExt } ;
121- #[ cfg( feature = "unstable" ) ]
122- use alloc:: alloc:: Global ;
12383
12484 /// Some simple dummy type to test [`make_boxed`].
12585 #[ derive( Debug ) ]
@@ -212,27 +172,20 @@ mod tests {
212172 assert_eq ! ( & data. 0.0 , & [ 1 , 2 , 3 , 4 ] ) ;
213173 }
214174
215- /// This unit tests checks the [`make_boxed`] utility. The test has different code and behavior
216- /// depending on whether the "unstable" feature is active or not.
175+ /// This unit tests checks the [`make_boxed`] utility.
176+ ///
177+ /// This test is especially useful when run by miri.
217178 #[ test]
218179 fn test_make_boxed_utility ( ) {
219180 let fetch_data_fn = |buf| uefi_function_stub_read ( buf) ;
220181
221- #[ cfg( not( feature = "unstable" ) ) ]
222182 let data: Box < SomeData > = make_boxed ( fetch_data_fn) . unwrap ( ) ;
223-
224- #[ cfg( feature = "unstable" ) ]
225- let data: Box < SomeData > = make_boxed ( fetch_data_fn, Global ) . unwrap ( ) ;
226183 assert_eq ! ( & data. 0 , & [ 1 , 2 , 3 , 4 ] ) ;
227184
228185 let fetch_data_fn = |buf| uefi_function_stub_read ( buf) ;
229186
230- #[ cfg( not( feature = "unstable" ) ) ]
231187 let data: Box < SomeDataAlign16 > = make_boxed ( fetch_data_fn) . unwrap ( ) ;
232188
233- #[ cfg( feature = "unstable" ) ]
234- let data: Box < SomeDataAlign16 > = make_boxed ( fetch_data_fn, Global ) . unwrap ( ) ;
235-
236189 assert_eq ! ( & data. 0.0 , & [ 1 , 2 , 3 , 4 ] ) ;
237190 }
238191}
0 commit comments