Expand description

This is a linked list allocator, inspired by the dlmalloc algorithm, to be used in no_std environments such as operating system kernels. The overhead for each allocation is a single usize. The implementation prioritizes runtime efficiency over memory efficiency, but also provides very good memory utilization. The allocator is heavily tested with test cases covering almost all code paths; fuzzing is used to cover the rest.

Usage

Create a static allocator:

use good_memory_allocator::SpinLockedAllocator;

#[global_allocator]
static ALLOCATOR: SpinLockedAllocator = SpinLockedAllocator::empty();

Before using this allocator, you need to initialize it:

pub fn init_heap() {
    unsafe {
        ALLOCATOR.init(heap_start, heap_size);
    }
}

SMALLBINS_AMOUNT and ALIGNMENT_SUB_BINS_AMOUNT.

The allocator allows configuring the amount of smallbins and alignment sub-bins that it uses. For those of you not familiar with smallbins, they are data structures used by the allocator to keep track of free chunks. Each smallbin is made up of multiple alignment sub-bins. The default amounts of smallbins and alignment sub-bins used by the allocator are stored in their respective constant DEFAULT_SMALLBINS_AMOUNT and DEFAULT_ALIGNMENT_SUB_BINS_AMOUNT.

Increasing the amount of smallbins will improve runtime performance and memory utilization, especially when you are making a lot of relatively small allocation, but it will also increase the size of the Allocator struct.

Increasing the amount of alignment sub-bins will will also improve runtime performance and memory utilization, especially when you are making a lot of allocations with relatively large alignments, but it will also increase the size of the Allocator struct. It is recommended to choose a value that is a power of 2 for the alignemnt sub-bins amount, since that will improve the memory utilization of the smallbins’ memory. The amount of alignment sub bins must be at least 2, otherwise you will get a compilation error.

If you are in a memory constrained environment, you might want to use lower values for these constants, since the size of the Allocator struct using the default values is relatively large.

Features

  • spin (default): Provide a SpinLockedAllocator type that implements the GlobalAlloc trait by using a spinlock.
  • allocator: Provides an implementation of the unstable Allocator trait for the SpinLockedAllocator type.

Structs

A linked list memory allocator.
A spin locked memory allocator that can be used as the global allocator.

Constants

The default amount of alignemnt sub-bins used by the allocator.
The default amount of smallbins used by the allocator.