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
use std::collections::HashSet;
use std::mem::swap;
use std::sync::Mutex;

use crate::util::ObjectReference;

use super::object_enum::ObjectEnumerator;

pub struct TreadMill {
    from_space: Mutex<HashSet<ObjectReference>>,
    to_space: Mutex<HashSet<ObjectReference>>,
    collect_nursery: Mutex<HashSet<ObjectReference>>,
    alloc_nursery: Mutex<HashSet<ObjectReference>>,
}

impl std::fmt::Debug for TreadMill {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TreadMill")
            .field("from", &self.from_space.lock().unwrap())
            .field("to", &self.to_space.lock().unwrap())
            .field("collect_nursery", &self.collect_nursery.lock().unwrap())
            .field("alloc_nursery", &self.alloc_nursery.lock().unwrap())
            .finish()
    }
}

impl TreadMill {
    pub fn new() -> Self {
        TreadMill {
            from_space: Mutex::new(HashSet::new()),
            to_space: Mutex::new(HashSet::new()),
            collect_nursery: Mutex::new(HashSet::new()),
            alloc_nursery: Mutex::new(HashSet::new()),
        }
    }

    pub fn add_to_treadmill(&self, object: ObjectReference, nursery: bool) {
        if nursery {
            trace!("Adding {} to nursery", object);
            self.alloc_nursery.lock().unwrap().insert(object);
        } else {
            trace!("Adding {} to to_space", object);
            self.to_space.lock().unwrap().insert(object);
        }
    }

    pub fn collect_nursery(&self) -> Vec<ObjectReference> {
        let mut guard = self.collect_nursery.lock().unwrap();
        let vals = guard.iter().copied().collect();
        guard.clear();
        drop(guard);
        vals
    }

    pub fn collect(&self) -> Vec<ObjectReference> {
        let mut guard = self.from_space.lock().unwrap();
        let vals = guard.iter().copied().collect();
        guard.clear();
        drop(guard);
        vals
    }

    pub fn copy(&self, object: ObjectReference, is_in_nursery: bool) {
        if is_in_nursery {
            let mut guard = self.collect_nursery.lock().unwrap();
            debug_assert!(
                guard.contains(&object),
                "copy source object ({}) must be in collect_nursery",
                object
            );
            guard.remove(&object);
        } else {
            let mut guard = self.from_space.lock().unwrap();
            debug_assert!(
                guard.contains(&object),
                "copy source object ({}) must be in from_space",
                object
            );
            guard.remove(&object);
        }
        self.to_space.lock().unwrap().insert(object);
    }

    pub fn is_to_space_empty(&self) -> bool {
        self.to_space.lock().unwrap().is_empty()
    }

    pub fn is_from_space_empty(&self) -> bool {
        self.from_space.lock().unwrap().is_empty()
    }

    pub fn is_nursery_empty(&self) -> bool {
        self.collect_nursery.lock().unwrap().is_empty()
    }

    pub fn flip(&mut self, full_heap: bool) {
        swap(&mut self.alloc_nursery, &mut self.collect_nursery);
        trace!("Flipped alloc_nursery and collect_nursery");
        if full_heap {
            swap(&mut self.from_space, &mut self.to_space);
            trace!("Flipped from_space and to_space");
        }
    }

    pub(crate) fn enumerate_objects(&self, enumerator: &mut dyn ObjectEnumerator) {
        let mut visit_objects = |set: &Mutex<HashSet<ObjectReference>>| {
            let set = set.lock().unwrap();
            for object in set.iter() {
                enumerator.visit_object(*object);
            }
        };
        visit_objects(&self.alloc_nursery);
        visit_objects(&self.to_space);
    }
}

impl Default for TreadMill {
    fn default() -> Self {
        Self::new()
    }
}