swc_allocator/allocators/
arena.rs1use std::ops::{Deref, DerefMut};
2
3use bumpalo::Bump;
4
5#[derive(Default)]
7pub struct Arena {
8 inner: Bump,
9}
10
11impl Deref for Arena {
12 type Target = Bump;
13
14 fn deref(&self) -> &Self::Target {
15 &self.inner
16 }
17}
18
19impl DerefMut for Arena {
20 fn deref_mut(&mut self) -> &mut Self::Target {
21 &mut self.inner
22 }
23}
24
25impl From<Bump> for Arena {
26 fn from(inner: Bump) -> Self {
27 Self { inner }
28 }
29}
30
31#[cfg(feature = "nightly")]
32unsafe impl std::alloc::Allocator for &'_ Arena {
33 #[inline]
34 fn allocate(
35 &self,
36 layout: std::alloc::Layout,
37 ) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
38 std::alloc::Allocator::allocate(&&self.inner, layout)
39 }
40
41 #[inline]
42 unsafe fn deallocate(&self, ptr: std::ptr::NonNull<u8>, layout: std::alloc::Layout) {
43 std::alloc::Allocator::deallocate(&&self.inner, ptr, layout)
44 }
45
46 #[inline]
47 fn allocate_zeroed(
48 &self,
49 layout: std::alloc::Layout,
50 ) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
51 std::alloc::Allocator::allocate_zeroed(&&self.inner, layout)
52 }
53
54 #[inline]
55 unsafe fn grow(
56 &self,
57 ptr: std::ptr::NonNull<u8>,
58 old_layout: std::alloc::Layout,
59 new_layout: std::alloc::Layout,
60 ) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
61 std::alloc::Allocator::grow(&&self.inner, ptr, old_layout, new_layout)
62 }
63
64 #[inline]
65 unsafe fn grow_zeroed(
66 &self,
67 ptr: std::ptr::NonNull<u8>,
68 old_layout: std::alloc::Layout,
69 new_layout: std::alloc::Layout,
70 ) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
71 std::alloc::Allocator::grow_zeroed(&&self.inner, ptr, old_layout, new_layout)
72 }
73
74 #[inline]
75 unsafe fn shrink(
76 &self,
77 ptr: std::ptr::NonNull<u8>,
78 old_layout: std::alloc::Layout,
79 new_layout: std::alloc::Layout,
80 ) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
81 std::alloc::Allocator::shrink(&&self.inner, ptr, old_layout, new_layout)
82 }
83}
84
85#[cfg(not(feature = "nightly"))]
86unsafe impl allocator_api2::alloc::Allocator for &'_ Arena {
87 #[inline]
88 fn allocate(
89 &self,
90 layout: std::alloc::Layout,
91 ) -> Result<std::ptr::NonNull<[u8]>, allocator_api2::alloc::AllocError> {
92 allocator_api2::alloc::Allocator::allocate(&&self.inner, layout)
93 }
94
95 #[inline]
96 unsafe fn deallocate(&self, ptr: std::ptr::NonNull<u8>, layout: std::alloc::Layout) {
97 allocator_api2::alloc::Allocator::deallocate(&&self.inner, ptr, layout)
98 }
99
100 #[inline]
101 fn allocate_zeroed(
102 &self,
103 layout: std::alloc::Layout,
104 ) -> Result<std::ptr::NonNull<[u8]>, allocator_api2::alloc::AllocError> {
105 allocator_api2::alloc::Allocator::allocate_zeroed(&&self.inner, layout)
106 }
107
108 #[inline]
109 unsafe fn grow(
110 &self,
111 ptr: std::ptr::NonNull<u8>,
112 old_layout: std::alloc::Layout,
113 new_layout: std::alloc::Layout,
114 ) -> Result<std::ptr::NonNull<[u8]>, allocator_api2::alloc::AllocError> {
115 allocator_api2::alloc::Allocator::grow(&&self.inner, ptr, old_layout, new_layout)
116 }
117
118 #[inline]
119 unsafe fn grow_zeroed(
120 &self,
121 ptr: std::ptr::NonNull<u8>,
122 old_layout: std::alloc::Layout,
123 new_layout: std::alloc::Layout,
124 ) -> Result<std::ptr::NonNull<[u8]>, allocator_api2::alloc::AllocError> {
125 allocator_api2::alloc::Allocator::grow_zeroed(&&self.inner, ptr, old_layout, new_layout)
126 }
127
128 #[inline]
129 unsafe fn shrink(
130 &self,
131 ptr: std::ptr::NonNull<u8>,
132 old_layout: std::alloc::Layout,
133 new_layout: std::alloc::Layout,
134 ) -> Result<std::ptr::NonNull<[u8]>, allocator_api2::alloc::AllocError> {
135 allocator_api2::alloc::Allocator::shrink(&&self.inner, ptr, old_layout, new_layout)
136 }
137}