pub use self::inner::Global;
pub(crate) use self::inner::{AllocWrapper, Allocator, global_alloc};
#[cfg(feature = "allocator-api2")]
mod inner {
use allocator_api2::alloc::AllocError;
pub use allocator_api2::alloc::{Allocator, Global, Layout};
use core::ptr::NonNull;
#[inline]
pub(crate) const fn global_alloc() -> Global {
Global
}
#[derive(Clone, Copy, Default)]
pub(crate) struct AllocWrapper<T>(pub(crate) T);
unsafe impl<T: Allocator> allocator_api2::alloc::Allocator for AllocWrapper<T> {
#[inline]
fn allocate(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
Allocator::allocate(&self.0, layout)
}
#[inline]
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
Allocator::allocate_zeroed(&self.0, layout)
}
#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { Allocator::deallocate(&self.0, ptr, layout) }
}
#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { Allocator::grow(&self.0, ptr, old_layout, new_layout) }
}
#[inline]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe {
Allocator::grow_zeroed(&self.0, ptr, old_layout, new_layout)
}
}
#[inline]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { Allocator::shrink(&self.0, ptr, old_layout, new_layout) }
}
}
}
#[cfg(not(feature = "allocator-api2"))]
mod inner {
use crate::alloc::alloc::Layout;
use allocator_api2::alloc::AllocError;
use core::ptr::NonNull;
#[inline]
pub(crate) const fn global_alloc() -> Global {
Global::new()
}
pub unsafe trait Allocator {
fn allocate(&self, layout: Layout)
-> Result<NonNull<[u8]>, AllocError>;
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
}
#[derive(Copy, Clone, Default)]
#[doc(hidden)]
pub struct Global(allocator_api2::alloc::Global);
impl Global {
#[inline]
pub const fn new() -> Self {
Global(allocator_api2::alloc::Global)
}
}
unsafe impl Allocator for Global {
#[inline]
fn allocate(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
allocator_api2::alloc::Allocator::allocate(&self.0, layout)
}
#[inline]
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
allocator_api2::alloc::Allocator::allocate_zeroed(&self.0, layout)
}
#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe {
allocator_api2::alloc::Allocator::deallocate(
&self.0, ptr, layout,
);
}
}
#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe {
allocator_api2::alloc::Allocator::grow(
&self.0, ptr, old_layout, new_layout,
)
}
}
#[inline]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe {
allocator_api2::alloc::Allocator::grow_zeroed(
&self.0, ptr, old_layout, new_layout,
)
}
}
#[inline]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe {
allocator_api2::alloc::Allocator::shrink(
&self.0, ptr, old_layout, new_layout,
)
}
}
}
#[derive(Clone, Copy, Default)]
pub(crate) struct AllocWrapper<T>(pub(crate) T);
unsafe impl<T: Allocator> allocator_api2::alloc::Allocator for AllocWrapper<T> {
#[inline]
fn allocate(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
Allocator::allocate(&self.0, layout)
}
#[inline]
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
Allocator::allocate_zeroed(&self.0, layout)
}
#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { Allocator::deallocate(&self.0, ptr, layout) }
}
#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { Allocator::grow(&self.0, ptr, old_layout, new_layout) }
}
#[inline]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe {
Allocator::grow_zeroed(&self.0, ptr, old_layout, new_layout)
}
}
#[inline]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { Allocator::shrink(&self.0, ptr, old_layout, new_layout) }
}
}
}