-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
662 lines (637 loc) · 22.3 KB
/
lib.rs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// TODO: Use stable rust
#![feature(
alloc_layout_extra, // Used for GcObject::from_raw
never_type, // Used for errors (which are currently impossible)
negative_impls, // impl !Send is much cleaner than PhantomData<Rc<()>>
exhaustive_patterns, // Allow exhaustive matching against never
const_alloc_layout, // Used for StaticType
const_fn, // We statically create type info
const_if_match, // Used for StaticType
const_panic, // Const panic should be stable
const_transmute, // This can already be acheived with unions...
untagged_unions, // Why isn't this stable?
new_uninit, // Until Rust has const generics, this is how we init arrays..
specialization, // Used for specialization (effectively required by GcRef)
)]
use zerogc::{GcSystem, GcSafe, Trace, GcContext, GcVisitor, GcSimpleAlloc, GcRef, GcBrand};
use std::alloc::Layout;
use std::cell::{RefCell, Cell};
use std::ptr::NonNull;
use std::rc::Rc;
use std::os::raw::c_void;
use std::mem::{transmute, ManuallyDrop};
use crate::alloc::{SmallArenaList, small_object_size};
use std::ops::Deref;
use std::hash::{Hash, Hasher};
use std::fmt::{Debug, Formatter};
use std::fmt;
use std::marker::PhantomData;
#[cfg(feature = "small-object-arenas")]
mod alloc;
#[cfg(not(feature = "small-object-arenas"))]
mod alloc {
pub const fn is_small_object<T>() -> bool {
false
}
pub const fn small_object_size<T>() -> usize {
unimplemented!()
}
pub struct FakeArena;
impl FakeArena {
pub(crate) fn alloc(&self) -> std::ptr::NonNull<super::GcHeader> {
unimplemented!()
}
}
pub struct SmallArenaList;
impl SmallArenaList {
// Create dummy
pub fn new() -> Self { SmallArenaList }
pub fn find<T>(&self) -> Option<FakeArena> { None }
}
}
/// A garbage collected pointer
///
/// See docs for [zerogc::GcRef]
pub struct Gc<'gc, T: GcSafe + 'gc> {
/// Used to uniquely identify the collector,
/// to ensure we aren't modifying another collector's pointers
///
/// As long as our memory is valid,
/// it implies this pointer is too..
collector_ptr: NonNull<RawSimpleCollector>,
value: NonNull<T>,
marker: PhantomData<&'gc T>
}
impl<'gc, T: GcSafe + 'gc> Gc<'gc, T> {
#[inline]
pub(crate) unsafe fn from_raw(
collector_ptr: NonNull<RawSimpleCollector>,
value: NonNull<T>
) -> Self {
Gc { collector_ptr, value, marker: PhantomData }
}
}
impl<'gc, T: GcSafe + 'gc> GcRef<'gc, T> for Gc<'gc, T> {
type System = SimpleCollector;
#[inline]
fn value(&self) -> &'gc T {
unsafe { &mut *self.value.as_ptr() }
}
}
/// Double-indirection is completely safe
unsafe impl<'gc, T: GcSafe + 'gc> GcSafe for Gc<'gc, T> {
const NEEDS_DROP: bool = true; // We are Copy
}
/// Rebrand
unsafe impl<'gc, 'new_gc, T> GcBrand<'new_gc, SimpleCollector> for Gc<'gc, T>
where T: GcSafe + GcBrand<'new_gc, SimpleCollector>,
T::Branded: GcSafe {
type Branded = Gc<'new_gc, <T as GcBrand<'new_gc, SimpleCollector>>::Branded>;
}
unsafe impl<'gc, T: GcSafe + 'gc> Trace for Gc<'gc, T> {
// We always need tracing....
const NEEDS_TRACE: bool = true;
#[inline]
fn visit<V: GcVisitor>(&mut self, visitor: &mut V) -> Result<(), V::Err> {
<V as GcVisit>::visit_gc(visitor, self);
Ok(())
}
}
impl<'gc, T: GcSafe + 'gc> Deref for Gc<'gc, T> {
type Target = &'gc T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*(&self.value as *const NonNull<T> as *const &'gc T) }
}
}
// We can be copied freely :)
impl<'gc, T: GcSafe + 'gc> Copy for Gc<'gc, T> {}
impl<'gc, T: GcSafe + 'gc> Clone for Gc<'gc, T> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
// Delegating impls
impl<'gc, T: GcSafe + Hash + 'gc> Hash for Gc<'gc, T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value().hash(state)
}
}
impl<'gc, T: GcSafe + PartialEq + 'gc> PartialEq for Gc<'gc, T> {
fn eq(&self, other: &Self) -> bool {
// NOTE: We compare by value, not identity
self.value() == other.value()
}
}
impl<'gc, T: GcSafe + Eq + 'gc> Eq for Gc<'gc, T> {}
impl<'gc, T: GcSafe + PartialEq + 'gc> PartialEq<T> for Gc<'gc, T> {
fn eq(&self, other: &T) -> bool {
self.value() == other
}
}
impl<'gc, T: GcSafe + Debug + 'gc> Debug for Gc<'gc, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if !f.alternate() {
// Pretend we're a newtype by default
f.debug_tuple("Gc").field(self.value()).finish()
} else {
// Alternate spec reveals `collector_ptr`
f.debug_struct("Gc")
.field("collector_ptr", &self.collector_ptr)
.field("value", self.value())
.finish()
}
}
}
// Visitor (specialized trait)
trait GcVisit: GcVisitor {
fn visit_gc<'gc, T: GcSafe + 'gc>(&mut self, gc: &mut Gc<'gc, T>);
}
impl<V: GcVisitor> GcVisit for V {
#[inline]
default fn visit_gc<'gc, T: GcSafe + 'gc>(&mut self, _gc: &mut Gc<'gc, T>) {}
}
pub struct SimpleCollector(Rc<RawSimpleCollector>);
impl SimpleCollector {
pub fn create() -> Self {
let collector = Rc::new(RawSimpleCollector {
shadow_stack: RefCell::new(ShadowStack(Vec::new())),
heap: GcHeap {
threshold: Cell::new(INITIAL_COLLECTION_THRESHOLD),
allocator: SimpleAlloc::new(
std::ptr::null_mut() // This is initialized later
)
}
});
collector.heap.allocator.collector_ptr.set(
&*collector as *const RawSimpleCollector
as *mut RawSimpleCollector
);
SimpleCollector(collector)
}
/// Make this collector into a context
#[inline]
pub fn into_context(self) -> SimpleCollectorContext {
SimpleCollectorContext {
collector: self.0
}
}
}
unsafe impl GcSystem for SimpleCollector {}
trait DynTrace {
fn trace(&mut self, visitor: &mut MarkVisitor);
}
impl<T: Trace + ?Sized> DynTrace for T {
fn trace(&mut self, visitor: &mut MarkVisitor) {
let Ok(()) = self.visit(visitor);
}
}
#[derive(Clone)]
struct ShadowStack(Vec<*mut dyn DynTrace>);
impl ShadowStack {
#[inline]
pub unsafe fn push<'a, T: Trace + 'a>(&mut self, value: &'a mut T) -> *mut dyn DynTrace {
let short_ptr = value as &mut (dyn DynTrace + 'a)
as *mut (dyn DynTrace + 'a);
let long_ptr = std::mem::transmute::<
*mut (dyn DynTrace + 'a),
*mut (dyn DynTrace + 'static)
>(short_ptr);
self.0.push(long_ptr);
long_ptr
}
#[inline]
pub fn pop(&mut self) -> Option<*mut dyn DynTrace> {
self.0.pop()
}
}
/// The initial memory usage to start a collection
const INITIAL_COLLECTION_THRESHOLD: usize = 2048;
struct GcHeap {
threshold: Cell<usize>,
allocator: SimpleAlloc
}
impl GcHeap {
#[inline]
fn should_collect(&self) -> bool {
self.allocator.allocated_size() >= self.threshold.get()
}
}
/// A link in the chain of `BigGcObject`s
type BigObjectLink = Option<NonNull<BigGcObject<DynamicObj>>>;
pub struct SimpleAlloc {
// NOTE: We lazy init this...
collector_ptr: Cell<*mut RawSimpleCollector>,
allocated_size: Cell<usize>,
small_arenas: SmallArenaList,
big_object_link: Cell<BigObjectLink>
}
impl SimpleAlloc {
fn new(collector_ptr: *mut RawSimpleCollector) -> SimpleAlloc {
SimpleAlloc {
collector_ptr: Cell::new(collector_ptr),
allocated_size: Cell::new(0),
small_arenas: SmallArenaList::new(),
big_object_link: Cell::new(None)
}
}
#[inline]
fn allocated_size(&self) -> usize {
self.allocated_size.get()
}
#[inline]
fn alloc<T: GcSafe>(&self, value: T) -> Gc<'_, T> {
if let Some(arena) = self.small_arenas.find::<T>() {
let header = arena.alloc();
unsafe {
header.as_ptr().write(GcHeader {
type_info: T::STATIC_TYPE,
state: MarkState::White
});
let value_ptr = header.as_ref().value().cast::<T>();
value_ptr.write(value);
self.allocated_size.set(
self.allocated_size.get()
+ small_object_size::<T>()
);
Gc::from_raw(
NonNull::new_unchecked(self.collector_ptr.get()),
NonNull::new_unchecked(value_ptr),
)
}
} else {
self.alloc_big(value)
}
}
fn alloc_big<T: GcSafe>(&self, value: T) -> Gc<'_, T> {
let mut object = Box::new(BigGcObject {
header: GcHeader { type_info: T::STATIC_TYPE, state: MarkState::White },
static_value: ManuallyDrop::new(value),
prev: self.big_object_link.get(),
});
let gc = unsafe { Gc::from_raw(
NonNull::new_unchecked(self.collector_ptr.get()),
NonNull::new_unchecked(&mut *object.static_value),
) };
{
let size = std::mem::size_of::<BigGcObject<T>>();
unsafe { self.big_object_link.set(Some(NonNull::new_unchecked(
Box::into_raw(BigGcObject::into_dynamic_box(object))
))); }
self.allocated_size.set(self.allocated_size() + size);
}
gc
}
unsafe fn sweep<'a>(&self, _roots: &[*mut (dyn DynTrace + 'a)]) {
let mut expected_size = self.allocated_size.get();
let mut actual_size = 0;
// Clear small arenas
#[cfg(feature = "small-object-arenas")]
for arena in self.small_arenas.iter() {
let mut last_free = arena.free.get();
arena.for_each(|slot| {
if (*slot).is_free() {
/*
* Just ignore this. It's already part of our linked list
* of allocated objects.
*/
} else {
match (*slot).header.state {
MarkState::White => {
// Free the object, dropping if necessary
expected_size -= (*slot).header.type_info.total_size();
if let Some(drop) = (*slot).header
.type_info.drop_func {
drop((*slot).header.value());
}
// Add to free list
(*slot).mark_free(last_free);
last_free = Some(NonNull::new_unchecked(slot));
},
MarkState::Grey => panic!("All grey objects should've been processed"),
MarkState::Black => {
// Retain the object
actual_size += (*slot).header.type_info.total_size();
// Reset state to white
(*slot).header.state = MarkState::White;
},
}
}
});
arena.free.set(last_free);
}
// Clear large objects
let mut last_linked = None;
while let Some(big_link) = self.big_object_link.get() {
let obj = &mut *big_link.as_ptr();
self.big_object_link.set(obj.prev);
match obj.header.state {
MarkState::White => {
// Free the object
expected_size -= obj.header.type_info.total_size();
drop(Box::from_raw(obj));
},
MarkState::Grey => panic!("All gray objects should've been processed"),
MarkState::Black => {
// Retain the object
actual_size += obj.header.type_info.total_size();
obj.prev = last_linked;
last_linked = Some(NonNull::from(&mut *obj));
// Reset state to white
obj.header.state = MarkState::White;
}
}
}
self.big_object_link.set(last_linked);
assert_eq!(expected_size, actual_size);
self.allocated_size.set(actual_size);
}
}
/// The internal data for a simple collector
struct RawSimpleCollector {
shadow_stack: RefCell<ShadowStack>,
heap: GcHeap
}
impl RawSimpleCollector {
#[inline]
unsafe fn maybe_collect(&self) {
if self.heap.should_collect() {
self.perform_collection()
}
}
#[cold]
#[inline(never)]
unsafe fn perform_collection(&self) {
let mut task = CollectionTask {
expected_collector: self as *const Self as *mut Self,
roots: self.shadow_stack.borrow().0.clone(),
heap: &self.heap,
grey_stack: if cfg!(feature = "implicit-grey-stack") {
Vec::new()
} else {
Vec::with_capacity(64)
}
};
task.run();
}
}
struct CollectionTask<'a> {
expected_collector: *mut RawSimpleCollector,
roots: Vec<*mut dyn DynTrace>,
heap: &'a GcHeap,
#[cfg_attr(feature = "implicit-grey-stack", allow(dead_code))]
grey_stack: Vec<*mut GcHeader>
}
impl<'a> CollectionTask<'a> {
fn run(&mut self) {
// Mark
for &root in &self.roots {
let mut visitor = MarkVisitor {
expected_collector: self.expected_collector,
grey_stack: &mut self.grey_stack,
};
// Dynamically dispatched
unsafe { (*root).trace(&mut visitor); }
}
#[cfg(not(feature = "implicit-grey-stack"))] unsafe {
while let Some(obj) = self.grey_stack.pop() {
debug_assert_eq!((*obj).state, MarkState::Grey);
let mut visitor = MarkVisitor {
expected_collector: self.expected_collector,
grey_stack: &mut self.grey_stack
};
((*obj).type_info.trace_func)(
&mut *(*obj).value(),
&mut visitor
);
// Mark the object black now it's innards have been traced
(*obj).state = MarkState::Black;
}
}
// Sweep
unsafe { self.heap.allocator.sweep(&self.roots) };
let updated_size = self.heap.allocator.allocated_size();
// Update the threshold to be 150% of currently used size
self.heap.threshold.set(updated_size + (updated_size / 2));
}
}
struct MarkVisitor<'a> {
expected_collector: *mut RawSimpleCollector,
#[cfg_attr(feature = "implicit-grey-stack", allow(dead_code))]
grey_stack: &'a mut Vec<*mut GcHeader>,
}
unsafe impl GcVisitor for MarkVisitor<'_> {
type Err = !;
}
impl GcVisit for MarkVisitor<'_> {
fn visit_gc<'gc, T: GcSafe + 'gc>(&mut self, gc: &mut Gc<'gc, T>) {
/*
* Check the collectors match. Otherwise we're mutating
* other people's data.
*/
assert_eq!(gc.collector_ptr.as_ptr(), self.expected_collector);
let obj = unsafe { &mut *GcHeader::from_value_ptr(gc.as_raw_ptr()) };
match obj.state {
MarkState::White => {
if !T::NEEDS_TRACE {
/*
* We don't need to mark this grey
* It has no internals that need to be traced.
* We can directly move it directly to the black set
*/
obj.state = MarkState::Black;
} else {
/*
* We need to mark this object grey and push it onto the grey stack.
* It will be processed later
*/
(*obj).state = MarkState::Grey;
#[cfg(not(feature = "implicit-grey-stack"))] {
self.grey_stack.push(obj as *mut GcHeader);
}
#[cfg(feature = "implicit-grey-stack")] unsafe {
/*
* The user wants an implicit grey stack using
* recursion. This risks stack overflow but can
* boost performance (See 9a9634d68a4933d).
* On some workloads this is fine.
*/
T::trace(
&mut *((*obj).value() as *mut T),
&mut *self
);
/*
* Mark the object black now it's innards have been traced
* NOTE: We do **not** do this with an implicit stack.
*/
(*obj).state = MarkState::Black;
}
}
},
MarkState::Grey => {
/*
* We've already pushed this object onto the gray stack
* It will be traversed eventually, so we don't need to do anything.
*/
},
MarkState::Black => {
// We've already traversed this object. It's already known to be reachable
},
}
}
}
/// A simple collector can only be used from a single thread
impl !Send for RawSimpleCollector {}
pub struct SimpleCollectorContext {
collector: Rc<RawSimpleCollector>
}
unsafe impl GcContext for SimpleCollectorContext {
type System = SimpleCollector;
unsafe fn basic_safepoint<T: Trace>(&mut self, value: &mut &mut T) {
let dyn_ptr = self.collector.shadow_stack.borrow_mut().push(value);
self.collector.maybe_collect();
assert_eq!(
self.collector.shadow_stack.borrow_mut().pop(),
Some(dyn_ptr)
);
}
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 {
let dyn_ptr = self.collector.shadow_stack.borrow_mut().push(value);
let mut sub_context = SimpleCollectorContext {
collector: self.collector.clone()
};
let result = func(&mut sub_context, value);
drop(sub_context);
assert_eq!(
self.collector.shadow_stack.borrow_mut().pop(),
Some(dyn_ptr)
);
result
}
}
unsafe impl<'gc, T: GcSafe + 'gc> GcSimpleAlloc<'gc, T> for &'gc SimpleCollectorContext {
type Ref = Gc<'gc, T>;
#[inline]
fn alloc(&self, value: T) -> Gc<'gc, T> {
self.collector.heap.allocator.alloc(value)
}
}
struct GcType {
value_size: usize,
value_offset: usize,
#[cfg_attr(feature = "implicit-grey-stack", allow(unused))]
trace_func: unsafe fn(*mut c_void, &mut MarkVisitor),
drop_func: Option<unsafe fn(*mut c_void)>,
}
impl GcType {
#[inline]
const fn total_size(&self) -> usize {
self.value_offset + self.value_size
}
}
trait StaticGcType {
const VALUE_OFFSET: usize;
const STATIC_TYPE: &'static GcType;
}
impl<T: GcSafe> StaticGcType for T {
const VALUE_OFFSET: usize = {
if alloc::is_small_object::<T>() {
// Small object
let layout = Layout::new::<GcHeader>();
layout.size() + layout.padding_needed_for(std::mem::align_of::<T>())
} else {
// Big object
let layout = Layout::new::<BigGcObject<()>>();
layout.size() + layout.padding_needed_for(std::mem::align_of::<T>())
}
};
const STATIC_TYPE: &'static GcType = &GcType {
value_size: std::mem::size_of::<T>(),
value_offset: Self::VALUE_OFFSET,
trace_func: unsafe { transmute::<_, unsafe fn(*mut c_void, &mut MarkVisitor)>(
<T as DynTrace>::trace as fn(&mut T, &mut MarkVisitor),
) },
drop_func: if <T as GcSafe>::NEEDS_DROP {
unsafe { Some(transmute::<_, unsafe fn(*mut c_void)>(
std::ptr::drop_in_place::<T> as unsafe fn(*mut T)
)) }
} else { None }
};
}
/// A header for a GC object
///
/// This is shared between both small arenas
/// and fallback alloc vis `BigGcObject`
#[repr(C)]
struct GcHeader {
type_info: &'static GcType,
/*
* NOTE: State byte should come last
* If the value is small `(u32)`, we could reduce
* the padding to a 3 bytes and fit everything in a word.
*/
state: MarkState
}
impl GcHeader {
#[inline]
pub fn value(&self) -> *mut c_void {
unsafe {
(self as *const GcHeader as *mut GcHeader as *mut u8)
// NOTE: This takes into account the possibility of `BigGcObject`
.add(self.type_info.value_offset)
.cast::<c_void>()
}
}
#[inline]
pub unsafe fn from_value_ptr<T: GcSafe>(ptr: *mut T) -> *mut GcHeader {
(ptr as *mut u8).sub(T::STATIC_TYPE.value_offset) as *mut GcHeader
}
}
/// Marker for an unknown GC object
struct DynamicObj;
#[repr(C)]
struct BigGcObject<T = DynamicObj> {
header: GcHeader,
/// The previous object in the linked list of allocated objects,
/// or null if its the end
prev: BigObjectLink,
/// This is dropped using dynamic type info
static_value: ManuallyDrop<T>
}
impl<T> BigGcObject<T> {
#[inline]
unsafe fn into_dynamic_box(val: Box<Self>) -> Box<BigGcObject<DynamicObj>> {
std::mem::transmute::<Box<BigGcObject<T>>, Box<BigGcObject<DynamicObj>>>(val)
}
}
impl<T> Drop for BigGcObject<T> {
fn drop(&mut self) {
unsafe {
if let Some(drop) = self.header.type_info.drop_func {
drop(&mut *self.static_value as *mut T as *mut c_void);
}
}
}
}
/// The current mark state of the object
///
/// See [Tri Color Marking](https://en.wikipedia.org/wiki/Tracing_garbage_collection#Tri-color_marking)
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
enum MarkState {
/// The object is in the "white set" and is a candidate for having its memory freed.
///
/// Once all the objects have been marked,
/// all remaining white objects will be freed.
White,
/// The object is in the gray set and needs to be traversed to look for reachable memory
///
/// After being scanned this object will end up in the black set.
Grey,
/// The object is in the black set and is reachable from the roots.
///
/// This object cannot be freed.
Black
}