use crate::core_alloc::{AllocError, Allocator, Layout};
use core::ptr::NonNull;
#[cfg(bao_nightly)]
macro_rules! bridge_allocator_api2 {
($t:ty) => {
unsafe impl ::allocator_api2::alloc::Allocator for $t {
#[inline]
fn allocate(
&self,
layout: ::core::alloc::Layout,
) -> ::core::result::Result<
::core::ptr::NonNull<[u8]>,
::allocator_api2::alloc::AllocError,
> {
::core::alloc::Allocator::allocate(self, layout)
.map_err(|_| ::allocator_api2::alloc::AllocError)
}
#[inline]
unsafe fn deallocate(
&self,
ptr: ::core::ptr::NonNull<u8>,
layout: ::core::alloc::Layout,
) {
unsafe { ::core::alloc::Allocator::deallocate(self, ptr, layout) }
}
}
};
}
#[cfg(not(bao_nightly))]
macro_rules! bridge_allocator_api2 {
($t:ty) => {};
}
use crate::DefaultAlloc;
unsafe impl Allocator for DefaultAlloc {
#[inline]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
crate::core_alloc::Global.allocate(layout)
}
#[inline]
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
crate::core_alloc::Global.allocate_zeroed(layout)
}
#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { crate::core_alloc::Global.deallocate(ptr, layout) }
}
#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old: Layout,
new: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { crate::core_alloc::Global.grow(ptr, old, new) }
}
#[inline]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old: Layout,
new: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { crate::core_alloc::Global.grow_zeroed(ptr, old, new) }
}
#[inline]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old: Layout,
new: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { crate::core_alloc::Global.shrink(ptr, old, new) }
}
}
bridge_allocator_api2!(DefaultAlloc);
bridge_allocator_api2!(crate::ast_alloc::AstAlloc);
bridge_allocator_api2!(crate::stack_fallback::ArenaPtr);