1
1
#![ feature( const_fn) ]
2
- #![ feature( allocator_api) ]
2
+ #![ cfg_attr ( feature = "alloc_ref" , feature ( allocator_api) ) ]
3
3
#![ no_std]
4
4
5
5
#[ cfg( test) ]
@@ -11,7 +11,9 @@ extern crate spinning_top;
11
11
12
12
extern crate alloc;
13
13
14
- use alloc:: alloc:: { AllocErr , AllocRef , Layout } ;
14
+ use alloc:: alloc:: Layout ;
15
+ #[ cfg( feature = "alloc_ref" ) ]
16
+ use alloc:: alloc:: { AllocErr , AllocRef } ;
15
17
use core:: alloc:: GlobalAlloc ;
16
18
use core:: mem;
17
19
#[ cfg( feature = "use_spin" ) ]
@@ -71,7 +73,7 @@ impl Heap {
71
73
/// This function scans the list of free memory blocks and uses the first block that is big
72
74
/// enough. The runtime is in O(n) where n is the number of free blocks, but it should be
73
75
/// 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 > , ( ) > {
75
77
let mut size = layout. size ( ) ;
76
78
if size < HoleList :: min_size ( ) {
77
79
size = HoleList :: min_size ( ) ;
@@ -129,9 +131,13 @@ impl Heap {
129
131
}
130
132
}
131
133
134
+ #[ cfg( feature = "alloc_ref" ) ]
132
135
unsafe impl AllocRef for Heap {
133
136
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
+ }
135
141
}
136
142
137
143
unsafe fn dealloc ( & mut self , ptr : NonNull < u8 > , layout : Layout ) {
0 commit comments