Skip to content

Commit 3fcb5fd

Browse files
committed
Auto merge of #231 - Amanieu:bumpalo, r=Amanieu
Add support for bumpalo Allows `Bump` and `&Bump` to be used as allocators without the nightly feature. This is currently blocked on fitzgen/bumpalo#92 for support on nightly.
2 parents d3297e4 + 5467e3f commit 3fcb5fd

File tree

6 files changed

+55
-11
lines changed

6 files changed

+55
-11
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.5.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`: Provides a `BumpWrapper` type which allows `bumpalo` to be used for memory allocation.
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)

ci/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if [ "${NO_STD}" = "1" ]; then
99
FEATURES="rustc-internal-api"
1010
OP="build"
1111
else
12-
FEATURES="rustc-internal-api,serde,rayon,raw"
12+
FEATURES="rustc-internal-api,serde,rayon,raw,bumpalo"
1313
OP="test"
1414
fi
1515
if [ "${TRAVIS_RUST_VERSION}" = "nightly" ]; then

src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
min_specialization,
2020
extend_one,
2121
allocator_api,
22-
slice_ptr_get
22+
slice_ptr_get,
23+
nonnull_slice_from_raw_parts
2324
)
2425
)]
2526
#![allow(
@@ -124,3 +125,21 @@ pub enum TryReserveError {
124125
layout: alloc::alloc::Layout,
125126
},
126127
}
128+
129+
/// Wrapper around `Bump` which allows it to be used as an allocator for
130+
/// `HashMap`, `HashSet` and `RawTable`.
131+
///
132+
/// `Bump` can be used directly without this wrapper on nightly if you enable
133+
/// the `allocator-api` feature of the `bumpalo` crate.
134+
#[cfg(feature = "bumpalo")]
135+
#[derive(Clone, Copy, Debug)]
136+
pub struct BumpWrapper<'a>(&'a bumpalo::Bump);
137+
138+
#[cfg(feature = "bumpalo")]
139+
#[test]
140+
fn test_bumpalo() {
141+
use bumpalo::Bump;
142+
let bump = Bump::new();
143+
let mut map = HashMap::new_in(BumpWrapper(&bump));
144+
map.insert(0, 1);
145+
}

src/raw/alloc.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,60 @@ mod inner {
1313
.map(|ptr| ptr.as_non_null_ptr())
1414
.map_err(|_| ())
1515
}
16+
17+
#[cfg(feature = "bumpalo")]
18+
unsafe impl Allocator for crate::BumpWrapper<'_> {
19+
#[inline]
20+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
21+
match self.0.try_alloc_layout(layout) {
22+
Ok(ptr) => Ok(NonNull::slice_from_raw_parts(ptr, layout.size())),
23+
Err(_) => Err(core::alloc::AllocError),
24+
}
25+
}
26+
#[inline]
27+
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
28+
}
1629
}
1730

1831
#[cfg(not(feature = "nightly"))]
1932
mod inner {
2033
use crate::alloc::alloc::{alloc, dealloc, Layout};
2134
use core::ptr::NonNull;
2235

23-
pub struct AllocError;
24-
2536
pub unsafe trait Allocator {
26-
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>;
37+
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()>;
2738
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
2839
}
2940

3041
#[derive(Copy, Clone)]
3142
pub struct Global;
3243
unsafe impl Allocator for Global {
33-
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
34-
unsafe { NonNull::new(alloc(layout)).ok_or(AllocError) }
44+
#[inline]
45+
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()> {
46+
unsafe { NonNull::new(alloc(layout)).ok_or(()) }
3547
}
48+
#[inline]
3649
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
3750
dealloc(ptr.as_ptr(), layout)
3851
}
3952
}
4053
impl Default for Global {
54+
#[inline]
4155
fn default() -> Self {
4256
Global
4357
}
4458
}
4559

46-
#[allow(clippy::map_err_ignore)]
4760
pub fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
48-
alloc.allocate(layout).map_err(|_| ())
61+
alloc.allocate(layout)
62+
}
63+
64+
#[cfg(feature = "bumpalo")]
65+
unsafe impl Allocator for crate::BumpWrapper<'_> {
66+
#[allow(clippy::map_err_ignore)]
67+
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()> {
68+
self.0.try_alloc_layout(layout).map_err(|_| ())
69+
}
70+
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
4971
}
5072
}

src/raw/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,8 +1989,7 @@ impl<T, A: Allocator + Clone> Drop for RawIntoIter<T, A> {
19891989

19901990
// Free the table
19911991
if let Some((ptr, layout)) = self.allocation {
1992-
self.alloc
1993-
.deallocate(NonNull::new_unchecked(ptr.as_ptr()), layout);
1992+
self.alloc.deallocate(ptr, layout);
19941993
}
19951994
}
19961995
}

0 commit comments

Comments
 (0)