Skip to content

Commit e23fbfc

Browse files
committed
Add support for bumpalo
1 parent def8aba commit e23fbfc

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core
2424
compiler_builtins = { version = "0.1.2", optional = true }
2525
alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-alloc" }
2626

27+
# Optional support for bumpalo
28+
bumpalo = { version = "3.4.0", optional = true }
29+
2730
[dev-dependencies]
2831
lazy_static = "1.4"
2932
rand = { version = "0.7.3", features = ["small_rng"] }

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ This crate has the following Cargo features:
105105
- `raw`: Enables access to the experimental and unsafe `RawTable` API.
106106
- `inline-more`: Adds inline hints to most functions, improving run-time performance at the cost
107107
of compilation time. (enabled by default)
108+
- `bumpalo`: Allows `Bump` and `&Bump` to be used as allocators.
108109
- `ahash`: Compiles with ahash as default hasher. (enabled by default)
109110
- `ahash-compile-time-rng`: Activates the `compile-time-rng` feature of ahash. For targets with no random number generator
110111
this pre-generates seeds at compile time and embeds them as constants. See [aHash's documentation](https://github.com/tkaitchuck/aHash#flags) (disabled by default)

src/raw/alloc.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@ mod inner {
4747
pub fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
4848
alloc.allocate(layout).map_err(|_| ())
4949
}
50+
51+
#[cfg(feature = "bumpalo")]
52+
unsafe impl Allocator for bumpalo::Bump {
53+
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
54+
self.try_alloc_layout(layout).map_err(|_| AllocError)
55+
}
56+
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
57+
}
58+
59+
#[cfg(feature = "bumpalo")]
60+
unsafe impl Allocator for &'_ bumpalo::Bump {
61+
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
62+
self.try_alloc_layout(layout).map_err(|_| AllocError)
63+
}
64+
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
65+
}
5066
}

0 commit comments

Comments
 (0)