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
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::gc_ptr::GcPtr;
use crate::trace::Trace;

static COUNTER: AtomicUsize = AtomicUsize::new(0);

pub struct Root {
    idx: usize,
}

impl Root {
    pub fn new() -> Root {
        // TODO better way to create index
        Root {
            idx: COUNTER.fetch_add(1, Ordering::Relaxed),
        }
    }

    pub unsafe fn enroot<T: Trace + ?Sized>(&self, gc_ptr: GcPtr<T>) {
        super::set_root(self.idx, gc_ptr)
    }
}

impl Drop for Root {
    fn drop(&mut self) {
        super::pop_root(self.idx);
    }
}