bump_local/
alloc_api.rs

1use alloc::alloc::Layout;
2use core::ptr::NonNull;
3
4#[cfg(feature = "allocator_api")]
5pub use core::alloc::{AllocError, Allocator};
6
7#[cfg(all(feature = "allocator-api2", not(feature = "allocator_api")))]
8pub use allocator_api2::alloc::{AllocError, Allocator};
9
10use crate::Bump;
11
12unsafe impl Allocator for Bump {
13    #[inline]
14    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
15        self.local().as_inner().allocate(layout)
16    }
17
18    #[inline]
19    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
20        unsafe {
21            self.local().as_inner().deallocate(ptr, layout);
22        }
23    }
24
25    #[inline]
26    unsafe fn shrink(
27        &self,
28        ptr: NonNull<u8>,
29        old_layout: Layout,
30        new_layout: Layout,
31    ) -> Result<NonNull<[u8]>, AllocError> {
32        unsafe { self.local().as_inner().shrink(ptr, old_layout, new_layout) }
33    }
34
35    #[inline]
36    unsafe fn grow(
37        &self,
38        ptr: NonNull<u8>,
39        old_layout: Layout,
40        new_layout: Layout,
41    ) -> Result<NonNull<[u8]>, AllocError> {
42        unsafe { self.local().as_inner().grow(ptr, old_layout, new_layout) }
43    }
44
45    #[inline]
46    unsafe fn grow_zeroed(
47        &self,
48        ptr: NonNull<u8>,
49        old_layout: Layout,
50        new_layout: Layout,
51    ) -> Result<NonNull<[u8]>, AllocError> {
52        unsafe { self.local().as_inner().grow_zeroed(ptr, old_layout, new_layout) }
53    }
54}