Crate bumpalo

source ·
Expand description

A fast bump allocation arena for Rust.

Travis CI Build Status

Bump Allocation

Bump allocation is a fast, but limited approach to allocation. We have a chunk of memory, and we maintain a pointer within that memory. Whenever we allocate an object, we do a quick test that we have enough capacity left in our chunk to allocate the object and then increment the pointer by the object’s size. That’s it!

The disadvantage of bump allocation is that there is no general way to deallocate individual objects or reclaim the memory region for a no-longer-in-use object.

These trade offs make bump allocation well-suited for phase-oriented allocations. That is, a group of objects that will all be allocated during the same program phase, used, and then can all be deallocated together as a group.

Deallocation en Masse, but No Drop

To deallocate all the objects in the arena at once, we can simply reset the bump pointer back to the start of the arena’s memory chunk. This makes mass deallocation extremely fast, but allocated objects’ Drop implementations are not invoked.

See the BumpAllocSafe marker trait for details.

What happens when the memory chunk is full?

This implementation will allocate a new memory chunk from the global allocator and then start bump allocating into this new memory chunk.

Example

use bumpalo::{Bump, BumpAllocSafe};
use std::u64;

struct Doggo {
    cuteness: u64,
    age: u8,
    scritches_required: bool,
}

// Mark `Doggo` as safe to put into bump allocation arenas.
impl BumpAllocSafe for Doggo {}

// Create a new arena to bump allocate into.
let bump = Bump::new();

// Allocate values into the arena.
let scooter = bump.alloc(Doggo {
    cuteness: u64::max_value(),
    age: 8,
    scritches_required: true,
});

assert!(scooter.scritches_required);

#![no_std] Support

Requires the alloc nightly feature. Disable the on-by-default "std" feature:

[dependencies.bumpalo]
version = "1"
default-features = false

Structs

An arena to bump allocate into.

Traits

A marker trait for types that are “safe” to bump alloc.