• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H03-May-2022-8263

benches/H03-May-2022-218192

src/H03-May-2022-9,0953,302

tests/H03-May-2022-1,3411,052

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitattributesH A D01-Jan-197023 21

.gitignoreH A D01-Jan-19707 21

CHANGELOG.mdH A D01-Jan-197015.8 KiB522335

Cargo.tomlH A D01-Jan-19701.3 KiB5547

Cargo.toml.orig-cargoH A D01-Jan-1970821 4436

LICENSE-APACHEH A D01-Jan-197010.6 KiB202169

LICENSE-MITH A D01-Jan-19701 KiB2622

README.mdH A D01-Jan-19706.4 KiB206146

README.tplH A D01-Jan-197026 42

valgrind.suppH A D01-Jan-1970675 3635

README.md

1# `bumpalo`
2
3
4**A fast bump allocation arena for Rust.**
5
6[![](https://docs.rs/bumpalo/badge.svg)](https://docs.rs/bumpalo/)
7[![](https://img.shields.io/crates/v/bumpalo.svg)](https://crates.io/crates/bumpalo)
8[![](https://img.shields.io/crates/d/bumpalo.svg)](https://crates.io/crates/bumpalo)
9[![Build Status](https://github.com/fitzgen/bumpalo/workflows/Rust/badge.svg)](https://github.com/fitzgen/bumpalo/actions?query=workflow%3ARust)
10
11![](https://github.com/fitzgen/bumpalo/raw/master/bumpalo.png)
12
13### Bump Allocation
14
15Bump allocation is a fast, but limited approach to allocation. We have a chunk
16of memory, and we maintain a pointer within that memory. Whenever we allocate an
17object, we do a quick test that we have enough capacity left in our chunk to
18allocate the object and then update the pointer by the object's size. *That's
19it!*
20
21The disadvantage of bump allocation is that there is no general way to
22deallocate individual objects or reclaim the memory region for a
23no-longer-in-use object.
24
25These trade offs make bump allocation well-suited for *phase-oriented*
26allocations. That is, a group of objects that will all be allocated during the
27same program phase, used, and then can all be deallocated together as a group.
28
29### Deallocation en Masse, but No `Drop`
30
31To deallocate all the objects in the arena at once, we can simply reset the bump
32pointer back to the start of the arena's memory chunk. This makes mass
33deallocation *extremely* fast, but allocated objects' `Drop` implementations are
34not invoked.
35
36> **However:** [`bumpalo::boxed::Box<T>`][crate::boxed::Box] can be used to wrap
37> `T` values allocated in the `Bump` arena, and calls `T`'s `Drop`
38> implementation when the `Box<T>` wrapper goes out of scope. This is similar to
39> how [`std::boxed::Box`] works, except without deallocating its backing memory.
40
41[`std::boxed::Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
42
43### What happens when the memory chunk is full?
44
45This implementation will allocate a new memory chunk from the global allocator
46and then start bump allocating into this new memory chunk.
47
48### Example
49
50```rust
51use bumpalo::Bump;
52use std::u64;
53
54struct Doggo {
55    cuteness: u64,
56    age: u8,
57    scritches_required: bool,
58}
59
60// Create a new arena to bump allocate into.
61let bump = Bump::new();
62
63// Allocate values into the arena.
64let scooter = bump.alloc(Doggo {
65    cuteness: u64::max_value(),
66    age: 8,
67    scritches_required: true,
68});
69
70// Exclusive, mutable references to the just-allocated value are returned.
71assert!(scooter.scritches_required);
72scooter.age += 1;
73```
74
75### Collections
76
77When the `"collections"` cargo feature is enabled, a fork of some of the `std`
78library's collections are available in the `collections` module. These
79collection types are modified to allocate their space inside `bumpalo::Bump`
80arenas.
81
82```rust
83use bumpalo::{Bump, collections::Vec};
84
85// Create a new bump arena.
86let bump = Bump::new();
87
88// Create a vector of integers whose storage is backed by the bump arena. The
89// vector cannot outlive its backing arena, and this property is enforced with
90// Rust's lifetime rules.
91let mut v = Vec::new_in(&bump);
92
93// Push a bunch of integers onto `v`!
94for i in 0..100 {
95    v.push(i);
96}
97```
98
99Eventually [all `std` collection types will be parameterized by an
100allocator](https://github.com/rust-lang/rust/issues/42774) and we can remove
101this `collections` module and use the `std` versions.
102
103For unstable, nightly-only support for custom allocators in `std`, see the
104`allocator_api` section below.
105
106### `bumpalo::boxed::Box`
107
108When the `"boxed"` cargo feature is enabled, a fork of `std::boxed::Box` library
109is available in the `boxed` module. This `Box` type is modified to allocate its
110space inside `bumpalo::Bump` arenas.
111
112**A `Box<T>` runs `T`'s drop implementation when the `Box<T>` is dropped.** You
113can use this to work around the fact that `Bump` does not drop values allocated
114in its space itself.
115
116```rust
117use bumpalo::{Bump, boxed::Box};
118use std::sync::atomic::{AtomicUsize, Ordering};
119
120static NUM_DROPPED: AtomicUsize = AtomicUsize::new(0);
121
122struct CountDrops;
123
124impl Drop for CountDrops {
125    fn drop(&mut self) {
126        NUM_DROPPED.fetch_add(1, Ordering::SeqCst);
127    }
128}
129
130// Create a new bump arena.
131let bump = Bump::new();
132
133// Create a `CountDrops` inside the bump arena.
134let mut c = Box::new_in(CountDrops, &bump);
135
136// No `CountDrops` have been dropped yet.
137assert_eq!(NUM_DROPPED.load(Ordering::SeqCst), 0);
138
139// Drop our `Box<CountDrops>`.
140drop(c);
141
142// Its `Drop` implementation was run, and so `NUM_DROPS` has been incremented.
143assert_eq!(NUM_DROPPED.load(Ordering::SeqCst), 1);
144```
145
146### `#![no_std]` Support
147
148Bumpalo is a `no_std` crate. It depends only on the `alloc` and `core` crates.
149
150### Thread support
151
152The `Bump` is `!Send`, which makes it hard to use in certain situations around threads ‒ for
153example in `rayon`.
154
155The [`bumpalo-herd`](https://crates.io/crates/bumpalo-herd) crate provides a pool of `Bump`
156allocators for use in such situations.
157
158### Nightly Rust `feature(allocator_api)` Support
159
160The unstable, nightly-only Rust `allocator_api` feature defines an `Allocator`
161trait and exposes custom allocators for `std` types. Bumpalo has a matching
162`allocator_api` cargo feature to enable implementing `Allocator` and using
163`Bump` with `std` collections. Note that, as `feature(allocator_api)` is
164unstable and only in nightly Rust, Bumpalo's matching `allocator_api` cargo
165feature should be considered unstable, and will not follow the semver
166conventions that the rest of the crate does.
167
168First, enable the `allocator_api` feature in your `Cargo.toml`:
169
170```toml
171[dependencies]
172bumpalo = { version = "3.4.0", features = ["allocator_api"] }
173```
174
175Next, enable the `allocator_api` nightly Rust feature in your `src/lib.rs` or `src/main.rs`:
176
177```rust
178#![feature(allocator_api)]
179```
180
181Finally, use `std` collections with `Bump`, so that their internal heap
182allocations are made within the given bump arena:
183
184```rust
185#![feature(allocator_api)]
186use bumpalo::Bump;
187
188// Create a new bump arena.
189let bump = Bump::new();
190
191// Create a `Vec` whose elements are allocated within the bump arena.
192let mut v = Vec::new_in(&bump);
193v.push(0);
194v.push(1);
195v.push(2);
196```
197
198#### Minimum Supported Rust Version (MSRV)
199
200This crate is guaranteed to compile on stable Rust 1.44 and up. It might compile
201with older versions but that may change in any new patch release.
202
203We reserve the right to increment the MSRV on minor releases, however we will strive
204to only do it deliberately and for good reasons.
205
206

README.tpl

1# `{{crate}}`
2
3{{readme}}
4