mmtk/plan/nogc/
mutator.rs

1use crate::plan::mutator_context::unreachable_prepare_func;
2use crate::plan::mutator_context::unreachable_release_func;
3use crate::plan::mutator_context::Mutator;
4use crate::plan::mutator_context::MutatorBuilder;
5use crate::plan::mutator_context::MutatorConfig;
6use crate::plan::mutator_context::{
7    create_allocator_mapping, create_space_mapping, ReservedAllocators,
8};
9use crate::plan::nogc::NoGC;
10use crate::plan::AllocationSemantics;
11use crate::util::alloc::allocators::AllocatorSelector;
12use crate::util::VMMutatorThread;
13use crate::vm::VMBinding;
14use crate::MMTK;
15use enum_map::{enum_map, EnumMap};
16
17/// We use three bump allocators when enabling nogc_multi_space.
18const MULTI_SPACE_RESERVED_ALLOCATORS: ReservedAllocators = ReservedAllocators {
19    n_bump_pointer: 3,
20    ..ReservedAllocators::DEFAULT
21};
22
23lazy_static! {
24    /// When nogc_multi_space is disabled, force all the allocation go to the default allocator and space.
25    static ref ALLOCATOR_MAPPING_SINGLE_SPACE: EnumMap<AllocationSemantics, AllocatorSelector> = enum_map! {
26        _ => AllocatorSelector::BumpPointer(0),
27    };
28    pub static ref ALLOCATOR_MAPPING: EnumMap<AllocationSemantics, AllocatorSelector> = {
29        if cfg!(feature = "nogc_multi_space") {
30            let mut map = create_allocator_mapping(MULTI_SPACE_RESERVED_ALLOCATORS, false);
31            map[AllocationSemantics::Default] = AllocatorSelector::BumpPointer(0);
32            map[AllocationSemantics::Immortal] = AllocatorSelector::BumpPointer(1);
33            map[AllocationSemantics::Los] = AllocatorSelector::BumpPointer(2);
34            map
35        } else {
36            *ALLOCATOR_MAPPING_SINGLE_SPACE
37        }
38    };
39}
40
41pub fn create_nogc_mutator<VM: VMBinding>(
42    mutator_tls: VMMutatorThread,
43    mmtk: &'static MMTK<VM>,
44) -> Mutator<VM> {
45    let plan = mmtk.get_plan().downcast_ref::<NoGC<VM>>().unwrap();
46    let config = MutatorConfig {
47        allocator_mapping: &ALLOCATOR_MAPPING,
48        space_mapping: Box::new({
49            let mut vec = create_space_mapping(MULTI_SPACE_RESERVED_ALLOCATORS, false, plan);
50            vec.push((AllocatorSelector::BumpPointer(0), &plan.nogc_space));
51            vec.push((AllocatorSelector::BumpPointer(1), &plan.immortal));
52            vec.push((AllocatorSelector::BumpPointer(2), &plan.los));
53            vec
54        }),
55        prepare_func: &unreachable_prepare_func,
56        release_func: &unreachable_release_func,
57    };
58
59    let builder = MutatorBuilder::new(mutator_tls, mmtk, config);
60    builder.build()
61}