Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Allocator APIs #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,25 @@ description = "Support for bumpalo in scoped threads & rayon"
[dependencies]
bumpalo = "~3"

# This dependency provides a version of the unstable nightly Rust `Allocator`
# trait on stable Rust. Enabling this feature means that `bumpalo` will
# implement its `Allocator` trait.
allocator-api2 = { version = "0.2.8", default-features = false, optional = true }

[dev-dependencies]
bumpalo = { version = "~3", features = ["collections"] }
bumpalo = "~3"
crossbeam-utils = "~0.8"
num_cpus = "~1"
rayon = "~1"

[features]
default = [
"collections", # Exists for backward compatibility. Please remove this line at 0.2.0 release.
"bumpalo/default"
]
collections = ["bumpalo/collections"]
boxed = ["bumpalo/boxed"]
allocator_api = ["bumpalo/allocator_api"]
allocator-api2 = ["bumpalo/allocator-api2", "dep:allocator-api2"]
serde = ["bumpalo/serde"]
std = ["bumpalo/std"]
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![doc(test(attr(deny(warnings))))]
#![warn(missing_docs)]
#![cfg_attr(feature = "allocator_api", feature(allocator_api))]

//! The Bumpalo Herd
//!
Expand Down Expand Up @@ -86,6 +87,12 @@ use std::mem::ManuallyDrop;
use std::ptr::NonNull;
use std::sync::Mutex;

#[cfg(feature = "allocator_api")]
use std::alloc::{AllocError, Allocator};

#[cfg(all(feature = "allocator-api2", not(feature = "allocator_api")))]
use allocator_api2::alloc::{AllocError, Allocator};

use bumpalo::Bump;

type HerdInner = Vec<Box<Bump>>;
Expand Down Expand Up @@ -263,6 +270,49 @@ impl Drop for Member<'_> {
}
}

#[cfg(any(feature = "allocator_api", feature = "allocator-api2"))]
unsafe impl Allocator for &Member<'_> {
#[inline]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.as_bump().allocate(layout)
}

#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
self.as_bump().deallocate(ptr, layout)
}

#[inline]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
self.as_bump().shrink(ptr, old_layout, new_layout)
}

#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
self.as_bump().grow(ptr, old_layout, new_layout)
}

#[inline]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
self.as_bump().grow_zeroed(ptr, old_layout, new_layout)
}
}

#[cfg(test)]
// We disable stuff on that platform and are lazy to disable all the imports too, this is shorter.
#[cfg_attr(all(miri, target_os = "windows"), allow(unused_imports))]
Expand Down