Skip to content

Commit b744b41

Browse files
committed
Add allocator_api example
1 parent e37e935 commit b744b41

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
with:
2525
targets: ${{ matrix.target }}
2626
toolchain: ${{ matrix.toolchain }}
27-
- run: cargo check --target=${{ matrix.target }} --examples
27+
- run: cargo check --target=${{ matrix.target }} --example global_alloc
2828
- if: ${{ matrix.toolchain == 'nightly' }}
2929
run: cargo check --target=${{ matrix.target }} --examples --all-features
3030

examples/allocator_api.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(allocator_api)]
2+
#![no_std]
3+
#![no_main]
4+
5+
extern crate alloc;
6+
7+
use alloc::vec::Vec;
8+
use core::mem::MaybeUninit;
9+
use core::panic::PanicInfo;
10+
use cortex_m_rt::entry;
11+
use embedded_alloc::Heap;
12+
13+
// This is not used, but as of 2023-10-29 allocator_api cannot be used without
14+
// a global heap
15+
#[global_allocator]
16+
static HEAP: Heap = Heap::empty();
17+
18+
#[entry]
19+
fn main() -> ! {
20+
const HEAP_SIZE: usize = 16;
21+
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
22+
let heap: Heap = Heap::empty();
23+
unsafe { heap.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
24+
25+
let mut xs = Vec::new_in(heap);
26+
xs.push(1);
27+
28+
#[allow(clippy::empty_loop)]
29+
loop { /* .. */ }
30+
}
31+
32+
#[panic_handler]
33+
fn panic(_: &PanicInfo) -> ! {
34+
loop {}
35+
}

0 commit comments

Comments
 (0)