11//! This is a utility module with helper methods for allocations/memory.
22
3- use crate :: ResultExt ;
4- use crate :: { Result , Status } ;
5- use :: alloc:: { alloc:: Global , boxed:: Box } ;
6- use core:: alloc:: { Allocator , Layout } ;
7- use core:: fmt:: Debug ;
8- use core:: ptr:: NonNull ;
9- use uefi:: data_types:: Align ;
10- use uefi:: Error ;
3+ #[ cfg( feature = "unstable_alloc" ) ]
4+ use {
5+ crate :: ResultExt ,
6+ crate :: { Result , Status } ,
7+ :: alloc:: boxed:: Box ,
8+ alloc:: alloc:: Global ,
9+ core:: alloc:: Allocator ,
10+ core:: alloc:: Layout ,
11+ core:: fmt:: Debug ,
12+ core:: ptr:: NonNull ,
13+ uefi:: data_types:: Align ,
14+ uefi:: Error ,
15+ } ;
1116
1217/// Wrapper around [`make_boxed_in`] that uses [`Global`]/the system allocator.
1318#[ allow( unused) ]
19+ #[ cfg( feature = "unstable_alloc" ) ]
1420pub fn make_boxed <
1521 ' a ,
1622 Data : Align + ?Sized + Debug + ' a ,
@@ -31,6 +37,7 @@ pub fn make_boxed<
3137/// success.
3238///
3339/// It takes a custom allocator via the `allocator_api`.
40+ #[ cfg( feature = "unstable_alloc" ) ]
3441pub fn make_boxed_in <
3542 ' a ,
3643 A : Allocator ,
@@ -71,7 +78,12 @@ pub fn make_boxed_in<
7178 let data: & mut Data = match data {
7279 Ok ( data) => data,
7380 Err ( err) => {
74- unsafe { allocator. deallocate ( heap_buf. as_non_null_ptr ( ) , layout) } ;
81+ // The deallocate function only takes the begin pointer but not a pointer to the
82+ // whole slice. To prevent unsafe slice features, we do the conversion here manually
83+ // - at the costs of some clutter code.
84+ let ptr = unsafe { & mut * heap_buf. as_ptr ( ) . cast :: < u8 > ( ) } ;
85+ let ptr = NonNull :: new ( ptr) . unwrap ( ) ;
86+ unsafe { allocator. deallocate ( ptr, layout) } ;
7587 return Err ( err) ;
7688 }
7789 } ;
0 commit comments