File tree Expand file tree Collapse file tree 6 files changed +55
-21
lines changed Expand file tree Collapse file tree 6 files changed +55
-21
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
5
5
The format is based on [ Keep a Changelog] ( http://keepachangelog.com/ )
6
6
and this project adheres to [ Semantic Versioning] ( http://semver.org/ ) .
7
7
8
+ ## [ Unreleased]
9
+
10
+ ### Added
11
+
12
+ - Added a ` init ` macro to make initialization easier.
13
+
8
14
## [ v0.6.0] - 2024-09-01
9
15
10
16
### Added
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 @@ -63,11 +63,7 @@ fn test_allocator_api() {
63
63
64
64
#[ entry]
65
65
fn 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 ) ;
71
67
72
68
#[ allow( clippy:: type_complexity) ]
73
69
let tests: & [ ( fn ( ) -> ( ) , & ' static str ) ] = & [
Original file line number Diff line number Diff line change @@ -81,10 +81,7 @@ fn test_allocator_api() {
81
81
82
82
#[ entry]
83
83
fn 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 ) ;
88
85
89
86
#[ allow( clippy:: type_complexity) ]
90
87
let tests: & [ ( fn ( ) -> ( ) , & ' static str ) ] = & [
Original file line number Diff line number Diff line change @@ -12,3 +12,48 @@ 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
+ /// Initialize the global heap.
17
+ ///
18
+ /// This macro creates a static, uninitialized memory buffer of the specified size and
19
+ /// initializes the heap instance with that buffer.
20
+ ///
21
+ /// # Parameters
22
+ ///
23
+ /// - `$heap:ident`: The identifier of the global heap instance to initialize.
24
+ /// - `$size:expr`: An expression evaluating to a `usize` that specifies the size of the
25
+ /// static memory buffer in bytes. It must be **greater than zero**.
26
+ ///
27
+ /// # Safety
28
+ ///
29
+ /// This macro must be called first, before any operations on the heap, and **only once**.
30
+ ///
31
+ /// # Example
32
+ ///
33
+ /// ```rust
34
+ /// use cortex_m_rt::entry;
35
+ /// use embedded_alloc::LlffHeap as Heap;
36
+ ///
37
+ /// #[global_allocator]
38
+ /// static HEAP: Heap = Heap::empty();
39
+ ///
40
+ /// #[entry]
41
+ /// fn main() -> ! {
42
+ /// // Initialize the allocator BEFORE you use it
43
+ /// embedded_alloc::init!(HEAP, 1024);
44
+ /// let mut xs = Vec::new();
45
+ /// // ...
46
+ /// }
47
+ /// ```
48
+ #[ macro_export]
49
+ macro_rules! init {
50
+ ( $heap: ident, $size: expr) => {
51
+ assert!( $size > 0 ) ;
52
+ static mut HEAP_MEM : [ core:: mem:: MaybeUninit <u8 >; $size] =
53
+ [ core:: mem:: MaybeUninit :: uninit( ) ; $size] ;
54
+ #[ allow( static_mut_refs) ]
55
+ unsafe {
56
+ $heap. init( & raw mut HEAP_MEM as usize , $size)
57
+ }
58
+ } ;
59
+ }
You can’t perform that action at this time.
0 commit comments