Skip to content

Add allocator_api example #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {}
}