Skip to content

Commit

Permalink
Add support for allocator_api on stable
Browse files Browse the repository at this point in the history
Add optional allocator-api2 package which provides the allocator api in Rust stable.
  • Loading branch information
jess-sol committed Apr 11, 2024
1 parent 618d505 commit a945304
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ default = ["lock_api", "allocator", "nightly_api"]


[dependencies]
allocator-api2 = { version = "0.2", optional = true, default-features = false }
lock_api = { version = "0.4", optional = true, default-features = false }

[dev-dependencies]
Expand Down
19 changes: 11 additions & 8 deletions src/talck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ use core::{
};

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

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

#[cfg(any(feature = "allocator", feature = "allocator-api2"))]
pub(crate) fn is_aligned_to(ptr: *mut u8, align: usize) -> bool {
(ptr as usize).trailing_zeros() >= align.trailing_zeros()
}
Expand Down Expand Up @@ -107,9 +110,9 @@ unsafe impl<R: lock_api::RawMutex, O: OomHandler> GlobalAlloc for Talck<R, O> {
}
}

#[cfg(feature = "allocator")]
unsafe impl<R: lock_api::RawMutex, O: OomHandler> core::alloc::Allocator for Talck<R, O> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
#[cfg(any(feature = "allocator", feature = "allocator-api2"))]
unsafe impl<R: lock_api::RawMutex, O: OomHandler> Allocator for Talck<R, O> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
if layout.size() == 0 {
return Ok(NonNull::slice_from_raw_parts(NonNull::dangling(), 0));
}
Expand All @@ -130,7 +133,7 @@ unsafe impl<R: lock_api::RawMutex, O: OomHandler> core::alloc::Allocator for Tal
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(new_layout.size() >= old_layout.size());

if old_layout.size() == 0 {
Expand Down Expand Up @@ -165,7 +168,7 @@ unsafe impl<R: lock_api::RawMutex, O: OomHandler> core::alloc::Allocator for Tal
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
) -> Result<NonNull<[u8]>, AllocError> {
let res = self.grow(ptr, old_layout, new_layout);

if let Ok(allocation) = res {
Expand All @@ -184,7 +187,7 @@ unsafe impl<R: lock_api::RawMutex, O: OomHandler> core::alloc::Allocator for Tal
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(new_layout.size() <= old_layout.size());

if new_layout.size() == 0 {
Expand Down

0 comments on commit a945304

Please sign in to comment.