sdd/
collectible.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
use std::ptr::{self, NonNull};
use std::sync::atomic::Ordering::Relaxed;
use std::sync::atomic::{AtomicPtr, AtomicUsize};

/// [`Collectible`] defines the memory layout for the type in order to be passed to the garbage
/// collector.
pub(super) trait Collectible {
    /// Returns the next [`Collectible`] pointer.
    fn next_ptr(&self) -> Option<NonNull<dyn Collectible>>;

    /// Sets the next [`Collectible`] pointer.
    fn set_next_ptr(&self, next_ptr: Option<NonNull<dyn Collectible>>);
}

/// [`Link`] implements [`Collectible`].
#[derive(Debug, Default)]
pub struct Link {
    data: (AtomicUsize, AtomicPtr<usize>),
}

/// [`DeferredClosure`] implements [`Collectible`] for a closure to execute it after all the
/// current readers in the process are gone.
pub(super) struct DeferredClosure<F: 'static + FnOnce()> {
    f: Option<F>,
    link: Link,
}

impl Link {
    #[inline]
    pub(super) const fn new_shared() -> Self {
        Link {
            data: (AtomicUsize::new(1), AtomicPtr::new(ptr::null_mut())),
        }
    }

    #[inline]
    pub(super) const fn new_unique() -> Self {
        Link {
            data: (AtomicUsize::new(0), AtomicPtr::new(ptr::null_mut())),
        }
    }

    #[inline]
    pub(super) const fn ref_cnt(&self) -> &AtomicUsize {
        &self.data.0
    }
}

impl Collectible for Link {
    #[inline]
    fn next_ptr(&self) -> Option<NonNull<dyn Collectible>> {
        let fat_ptr: (*mut usize, *mut usize) = (
            self.data.0.load(Relaxed) as *mut usize,
            self.data.1.load(Relaxed),
        );
        unsafe { std::mem::transmute(fat_ptr) }
    }

    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    #[inline]
    fn set_next_ptr(&self, next_ptr: Option<NonNull<dyn Collectible>>) {
        let data: (*mut usize, *mut usize) = next_ptr.map_or_else(
            || (ptr::null_mut(), ptr::null_mut()),
            |p| unsafe { std::mem::transmute(p) },
        );
        self.data.0.store(data.0 as usize, Relaxed);
        self.data.1.store(data.1, Relaxed);
    }
}

impl<F: 'static + FnOnce()> DeferredClosure<F> {
    /// Creates a new [`DeferredClosure`].
    #[inline]
    pub fn new(f: F) -> Self {
        DeferredClosure {
            f: Some(f),
            link: Link::default(),
        }
    }
}

impl<F: 'static + FnOnce()> Collectible for DeferredClosure<F> {
    #[inline]
    fn next_ptr(&self) -> Option<NonNull<dyn Collectible>> {
        self.link.next_ptr()
    }

    #[inline]
    fn set_next_ptr(&self, next_ptr: Option<NonNull<dyn Collectible>>) {
        self.link.set_next_ptr(next_ptr);
    }
}

impl<F: 'static + FnOnce()> Drop for DeferredClosure<F> {
    #[inline]
    fn drop(&mut self) {
        if let Some(f) = self.f.take() {
            f();
        }
    }
}