1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::{
alloc,
cell::{Cell, RefCell},
marker::PhantomData,
mem,
ptr::NonNull,
};
use crate::{
marker::Invariant,
ptr::{Color, GcBox, GcBoxHead, DynGcBoxPtr},
Bound, Gc, Owner, Trace,
};
/// Object passed to [`Trace::trace`] method, used to mark pointers.
#[derive(Clone, Copy)]
pub struct Tracer<'gc, 'own>(&'gc Root<'own>);
impl<'gc, 'own> Tracer<'gc, 'own> {
/// Mark a pointer as alive.
pub fn mark<T: Trace<'own>>(self, ptr: Gc<'_, 'own, T>) {
unsafe {
if ptr.ptr.as_ref().head.color.get() != Color::White {
return;
}
ptr.ptr.as_ref().head.color.set(Color::Gray);
if T::needs_trace() {
let ptr: DynGcBoxPtr<'own, '_> = ptr.ptr.cast::<GcBox<'own, T>>();
let ptr: DynGcBoxPtr<'own, 'static> = mem::transmute(ptr);
self.0.grays.borrow_mut().push(ptr);
}
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Phase {
Sleep,
Wake,
Mark,
Sweep,
}
/// A guard which will keep a value on the stack rooted for as long as it is alive.
///
/// This struct should not be used in safe code.
pub struct RootGuard<'own>(*const Root<'own>);
impl<'own> RootGuard<'own> {
/// Rebind a value to the lifetime of the root guard.
/// # Safety
/// This method should only ever be called with the object this root guard was created in
/// [`Root::root_gc`].
pub unsafe fn bind<R: Bound<'own>>(&self, r: R) -> R::Rebound {
crate::rebind(r)
}
}
impl<'cell> Drop for RootGuard<'cell> {
fn drop(&mut self) {
unsafe {
(*self.0).roots.borrow_mut().pop();
}
}
}
/// Root of the dreck GC, used for allocating [`Gc`] pointers and run collection.
pub struct Root<'own> {
roots: RefCell<Vec<DynGcBoxPtr<'own, 'static>>>,
grays: RefCell<Vec<DynGcBoxPtr<'own, 'static>>>,
grays_again: RefCell<Vec<DynGcBoxPtr<'own, 'static>>>,
sweep: Option<DynGcBoxPtr<'own, 'static>>,
sweep_prev: Cell<Option<DynGcBoxPtr<'own, 'static>>>,
all: Cell<Option<DynGcBoxPtr<'own, 'static>>>,
total_allocated: Cell<usize>,
remembered_size: usize,
wakeup_total: usize,
allocation_debt: Cell<f64>,
phase: Cell<Phase>,
_own: Invariant<'own>,
}
impl<'own> Root<'own> {
const PAUSE_FACTOR: f64 = 0.5;
const TIMING_FACTOR: f64 = 1.5;
const MIN_SLEEP: usize = 4096;
/// Create a new `Root`.
///
/// This function is unsafe as creating two roots with the same `'own` lifetime is unsound. Prefer
/// the use of the [`new_root!`](crate::new_root!) macro.
///
/// # Safety
/// The `'own` lifetime must be different from all other [`Root`] objects created.
pub unsafe fn new(owner: &Owner<'own>) -> Self {
Root {
roots: RefCell::new(Vec::new()),
grays: RefCell::new(Vec::new()),
grays_again: RefCell::new(Vec::new()),
sweep: None,
sweep_prev: Cell::new(None),
all: Cell::new(None),
total_allocated: Cell::new(0),
remembered_size: 0,
wakeup_total: Self::MIN_SLEEP,
allocation_debt: Cell::new(0.0),
phase: Cell::new(Phase::Sleep),
_own: owner.0,
}
}
pub(crate) unsafe fn add_raw<T: Trace<'own>>(&self, v: T) -> NonNull<GcBox<'own, T>> {
let layout = alloc::Layout::new::<GcBox<T>>();
let ptr = NonNull::new(alloc::alloc(layout).cast::<GcBox<T>>()).unwrap();
ptr.as_ptr().write(GcBox {
head: GcBoxHead {
color: Cell::new(Color::White),
next: Cell::new(self.all.get()),
},
value: v,
});
self.total_allocated
.set(self.total_allocated.get() + layout.size());
if self.phase.get() == Phase::Sleep && self.total_allocated.get() > self.wakeup_total {
self.phase.set(Phase::Wake);
}
if self.phase.get() != Phase::Sleep {
self.allocation_debt.set(
self.allocation_debt.get()
+ layout.size() as f64
+ layout.size() as f64 / Self::TIMING_FACTOR,
);
}
let dyn_ptr: DynGcBoxPtr = ptr;
self.all.set(Some(mem::transmute(dyn_ptr)));
if self.phase.get() == Phase::Sweep && self.sweep_prev.get().is_none() {
self.sweep_prev.set(self.all.get());
}
ptr
}
/// Allocated a value as a garbage collected pointer.
#[must_use]
pub fn add<'gc, T, R>(&'gc self, v: T) -> Gc<'gc, 'own, R>
where
T: Bound<'gc, Rebound = R>,
R: Trace<'own>,
{
unsafe {
let ptr = self.add_raw(crate::rebind(v));
Gc {
ptr,
_gc: PhantomData,
_own: Invariant::new(),
}
}
}
/// Indicate a point at which garbage collection can run.
///
/// The GC will only run if enough objects have been allocated.
/// As the GC is incremental it will also only run only a part of the collection cycle.
pub fn collect(&mut self, _owner: &Owner<'own>) {
unsafe { self.inner_collect() };
}
/// Run a full cycle of the garbage collection.
///
/// Unlike [`Root::collect`] this method will allways collect all unreachable Gc'd objects.
pub fn collect_full(&mut self, _owner: &Owner<'own>) {
self.allocation_debt.set(f64::INFINITY);
self.phase.set(Phase::Wake);
unsafe { self.inner_collect() };
}
/// Mark a pointer value as possibly containing new [`Gc`] pointers.
///
/// In safe code you should never have to call this method as the [`Gc`] struct will manage
/// write barriers for you.
///
/// If a type has an unsafe trace implementation and could ever contain new GC'd values within
/// itself, One must call this function on objects of that type before running collection, everytime that object could
/// possibly contain new GC'd values.
#[inline]
pub fn write_barrier<T: Trace<'own>>(&self, gc: Gc<'_, 'own, T>) {
if !T::needs_trace() {
return;
}
unsafe {
if self.phase.get() == Phase::Mark && gc.ptr.as_ref().head.color.get() == Color::Black {
gc.ptr.as_ref().head.color.set(Color::Gray);
let ptr: DynGcBoxPtr<'own, '_> = gc.ptr.cast::<GcBox<T>>();
let ptr: DynGcBoxPtr<'own, 'static> = mem::transmute(ptr);
self.grays_again.borrow_mut().push(ptr);
}
}
}
/// Rebind a pointer to the lifetime of this root guard.
///
/// On should prefer the [`rebind!`](crate::rebind!) macro instead of this function as it is more permissive
/// with which pointers it allows rebinding.
pub fn rebind_to<'r, T: Trace<'own> + Bound<'r> + 'r>(
&'r self,
t: Gc<'_, 'own, T>,
) -> Gc<'r, 'own, T::Rebound>
where
T::Rebound: Trace<'own>,
{
unsafe { crate::rebind(t) }
}
/// Root a gc pointer for the duration of root guard's lifetime.
/// Prefer the use of the [`root!`](crate::root!) macro.
///
/// # Safety
/// - The `Root` object must outlife the returned `RootGuard`
/// - All `RootGuard`'s must be dropped in the reverse order of which they where created.
pub unsafe fn root_gc<T: Trace<'own>>(&self, t: Gc<'_, 'own, T>) -> RootGuard<'own> {
let ptr: DynGcBoxPtr<'own, '_> = t.ptr.cast::<GcBox<T>>();
let ptr: DynGcBoxPtr<'own, 'static> = std::mem::transmute(ptr);
self.roots.borrow_mut().push(ptr);
RootGuard(self)
}
unsafe fn inner_collect(&mut self) {
if self.phase.get() == Phase::Sleep {
return;
}
let work = self.allocation_debt.get();
let mut work_done = 0usize;
while work > work_done as f64 {
match self.phase.get() {
Phase::Wake => {
self.sweep_prev.set(None);
for root in self.roots.borrow().iter() {
root.as_ref().head.color.set(Color::Black);
}
for root in self.roots.borrow().iter() {
root.as_ref().value.trace(Tracer(self));
work_done += mem::size_of_val(root.as_ref());
}
self.phase.set(Phase::Mark);
}
Phase::Mark => {
let ptr = self.grays.borrow_mut().pop();
if let Some(ptr) = ptr {
work_done += mem::size_of_val(ptr.as_ref());
ptr.as_ref().value.trace(Tracer(self));
ptr.as_ref().head.color.set(Color::Black);
} else if let Some(ptr) = self.grays_again.borrow_mut().pop() {
ptr.as_ref().value.trace(Tracer(self));
ptr.as_ref().head.color.set(Color::Black);
} else {
self.phase.set(Phase::Sweep);
self.sweep = self.all.get();
self.remembered_size = 0;
}
}
Phase::Sweep => {
if let Some(ptr) = self.sweep {
self.sweep = ptr.as_ref().head.next.get();
let layout = alloc::Layout::for_value(ptr.as_ref());
if ptr.as_ref().head.color.get() == Color::White {
if let Some(prev) = self.sweep_prev.get() {
prev.as_ref().head.next.set(ptr.as_ref().head.next.get());
} else {
self.all.set(ptr.as_ref().head.next.get());
}
self.total_allocated
.set(self.total_allocated.get() - layout.size());
} else {
self.remembered_size += layout.size();
ptr.as_ref().head.color.set(Color::White);
self.sweep_prev.set(Some(ptr));
}
} else {
self.phase.set(Phase::Sleep);
self.allocation_debt.set(0.0);
self.wakeup_total = self.total_allocated.get()
+ ((self.remembered_size as f64 * Self::PAUSE_FACTOR)
.round()
.min(usize::MAX as f64) as usize)
.max(Self::MIN_SLEEP);
return;
}
}
Phase::Sleep => break,
}
}
}
}