11#![ feature( const_fn) ]
2- #![ feature( allocator_api) ]
2+ #![ cfg_attr ( feature = "alloc_ref" , feature ( allocator_api) ) ]
33#![ no_std]
44
55#[ cfg( test) ]
@@ -11,7 +11,9 @@ extern crate spinning_top;
1111
1212extern crate alloc;
1313
14- use alloc:: alloc:: { AllocErr , AllocRef , Layout } ;
14+ use alloc:: alloc:: Layout ;
15+ #[ cfg( feature = "alloc_ref" ) ]
16+ use alloc:: alloc:: { AllocErr , AllocRef } ;
1517use core:: alloc:: GlobalAlloc ;
1618use core:: mem;
1719#[ cfg( feature = "use_spin" ) ]
@@ -71,7 +73,7 @@ impl Heap {
7173 /// This function scans the list of free memory blocks and uses the first block that is big
7274 /// enough. The runtime is in O(n) where n is the number of free blocks, but it should be
7375 /// reasonably fast for small allocations.
74- pub fn allocate_first_fit ( & mut self , layout : Layout ) -> Result < NonNull < u8 > , AllocErr > {
76+ pub fn allocate_first_fit ( & mut self , layout : Layout ) -> Result < NonNull < u8 > , ( ) > {
7577 let mut size = layout. size ( ) ;
7678 if size < HoleList :: min_size ( ) {
7779 size = HoleList :: min_size ( ) ;
@@ -129,9 +131,13 @@ impl Heap {
129131 }
130132}
131133
134+ #[ cfg( feature = "alloc_ref" ) ]
132135unsafe impl AllocRef for Heap {
133136 unsafe fn alloc ( & mut self , layout : Layout ) -> Result < ( NonNull < u8 > , usize ) , AllocErr > {
134- Ok ( ( self . allocate_first_fit ( layout) ?, layout. size ( ) ) )
137+ match self . allocate_first_fit ( layout) {
138+ Ok ( ptr) => Ok ( ( ptr, layout. size ( ) ) ) ,
139+ Err ( ( ) ) => Err ( AllocErr ) ,
140+ }
135141 }
136142
137143 unsafe fn dealloc ( & mut self , ptr : NonNull < u8 > , layout : Layout ) {
0 commit comments