Struct bumpalo::Bump

source ·
pub struct Bump { /* private fields */ }
Expand description

An arena to bump allocate into.

Example

use bumpalo::Bump;

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

// Allocate values into the arena.
let forty_two = bump.alloc(42);
assert_eq!(*forty_two, 42);

// Mutable references are returned from allocation.
let mut s = bump.alloc("bumpalo");
*s = "the bump allocator; and also is a buffalo";

Implementations§

Construct a new arena to bump allocate into.

Example
let bump = bumpalo::Bump::new();

Reset this bump allocator.

Performs mass deallocation on everything allocated in this arena by resetting the pointer into the underlying chunk of memory to the start of the chunk. Does not run any Drop implementations on deallocated objects; see the BumpAllocSafe marker trait for details.

If this arena has allocated multiple chunks to bump allocate into, then the excess chunks are returned to the global allocator.

Example
let mut bump = bumpalo::Bump::new();

// Allocate a bunch of things.
{
    for i in 0..100 {
        bump.alloc(i);
    }
}

// Reset the arena.
bump.reset();

// Allocate some new things in the space previously occupied by the
// original things.
for j in 200..400 {
    bump.alloc(j);
}

Allocate an object.

Example
let bump = bumpalo::Bump::new();
let x = bump.alloc("hello");
assert_eq!(*x, "hello");
Panics

Panics if size_of::<T>() > 65520 or if align_of::<T>() > 8.

Call f on each chunk of allocated memory that this arena has bump allocated into.

f is invoked in order of allocation: oldest chunks first, newest chunks last.

Example
let mut bump = bumpalo::Bump::new();

// Allocate a bunch of things in this bump arena, potentially causing
// additional memory chunks to be reserved.
for i in 0..1000 {
    bump.alloc((i, i + 1, i + 2, i + 3));
}

// Iterate over each chunk we've bump allocated into.
bump.each_allocated_chunk(|ch| println!("chunk: {:?}", ch));

Trait Implementations§

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.