Crate defrag [] [src]

defrag: safe and efficient memory manager for microcontrollers

This library is in the Beta release and is subject to change

This library aims to bring safe heap memory management to microcontrollers. Combined with rust's excellent type system and borrow checker, creating complex applications with limited resources is easier than it has ever been before.

The possibility of fragmentation is the primary reason that dynamic memory allocation is not used on microcontrollers. defrag, as the name implies, is able to defragment memory -- giving your embedded application power, reliability and simplicity. Also, the overhead of each allocated block is only 8bytes, so allocations are very cheap.

How it works

The primary manager of memory is the Pool, from which the user can call Pool.alloc::<T>() or Pool.alloc_slice::<T>(len). From this they will get a Mutex<T> like object which behaves very similarily to rust's stdlib Mutex.

When the data is not locked, the underlying pool is allowed to move it in order to solve potential fragmentation issues. pool.clean() combines contiguous free blocks and pool.defrag() defragments memory. In addition, there are various strategies for utilzing freed blocks of memory.

Note: This library is intended only for (single threaded) microcontrollers, so it's Mutex does not implement Send or Sync (it cannot be shared between threads). Depending on what kind of architectures or OS's spring up on uC rust code, this may change.



The Block is the smallest unit of allocation allowed. It is currently equal to 16 bytes


All allocated data is represented as some kind of a Mutex. When the data is unlocked, the underlying Pool is free to move it and reduce fragmentation


Pool contains a "pool" of memory which can be allocated and used


A slice which can be used through Deref.


same as Mutex except locks a Slice


A value which can be used through Deref



memory error codes

Type Definitions