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
use core::nonzero::NonZero;
use std::ptr;
use std::mem;
use std::cell::{Cell, RefCell};
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<Box<GcBox<Trace + 'static>>>,
boxes_end: *mut Option<Box<GcBox<Trace + 'static>>>,
}
thread_local!(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,
boxes_end: ptr::null_mut(),
}));
pub struct GcBoxHeader {
roots: Cell<usize>,
next: Option<Box<GcBox<Trace + 'static>>>,
marked: Cell<bool>,
}
pub struct GcBox<T: Trace + ?Sized + 'static> {
header: GcBoxHeader,
data: T,
}
impl<T: Trace> GcBox<T> {
pub fn new(value: T) -> NonZero<*mut GcBox<T>> {
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 mut gcbox = Box::new(GcBox {
header: GcBoxHeader {
roots: Cell::new(1),
marked: Cell::new(false),
next: None,
},
data: value,
});
let gcbox_ptr = unsafe { NonZero::new(&mut *gcbox as *mut _) };
let next_boxes_end = &mut gcbox.header.next as *mut _;
if st.boxes_end.is_null() {
assert!(st.boxes_start.is_none(),
"If something had been allocated, boxes_end would be set");
st.boxes_end = next_boxes_end;
st.boxes_start = Some(gcbox);
} else {
unsafe {
*st.boxes_end = Some(gcbox);
}
st.boxes_end = next_boxes_end;
}
st.bytes_allocated += mem::size_of::<GcBox<T>>();
gcbox_ptr
})
}
}
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() + 1);
}
pub unsafe fn unroot_inner(&self) {
self.header.roots.set(self.header.roots.get() - 1);
}
pub fn value(&self) -> &T {
GC_SWEEPING.with(|sweeping| assert!(!sweeping.get(),
"Gc pointers may be invalid when GC is running"));
&self.data
}
fn header_mut(&mut self) -> &mut GcBoxHeader {
&mut self.header
}
fn size_of(&self) -> usize { mem::size_of_val(self) }
}
fn collect_garbage(st: &mut GcState) {
let mut next_node = &mut st.boxes_start
as *mut Option<Box<GcBox<Trace + 'static>>>;
while let Some(ref mut node) = *unsafe { &mut *next_node } {
{
let header = node.header_mut();
next_node = &mut header.next as *mut _;
if header.roots.get() == 0 { continue }
}
unsafe { node.trace_inner(); }
}
GC_SWEEPING.with(|collecting| collecting.set(true));
let mut next_node = &mut st.boxes_start
as *mut Option<Box<GcBox<Trace + 'static>>>;
while let Some(ref mut node) = *unsafe { &mut *next_node } {
let size = node.size_of();
let header = node.header_mut();
if header.marked.get() {
header.marked.set(false);
next_node = &mut header.next;
} else {
st.bytes_allocated -= size;
let mut tmp = None;
mem::swap(&mut tmp, &mut header.next);
mem::swap(&mut tmp, unsafe { &mut *next_node });
}
}
st.boxes_end = next_node;
GC_SWEEPING.with(|collecting| collecting.set(false));
}
pub fn force_collect() {
GC_STATE.with(|_st| {
let mut st = _st.borrow_mut();
collect_garbage(&mut *st);
});
}