File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,37 @@ This project is developed and maintained by the [Cortex-M team][team].
12
12
13
13
## Example
14
14
15
- For a usage example, see ` examples/global_alloc.rs ` .
15
+ Starting with Rust 1.68, this crate can be used as a global allocator:
16
+
17
+ ```
18
+ #![no_std]
19
+ #![no_main]
20
+
21
+ extern crate alloc;
22
+
23
+ use cortex_m_rt::entry;
24
+ use embedded_alloc::Heap;
25
+
26
+ #[global_allocator]
27
+ static HEAP: Heap = Heap::empty();
28
+
29
+ #[entry]
30
+ fn main() -> ! {
31
+ // Initialize the allocator BEFORE you use it
32
+ {
33
+ use core::mem::MaybeUninit;
34
+ const HEAP_SIZE: usize = 1024;
35
+ static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
36
+ unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
37
+ }
38
+
39
+ // now the allocator is ready types like Box, Vec can be used.
40
+
41
+ loop { /* .. */ }
42
+ }
43
+ ```
44
+
45
+ For a full usage example, see ` examples/global_alloc.rs ` .
16
46
17
47
## [ Documentation] ( https://docs.rs/embedded-alloc )
18
48
You can’t perform that action at this time.
0 commit comments