Git Product home page Git Product logo

emballoc's Introduction

emballoc โ€” Embedded Memory Allocator

crates.io circleci codecov docs.rs

This repository provides the emballoc crate: a simple memory allocator developed for usage in small embedded systems. It is one possible way to support dynamic memory on targets without the standard library, i.e. ones with #![no_std]. This is achieved by providing a type Allocation which can be registered as the global allocator for the binary. See the usage description below.

An allocator is a rather critical part of a software project: when using dynamic memory many operations implicitly can or will allocate, sometimes unexpectedly. Therefore a misbehaving allocator can "randomly" crash the program in very obscure ways. As such an allocator has to be well-tested and battle-proven (see more information here and a real world example). Furthermore it has to be simple: the simpler the algorithm is, the more likely is a correct implementation.

Refer to the crate-documentation for details on the algorithm and usage hints.

Usage

Copy the following snippet to your Cargo.toml to pull the crate in as one of your dependencies.

[dependencies.emballoc]
version = "*" # replace with current version from crates.io

After that the usage is very simple: just copy the following code to the binary crate of the project. Substitute the 4096 with the desired heap size in bytes.

#[global_allocator]
static ALLOCATOR: emballoc::Allocator<4096> = emballoc::Allocator::new();

extern crate alloc;

Now the crate can use the std collections such as Vec<T>, BTreeMap<K, V>, etc. together with important types like Box<T> and Rc<T>. Note, that things in the std-prelude (e.g. Vec<T>, Box<T>, ...) have to be imported explicitly.

Why choosing this crate

This crate started as part of an embedded project, but was extracted to make it usable in other projects and for other users. This sections answers the question:

Why should you consider using this crate in your project?

  • the core algorithm is simple and thus implementation errors are less likely
  • rigorous testing is done (see here)
  • crate is free of undefined behavior according to miri
  • statically determined heap size preventing growing the heap into the stack
  • it is used in real-world applications
  • it even works on a PC (see here), although that is not the primary use case
  • supports the stable compiler as there are only stable features used
  • has only a single dependency on the popular spin-crate (without any transitive dependencies)

I'm glad, if that convinced you, but if you have any questions simply open an issue.

A note to users on systems with advanced memory features like MMUs and MPUs:

  • if you have an memory protection unit (MPU) or similar available, you have to configure it yourself as this crate is platform-agnostic. An example usage might be to configure it, that reading from and writing to the heap is allowed, but execution is not. It is not possible to surround the heap with guard pages, as this allocator will never read/write outside of the internal byte array. However it might be advised to guard the stack, so that it doesn't grow into the heap (or any other variable).
  • if you have an (active) memory management unit (MMU), this is likely not the crate for you: it doesn't use any of the important features, which makes it perform much worse than possible. Use a proper memory allocator for that use case (one that supports paging, etc.). However, if you need dynamic memory before enabling the MMU, this crate certainly is an option.

Minimum supported Rust version

This crate has a stability guarantee about the compiler version supported. The so-called minimum supported Rust version is currently set to 1.57 and won't be raised without a proper increase in the semantic version number scheme. This MSRV is specified in Cargo.toml and is tested in CI.

License

Licensed under either of

at your option.

emballoc's People

Contributors

jfrimmel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.