File tree Expand file tree Collapse file tree 5 files changed +17
-21
lines changed Expand file tree Collapse file tree 5 files changed +17
-21
lines changed Original file line number Diff line number Diff line change @@ -31,12 +31,7 @@ static HEAP: Heap = Heap::empty();
3131#[entry]
3232fn main () -> ! {
3333 // 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 );
4035
4136 // now the allocator is ready types like Box, Vec can be used.
4237
Original file line number Diff line number Diff line change @@ -17,12 +17,7 @@ static HEAP: Heap = Heap::empty();
1717#[ entry]
1818fn main ( ) -> ! {
1919 // 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 ) ;
2621
2722 let mut xs = Vec :: new ( ) ;
2823 xs. push ( 1 ) ;
Original file line number Diff line number Diff line change @@ -63,11 +63,7 @@ fn test_allocator_api() {
6363
6464#[ entry]
6565fn main ( ) -> ! {
66- {
67- const HEAP_SIZE : usize = 1024 ;
68- static mut HEAP_MEM : [ MaybeUninit < u8 > ; HEAP_SIZE ] = [ MaybeUninit :: uninit ( ) ; HEAP_SIZE ] ;
69- unsafe { HEAP . init ( & raw mut HEAP_MEM as usize , HEAP_SIZE ) }
70- }
66+ embedded_alloc:: init!( HEAP , 1024 ) ;
7167
7268 #[ allow( clippy:: type_complexity) ]
7369 let tests: & [ ( fn ( ) -> ( ) , & ' static str ) ] = & [
Original file line number Diff line number Diff line change @@ -81,10 +81,7 @@ fn test_allocator_api() {
8181
8282#[ entry]
8383fn main ( ) -> ! {
84- {
85- static mut HEAP_MEM : [ MaybeUninit < u8 > ; HEAP_SIZE ] = [ MaybeUninit :: uninit ( ) ; HEAP_SIZE ] ;
86- unsafe { HEAP . init ( & raw mut HEAP_MEM as usize , HEAP_SIZE ) }
87- }
84+ embedded_alloc:: init!( HEAP , HEAP_SIZE ) ;
8885
8986 #[ allow( clippy:: type_complexity) ]
9087 let tests: & [ ( fn ( ) -> ( ) , & ' static str ) ] = & [
Original file line number Diff line number Diff line change @@ -12,3 +12,16 @@ mod tlsf;
1212pub use llff:: Heap as LlffHeap ;
1313#[ cfg( feature = "tlsf" ) ]
1414pub 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+ static mut HEAP_MEM : [ core:: mem:: MaybeUninit <u8 >; $size] =
21+ [ core:: mem:: MaybeUninit :: uninit( ) ; $size] ;
22+ #[ allow( static_mut_refs) ]
23+ unsafe {
24+ $heap. init( & raw mut HEAP_MEM as usize , $size)
25+ }
26+ } ;
27+ }
You can’t perform that action at this time.
0 commit comments