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
use std::cell::{Cell, RefCell};
use std::mem;
use std::ptr::Shared;
use trace::Trace;
const INITIAL_THRESHOLD: usize = 100;
const USED_SPACE_RATIO: f64 = 0.7;
struct GcState {
bytes_allocated: usize,
threshold: usize,
boxes_start: Option<Shared<GcBox<Trace>>>,
}
impl Drop for GcState {
fn drop(&mut self) {
unsafe {
GC_SWEEPING.with(|collecting| collecting.set(true));
while let Some(node) = self.boxes_start {
let node = Box::from_raw(*node);
self.boxes_start = node.header.next;
}
}
}
}
thread_local!(pub static GC_SWEEPING: Cell<bool> = Cell::new(false));
thread_local!(static GC_STATE: RefCell<GcState> = RefCell::new(GcState {
bytes_allocated: 0,
threshold: INITIAL_THRESHOLD,
boxes_start: None,
}));
pub struct GcBoxHeader {
roots: Cell<usize>,
next: Option<Shared<GcBox<Trace>>>,
marked: Cell<bool>,
}
pub struct GcBox<T: Trace + ?Sized + 'static> {
header: GcBoxHeader,
data: T,
}
impl<T: Trace> GcBox<T> {
pub fn new(value: T) -> Shared<Self> {
GC_STATE.with(|st| {
let mut st = st.borrow_mut();
if st.bytes_allocated > st.threshold {
collect_garbage(&mut *st);
if st.bytes_allocated as f64 > st.threshold as f64 * USED_SPACE_RATIO {
st.threshold = (st.bytes_allocated as f64 / USED_SPACE_RATIO) as usize
}
}
let gcbox = unsafe {
Shared::new(Box::into_raw(Box::new(GcBox {
header: GcBoxHeader {
roots: Cell::new(1),
marked: Cell::new(false),
next: st.boxes_start.take(),
},
data: value,
})))
};
st.boxes_start = Some(gcbox);
st.bytes_allocated += mem::size_of::<GcBox<T>>();
gcbox
})
}
}
impl<T: Trace + ?Sized> GcBox<T> {
pub unsafe fn trace_inner(&self) {
let marked = self.header.marked.get();
if !marked {
self.header.marked.set(true);
self.data.trace();
}
}
pub unsafe fn root_inner(&self) {
self.header.roots.set(self.header.roots.get()
.checked_add(1).unwrap_or_else(|| ::std::intrinsics::abort()));
}
pub unsafe fn unroot_inner(&self) {
self.header.roots.set(self.header.roots.get() - 1);
}
pub fn value(&self) -> &T {
&self.data
}
fn size_of(&self) -> usize { mem::size_of_val(self) }
}
fn collect_garbage(st: &mut GcState) {
unsafe fn mark(mut head: Option<Shared<GcBox<Trace>>>) {
while let Some(node) = head {
if (**node).header.roots.get() > 0 {
(**node).trace_inner();
}
head = (**node).header.next;
}
}
unsafe fn sweep(mut head: &mut Option<Shared<GcBox<Trace>>>, bytes_allocated: &mut usize) {
GC_SWEEPING.with(|collecting| collecting.set(true));
while let Some(node) = *head {
if (**node).header.marked.get() {
(**node).header.marked.set(false);
head = &mut (**node).header.next;
} else {
let mut node = Box::from_raw(*node);
*bytes_allocated -= node.size_of();
*head = node.header.next.take();
}
}
GC_SWEEPING.with(|collecting| collecting.set(false));
}
unsafe {
mark(st.boxes_start);
sweep(&mut st.boxes_start, &mut st.bytes_allocated);
}
}
pub fn force_collect() {
GC_STATE.with(|st| {
let mut st = st.borrow_mut();
collect_garbage(&mut *st);
});
}