atmos/kernel/
kill_drop.rs1use alloc::boxed::Box;
27use core::marker::PhantomData;
28use core::ptr::NonNull;
29use core::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
30
31pub const ROWS: usize = 16;
33pub const SLOTS: usize = 32;
35
36pub struct KillDropTable {
38 ptr: [[AtomicUsize; SLOTS]; ROWS],
40 drop_fn: [[AtomicUsize; SLOTS]; ROWS],
42 seq: [[AtomicU32; SLOTS]; ROWS],
44 next_seq: [AtomicU32; ROWS],
46}
47
48impl Default for KillDropTable {
49 fn default() -> Self {
50 Self::new()
51 }
52}
53
54type DropFn = unsafe fn(*mut u8);
56
57unsafe fn drop_box<T>(p: *mut u8) {
62 drop(unsafe { Box::from_raw(p as *mut T) });
64}
65
66impl KillDropTable {
67 pub const fn new() -> Self {
68 Self {
69 ptr: [const { [const { AtomicUsize::new(0) }; SLOTS] }; ROWS],
70 drop_fn: [const { [const { AtomicUsize::new(0) }; SLOTS] }; ROWS],
71 seq: [const { [const { AtomicU32::new(0) }; SLOTS] }; ROWS],
72 next_seq: [const { AtomicU32::new(0) }; ROWS],
73 }
74 }
75
76 fn register(&self, row: usize, p: usize, f: DropFn) -> Option<usize> {
78 let ptrs = self.ptr.get(row)?;
79 let fns = self.drop_fn.get(row)?;
80 let seqs = self.seq.get(row)?;
81 let next = self.next_seq.get(row)?;
82 for (e, slot) in ptrs.iter().enumerate() {
83 if slot.load(Ordering::Acquire) != 0 {
84 continue;
85 }
86 fns.get(e)?.store(f as usize, Ordering::Relaxed);
88 seqs.get(e)?.store(next.fetch_add(1, Ordering::Relaxed), Ordering::Relaxed);
89 slot.store(p, Ordering::Release);
90 return Some(e);
91 }
92 None
93 }
94
95 fn release(&self, row: usize, e: usize, p: usize) -> bool {
98 match self.ptr.get(row).and_then(|r| r.get(e)) {
99 Some(slot) => slot.compare_exchange(p, 0, Ordering::AcqRel, Ordering::Relaxed).is_ok(),
100 None => false,
101 }
102 }
103
104 pub fn reset_row(&self, row: usize) {
107 if let Some(r) = self.ptr.get(row) {
108 for slot in r {
109 slot.store(0, Ordering::Release);
110 }
111 }
112 }
113
114 pub fn live_in_row(&self, row: usize) -> usize {
116 self.ptr
117 .get(row)
118 .map(|r| r.iter().filter(|s| s.load(Ordering::Acquire) != 0).count())
119 .unwrap_or(0)
120 }
121
122 pub unsafe fn drain(&self, row: usize) -> usize {
129 let (Some(ptrs), Some(fns), Some(seqs)) = (self.ptr.get(row), self.drop_fn.get(row), self.seq.get(row)) else {
130 return 0;
131 };
132 let mut n = 0;
133 loop {
134 let mut best: Option<(usize, u32)> = None;
136 for (e, slot) in ptrs.iter().enumerate() {
137 if slot.load(Ordering::Acquire) == 0 {
138 continue;
139 }
140 let s = seqs.get(e).map(|x| x.load(Ordering::Relaxed)).unwrap_or(0);
141 if best.is_none_or(|(_, bs)| s > bs) {
143 best = Some((e, s));
144 }
145 }
146 let Some((e, _)) = best else { break };
147 let Some(slot) = ptrs.get(e) else { break };
148 let p = slot.swap(0, Ordering::AcqRel);
149 let f = fns.get(e).map(|x| x.load(Ordering::Relaxed)).unwrap_or(0);
150 if p != 0 && f != 0 {
151 unsafe {
154 let f: DropFn = core::mem::transmute::<usize, DropFn>(f);
155 f(p as *mut u8);
156 }
157 n += 1;
158 }
159 }
160 n
161 }
162}
163
164pub struct KillOwned<T> {
166 ptr: NonNull<T>,
167 slot: Option<(&'static KillDropTable, usize, usize)>,
169 _own: PhantomData<(T, *const ())>,
171}
172
173impl<T> KillOwned<T> {
174 pub fn new_in(table: &'static KillDropTable, row: Option<usize>, value: T) -> Self {
176 let raw = Box::into_raw(Box::new(value));
177 let ptr = unsafe { NonNull::new_unchecked(raw) };
179 let slot = row.and_then(|r| table.register(r, raw as usize, drop_box::<T> as DropFn).map(|e| (table, r, e)));
180 Self { ptr, slot, _own: PhantomData }
181 }
182
183 pub fn is_registered(&self) -> bool {
185 self.slot.is_some()
186 }
187}
188
189impl<T> core::ops::Deref for KillOwned<T> {
190 type Target = T;
191 fn deref(&self) -> &T {
192 unsafe { self.ptr.as_ref() }
194 }
195}
196
197impl<T> core::ops::DerefMut for KillOwned<T> {
198 fn deref_mut(&mut self) -> &mut T {
199 unsafe { self.ptr.as_mut() }
201 }
202}
203
204impl<T> Drop for KillOwned<T> {
205 fn drop(&mut self) {
206 let mine = match self.slot {
207 Some((table, row, e)) => table.release(row, e, self.ptr.as_ptr() as usize),
208 None => true,
209 };
210 if mine {
211 drop(unsafe { Box::from_raw(self.ptr.as_ptr()) });
213 }
214 }
215}