solar_data_structures/
drop_arena.rs1use bumpalo::Bump;
2use std::{
3 cell::RefCell,
4 ptr::{self, NonNull},
5};
6
7pub struct DropArena {
9 bump: Bump,
10 drops: RefCell<Vec<DropEntry>>,
11}
12
13unsafe impl Send for DropArena {}
15
16struct DropEntry {
17 ptr: NonNull<()>,
18 len: usize,
19 drop: unsafe fn(NonNull<()>, usize),
20}
21
22struct DropEntries(*mut DropArena);
23
24impl Drop for DropEntries {
25 fn drop(&mut self) {
26 unsafe { (*self.0).drop_entries() };
28 }
29}
30
31impl DropArena {
32 #[inline]
34 pub fn new() -> Self {
35 Self { bump: Bump::new(), drops: RefCell::new(Vec::new()) }
36 }
37
38 #[inline]
40 pub fn alloc<T: Send>(&self, value: T) -> &mut T {
41 let value = self.bump.alloc(value);
42 self.register::<T>(NonNull::from(&mut *value).cast(), 1, drop_value::<T>);
43 value
44 }
45
46 #[inline]
48 pub fn alloc_slice_fill_iter<T: Send, I>(&self, iter: I) -> &mut [T]
49 where
50 I: IntoIterator<Item = T>,
51 I::IntoIter: ExactSizeIterator,
52 {
53 let values = self.bump.alloc_slice_fill_iter(iter);
54 if let Some(ptr) = NonNull::new(values.as_mut_ptr()) {
55 self.register::<T>(ptr.cast(), values.len(), drop_slice::<T>);
56 }
57 values
58 }
59
60 #[inline]
61 fn register<T>(&self, ptr: NonNull<()>, len: usize, drop: unsafe fn(NonNull<()>, usize)) {
62 if std::mem::needs_drop::<T>() && len != 0 {
63 self.drops.borrow_mut().push(DropEntry { ptr, len, drop });
64 }
65 }
66
67 unsafe fn drop_entries(&self) {
68 loop {
69 let Some(current) = self.drops.borrow_mut().pop() else { return };
70 unsafe { (current.drop)(current.ptr, current.len) };
72 }
73 }
74}
75
76impl Default for DropArena {
77 #[inline]
78 fn default() -> Self {
79 Self::new()
80 }
81}
82
83impl Drop for DropArena {
84 fn drop(&mut self) {
85 let _guard = DropEntries(self);
86 unsafe { self.drop_entries() };
88 }
89}
90
91unsafe fn drop_value<T>(ptr: NonNull<()>, _: usize) {
92 unsafe { ptr.cast::<T>().drop_in_place() };
94}
95
96unsafe fn drop_slice<T>(ptr: NonNull<()>, len: usize) {
97 unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(ptr.cast::<T>().as_ptr(), len)) };
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105 use std::{
106 panic::{AssertUnwindSafe, catch_unwind},
107 sync::{Arc, Mutex},
108 };
109
110 struct DropCounter(Arc<Mutex<Vec<usize>>>, usize);
111
112 impl Drop for DropCounter {
113 fn drop(&mut self) {
114 self.0.lock().unwrap().push(self.1);
115 }
116 }
117
118 struct PanicOnDrop;
119
120 impl Drop for PanicOnDrop {
121 fn drop(&mut self) {
122 panic!("boom");
123 }
124 }
125
126 #[test]
127 fn drops_allocated_values() {
128 let dropped = Arc::new(Mutex::new(Vec::new()));
129 {
130 let arena = DropArena::new();
131 arena.alloc(DropCounter(dropped.clone(), 1));
132 arena.alloc_slice_fill_iter([2, 3].map(|i| DropCounter(dropped.clone(), i)));
133 }
134 assert_eq!(*dropped.lock().unwrap(), [2, 3, 1]);
135 }
136
137 #[test]
138 fn drops_remaining_values_after_panic() {
139 let dropped = Arc::new(Mutex::new(Vec::new()));
140 let result = catch_unwind(AssertUnwindSafe(|| {
141 let arena = DropArena::new();
142 arena.alloc(DropCounter(dropped.clone(), 1));
143 arena.alloc(PanicOnDrop);
144 }));
145 assert!(result.is_err());
146 assert_eq!(*dropped.lock().unwrap(), [1]);
147 }
148}