diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff2e225..69a3600 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/examples/allocator_api.rs b/examples/allocator_api.rs new file mode 100644 index 0000000..95ab702 --- /dev/null +++ b/examples/allocator_api.rs @@ -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; 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 {} +}