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
#![feature(
negative_impls,
untagged_unions,
const_fn,
)]
use std::mem::ManuallyDrop;
use std::fmt::{self, Debug, Formatter};
use zerogc::prelude::*;
pub mod state;
pub mod utils;
pub mod collector;
pub mod handle;
use crate::collector::{RawCollectorImpl};
pub use crate::collector::{WeakCollectorRef, CollectorRef, CollectorId};
pub use crate::state::{CollectionManager, RawContext};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ContextState {
Active,
SafePoint {
collection_id: u64
},
#[cfg_attr(not(feature = "sync"), allow(unused))]
Frozen,
}
impl ContextState {
#[cfg_attr(not(feature = "sync"), allow(unused))]
fn is_frozen(&self) -> bool {
matches!(*self, ContextState::Frozen)
}
}
pub struct CollectorContext<C: RawCollectorImpl> {
raw: *mut C::RawContext,
root: bool
}
impl<C: RawCollectorImpl> CollectorContext<C> {
pub(crate) unsafe fn register_root(collector: &CollectorRef<C>) -> Self {
CollectorContext {
raw: Box::into_raw(ManuallyDrop::into_inner(
C::RawContext::register_new(&collector)
)),
root: true,
}
}
#[inline]
pub fn collector(&self) -> &C {
unsafe { &(*self.raw).collector() }
}
#[inline(always)]
unsafe fn with_shadow_stack<R, T: Trace>(
&self, value: *mut &mut T, func: impl FnOnce() -> R
) -> R {
let old_link = (*(*self.raw).shadow_stack_ptr()).last;
let new_link = ShadowStackLink {
element: C::create_dyn_pointer(value),
prev: old_link
};
(*(*self.raw).shadow_stack_ptr()).last = &new_link;
let result = func();
debug_assert_eq!(
(*(*self.raw).shadow_stack_ptr()).last,
&new_link
);
(*(*self.raw).shadow_stack_ptr()).last = new_link.prev;
result
}
#[cold]
unsafe fn trigger_basic_safepoint<T: Trace>(&self, element: &mut &mut T) {
self.with_shadow_stack(element, || {
(*self.raw).trigger_safepoint();
})
}
}
impl<C: RawCollectorImpl> Drop for CollectorContext<C> {
#[inline]
fn drop(&mut self) {
if self.root {
unsafe {
C::Manager::free_context(self.collector(), self.raw);
}
}
}
}
unsafe impl<C: RawCollectorImpl> GcContext for CollectorContext<C> {
type System = CollectorRef<C>;
type Id = CollectorId<C>;
#[inline]
unsafe fn basic_safepoint<T: Trace>(&mut self, value: &mut &mut T) {
debug_assert_eq!((*self.raw).state(), ContextState::Active);
if (*self.raw).collector().should_collect() {
self.trigger_basic_safepoint(value);
}
debug_assert_eq!((*self.raw).state(), ContextState::Active);
}
unsafe fn freeze(&mut self) {
(*self.raw).collector().manager().freeze_context(&*self.raw);
}
unsafe fn unfreeze(&mut self) {
(*self.raw).collector().manager().unfreeze_context(&*self.raw);
}
#[inline]
unsafe fn recurse_context<T, F, R>(&self, value: &mut &mut T, func: F) -> R
where T: Trace, F: for<'gc> FnOnce(&'gc mut Self, &'gc mut T) -> R {
debug_assert_eq!((*self.raw).state(), ContextState::Active);
self.with_shadow_stack(value, || {
let mut sub_context = ManuallyDrop::new(CollectorContext {
raw: self.raw,
root: false
});
let result = func(&mut *sub_context, value);
debug_assert!(!sub_context.root);
std::mem::forget(sub_context);
debug_assert_eq!((*self.raw).state(), ContextState::Active);
result
})
}
}
impl<C: RawCollectorImpl> !Send for CollectorContext<C> {}
#[repr(C)]
#[derive(Debug)]
pub(crate) struct ShadowStackLink<T> {
pub element: T,
pub prev: *const ShadowStackLink<T>
}
impl<C: RawCollectorImpl> Debug for ShadowStack<C> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("ShadowStack")
.field("last", &format_args!("{:p}", self.last))
.finish()
}
}
#[derive(Clone)]
pub struct ShadowStack<C: RawCollectorImpl> {
pub(crate) last: *const ShadowStackLink<C::GcDynPointer>
}
impl<C: RawCollectorImpl> ShadowStack<C> {
unsafe fn as_vec(&self) -> Vec<C::GcDynPointer> {
let mut result: Vec<_> = self.reverse_iter().collect();
result.reverse();
result
}
#[inline]
pub unsafe fn reverse_iter(&self) -> impl Iterator<Item=C::GcDynPointer> + '_ {
std::iter::successors(
self.last.as_ref(),
|link| link.prev.as_ref()
).map(|link| link.element)
}
}