Skip to content

Commit

Permalink
Add allocator_api example
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Oct 29, 2023
1 parent e37e935 commit b744b41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
with:
targets: ${{ matrix.target }}
toolchain: ${{ matrix.toolchain }}
- run: cargo check --target=${{ matrix.target }} --examples
- run: cargo check --target=${{ matrix.target }} --example global_alloc
- if: ${{ matrix.toolchain == 'nightly' }}
run: cargo check --target=${{ matrix.target }} --examples --all-features

Expand Down
35 changes: 35 additions & 0 deletions examples/allocator_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![feature(allocator_api)]
#![no_std]
#![no_main]

extern crate alloc;

use alloc::vec::Vec;
use core::mem::MaybeUninit;
use core::panic::PanicInfo;
use cortex_m_rt::entry;
use embedded_alloc::Heap;

// This is not used, but as of 2023-10-29 allocator_api cannot be used without
// a global heap
#[global_allocator]
static HEAP: Heap = Heap::empty();

#[entry]
fn main() -> ! {
const HEAP_SIZE: usize = 16;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
let heap: Heap = Heap::empty();
unsafe { heap.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }

let mut xs = Vec::new_in(heap);
xs.push(1);

#[allow(clippy::empty_loop)]
loop { /* .. */ }
}

#[panic_handler]
fn panic(_: &PanicInfo) -> ! {
loop {}
}

0 comments on commit b744b41

Please sign in to comment.