4
4
5
5
use crate :: data_types:: Align ;
6
6
use crate :: { Error , Result , ResultExt , Status } ;
7
+ use :: alloc:: alloc:: { alloc, dealloc} ;
7
8
use :: alloc:: boxed:: Box ;
8
9
use core:: alloc:: Layout ;
9
10
use core:: fmt:: Debug ;
10
11
use core:: slice;
11
12
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
-
18
13
/// Helper to return owned versions of certain UEFI data structures on the heap in a [`Box`]. This
19
14
/// function is intended to wrap low-level UEFI functions of this crate that
20
15
/// - 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};
23
18
/// buffer size is sufficient, and
24
19
/// - return a mutable typed reference that points to the same memory as the input buffer on
25
20
/// 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
39
21
pub ( crate ) fn make_boxed <
40
22
' a ,
41
23
// The UEFI data structure.
42
24
Data : Align + ?Sized + Debug + ' a ,
43
25
F : FnMut ( & ' a mut [ u8 ] ) -> Result < & ' a mut Data , Option < usize > > ,
44
- #[ cfg( feature = "unstable" ) ] A : Allocator ,
45
26
> (
46
27
// A function to read the UEFI data structure into a provided buffer.
47
28
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 ,
51
29
) -> Result < Box < Data > > {
52
30
let required_size = match fetch_data_fn ( & mut [ ] ) . map_err ( Error :: split) {
53
31
// This is the expected case: the empty buffer passed in is too
@@ -70,21 +48,13 @@ pub(crate) fn make_boxed<
70
48
71
49
// Allocate the buffer on the heap.
72
50
let heap_buf: * mut u8 = {
73
- #[ cfg( not( feature = "unstable" ) ) ]
74
51
{
75
52
let ptr = unsafe { alloc ( layout) } ;
76
53
if ptr. is_null ( ) {
77
54
return Err ( Status :: OUT_OF_RESOURCES . into ( ) ) ;
78
55
}
79
56
ptr
80
57
}
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 > ( )
88
58
} ;
89
59
90
60
// Read the data into the provided buffer.
@@ -97,29 +67,19 @@ pub(crate) fn make_boxed<
97
67
let data: & mut Data = match data {
98
68
Ok ( data) => data,
99
69
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) } ;
108
71
return Err ( err) ;
109
72
}
110
73
} ;
111
74
112
75
let data = unsafe { Box :: from_raw ( data) } ;
113
-
114
76
Ok ( data)
115
77
}
116
78
117
79
#[ cfg( test) ]
118
80
mod tests {
119
81
use super :: * ;
120
82
use crate :: { ResultExt , StatusExt } ;
121
- #[ cfg( feature = "unstable" ) ]
122
- use alloc:: alloc:: Global ;
123
83
124
84
/// Some simple dummy type to test [`make_boxed`].
125
85
#[ derive( Debug ) ]
@@ -212,27 +172,20 @@ mod tests {
212
172
assert_eq ! ( & data. 0.0 , & [ 1 , 2 , 3 , 4 ] ) ;
213
173
}
214
174
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.
217
178
#[ test]
218
179
fn test_make_boxed_utility ( ) {
219
180
let fetch_data_fn = |buf| uefi_function_stub_read ( buf) ;
220
181
221
- #[ cfg( not( feature = "unstable" ) ) ]
222
182
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 ( ) ;
226
183
assert_eq ! ( & data. 0 , & [ 1 , 2 , 3 , 4 ] ) ;
227
184
228
185
let fetch_data_fn = |buf| uefi_function_stub_read ( buf) ;
229
186
230
- #[ cfg( not( feature = "unstable" ) ) ]
231
187
let data: Box < SomeDataAlign16 > = make_boxed ( fetch_data_fn) . unwrap ( ) ;
232
188
233
- #[ cfg( feature = "unstable" ) ]
234
- let data: Box < SomeDataAlign16 > = make_boxed ( fetch_data_fn, Global ) . unwrap ( ) ;
235
-
236
189
assert_eq ! ( & data. 0.0 , & [ 1 , 2 , 3 , 4 ] ) ;
237
190
}
238
191
}
0 commit comments