File tree Expand file tree Collapse file tree 3 files changed +16
-12
lines changed Expand file tree Collapse file tree 3 files changed +16
-12
lines changed Original file line number Diff line number Diff line change @@ -31,12 +31,7 @@ static HEAP: Heap = Heap::empty();
31
31
#[entry]
32
32
fn main () -> ! {
33
33
// Initialize the allocator BEFORE you use it
34
- {
35
- use core :: mem :: MaybeUninit ;
36
- const HEAP_SIZE : usize = 1024 ;
37
- static mut HEAP_MEM : [MaybeUninit <u8 >; HEAP_SIZE ] = [MaybeUninit :: uninit (); HEAP_SIZE ];
38
- unsafe { HEAP . init (& raw mut HEAP_MEM as usize , HEAP_SIZE ) }
39
- }
34
+ embedded_alloc :: init! (HEAP , 1024 );
40
35
41
36
// now the allocator is ready types like Box, Vec can be used.
42
37
Original file line number Diff line number Diff line change @@ -17,12 +17,7 @@ static HEAP: Heap = Heap::empty();
17
17
#[ entry]
18
18
fn main ( ) -> ! {
19
19
// Initialize the allocator BEFORE you use it
20
- {
21
- use core:: mem:: MaybeUninit ;
22
- const HEAP_SIZE : usize = 1024 ;
23
- static mut HEAP_MEM : [ MaybeUninit < u8 > ; HEAP_SIZE ] = [ MaybeUninit :: uninit ( ) ; HEAP_SIZE ] ;
24
- unsafe { HEAP . init ( & raw mut HEAP_MEM as usize , HEAP_SIZE ) }
25
- }
20
+ embedded_alloc:: init!( HEAP , 1024 ) ;
26
21
27
22
let mut xs = Vec :: new ( ) ;
28
23
xs. push ( 1 ) ;
Original file line number Diff line number Diff line change @@ -12,3 +12,17 @@ mod tlsf;
12
12
pub use llff:: Heap as LlffHeap ;
13
13
#[ cfg( feature = "tlsf" ) ]
14
14
pub use tlsf:: Heap as TlsfHeap ;
15
+
16
+ /// Init a heap with a static memory region.
17
+ #[ macro_export]
18
+ macro_rules! init {
19
+ ( $heap: ident, $size: expr) => {
20
+ use core:: mem:: MaybeUninit ;
21
+ const HEAP_SIZE : usize = $size;
22
+ static mut HEAP_MEM : [ MaybeUninit <u8 >; HEAP_SIZE ] = [ MaybeUninit :: uninit( ) ; HEAP_SIZE ] ;
23
+ #[ allow( static_mut_refs) ]
24
+ unsafe {
25
+ $heap. init( & raw mut HEAP_MEM as usize , HEAP_SIZE )
26
+ }
27
+ } ;
28
+ }
You can’t perform that action at this time.
0 commit comments