pub struct Bump { /* fields omitted */ }
An arena to bump allocate into.
use bumpalo::Bump;
let bump = Bump::new();
let forty_two = bump.alloc(42);
assert_eq!(*forty_two, 42);
let mut s = bump.alloc("bumpalo");
*s = "the bump allocator; and also is a buffalo";
Construct a new arena to bump allocate into.
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.
let mut bump = bumpalo::Bump::new();
{
for i in 0..100 {
bump.alloc(i);
}
}
bump.reset();
for j in 200..400 {
bump.alloc(j);
}
Allocate an object.
let bump = bumpalo::Bump::new();
let x = bump.alloc("hello");
assert_eq!(*x, "hello");
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.
Because this method takes &mut self
, we know that the bump arena
reference is unique and therefore there aren't any active references to
any of the objects we've allocated in it either. This potential aliasing
of exclusive references is one common footgun for unsafe code that we
don't need to worry about here.
However, there could be regions of uninitilized memory used as padding
between allocations. Reading uninitialized memory is big time undefined
behavior!
The only way to guarantee that there is no padding between allocations
or within allocated objects is if all of these properties hold:
- Every object allocated in this arena has the same alignment.
- Every object's size is a multiple of its alignment.
- None of the objects allocated in this arena contain any internal
padding.
If you want to use this each_allocated_chunk
method, it is your
responsibility to ensure that these properties hold!
let mut bump = bumpalo::Bump::new();
for i in 0..10000 {
bump.alloc(i);
}
unsafe {
bump.each_allocated_chunk(|ch| {
println!("Used a chunk that is {} bytes long", ch.len());
});
}
Executes the destructor for this type. Read more
Formats the value using the given formatter. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static