Skip to content

Commit

Permalink
Rename to embedded-alloc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Dec 6, 2022
1 parent 2444b77 commit 0af58b7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [v0.5.0] - 2022-12-06

### Changed

- Renamed crate from `alloc-cortex-m` to `embedded-alloc`.
- Renamed `CortexMHeap` to `Heap`.
- Use `critical-section` to lock the heap, instead of `cortex_m::interrupt::free()`.
This allows using this crate on non-Cortex-M systems, or on
Cortex-M systems that require a custom critical section implementation.

## [v0.4.2] - 2022-01-04

### Changed
Expand Down Expand Up @@ -89,7 +99,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Initial version of the allocator

[Unreleased]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.2...HEAD
[Unreleased]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.5.0...HEAD
[v0.5.0]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.2...v0.5.0
[v0.4.2]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.1...v0.4.2
[v0.4.1]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.4.0...v0.4.1
[v0.4.0]: https://github.com/rust-embedded/alloc-cortex-m/compare/v0.3.5...v0.4.0
Expand Down
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ authors = [
"Sébastien Béchet <sebastien.bechet@osinix.com>",
]

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

keywords = [
"allocator",
"embedded",
"arm",
"riscv",
"cortex-m",
]
license = "MIT OR Apache-2.0"
name = "alloc-cortex-m"
version = "0.4.2"
name = "embedded-alloc"
version = "0.5.0"

[dependencies]
critical-section = "1.0"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

# `alloc-cortex-m`

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

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

## Example

For a usage example, see `examples/global_alloc.rs`.

## [Documentation](https://docs.rs/alloc-cortex-m)

## [Change log](CHANGELOG.md)
Expand Down
8 changes: 4 additions & 4 deletions examples/global_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
extern crate alloc;

use alloc::vec::Vec;
use alloc_cortex_m::CortexMHeap;
use core::alloc::Layout;
use core::panic::PanicInfo;
use cortex_m_rt::entry;
use embedded_alloc::Heap;

#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
static HEAP: Heap = Heap::empty();

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

let mut xs = Vec::new();
Expand Down
25 changes: 9 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
//! A heap allocator for Cortex-M processors.
//!
//! Note that using this as your global allocator requires nightly Rust.
//!
//! # Example
//!
//! For a usage example, see `examples/global_alloc.rs`.

#![doc = include_str!("../README.md")]
#![no_std]

use core::alloc::{GlobalAlloc, Layout};
use core::cell::RefCell;
use core::ptr::{self, NonNull};

use critical_section::Mutex;
use linked_list_allocator::Heap;
use linked_list_allocator::Heap as LLHeap;

pub struct CortexMHeap {
heap: Mutex<RefCell<Heap>>,
pub struct Heap {
heap: Mutex<RefCell<LLHeap>>,
}

impl CortexMHeap {
impl Heap {
/// Crate a new UNINITIALIZED heap allocator
///
/// You must initialize this heap using the
/// [`init`](struct.CortexMHeap.html#method.init) method before using the allocator.
pub const fn empty() -> CortexMHeap {
CortexMHeap {
heap: Mutex::new(RefCell::new(Heap::empty())),
pub const fn empty() -> Heap {
Heap {
heap: Mutex::new(RefCell::new(LLHeap::empty())),
}
}

Expand Down Expand Up @@ -70,7 +63,7 @@ impl CortexMHeap {
}
}

unsafe impl GlobalAlloc for CortexMHeap {
unsafe impl GlobalAlloc for Heap {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
critical_section::with(|cs| {
self.heap
Expand Down

0 comments on commit 0af58b7

Please sign in to comment.