Skip to content

Commit 89cb8d5

Browse files
bors[bot]Dirbaio
andauthored
Merge #56
56: Use critical-section for heap locking, rename to `embedded-alloc`. r=adamgreig a=Dirbaio This uses `critical_section::with` instead of `cortex_m::interrupt::free` to acquire a critical section. This allows customizing the critical section implementation, to make it sound for multicore chips for example. This is a breaking change, so it'll require a 0.5 release. Interestingly this makes the crate not cortex-m specific anymore. Perhaps it could be renamed to something more general? TODO - [x] Wait for cortex-m 0.7.6 release rust-embedded/cortex-m#449 Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents e805a4a + a807592 commit 89cb8d5

File tree

5 files changed

+66
-53
lines changed

5 files changed

+66
-53
lines changed

CHANGELOG.md

+25-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
## [v0.5.0] - 2022-12-06
11+
12+
### Changed
13+
14+
- Renamed crate from `alloc-cortex-m` to `embedded-alloc`.
15+
- Renamed `CortexMHeap` to `Heap`.
16+
- Use `critical-section` to lock the heap, instead of `cortex_m::interrupt::free()`.
17+
This allows using this crate on non-Cortex-M systems, or on
18+
Cortex-M systems that require a custom critical section implementation.
19+
1020
## [v0.4.3] - 2022-11-03
1121

1222
### Changed
@@ -96,17 +106,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
96106

97107
- Initial version of the allocator
98108

99-
[Unreleased]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.3...HEAD
100-
[v0.4.3]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.2...v0.4.3
101-
[v0.4.2]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.1...v0.4.2
102-
[v0.4.1]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.0...v0.4.1
103-
[v0.4.0]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.3.5...v0.4.0
104-
[v0.3.5]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.3.4...v0.3.5
105-
[v0.3.4]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.3.3...v0.3.4
106-
[v0.3.3]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.3.2...v0.3.3
107-
[v0.3.2]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.3.1...v0.3.2
108-
[v0.3.1]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.3.0...v0.3.1
109-
[v0.3.0]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.2.2...v0.3.0
110-
[v0.2.2]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.2.1...v0.2.2
111-
[v0.2.1]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.2.0...v0.2.1
112-
[v0.2.0]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.1.0...v0.2.0
109+
[Unreleased]: https://github.com/rust-embedded/embedded-alloc/compare/v0.5.0...HEAD
110+
[v0.5.0]: https://github.com/rust-embedded/embedded-alloc/compare/v0.4.3...v0.5.0
111+
[v0.4.3]: https://github.com/rust-embedded/embedded-alloc/compare/v0.4.2...v0.4.3
112+
[v0.4.2]: https://github.com/rust-embedded/embedded-alloc/compare/v0.4.1...v0.4.2
113+
[v0.4.1]: https://github.com/rust-embedded/embedded-alloc/compare/v0.4.0...v0.4.1
114+
[v0.4.0]: https://github.com/rust-embedded/embedded-alloc/compare/v0.3.5...v0.4.0
115+
[v0.3.5]: https://github.com/rust-embedded/embedded-alloc/compare/v0.3.4...v0.3.5
116+
[v0.3.4]: https://github.com/rust-embedded/embedded-alloc/compare/v0.3.3...v0.3.4
117+
[v0.3.3]: https://github.com/rust-embedded/embedded-alloc/compare/v0.3.2...v0.3.3
118+
[v0.3.2]: https://github.com/rust-embedded/embedded-alloc/compare/v0.3.1...v0.3.2
119+
[v0.3.1]: https://github.com/rust-embedded/embedded-alloc/compare/v0.3.0...v0.3.1
120+
[v0.3.0]: https://github.com/rust-embedded/embedded-alloc/compare/v0.2.2...v0.3.0
121+
[v0.2.2]: https://github.com/rust-embedded/embedded-alloc/compare/v0.2.1...v0.2.2
122+
[v0.2.1]: https://github.com/rust-embedded/embedded-alloc/compare/v0.2.0...v0.2.1
123+
[v0.2.0]: https://github.com/rust-embedded/embedded-alloc/compare/v0.1.0...v0.2.0

Cargo.toml

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,31 @@ authors = [
66
"Sébastien Béchet <[email protected]>",
77
]
88

9-
description = "A heap allocator for Cortex-M processors"
10-
repository = "https://github.com/rust-embedded/alloc-cortex-m"
11-
documentation = "https://docs.rs/alloc-cortex-m"
9+
description = "A heap allocator for embedded systems"
10+
repository = "https://github.com/rust-embedded/embedded-alloc"
11+
documentation = "https://docs.rs/embedded-alloc"
1212
readme = "README.md"
1313
edition = "2018"
1414

1515
keywords = [
1616
"allocator",
17+
"embedded",
1718
"arm",
19+
"riscv",
1820
"cortex-m",
1921
]
2022
license = "MIT OR Apache-2.0"
21-
name = "alloc-cortex-m"
22-
version = "0.4.3"
23+
name = "embedded-alloc"
24+
version = "0.5.0"
2325

2426
[dependencies]
25-
cortex-m = "0.7.2"
27+
critical-section = "1.0"
2628

2729
[dependencies.linked_list_allocator]
2830
default-features = false
2931
version = "0.10.4"
3032
features = ["const_mut_refs"]
3133

3234
[dev-dependencies]
33-
cortex-m-rt = "0.6.12"
35+
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
36+
cortex-m-rt = "0.7"

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
[![crates.io](https://img.shields.io/crates/d/alloc-cortex-m.svg)](https://crates.io/crates/alloc-cortex-m)
2-
[![crates.io](https://img.shields.io/crates/v/alloc-cortex-m.svg)](https://crates.io/crates/alloc-cortex-m)
1+
[![crates.io](https://img.shields.io/crates/d/embedded-alloc.svg)](https://crates.io/crates/embedded-alloc)
2+
[![crates.io](https://img.shields.io/crates/v/embedded-alloc.svg)](https://crates.io/crates/embedded-alloc)
33

4-
# `alloc-cortex-m`
4+
# `embedded-alloc`
55

6-
> A heap allocator for Cortex-M processors
6+
> A heap allocator for embedded systems.
7+
8+
Note that using this as your global allocator requires nightly Rust.
79

810
This project is developed and maintained by the [Cortex-M team][team].
911

10-
## [Documentation](https://docs.rs/alloc-cortex-m)
12+
## Example
13+
14+
For a usage example, see `examples/global_alloc.rs`.
15+
16+
## [Documentation](https://docs.rs/embedded-alloc)
1117

1218
## [Change log](CHANGELOG.md)
1319

examples/global_alloc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
extern crate alloc;
66

77
use alloc::vec::Vec;
8-
use alloc_cortex_m::CortexMHeap;
98
use core::alloc::Layout;
109
use core::panic::PanicInfo;
1110
use cortex_m_rt::entry;
11+
use embedded_alloc::Heap;
1212

1313
#[global_allocator]
14-
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
14+
static HEAP: Heap = Heap::empty();
1515

1616
#[entry]
1717
fn main() -> ! {
1818
// Initialize the allocator BEFORE you use it
1919
{
2020
use core::mem::MaybeUninit;
2121
const HEAP_SIZE: usize = 1024;
22-
static mut HEAP: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
23-
unsafe { ALLOCATOR.init(HEAP.as_ptr() as usize, HEAP_SIZE) }
22+
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
23+
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
2424
}
2525

2626
let mut xs = Vec::new();

src/lib.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
1-
//! A heap allocator for Cortex-M processors.
2-
//!
3-
//! Note that using this as your global allocator requires nightly Rust.
4-
//!
5-
//! # Example
6-
//!
7-
//! For a usage example, see `examples/global_alloc.rs`.
8-
1+
#![doc = include_str!("../README.md")]
92
#![no_std]
103

114
use core::alloc::{GlobalAlloc, Layout};
125
use core::cell::RefCell;
136
use core::ptr::{self, NonNull};
147

15-
use cortex_m::interrupt::Mutex;
16-
use linked_list_allocator::Heap;
8+
use critical_section::Mutex;
9+
use linked_list_allocator::Heap as LLHeap;
1710

18-
pub struct CortexMHeap {
19-
heap: Mutex<RefCell<Heap>>,
11+
pub struct Heap {
12+
heap: Mutex<RefCell<LLHeap>>,
2013
}
2114

22-
impl CortexMHeap {
15+
impl Heap {
2316
/// Crate a new UNINITIALIZED heap allocator
2417
///
2518
/// You must initialize this heap using the
26-
/// [`init`](struct.CortexMHeap.html#method.init) method before using the allocator.
27-
pub const fn empty() -> CortexMHeap {
28-
CortexMHeap {
29-
heap: Mutex::new(RefCell::new(Heap::empty())),
19+
/// [`init`](Self::init) method before using the allocator.
20+
pub const fn empty() -> Heap {
21+
Heap {
22+
heap: Mutex::new(RefCell::new(LLHeap::empty())),
3023
}
3124
}
3225

@@ -54,7 +47,7 @@ impl CortexMHeap {
5447
/// - This function must be called exactly ONCE.
5548
/// - `size > 0`
5649
pub unsafe fn init(&self, start_addr: usize, size: usize) {
57-
cortex_m::interrupt::free(|cs| {
50+
critical_section::with(|cs| {
5851
self.heap
5952
.borrow(cs)
6053
.borrow_mut()
@@ -64,18 +57,18 @@ impl CortexMHeap {
6457

6558
/// Returns an estimate of the amount of bytes in use.
6659
pub fn used(&self) -> usize {
67-
cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().used())
60+
critical_section::with(|cs| self.heap.borrow(cs).borrow_mut().used())
6861
}
6962

7063
/// Returns an estimate of the amount of bytes available.
7164
pub fn free(&self) -> usize {
72-
cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().free())
65+
critical_section::with(|cs| self.heap.borrow(cs).borrow_mut().free())
7366
}
7467
}
7568

76-
unsafe impl GlobalAlloc for CortexMHeap {
69+
unsafe impl GlobalAlloc for Heap {
7770
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
78-
cortex_m::interrupt::free(|cs| {
71+
critical_section::with(|cs| {
7972
self.heap
8073
.borrow(cs)
8174
.borrow_mut()
@@ -86,7 +79,7 @@ unsafe impl GlobalAlloc for CortexMHeap {
8679
}
8780

8881
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
89-
cortex_m::interrupt::free(|cs| {
82+
critical_section::with(|cs| {
9083
self.heap
9184
.borrow(cs)
9285
.borrow_mut()

0 commit comments

Comments
 (0)