emheap

Crates.ioemheap
lib.rsemheap
version
sourcesrc
created_at2022-04-20 04:07:37.893599
updated_at2022-04-20 04:45:09.393
descriptionTiny memory manager for embedded system.
homepagehttps://github.com/XiangYyang/emheap
repositoryhttps://github.com/XiangYyang/emheap
max_upload_size
id570755
size0
向阳 (XiangYyang)

documentation

README

emheap

cargo

The emheap crate is a simple memory manager for embedded systems and microprocessors.

Here are the main features:

  1. small: total no more than 350 lines of code.
  2. fragmentation collection: the programming will merge the closest memory block to avoid fragmentation.
  3. cross-platform: it does not rely on any libraries.
  4. memory alignment: support memory alignment.

WARNING: DO NOT use this library on your PC.

HOW TO USE

In cargo.toml

[dependencies]
emheap = "0"

Then, in heap.c, change the heap memory size:

#define HEAP_SIZE           (4 * 1024)

Example

Consider a Direct Computer Control System, it uses the ARM Cortex-M0+ microprocessor. Now, we want to use the alloc crate.

At first, we should check out to the nightly channel:

rustup default nightly

Now, declare the alloc crate in your codes:

#![feature(alloc_error_handler)]

extern crate alloc;

This crate is not dependent on unstable features, however, you need to use alloc_error_handler to cause panic. Let's special the global allocator and the error handler:

use alloc::alloc::Layout;
use emheap::{heap, rsalloc::Allocator};

#[global_allocator]
pub static HEAP: Allocator = Allocator {};

#[alloc_error_handler]
fn alloc_error(_layout: Layout) -> ! {
    // your code...
    loop{}
}

Once all that is in place, now you can finally use the collections in alloc:

use alloc::vec;

fn test() {
    let arr = vec![1, 2, 3, 4, 5];
    for i in arr {
        do_other(i);
    }
}
Commit count: 5

cargo fmt