Skip to main content

oxc_allocator/
allocator_api2.rs

1// All methods just delegate to `Arena`, so all marked `#[inline(always)]`.
2// All have same safety preconditions of `Arena` methods of the same name.
3#![expect(clippy::inline_always, clippy::undocumented_unsafe_blocks)]
4
5use std::{alloc::Layout, ptr::NonNull};
6
7use allocator_api2::alloc::{AllocError, Allocator};
8
9/// SAFETY: See `arena.rs` for the implementation of `Allocator` for `&Arena`.
10unsafe impl Allocator for &crate::Allocator {
11    #[inline(always)]
12    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
13        Allocator::allocate(&self.arena(), layout)
14    }
15
16    #[inline(always)]
17    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
18        unsafe {
19            Allocator::deallocate(&self.arena(), ptr, layout);
20        }
21    }
22
23    #[inline(always)]
24    unsafe fn shrink(
25        &self,
26        ptr: NonNull<u8>,
27        old_layout: Layout,
28        new_layout: Layout,
29    ) -> Result<NonNull<[u8]>, AllocError> {
30        unsafe { Allocator::shrink(&self.arena(), ptr, old_layout, new_layout) }
31    }
32
33    #[inline(always)]
34    unsafe fn grow(
35        &self,
36        ptr: NonNull<u8>,
37        old_layout: Layout,
38        new_layout: Layout,
39    ) -> Result<NonNull<[u8]>, AllocError> {
40        unsafe { Allocator::grow(&self.arena(), ptr, old_layout, new_layout) }
41    }
42
43    #[inline(always)]
44    unsafe fn grow_zeroed(
45        &self,
46        ptr: NonNull<u8>,
47        old_layout: Layout,
48        new_layout: Layout,
49    ) -> Result<NonNull<[u8]>, AllocError> {
50        unsafe { Allocator::grow_zeroed(&self.arena(), ptr, old_layout, new_layout) }
51    }
52}