@@ -66,18 +66,17 @@ impl Heap {
6666 pub fn free ( & self ) -> usize {
6767 critical_section:: with ( |cs| self . heap . borrow ( cs) . borrow_mut ( ) . free ( ) )
6868 }
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+ }
6973}
7074
7175unsafe impl GlobalAlloc for Heap {
7276 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 ( ) )
8180 }
8281
8382 unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
@@ -92,36 +91,23 @@ unsafe impl GlobalAlloc for Heap {
9291
9392#[ cfg( feature = "allocator_api" ) ]
9493mod 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 ;
10196
102- unsafe impl Allocator for Heap {
97+ unsafe impl Allocator for crate :: Heap {
10398 fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
10499 match layout. size ( ) {
105100 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 ) ,
114105 }
115106 }
116107
117108 unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) {
118109 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) ;
125111 }
126112 }
127113 }
0 commit comments