mmtk/plan/compressor/
mutator.rs

1use crate::plan::compressor::Compressor;
2use crate::plan::mutator_context::common_prepare_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    common_release_func, create_allocator_mapping, create_space_mapping, ReservedAllocators,
8};
9use crate::plan::AllocationSemantics;
10use crate::util::alloc::allocators::AllocatorSelector;
11use crate::util::alloc::BumpAllocator;
12use crate::util::{VMMutatorThread, VMWorkerThread};
13use crate::vm::VMBinding;
14use crate::MMTK;
15use enum_map::{enum_map, EnumMap};
16
17const RESERVED_ALLOCATORS: ReservedAllocators = ReservedAllocators {
18    n_bump_pointer: 1,
19    ..ReservedAllocators::DEFAULT
20};
21
22lazy_static! {
23    /// When compressor_single_space is enabled, force all allocations to go to the default allocator and space.
24    static ref ALLOCATOR_MAPPING_SINGLE_SPACE: EnumMap<AllocationSemantics, AllocatorSelector> = enum_map! {
25        _ => AllocatorSelector::BumpPointer(0),
26    };
27    pub static ref ALLOCATOR_MAPPING: EnumMap<AllocationSemantics, AllocatorSelector> = {
28        if cfg!(feature = "compressor_single_space") {
29            *ALLOCATOR_MAPPING_SINGLE_SPACE
30        } else {
31            let mut map = create_allocator_mapping(RESERVED_ALLOCATORS, true);
32            map[AllocationSemantics::Default] = AllocatorSelector::BumpPointer(0);
33            map
34        }
35    };
36}
37
38pub fn create_compressor_mutator<VM: VMBinding>(
39    mutator_tls: VMMutatorThread,
40    mmtk: &'static MMTK<VM>,
41) -> Mutator<VM> {
42    let plan = mmtk.get_plan().downcast_ref::<Compressor<VM>>().unwrap();
43    let config = MutatorConfig {
44        allocator_mapping: &ALLOCATOR_MAPPING,
45        space_mapping: Box::new({
46            let mut vec = create_space_mapping(
47                RESERVED_ALLOCATORS,
48                !cfg!(feature = "compressor_single_space"),
49                plan,
50            );
51            vec.push((AllocatorSelector::BumpPointer(0), &plan.compressor_space));
52            vec
53        }),
54        prepare_func: &common_prepare_func,
55        release_func: &compressor_mutator_release,
56    };
57
58    let builder = MutatorBuilder::new(mutator_tls, mmtk, config);
59    builder.build()
60}
61
62pub fn compressor_mutator_release<VM: VMBinding>(mutator: &mut Mutator<VM>, tls: VMWorkerThread) {
63    // reset the thread-local allocation bump pointer
64    let bump_allocator = unsafe {
65        mutator
66            .allocators
67            .get_allocator_mut(mutator.config.allocator_mapping[AllocationSemantics::Default])
68    }
69    .downcast_mut::<BumpAllocator<VM>>()
70    .unwrap();
71    bump_allocator.reset();
72    common_release_func(mutator, tls);
73}