@@ -66,18 +66,17 @@ impl Heap {
66
66
pub fn free ( & self ) -> usize {
67
67
critical_section:: with ( |cs| self . heap . borrow ( cs) . borrow_mut ( ) . free ( ) )
68
68
}
69
+
70
+ fn alloc_first_fit ( & self , layout : Layout ) -> Result < NonNull < u8 > , ( ) > {
71
+ critical_section:: with ( |cs| self . heap . borrow ( cs) . borrow_mut ( ) . allocate_first_fit ( layout) )
72
+ }
69
73
}
70
74
71
75
unsafe impl GlobalAlloc for Heap {
72
76
unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
73
- critical_section:: with ( |cs| {
74
- self . heap
75
- . borrow ( cs)
76
- . borrow_mut ( )
77
- . allocate_first_fit ( layout)
78
- . ok ( )
79
- . map_or ( ptr:: null_mut ( ) , |allocation| allocation. as_ptr ( ) )
80
- } )
77
+ self . alloc_first_fit ( layout)
78
+ . ok ( )
79
+ . map_or ( ptr:: null_mut ( ) , |allocation| allocation. as_ptr ( ) )
81
80
}
82
81
83
82
unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
@@ -92,36 +91,23 @@ unsafe impl GlobalAlloc for Heap {
92
91
93
92
#[ cfg( feature = "allocator_api" ) ]
94
93
mod allocator_api {
95
- use core:: {
96
- alloc:: { AllocError , Allocator , Layout } ,
97
- ptr:: NonNull ,
98
- } ;
99
-
100
- use crate :: Heap ;
94
+ use core:: alloc:: { AllocError , Allocator , GlobalAlloc , Layout } ;
95
+ use core:: ptr:: NonNull ;
101
96
102
- unsafe impl Allocator for Heap {
97
+ unsafe impl Allocator for crate :: Heap {
103
98
fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
104
99
match layout. size ( ) {
105
100
0 => Ok ( NonNull :: slice_from_raw_parts ( layout. dangling ( ) , 0 ) ) ,
106
- size => critical_section:: with ( |cs| {
107
- self . heap
108
- . borrow ( cs)
109
- . borrow_mut ( )
110
- . allocate_first_fit ( layout)
111
- . map ( |allocation| NonNull :: slice_from_raw_parts ( allocation, size) )
112
- . map_err ( |_| AllocError )
113
- } ) ,
101
+ size => self
102
+ . alloc_first_fit ( layout)
103
+ . map ( |allocation| NonNull :: slice_from_raw_parts ( allocation, size) )
104
+ . map_err ( |_| AllocError ) ,
114
105
}
115
106
}
116
107
117
108
unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) {
118
109
if layout. size ( ) != 0 {
119
- critical_section:: with ( |cs| {
120
- self . heap
121
- . borrow ( cs)
122
- . borrow_mut ( )
123
- . deallocate ( NonNull :: new_unchecked ( ptr. as_ptr ( ) ) , layout)
124
- } ) ;
110
+ self . dealloc ( ptr. as_ptr ( ) , layout) ;
125
111
}
126
112
}
127
113
}
0 commit comments