strata_primitives/
arena.rs1use std::alloc::{alloc, dealloc, Layout};
4use std::ptr::NonNull;
5
6pub const DEFAULT_ARENA_CAPACITY: usize = 1024 * 1024; pub const SIMD_SENTRY_BYTES: usize = 32; type DropFn = unsafe fn(*mut u8);
11
12pub struct BumpArena {
14 memory: NonNull<u8>,
15 capacity: usize,
16 offset: usize,
17 drop_chain: Vec<(*mut u8, DropFn)>,
18}
19
20unsafe impl Send for BumpArena {}
21
22impl BumpArena {
23 pub fn new(capacity: usize) -> Self {
24 let total_size = capacity + SIMD_SENTRY_BYTES;
26 let layout = Layout::from_size_align(total_size, 64).expect("Invalid arena layout");
27 let ptr = unsafe { alloc(layout) };
28 assert!(!ptr.is_null(), "OOM: Failed to allocate BumpArena memory");
29
30 unsafe {
32 std::ptr::write_bytes(ptr.add(capacity), 0, SIMD_SENTRY_BYTES);
33 }
34
35 Self {
36 memory: NonNull::new(ptr).unwrap(),
37 capacity,
38 offset: 0,
39 drop_chain: Vec::new(),
40 }
41 }
42
43 #[inline(always)]
47 pub fn alloc<T>(&mut self, value: T) -> &mut T {
48 let layout = Layout::new::<T>();
49 let current_offset = self.offset;
50 let aligned_offset = (current_offset + (layout.align() - 1)) & !(layout.align() - 1);
51 let next_offset = aligned_offset + layout.size();
52
53 assert!(
54 next_offset <= self.capacity,
55 "BumpArena Overflow! Allocated {} bytes exceeds capacity {}",
56 next_offset,
57 self.capacity
58 );
59
60 self.offset = next_offset;
61 unsafe {
62 let dest = self.memory.as_ptr().add(aligned_offset) as *mut T;
63 std::ptr::write(dest, value);
64
65 if std::mem::needs_drop::<T>() {
66 unsafe fn drop_glue<T>(ptr: *mut u8) {
67 std::ptr::drop_in_place(ptr as *mut T);
68 }
69 self.drop_chain.push((dest as *mut u8, drop_glue::<T>));
70 }
71
72 &mut *dest
73 }
74 }
75
76 #[inline(always)]
79 pub fn alloc_slice<'a, T: Copy>(&'a mut self, items: &[T]) -> &'a mut [T] {
80 if items.is_empty() {
81 return &mut [];
82 }
83
84 let layout = Layout::array::<T>(items.len()).expect("Array layout error");
85 let current_offset = self.offset;
86 let aligned_offset = (current_offset + (layout.align() - 1)) & !(layout.align() - 1);
87 let next_offset = aligned_offset + layout.size();
88
89 assert!(next_offset <= self.capacity, "BumpArena slice overflow!");
90
91 self.offset = next_offset;
92 unsafe {
93 let dest = self.memory.as_ptr().add(aligned_offset) as *mut T;
94 std::ptr::copy_nonoverlapping(items.as_ptr(), dest, items.len());
95 std::slice::from_raw_parts_mut(dest, items.len())
96 }
97 }
98
99 #[inline(always)]
101 pub fn alloc_str<'a>(&'a mut self, text: &str) -> &'a str {
102 let bytes = self.alloc_slice(text.as_bytes());
103 unsafe { std::str::from_utf8_unchecked(bytes) }
104 }
105
106 #[inline(always)]
109 pub fn reset(&mut self) {
110 for (ptr, drop_fn) in self.drop_chain.drain(..).rev() {
111 unsafe {
112 drop_fn(ptr);
113 }
114 }
115 self.offset = 0;
116 }
117
118 #[inline(always)]
119 pub fn allocated_bytes(&self) -> usize {
120 self.offset
121 }
122
123 #[inline(always)]
124 pub fn capacity(&self) -> usize {
125 self.capacity
126 }
127}
128
129impl Drop for BumpArena {
130 fn drop(&mut self) {
131 for (ptr, drop_fn) in self.drop_chain.drain(..).rev() {
132 unsafe {
133 drop_fn(ptr);
134 }
135 }
136 let total_size = self.capacity + SIMD_SENTRY_BYTES;
137 let layout = Layout::from_size_align(total_size, 64).unwrap();
138 unsafe {
139 dealloc(self.memory.as_ptr(), layout);
140 }
141 }
142}
143
144pub struct FrameArenaManager {
146 arenas: [BumpArena; 2],
147 current_index: usize,
148 frame_epoch: u64,
149}
150
151impl FrameArenaManager {
152 pub fn new(capacity_per_frame: usize) -> Self {
153 Self {
154 arenas: [
155 BumpArena::new(capacity_per_frame),
156 BumpArena::new(capacity_per_frame),
157 ],
158 current_index: 0,
159 frame_epoch: 0,
160 }
161 }
162
163 #[inline(always)]
164 pub fn active_arena(&self) -> &BumpArena {
165 &self.arenas[self.current_index]
166 }
167
168 #[inline(always)]
169 pub fn active_arena_mut(&mut self) -> &mut BumpArena {
170 &mut self.arenas[self.current_index]
171 }
172
173 #[inline(always)]
174 pub fn previous_arena(&self) -> &BumpArena {
175 &self.arenas[1 - self.current_index]
176 }
177
178 #[inline(always)]
179 pub fn previous_arena_mut(&mut self) -> &mut BumpArena {
180 &mut self.arenas[1 - self.current_index]
181 }
182
183 #[inline(always)]
184 pub fn frame_epoch(&self) -> u64 {
185 self.frame_epoch
186 }
187
188 #[inline(always)]
190 pub fn swap_and_reset(&mut self) {
191 self.current_index = 1 - self.current_index;
192 self.frame_epoch += 1;
193 self.arenas[self.current_index].reset();
194 }
195}
196
197type DropEntry = (*mut u8, unsafe fn(*mut u8));
198
199#[repr(align(64))]
201pub struct ThreadLocalArena {
202 start: *mut u8,
203 ptr: *mut u8,
204 end: *mut u8,
205 capacity: usize,
206 drop_chain: Vec<DropEntry>,
207}
208
209impl ThreadLocalArena {
210 pub fn new(capacity: usize) -> Self {
211 let layout =
212 Layout::from_size_align(capacity, 64).expect("Invalid layout for ThreadLocalArena");
213 let start = unsafe { alloc(layout) };
214 assert!(!start.is_null(), "OOM: Failed to allocate ThreadLocalArena");
215
216 Self {
217 start,
218 ptr: start,
219 end: unsafe { start.add(capacity) },
220 capacity,
221 drop_chain: Vec::new(),
222 }
223 }
224
225 #[inline(always)]
227 pub fn alloc<T>(&mut self, value: T) -> &mut T {
228 let layout = Layout::new::<T>();
229 let current = self.ptr as usize;
230 let aligned = (current + layout.align() - 1) & !(layout.align() - 1);
231 let next = aligned + layout.size();
232
233 assert!(next <= self.end as usize, "ThreadLocalArena overflow!");
234
235 self.ptr = next as *mut u8;
236 unsafe {
237 let target = aligned as *mut T;
238 target.write(value);
239
240 if std::mem::needs_drop::<T>() {
241 unsafe fn drop_glue<T>(ptr: *mut u8) {
242 std::ptr::drop_in_place(ptr as *mut T);
243 }
244 self.drop_chain.push((target as *mut u8, drop_glue::<T>));
245 }
246
247 &mut *target
248 }
249 }
250
251 #[inline(always)]
253 pub fn alloc_copy<T: Copy>(&mut self, value: T) -> &mut T {
254 let layout = Layout::new::<T>();
255 let current = self.ptr as usize;
256 let aligned = (current + layout.align() - 1) & !(layout.align() - 1);
257 let next = aligned + layout.size();
258
259 assert!(next <= self.end as usize, "ThreadLocalArena overflow!");
260
261 self.ptr = next as *mut u8;
262 unsafe {
263 let target = aligned as *mut T;
264 target.write(value);
265 &mut *target
266 }
267 }
268
269 #[inline(always)]
270 pub fn alloc_slice<'a, T: Copy>(&'a mut self, items: &[T]) -> &'a mut [T] {
271 if items.is_empty() {
272 return &mut [];
273 }
274
275 let layout = Layout::array::<T>(items.len()).expect("Array layout error");
276 let current = self.ptr as usize;
277 let aligned = (current + layout.align() - 1) & !(layout.align() - 1);
278 let next = aligned + layout.size();
279
280 assert!(
281 next <= self.end as usize,
282 "ThreadLocalArena slice overflow!"
283 );
284
285 self.ptr = next as *mut u8;
286 unsafe {
287 let target = aligned as *mut T;
288 std::ptr::copy_nonoverlapping(items.as_ptr(), target, items.len());
289 std::slice::from_raw_parts_mut(target, items.len())
290 }
291 }
292
293 #[inline(always)]
294 pub fn reset(&mut self) {
295 for (ptr, drop_fn) in self.drop_chain.drain(..).rev() {
296 unsafe {
297 drop_fn(ptr);
298 }
299 }
300 self.ptr = self.start;
301 }
302
303 #[inline(always)]
304 pub fn bytes_allocated(&self) -> usize {
305 (self.ptr as usize).saturating_sub(self.start as usize)
306 }
307
308 #[inline(always)]
309 pub fn capacity(&self) -> usize {
310 self.capacity
311 }
312}
313
314impl Drop for ThreadLocalArena {
315 fn drop(&mut self) {
316 for (ptr, drop_fn) in self.drop_chain.drain(..).rev() {
317 unsafe {
318 drop_fn(ptr);
319 }
320 }
321 let layout = Layout::from_size_align(self.capacity, 64).unwrap();
322 unsafe {
323 dealloc(self.start, layout);
324 }
325 }
326}