Skip to content

Commit fc144cf

Browse files
authored
Turn SFTMap into trait object (#711)
This PR makes MMTk use `dyn SFTMap` instead of defining multiple `type SFTMapType<'a>` based on compiler flags. This is part of the necessary refactoring to make MMTk able to select address space at runtime. For 35-bit address space (pointer compression), MMTk should use `SFTSparseChunkMap` instead of `SFTSpaceMap`.
1 parent 4aa6437 commit fc144cf

File tree

10 files changed

+18
-31
lines changed

10 files changed

+18
-31
lines changed

src/memory_manager.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,6 @@ pub fn is_live_object(object: ObjectReference) -> bool {
625625
#[cfg(feature = "is_mmtk_object")]
626626
pub fn is_mmtk_object(addr: Address) -> bool {
627627
use crate::mmtk::SFT_MAP;
628-
use crate::policy::sft_map::SFTMap;
629628
SFT_MAP.get_checked(addr).is_mmtk_object(addr)
630629
}
631630

@@ -659,7 +658,6 @@ pub fn is_mmtk_object(addr: Address) -> bool {
659658
/// * `object`: The object reference to query.
660659
pub fn is_in_mmtk_spaces<VM: VMBinding>(object: ObjectReference) -> bool {
661660
use crate::mmtk::SFT_MAP;
662-
use crate::policy::sft_map::SFTMap;
663661
if object.is_null() {
664662
return false;
665663
}
@@ -771,7 +769,6 @@ pub fn add_finalizer<VM: VMBinding>(
771769
#[cfg(feature = "object_pinning")]
772770
pub fn pin_object<VM: VMBinding>(object: ObjectReference) -> bool {
773771
use crate::mmtk::SFT_MAP;
774-
use crate::policy::sft_map::SFTMap;
775772
SFT_MAP
776773
.get_checked(object.to_address::<VM>())
777774
.pin_object(object)
@@ -786,7 +783,6 @@ pub fn pin_object<VM: VMBinding>(object: ObjectReference) -> bool {
786783
#[cfg(feature = "object_pinning")]
787784
pub fn unpin_object<VM: VMBinding>(object: ObjectReference) -> bool {
788785
use crate::mmtk::SFT_MAP;
789-
use crate::policy::sft_map::SFTMap;
790786
SFT_MAP
791787
.get_checked(object.to_address::<VM>())
792788
.unpin_object(object)
@@ -799,7 +795,6 @@ pub fn unpin_object<VM: VMBinding>(object: ObjectReference) -> bool {
799795
#[cfg(feature = "object_pinning")]
800796
pub fn is_pinned<VM: VMBinding>(object: ObjectReference) -> bool {
801797
use crate::mmtk::SFT_MAP;
802-
use crate::policy::sft_map::SFTMap;
803798
SFT_MAP
804799
.get_checked(object.to_address::<VM>())
805800
.is_object_pinned(object)

src/mmtk.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///! MMTk instance.
22
use crate::plan::Plan;
3-
use crate::policy::sft_map::SFTMapType;
3+
use crate::policy::sft_map::{create_sft_map, SFTMap};
44
use crate::scheduler::GCWorkScheduler;
55

66
#[cfg(feature = "extreme_assertions")]
@@ -40,7 +40,7 @@ lazy_static! {
4040
use crate::util::rust_util::InitializeOnce;
4141

4242
// A global space function table that allows efficient dispatch space specific code for addresses in our heap.
43-
pub static SFT_MAP: InitializeOnce<SFTMapType<'static>> = InitializeOnce::new();
43+
pub static SFT_MAP: InitializeOnce<Box<dyn SFTMap>> = InitializeOnce::new();
4444

4545
// MMTk builder. This is used to set options before actually creating an MMTk instance.
4646
pub struct MMTKBuilder {
@@ -99,7 +99,7 @@ impl<VM: VMBinding> MMTK<VM> {
9999
pub fn new(options: Arc<Options>) -> Self {
100100
// Initialize SFT first in case we need to use this in the constructor.
101101
// The first call will initialize SFT map. Other calls will be blocked until SFT map is initialized.
102-
SFT_MAP.initialize_once(&SFTMapType::new);
102+
SFT_MAP.initialize_once(&create_sft_map);
103103

104104
let num_workers = if cfg!(feature = "single_worker") {
105105
1

src/policy/lockfreeimmortalspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::util::heap::PageResource;
66
use crate::util::ObjectReference;
77

88
use crate::policy::sft::GCWorkerMutRef;
9-
use crate::policy::sft_map::SFTMap;
109
use crate::util::conversions;
1110
use crate::util::heap::layout::vm_layout_constants::{AVAILABLE_BYTES, AVAILABLE_START};
1211
use crate::util::metadata::side_metadata::SideMetadataSanity;

src/policy/marksweepspace/malloc_ms/global.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ impl<VM: VMBinding> MallocSpace<VM> {
350350

351351
// If the side metadata for the address has not yet been mapped, we will map all the side metadata for the range [address, address + actual_size).
352352
if !is_meta_space_mapped(address, actual_size) {
353-
use crate::policy::sft_map::SFTMap;
354353
// Map the metadata space for the associated chunk
355354
self.map_metadata_and_update_bound(address, actual_size);
356355
// Update SFT
@@ -531,7 +530,6 @@ impl<VM: VMBinding> MallocSpace<VM> {
531530

532531
/// Clean up for an empty chunk
533532
fn clean_up_empty_chunk(&self, chunk_start: Address) {
534-
use crate::policy::sft_map::SFTMap;
535533
// Since the chunk mark metadata is a byte, we don't need synchronization
536534
unsafe { unset_chunk_mark_unsafe(chunk_start) };
537535
// Clear the SFT entry

src/policy/sft_map.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,18 @@ pub trait SFTMap {
6060
unsafe fn clear(&self, address: Address);
6161
}
6262

63-
cfg_if::cfg_if! {
64-
if #[cfg(all(feature = "malloc_mark_sweep", target_pointer_width = "64"))] {
65-
// 64-bit malloc mark sweep needs a chunk-based SFT map, but the sparse map is not suitable for 64bits.
66-
pub type SFTMapType<'a> = dense_chunk_map::SFTDenseChunkMap<'a>;
67-
} else if #[cfg(target_pointer_width = "64")] {
68-
pub type SFTMapType<'a> = space_map::SFTSpaceMap<'a>;
69-
} else if #[cfg(target_pointer_width = "32")] {
70-
pub type SFTMapType<'a> = sparse_chunk_map::SFTSparseChunkMap<'a>;
71-
} else {
72-
compile_err!("Cannot figure out which SFT map to use.");
63+
pub(crate) fn create_sft_map() -> Box<dyn SFTMap> {
64+
cfg_if::cfg_if! {
65+
if #[cfg(all(feature = "malloc_mark_sweep", target_pointer_width = "64"))] {
66+
// 64-bit malloc mark sweep needs a chunk-based SFT map, but the sparse map is not suitable for 64bits.
67+
return Box::new(dense_chunk_map::SFTDenseChunkMap::<'static>::new());
68+
} else if #[cfg(target_pointer_width = "64")] {
69+
return Box::new(space_map::SFTSpaceMap::<'static>::new());
70+
} else if #[cfg(target_pointer_width = "32")] {
71+
return Box::new(sparse_chunk_map::SFTSparseChunkMap::<'static>::new());
72+
} else {
73+
compile_err!("Cannot figure out which SFT map to use.");
74+
}
7375
}
7476
}
7577

src/policy/space.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::mmtk::SFT_MAP;
1616
#[cfg(debug_assertions)]
1717
use crate::policy::sft::EMPTY_SFT_NAME;
1818
use crate::policy::sft::SFT;
19-
use crate::policy::sft_map::SFTMap;
2019
use crate::util::copy::*;
2120
use crate::util::heap::layout::heap_layout::Mmapper;
2221
use crate::util::heap::layout::heap_layout::VMMap;

src/scheduler/gc_work.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ impl<VM: VMBinding> ProcessEdgesWork for SFTProcessEdges<VM> {
516516
#[inline]
517517
fn trace_object(&mut self, object: ObjectReference) -> ObjectReference {
518518
use crate::policy::sft::GCWorkerMutRef;
519-
use crate::policy::sft_map::SFTMap;
520519

521520
if object.is_null() {
522521
return object;

src/util/address.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::ops::*;
55
use std::sync::atomic::Ordering;
66

77
use crate::mmtk::{MMAPPER, SFT_MAP};
8-
use crate::policy::sft_map::SFTMap;
98
use crate::util::heap::layout::mmapper::Mmapper;
109

1110
/// size in bytes

src/util/heap/layout/map32.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::map::Map;
22
use crate::mmtk::SFT_MAP;
3-
use crate::policy::sft_map::SFTMap;
43
use crate::util::conversions;
54
use crate::util::generic_freelist::GenericFreeList;
65
use crate::util::heap::freelistpageresource::CommonFreeListPageResource;

src/util/metadata/side_metadata/global.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -832,12 +832,9 @@ impl SideMetadataContext {
832832
#[cfg(feature = "global_alloc_bit")]
833833
ret.push(ALLOC_SIDE_METADATA_SPEC);
834834

835-
{
836-
use crate::policy::sft_map::SFTMap;
837-
if let Some(spec) = crate::mmtk::SFT_MAP.get_side_metadata() {
838-
if spec.is_global {
839-
ret.push(*spec);
840-
}
835+
if let Some(spec) = crate::mmtk::SFT_MAP.get_side_metadata() {
836+
if spec.is_global {
837+
ret.push(*spec);
841838
}
842839
}
843840

0 commit comments

Comments
 (0)