Skip to content

Commit 41e539e

Browse files
committed
Add support for bumpalo
1 parent def8aba commit 41e539e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-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"] }

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)