sandpit 0.5.2

A concurrent garbage collected arena
Documentation
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
//! Module containing the three types of GC pointers.
//!
//! ## Overview
//! * An object may only be referenced by a GC pointer if it implements [`crate::Trace`].
//! * Any object capable of containing a GC pointer may not impl [`crate::TraceLeaf`].
//!
//! [`Gc`] and [`GcOpt`] may be updated via a [`crate::WriteBarrier`] to point
//! at different values.
use super::trace::Trace;
use crate::barrier::WriteBarrier;
use crate::header::GcHeader;
use crate::mutator::Mutator;
use crate::pointee::{GcPointee, Thin};

use alloc::alloc::Layout;
use core::marker::PhantomData;
use core::ops::Deref;
use core::ptr::{null_mut, NonNull};
use core::sync::atomic::{AtomicPtr, Ordering};

// A Gc points to a valid T within a GC Arena which is also succeeded by its
// GC header which may or may not be padded.
// This holds true for Gc as well as GcOpt if it is not null.
//
//                                         Gc<T>
//                                          |
//                                          V
// [ <T as GcPointee>::GcHeader ][ padding ][ T value ]
//
// Since Gc cannot be mutated and therefore has no need to be atomic,
// it is able to be a wide pointer.

/// Immutable Gc pointer, meaning a write barrier cannot be used to update this pointer.
///
/// It's inner value can still be mutated.
///
/// A [`Gc`] can safely dereference into
/// a `&'gc T`, but provides no option to obtain mutable references to it's
/// inner value. Due to all GC values sharing the same 'gc lifetime,
/// any number of GC values are allowed to reference each other at anytime. This
/// is beneficial in easing the creation of graphs and cyclical data structures,
/// but means any mutation of a GC value requires some form of interior mutatbility.
///
/// A [`Gc`] is itself immutable in that it's inner pointer may never be
/// changed. The [`Gc`] and [`GcOpt`] types allow for updating
/// which value it is referencing through the means of a write barrier.
/// A [`Gc`] may also point at a garbage collected array like `Gc<'gc, [T]>`. A Gc referencing an
/// array can be obtained via the mutator by using one of several array allocation methods
/// including [`Mutator::alloc_array`].

// Gc may be updated to point somewhere else which requires it to be atomic
// in order to sync with the tracing threads.

/// Mutable GC pointer, meaning a write barrier can be used to update this pointer.
///
/// See [`crate::barrier::WriteBarrier`] for how to update a [`Gc`].
pub struct Gc<'gc, T: Trace + ?Sized> {
    ptr: AtomicPtr<Thin<T>>,
    scope: PhantomData<&'gc *mut T>,
}

impl<'gc, T: Trace> Gc<'gc, T> {
    /// Provides a way to allocate a value into the GC arena, returning a `Gc<T>`.
    ///
    /// This method is equivalent to calling [`crate::mutator::Mutator::alloc`].
    ///
    /// # Example
    /// ```rust
    /// # use sandpit::{Arena, Gc, Root};
    /// # let arena: Arena<Root![()]> = Arena::new(|mu| ());
    /// arena.mutate(|mu, root| {
    ///    let new = Gc::new(mu, 69);
    /// });
    pub fn new(m: &'gc Mutator<'gc>, obj: T) -> Self {
        m.alloc(obj)
    }
}

impl<'gc, T: Trace + ?Sized> Gc<'gc, T> {
    pub(crate) fn get_layout(&self) -> Layout {
        <T as GcPointee>::get_header(self.as_thin()).get_alloc_layout()
    }
}

impl<'gc, T: Trace + ?Sized + 'gc> Deref for Gc<'gc, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        let thin_ptr = self.ptr.load(Ordering::Relaxed);

        <T as GcPointee>::deref(NonNull::new(thin_ptr).unwrap())
    }
}

impl<'gc, T: Trace + ?Sized> Clone for Gc<'gc, T> {
    fn clone(&self) -> Self {
        let ptr = self.ptr.load(Ordering::Relaxed);

        Self {
            ptr: AtomicPtr::new(ptr),
            scope: PhantomData::<&'gc *mut T>,
        }
    }
}

impl<'gc, T: Trace + ?Sized> Gc<'gc, T> {
    pub(crate) unsafe fn set(&self, value: Gc<'gc, T>) {
        let thin_ptr = value.ptr.load(Ordering::Relaxed);

        self.ptr.store(thin_ptr, Ordering::Relaxed);
    }

    // SAFETY: the pointer must have a valid GcHeader for T, and be allocated
    // within a GC Arena
    pub(crate) unsafe fn from_ptr(ptr: *const T) -> Self {
        Self {
            ptr: AtomicPtr::new(ptr as *mut T as *mut Thin<T>),
            scope: PhantomData::<&'gc *mut T>,
        }
    }

    pub(crate) fn get_header(&self) -> &<T as GcPointee>::GcHeader {
        <T as GcPointee>::get_header(self.as_thin())
    }

    // HACK: THIS EXIST FOR PROVENANCE
    pub(crate) fn get_header_ptr(&self) -> *const <T as GcPointee>::GcHeader {
        <T as GcPointee>::get_header_ptr(self.as_thin())
    }

    pub(crate) fn as_thin(&self) -> NonNull<Thin<T>> {
        unsafe { NonNull::new_unchecked(self.ptr.load(Ordering::Relaxed)) }
    }

    /// Get a reference to a garabage collected value with the lifetime of the mutation.
    ///
    /// Becuase all Gc pointers point to values valid for the entire mutation
    /// lifetime, it is fine to dereference them with that lifetime.
    ///
    /// A regular deref of a `Gc<'gc, T>` gives `&'a T` where `'a` is the lifetime
    /// of the pointer.
    ///
    /// # Example
    ///
    /// In this example scoded deref is needed to implement Foo's set_inner method.
    ///
    ///```rust
    /// # use sandpit::{Arena, Root, Gc};
    /// let arena: Arena<Root![Gc<'_, usize>]> = Arena::new(|mu| Gc::new(mu, 69));
    ///
    /// arena.mutate(|mu, root| {
    ///     struct Foo<'gc> {
    ///         inner: &'gc usize
    ///     }
    ///
    ///     impl<'gc> Foo<'gc> {
    ///         fn set_inner(&mut self, gc: Gc<'gc, usize>) {
    ///             // DOES NOT COMPILE
    ///             // self.inner = &gc;
    ///             self.inner = &gc.scoped_deref();
    ///         }
    ///     }
    ///
    ///     let mut foo = Foo {
    ///         inner: root.scoped_deref()
    ///     };
    ///
    ///     let gc = Gc::new(mu, 2);
    ///
    ///     foo.set_inner(gc);
    /// });
    ///```
    pub fn scoped_deref(&self) -> &'gc T {
        let thin_ptr = self.ptr.load(Ordering::Relaxed);

        <T as GcPointee>::deref(NonNull::new(thin_ptr).unwrap())
    }

    /// Allows for updating internal `Gc`'s and `GcOpt`'s.
    ///
    /// Returns a reference to the pointed at value that is wrapped in a
    /// [`crate::barrier::WriteBarrier`] which allows for mutating `Gc` and
    /// `GcOpt`'s.
    ///
    /// # Example
    ///
    /// Get a writer barrier to a index of a slice behind write barrier.
    ///
    /// ## Example
    ///
    /// See [`crate::barrier::WriteBarrier`] for more examples.
    ///
    /// ```rust
    /// use sandpit::{Arena, Gc, Root};
    ///
    /// let arena: Arena<Root![Gc<'_, Gc<'_, usize>>]> = Arena::new(|mu| {
    ///    Gc::new(mu, Gc::new(mu, 69))
    /// });
    ///
    /// arena.mutate(|mu, root| {
    ///     root.write_barrier(mu, |barrier| {
    ///         let new_value = Gc::new(mu, 420);
    ///
    ///         barrier.set(new_value);
    ///
    ///         assert!(**barrier.inner() == 420);
    ///     });
    /// });
    ///```
    pub fn write_barrier<F>(&self, mu: &'gc Mutator, f: F)
    where
        F: FnOnce(&WriteBarrier<T>),
    {
        // SAFETY: Its safe to create a writebarrier over this pointer b/c it is guaranteed
        // to be retraced after the closure ends.
        let barrier = unsafe { WriteBarrier::new(self.scoped_deref()) };

        f(&barrier);

        if mu.has_marked(&self) {
            mu.retrace(self.scoped_deref());
        }
    }
}

/// A GC pointer which is able of pointing to null.
///
/// [`GcOpt`] can be unwrapped into a Gc, and can also be updated via
/// a [`crate::barrier::WriteBarrier`].
pub struct GcOpt<'gc, T: Trace + ?Sized> {
    ptr: AtomicPtr<Thin<T>>,
    scope: PhantomData<&'gc *mut T>,
}

impl<'gc, T: Trace + ?Sized> From<Gc<'gc, T>> for GcOpt<'gc, T> {
    fn from(gc: Gc<'gc, T>) -> Self {
        Self {
            ptr: AtomicPtr::new(gc.ptr.load(Ordering::Relaxed)),
            scope: PhantomData::<&'gc *mut T>,
        }
    }
}

impl<'gc, T: Trace + ?Sized> Clone for GcOpt<'gc, T> {
    fn clone(&self) -> Self {
        Self {
            ptr: AtomicPtr::new(self.ptr.load(Ordering::Relaxed)),
            scope: PhantomData::<&'gc *mut T>,
        }
    }
}

impl<'gc, T: Trace + ?Sized> GcOpt<'gc, T> {
    /// Creates a new GcOpt which points to null.
    ///
    /// A GcOpt can also be created from a [`Gc`] or [`Gc`].
    ///
    /// # Example
    /// ```rust
    /// use sandpit::{Arena, GcOpt, Root};
    ///
    /// let arena: Arena<Root![GcOpt<'_, usize>]> = Arena::new(|mu| {
    ///    GcOpt::new_none()
    /// });
    ///```
    pub fn new_none() -> Self {
        Self {
            ptr: AtomicPtr::new(null_mut()),
            scope: PhantomData::<&'gc *mut T>,
        }
    }

    /// Check whether this [`GcOpt`] is null.
    ///
    /// # Example
    /// ```rust
    /// # use sandpit::{Arena, GcOpt, Root};
    /// # let arena: Arena<Root![()]> = Arena::new(|mu| {
    ///    let gc_opt: GcOpt<()> = GcOpt::new_none();
    ///
    ///    assert!(gc_opt.is_none());
    /// # });
    ///```
    pub fn is_none(&self) -> bool {
        self.ptr.load(Ordering::Relaxed).is_null()
    }

    /// Check whether this [`GcOpt`] contains a valid [`Gc`].
    ///
    /// # Example
    /// ```rust
    /// # use sandpit::{Arena, Gc, GcOpt, Root};
    /// # let arena: Arena<Root![()]> = Arena::new(|mu| {
    ///    let gc_opt = GcOpt::from(Gc::new(mu, 123));
    ///
    ///    assert!(gc_opt.is_some());
    /// # });
    ///```
    pub fn is_some(&self) -> bool {
        !self.is_none()
    }

    /// Mutate this [`GcOpt`] so that it is null.
    ///
    /// Normally updating a Gc pointer requires a write barrier, however,
    /// this method is an exception as the null pointer requires no tracing.
    ///
    /// # Example
    /// ```rust
    /// # use sandpit::{Arena, Gc, GcOpt, Root};
    /// # let arena: Arena<Root![()]> = Arena::new(|mu| {
    ///    let gc_opt = GcOpt::from(Gc::new(mu, 123));
    ///
    ///    assert!(gc_opt.is_some());
    ///
    ///    gc_opt.set_none();
    ///
    ///    assert!(gc_opt.is_none());
    /// # });
    ///```
    pub fn set_none(&self) {
        self.ptr.store(null_mut(), Ordering::Relaxed)
    }

    /// Convert into a Option of [`Gc`].
    ///
    /// # Example
    /// ```rust
    /// # use sandpit::{Arena, Gc, GcOpt, Root};
    /// # let arena: Arena<Root![()]> = Arena::new(|mu| {
    ///    let gc_opt = GcOpt::from(Gc::new(mu, 123));
    ///
    ///    let gc_mut = gc_opt.as_option().unwrap();
    ///
    ///    assert!(*gc_mut == 123);
    /// # });
    ///```
    pub fn as_option(&self) -> Option<Gc<'gc, T>> {
        if self.is_some() {
            Some(Gc {
                ptr: AtomicPtr::new(self.ptr.load(Ordering::Relaxed)),
                scope: PhantomData::<&'gc *mut T>,
            })
        } else {
            None
        }
    }

    /// ## Panics
    ///
    /// Panics if it is in the null state
    pub fn unwrap(&self) -> Gc<'gc, T> {
        self.as_option().unwrap()
    }

    // If the tracers have already traced this pointer, than the new pointer
    // must be retraced before the end of the mutation context.
    //
    // Use a write barrier to call this method safely.
    pub(crate) unsafe fn set(&self, new: GcOpt<'gc, T>) {
        let thin_ptr = new.ptr.load(Ordering::Relaxed);

        self.ptr.store(thin_ptr, Ordering::SeqCst);
    }
}

impl<'gc, T: Trace> GcOpt<'gc, T> {
    pub fn new(m: &'gc Mutator<'gc>, obj: T) -> Self {
        m.alloc(obj).into()
    }
}

impl<'gc, T: Trace + ?Sized> GcOpt<'gc, T> {
    pub unsafe fn from_ptr(ptr: *mut T) -> Self {
        Self {
            ptr: AtomicPtr::new(ptr as *mut Thin<T>),
            scope: PhantomData::<&'gc *mut T>,
        }
    }
}

/*
pub trait GcPointer: Trace + Clone {
    const POINTEE_ALIGNMENT: usize;

    unsafe fn set(&self, value: Self);
}

impl<'gc, T: Trace> GcPointer for Gc<'gc, T> {
    const POINTEE_ALIGNMENT: usize = core::mem::align_of::<T>();

    unsafe fn set(&self, value: Self) {
        self.set(value)
    }
}

impl<'gc, T: Trace> GcPointer for GcOpt<'gc, T> {
    const POINTEE_ALIGNMENT: usize = core::mem::align_of::<T>();

    unsafe fn set(&self, value: Self) {
        self.set(value)
    }
}
*/

#[cfg(test)]
mod tests {
    use super::*;
    use crate::header::GcMark;
    use crate::{Arena, Root};

    #[test]
    fn set_header_marker() {
        let _: Arena<Root![_]> = Arena::new(|mu| {
            let gc = Gc::new(mu, 69);
            let header = gc.get_header();

            assert!(*gc == 69);
            assert_eq!(header.get_mark(), GcMark::Blue);
            header.set_mark(GcMark::Green);
            assert_eq!(header.get_mark(), GcMark::Green);
            assert!(*gc == 69);
        });
    }

    #[test]
    fn gc_from_gcopt() {
        let _: Arena<Root![_]> = Arena::new(|mu| {
            let gc = Gc::new(mu, 69);
            let gc_opt = GcOpt::from(gc);
            let gc = Gc::from(gc_opt.unwrap());
            let header = gc.get_header();

            assert!(*gc == 69);
            assert_eq!(header.get_mark(), GcMark::Blue);
        });
    }

    #[test]
    fn test_gc_sizes() {
        let _: Arena<Root![_]> = Arena::new(|_| {
            assert!(size_of::<Gc<()>>() == size_of::<*const ()>());
            assert!(size_of::<GcOpt<()>>() == size_of::<*const ()>());
        });
    }
}