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

Add nightly feature flag for Alloc -> AllocRef changes #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ debug = []

# Enable experimental support for the standard library's unstable allocator API.
allocator-api = []
nightly = []
rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std']
50 changes: 48 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
#![no_std]
#![deny(missing_docs)]

#[cfg(all(feature = "allocator-api", not(feature = "nightly")))]
use core::alloc::Alloc;
#[cfg(all(feature = "allocator-api", feature = "nightly"))]
use core::alloc::AllocRef;
#[cfg(feature = "allocator-api")]
use core::alloc::{Alloc, AllocErr, Layout};
use core::alloc::{AllocErr, Layout};
use core::cmp;
use core::ptr;

Expand Down Expand Up @@ -130,7 +134,7 @@ impl Dlmalloc {
}
}

#[cfg(feature = "allocator-api")]
#[cfg(all(feature = "allocator-api", not(feature = "nightly")))]
unsafe impl Alloc for Dlmalloc {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<ptr::NonNull<u8>, AllocErr> {
Expand Down Expand Up @@ -160,3 +164,45 @@ unsafe impl Alloc for Dlmalloc {
ptr::NonNull::new(ptr).ok_or(AllocErr)
}
}

#[cfg(all(feature = "allocator-api", feature = "nightly"))]
unsafe impl AllocRef for Dlmalloc {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<(ptr::NonNull<u8>, usize), AllocErr> {
let layout_size = layout.size();
let ptr = <Dlmalloc>::malloc(self, layout_size, layout.align());
ptr::NonNull::new(ptr)
.ok_or(AllocErr)
.map(|ptr| (ptr, layout_size))
}

#[inline]
unsafe fn dealloc(&mut self, ptr: ptr::NonNull<u8>, layout: Layout) {
<Dlmalloc>::free(self, ptr.as_ptr(), layout.size(), layout.align())
}

#[inline]
unsafe fn realloc(
&mut self,
ptr: ptr::NonNull<u8>,
layout: Layout,
new_size: usize,
) -> Result<(ptr::NonNull<u8>, usize), AllocErr> {
let ptr = <Dlmalloc>::realloc(self, ptr.as_ptr(), layout.size(), layout.align(), new_size);
ptr::NonNull::new(ptr)
.ok_or(AllocErr)
.map(|ptr| (ptr, new_size))
}

#[inline]
unsafe fn alloc_zeroed(
&mut self,
layout: Layout,
) -> Result<(ptr::NonNull<u8>, usize), AllocErr> {
let layout_size = layout.size();
let ptr = <Dlmalloc>::calloc(self, layout_size, layout.align());
ptr::NonNull::new(ptr)
.ok_or(AllocErr)
.map(|ptr| (ptr, layout_size))
}
}