@@ -13,11 +13,8 @@ compile_error!("Missing kernel configuration for conditional compilation");
13
13
extern crate alloc;
14
14
15
15
use alloc:: boxed:: Box ;
16
- use core:: alloc:: Layout ;
17
- use core:: mem:: MaybeUninit ;
18
16
use core:: panic:: PanicInfo ;
19
17
use core:: pin:: Pin ;
20
- use core:: ptr:: NonNull ;
21
18
22
19
mod allocator;
23
20
pub mod bindings;
@@ -66,25 +63,7 @@ static ALLOCATOR: allocator::KernelAllocator = allocator::KernelAllocator;
66
63
/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is
67
64
/// moved into it and returned to the caller wrapped in a `Box`.
68
65
pub fn try_alloc < T > ( value : T ) -> KernelResult < Box < T > > {
69
- let layout = Layout :: new :: < MaybeUninit < T > > ( ) ;
70
- let ptr: NonNull < MaybeUninit < T > > = if layout. size ( ) == 0 {
71
- NonNull :: dangling ( )
72
- // SAFETY: We checked that the layout size is nonzero.
73
- } else if let Some ( nn) = NonNull :: new ( unsafe { alloc:: alloc:: alloc ( layout) } ) {
74
- nn. cast ( )
75
- } else {
76
- return Err ( Error :: ENOMEM ) ;
77
- } ;
78
-
79
- unsafe {
80
- // SAFETY: `ptr` was just allocated and isn't used afterwards.
81
- let mut b = Box :: from_raw ( ptr. as_ptr ( ) ) ;
82
- // SAFETY: The pointer is valid for write and is properly aligned. The dangling pointer
83
- // case is only when the size of the value is zero; writing zero bytes to it is allowed.
84
- b. as_mut_ptr ( ) . write ( value) ;
85
- // SAFETY: The value was initialised in the call above.
86
- Ok ( Box :: from_raw ( Box :: into_raw ( b) as * mut T ) )
87
- }
66
+ Box :: try_new ( value) . map_err ( |_| Error :: ENOMEM )
88
67
}
89
68
90
69
/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is
0 commit comments