Skip to content

Commit dcae426

Browse files
Merge #32
32: Build with 2018 edition, and fix the example r=korken89 a=jonas-schievink r? @therealprof Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents a4f8788 + 4b85e49 commit dcae426

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ description = "A heap allocator for Cortex-M processors"
99
repository = "https://github.com/rust-embedded/alloc-cortex-m"
1010
documentation = "https://docs.rs/alloc-cortex-m"
1111
readme = "README.md"
12+
edition = "2018"
1213

1314
keywords = [
1415
"allocator",
@@ -25,3 +26,6 @@ cortex-m = "0.1.5"
2526
[dependencies.linked_list_allocator]
2627
default-features = false
2728
version = "0.8.1"
29+
30+
[dev-dependencies]
31+
cortex-m-rt = "0.6.12"

ci/script.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
#!/usr/bin/env bash
2+
13
set -euxo pipefail
24

35
main() {
4-
cargo check --target $TARGET
6+
cargo check --target "$TARGET"
7+
8+
if [ "$TARGET" != x86_64-unknown-linux-gnu ]; then
9+
cargo build --target "$TARGET" --examples
10+
fi
511
}
612

713
main

examples/global_alloc.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![no_std]
2+
#![no_main]
3+
#![feature(alloc_error_handler)]
4+
5+
extern crate alloc;
6+
7+
use core::panic::PanicInfo;
8+
use core::alloc::Layout;
9+
use alloc::vec::Vec;
10+
use alloc_cortex_m::CortexMHeap;
11+
use cortex_m_rt::entry;
12+
13+
#[global_allocator]
14+
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
15+
16+
#[entry]
17+
fn main() -> ! {
18+
// Initialize the allocator BEFORE you use it
19+
let start = cortex_m_rt::heap_start() as usize;
20+
let size = 1024; // in bytes
21+
unsafe { ALLOCATOR.init(start, size) }
22+
23+
let mut xs = Vec::new();
24+
xs.push(1);
25+
26+
loop { /* .. */ }
27+
}
28+
29+
#[alloc_error_handler]
30+
fn oom(_: Layout) -> ! {
31+
loop {}
32+
}
33+
34+
#[panic_handler]
35+
fn panic(_: &PanicInfo) -> ! {
36+
loop {}
37+
}

src/lib.rs

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,13 @@
1-
//! A heap allocator for Cortex-M processors
1+
//! A heap allocator for Cortex-M processors.
22
//!
3-
//! # Example
4-
//!
5-
//! ```
6-
//! #![feature(alloc)]
7-
//! #![feature(global_allocator)]
8-
//! #![feature(lang_items)]
9-
//!
10-
//! // Plug in the allocator crate
11-
//! extern crate alloc;
12-
//! extern crate alloc_cortex_m;
13-
//! #[macro_use]
14-
//! extern crate cortex_m_rt as rt; // v0.5.x
15-
//!
16-
//! use alloc::Vec;
17-
//! use alloc_cortex_m::CortexMHeap;
18-
//!
19-
//! #[global_allocator]
20-
//! static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
21-
//!
22-
//! entry!(main);
23-
//!
24-
//! fn main() -> ! {
25-
//! // Initialize the allocator BEFORE you use it
26-
//! let start = rt::heap_start() as usize;
27-
//! let size = 1024; // in bytes
28-
//! unsafe { ALLOCATOR.init(start, size) }
29-
//!
30-
//! let mut xs = Vec::new();
31-
//! xs.push(1);
32-
//!
33-
//! loop { /* .. */ }
34-
//! }
35-
//!
36-
//! // required: define how Out Of Memory (OOM) conditions should be handled
37-
//! // *if* no other crate has already defined `oom`
38-
//! #[lang = "oom"]
39-
//! #[no_mangle]
40-
//! pub fn rust_oom() -> ! {
41-
//! // ..
42-
//! }
3+
//! Note that using this as your global allocator requires nightly Rust.
434
//!
5+
//! # Example
446
//!
45-
//! // omitted: exception handlers
46-
//! ```
7+
//! For a usage example, see `examples/global_alloc.rs`.
478
48-
#![feature(allocator_api)]
49-
#![feature(const_fn)]
509
#![no_std]
5110

52-
extern crate alloc;
53-
extern crate cortex_m;
54-
extern crate linked_list_allocator;
55-
5611
use core::alloc::{GlobalAlloc, Layout};
5712
use core::ptr::NonNull;
5813

0 commit comments

Comments
 (0)