mmtk/plan/marksweep/
global.rs

1use crate::plan::global::BasePlan;
2use crate::plan::global::CommonPlan;
3use crate::plan::global::CreateGeneralPlanArgs;
4use crate::plan::global::CreateSpecificPlanArgs;
5use crate::plan::marksweep::gc_work::MSGCWorkContext;
6use crate::plan::marksweep::mutator::ALLOCATOR_MAPPING;
7use crate::plan::AllocationSemantics;
8use crate::plan::Plan;
9use crate::plan::PlanConstraints;
10use crate::policy::space::Space;
11use crate::scheduler::GCWorkScheduler;
12use crate::util::alloc::allocators::AllocatorSelector;
13use crate::util::heap::gc_trigger::SpaceStats;
14use crate::util::heap::VMRequest;
15use crate::util::metadata::side_metadata::SideMetadataContext;
16use crate::util::VMWorkerThread;
17use crate::vm::VMBinding;
18use crate::MMTK;
19use enum_map::EnumMap;
20use mmtk_macros::{HasSpaces, PlanTraceObject};
21
22#[cfg(feature = "malloc_mark_sweep")]
23pub type MarkSweepSpace<VM> = crate::policy::marksweepspace::malloc_ms::MallocSpace<VM>;
24#[cfg(feature = "malloc_mark_sweep")]
25use crate::policy::marksweepspace::malloc_ms::MAX_OBJECT_SIZE;
26
27#[cfg(not(feature = "malloc_mark_sweep"))]
28pub type MarkSweepSpace<VM> = crate::policy::marksweepspace::native_ms::MarkSweepSpace<VM>;
29#[cfg(not(feature = "malloc_mark_sweep"))]
30use crate::policy::marksweepspace::native_ms::MAX_OBJECT_SIZE;
31
32#[derive(HasSpaces, PlanTraceObject)]
33pub struct MarkSweep<VM: VMBinding> {
34    #[parent]
35    common: CommonPlan<VM>,
36    #[space]
37    ms: MarkSweepSpace<VM>,
38}
39
40/// The plan constraints for the mark sweep plan.
41pub const MS_CONSTRAINTS: PlanConstraints = PlanConstraints {
42    moves_objects: false,
43    max_non_los_default_alloc_bytes: MAX_OBJECT_SIZE,
44    may_trace_duplicate_edges: true,
45    needs_prepare_mutator: (!cfg!(feature = "malloc_mark_sweep")
46        && !cfg!(feature = "eager_sweeping"))
47        || PlanConstraints::default().needs_prepare_mutator,
48    ..PlanConstraints::default()
49};
50
51impl<VM: VMBinding> Plan for MarkSweep<VM> {
52    fn schedule_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
53        scheduler.schedule_common_work::<MSGCWorkContext<VM>>(self);
54    }
55
56    fn get_allocator_mapping(&self) -> &'static EnumMap<AllocationSemantics, AllocatorSelector> {
57        &ALLOCATOR_MAPPING
58    }
59
60    fn prepare(&mut self, tls: VMWorkerThread) {
61        self.common.prepare(tls, true);
62        self.ms.prepare(true);
63    }
64
65    fn release(&mut self, tls: VMWorkerThread) {
66        self.ms.release();
67        self.common.release(tls, true);
68    }
69
70    fn end_of_pause(&mut self, mmtk: &'static MMTK<VM>, tls: VMWorkerThread) {
71        self.ms.end_of_gc();
72        self.common.end_of_pause(tls);
73        mmtk.gc_trigger.policy.on_gc_end(mmtk);
74    }
75
76    fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
77        self.base().collection_required(self, space_full)
78    }
79
80    fn current_gc_may_move_object(&self) -> bool {
81        false
82    }
83
84    fn get_used_pages(&self) -> usize {
85        self.common.get_used_pages() + self.ms.reserved_pages()
86    }
87
88    fn base(&self) -> &BasePlan<VM> {
89        &self.common.base
90    }
91
92    fn base_mut(&mut self) -> &mut BasePlan<Self::VM> {
93        &mut self.common.base
94    }
95
96    fn common(&self) -> &CommonPlan<VM> {
97        &self.common
98    }
99
100    fn constraints(&self) -> &'static PlanConstraints {
101        &MS_CONSTRAINTS
102    }
103}
104
105impl<VM: VMBinding> MarkSweep<VM> {
106    pub fn new(args: CreateGeneralPlanArgs<VM>) -> Self {
107        let mut global_side_metadata_specs = SideMetadataContext::new_global_specs(&[]);
108        MarkSweepSpace::<VM>::extend_global_side_metadata_specs(&mut global_side_metadata_specs);
109
110        let mut plan_args = CreateSpecificPlanArgs {
111            global_args: args,
112            constraints: &MS_CONSTRAINTS,
113            global_side_metadata_specs,
114        };
115
116        MarkSweep {
117            ms: MarkSweepSpace::new(plan_args.get_normal_space_args(
118                "ms",
119                true,
120                false,
121                VMRequest::discontiguous(),
122            )),
123            common: CommonPlan::new(plan_args),
124        }
125    }
126
127    pub fn ms_space(&self) -> &MarkSweepSpace<VM> {
128        &self.ms
129    }
130}