mmtk/
mmtk.rs

1//! MMTk instance.
2use crate::global_state::{GcStatus, GlobalState};
3use crate::plan::CreateGeneralPlanArgs;
4use crate::plan::Plan;
5use crate::policy::sft_map::{create_sft_map, SFTMap};
6use crate::scheduler::GCWorkScheduler;
7
8#[cfg(feature = "vo_bit")]
9use crate::util::address::ObjectReference;
10#[cfg(feature = "analysis")]
11use crate::util::analysis::AnalysisManager;
12use crate::util::finalizable_processor::FinalizableProcessor;
13use crate::util::heap::gc_trigger::GCTrigger;
14use crate::util::heap::layout::heap_parameters::MAX_SPACES;
15use crate::util::heap::layout::vm_layout::{vm_layout, VMLayout};
16use crate::util::heap::layout::{self, Mmapper, VMMap};
17use crate::util::heap::HeapMeta;
18use crate::util::opaque_pointer::*;
19use crate::util::options::Options;
20use crate::util::reference_processor::ReferenceProcessors;
21#[cfg(feature = "sanity")]
22use crate::util::sanity::sanity_checker::SanityChecker;
23#[cfg(feature = "extreme_assertions")]
24use crate::util::slot_logger::SlotLogger;
25use crate::util::statistics::stats::Stats;
26#[cfg(feature = "vm_space")]
27use crate::vm::object_model::ObjectModel;
28use crate::vm::ReferenceGlue;
29use crate::vm::VMBinding;
30use std::cell::UnsafeCell;
31use std::collections::HashMap;
32use std::default::Default;
33#[cfg(feature = "sanity")]
34use std::sync::atomic::AtomicBool;
35use std::sync::atomic::Ordering;
36use std::sync::Arc;
37use std::sync::Mutex;
38
39lazy_static! {
40    // I am not sure if we should include these mmappers as part of MMTk struct.
41    // The considerations are:
42    // 1. We need VMMap and Mmapper to create spaces. It is natural that the mappers are not
43    //    part of MMTK, as creating MMTK requires these mappers. We could use Rc/Arc for these mappers though.
44    // 2. These mmappers are possibly global across multiple MMTk instances, as they manage the
45    //    entire address space.
46    // TODO: We should refactor this when we know more about how multiple MMTK instances work.
47
48    /// A global VMMap that manages the mapping of spaces to virtual memory ranges.
49    pub static ref VM_MAP: Box<dyn VMMap + Send + Sync> = layout::create_vm_map();
50
51    /// A global Mmapper for mmaping and protection of virtual memory.
52    pub static ref MMAPPER: Box<dyn Mmapper> = layout::create_mmapper();
53}
54
55use crate::util::rust_util::InitializeOnce;
56
57// A global space function table that allows efficient dispatch space specific code for addresses in our heap.
58pub static SFT_MAP: InitializeOnce<Box<dyn SFTMap>> = InitializeOnce::new();
59
60/// MMTk builder. This is used to set options and other settings before actually creating an MMTk instance.
61pub struct MMTKBuilder {
62    /// The options for this instance.
63    pub options: Options,
64}
65
66impl MMTKBuilder {
67    /// Create an MMTK builder with options read from environment variables, or using built-in
68    /// default if not overridden by environment variables.
69    pub fn new() -> Self {
70        let mut builder = Self::new_no_env_vars();
71        builder.options.read_env_var_settings();
72        builder
73    }
74
75    /// Create an MMTK builder with build-in default options, but without reading options from
76    /// environment variables.
77    pub fn new_no_env_vars() -> Self {
78        MMTKBuilder {
79            options: Options::default(),
80        }
81    }
82
83    /// Set an option.
84    pub fn set_option(&mut self, name: &str, val: &str) -> bool {
85        self.options.set_from_string(name, val)
86    }
87
88    /// Set multiple options by a string. The string should be key-value pairs separated by white spaces,
89    /// such as `threads=1 stress_factor=4096`.
90    pub fn set_options_bulk_by_str(&mut self, options: &str) -> bool {
91        self.options.set_bulk_from_string(options)
92    }
93
94    /// Custom VM layout constants. VM bindings may use this function for compressed or 39-bit heap support.
95    /// This function must be called before MMTk::new()
96    pub fn set_vm_layout(&mut self, constants: VMLayout) {
97        VMLayout::set_custom_vm_layout(constants)
98    }
99
100    /// Build an MMTk instance from the builder.
101    pub fn build<VM: VMBinding>(&self) -> MMTK<VM> {
102        MMTK::new(Arc::new(self.options.clone()))
103    }
104}
105
106impl Default for MMTKBuilder {
107    fn default() -> Self {
108        Self::new()
109    }
110}
111
112/// An MMTk instance. MMTk allows multiple instances to run independently, and each instance gives users a separate heap.
113/// *Note that multi-instances is not fully supported yet*
114pub struct MMTK<VM: VMBinding> {
115    pub(crate) options: Arc<Options>,
116    pub(crate) state: Arc<GlobalState>,
117    pub(crate) plan: UnsafeCell<Box<dyn Plan<VM = VM>>>,
118    pub(crate) reference_processors: ReferenceProcessors,
119    pub(crate) finalizable_processor:
120        Mutex<FinalizableProcessor<<VM::VMReferenceGlue as ReferenceGlue<VM>>::FinalizableType>>,
121    pub(crate) scheduler: Arc<GCWorkScheduler<VM>>,
122    #[cfg(feature = "sanity")]
123    pub(crate) sanity_checker: Mutex<SanityChecker<VM::VMSlot>>,
124    #[cfg(feature = "extreme_assertions")]
125    pub(crate) slot_logger: SlotLogger<VM::VMSlot>,
126    pub(crate) gc_trigger: Arc<GCTrigger<VM>>,
127    pub(crate) stats: Arc<Stats>,
128    #[cfg(feature = "sanity")]
129    inside_sanity: AtomicBool,
130    /// Analysis counters. The feature analysis allows us to periodically stop the world and collect some statistics.
131    #[cfg(feature = "analysis")]
132    pub(crate) analysis_manager: Arc<AnalysisManager<VM>>,
133}
134
135unsafe impl<VM: VMBinding> Sync for MMTK<VM> {}
136unsafe impl<VM: VMBinding> Send for MMTK<VM> {}
137
138impl<VM: VMBinding> MMTK<VM> {
139    /// Create an MMTK instance. This is not public. Bindings should use [`MMTKBuilder::build`].
140    pub(crate) fn new(options: Arc<Options>) -> Self {
141        // Verify the Mmapper can handle the required address space size.
142        vm_layout().validate_address_space();
143
144        // Initialize SFT first in case we need to use this in the constructor.
145        // The first call will initialize SFT map. Other calls will be blocked until SFT map is initialized.
146        crate::policy::sft_map::SFTRefStorage::pre_use_check();
147        SFT_MAP.initialize_once(&create_sft_map);
148
149        let num_workers = if cfg!(feature = "single_worker") {
150            1
151        } else {
152            *options.threads
153        };
154
155        let scheduler = GCWorkScheduler::new(num_workers, (*options.thread_affinity).clone());
156
157        let state = Arc::new(GlobalState::default());
158
159        let gc_trigger = Arc::new(GCTrigger::new(
160            options.clone(),
161            scheduler.clone(),
162            state.clone(),
163        ));
164
165        let stats = Arc::new(Stats::new(&options));
166
167        // We need this during creating spaces, but we do not use this once the MMTk instance is created.
168        // So we do not save it in MMTK. This may change in the future.
169        let mut heap = HeapMeta::new();
170
171        // Create plan and spaces. Note that side metadata is not initialized yet. Plan creation should avoid using it.
172        let mut plan = crate::plan::create_plan(
173            *options.plan,
174            CreateGeneralPlanArgs {
175                vm_map: VM_MAP.as_ref(),
176                mmapper: MMAPPER.as_ref(),
177                options: options.clone(),
178                state: state.clone(),
179                gc_trigger: gc_trigger.clone(),
180                scheduler: scheduler.clone(),
181                stats: &stats,
182                heap: &mut heap,
183            },
184        );
185
186        // Initialize side metadata runtime state and reserve its address range after creating spaces.
187        crate::util::metadata::side_metadata::initialize_side_metadata::<VM>(&options);
188
189        // We haven't finished creating MMTk. No one is using the GC trigger. We cast the arc into a mutable reference.
190        {
191            // TODO: use Arc::get_mut_unchecked() when it is availble.
192            let gc_trigger: &mut GCTrigger<VM> =
193                unsafe { &mut *(Arc::as_ptr(&gc_trigger) as *mut _) };
194            // We know the plan address will not change. Cast it to a static reference.
195            let static_plan: &'static dyn Plan<VM = VM> = unsafe { &*(&*plan as *const _) };
196            // Set the plan so we can trigger GC and check GC condition without using plan
197            gc_trigger.set_plan(static_plan);
198        }
199
200        // TODO: This probably does not work if we have multiple MMTk instances.
201        // This needs to be called after we create Plan. It needs to use HeapMeta, which is gradually built when we create spaces.
202        VM_MAP.finalize_static_space_map(
203            heap.get_discontig_start(),
204            heap.get_discontig_end(),
205            &mut |start_address| {
206                plan.for_each_space_mut(&mut |space| {
207                    // If the `VMMap` has a discontiguous memory range, we notify all discontiguous
208                    // space that the starting address has been determined.
209                    if let Some(pr) = space.maybe_get_page_resource_mut() {
210                        pr.update_discontiguous_start(start_address);
211                    }
212                })
213            },
214        );
215
216        // The order here is important:
217        plan.initialize_side_metadata();
218        // Initialize side metadat sanity first
219        plan.verify_side_metadata_sanity();
220        // Then intiialize SFT because it may use side metadata
221        plan.initialize_sft();
222
223        MMTK {
224            options,
225            state,
226            plan: UnsafeCell::new(plan),
227            reference_processors: ReferenceProcessors::new(),
228            finalizable_processor: Mutex::new(FinalizableProcessor::<
229                <VM::VMReferenceGlue as ReferenceGlue<VM>>::FinalizableType,
230            >::new()),
231            scheduler,
232            #[cfg(feature = "sanity")]
233            sanity_checker: Mutex::new(SanityChecker::new()),
234            #[cfg(feature = "sanity")]
235            inside_sanity: AtomicBool::new(false),
236            #[cfg(feature = "extreme_assertions")]
237            slot_logger: SlotLogger::new(),
238            #[cfg(feature = "analysis")]
239            analysis_manager: Arc::new(AnalysisManager::new(stats.clone())),
240            gc_trigger,
241            stats,
242        }
243    }
244
245    /// Initialize the GC worker threads that are required for doing garbage collections.
246    /// This is a mandatory call for a VM during its boot process once its thread system
247    /// is ready.
248    ///
249    /// Internally, this function will invoke [`Collection::spawn_gc_thread()`] to spawn GC worker
250    /// threads.
251    ///
252    /// # Arguments
253    ///
254    /// *   `tls`: The thread that wants to enable the collection. This value will be passed back
255    ///     to the VM in [`Collection::spawn_gc_thread()`] so that the VM knows the context.
256    ///
257    /// [`Collection::spawn_gc_thread()`]: crate::vm::Collection::spawn_gc_thread()
258    pub fn initialize_collection(&'static self, tls: VMThread) {
259        assert!(
260            !self.state.is_initialized(),
261            "MMTk collection has been initialized (was initialize_collection() already called before?)"
262        );
263        self.scheduler.spawn_gc_threads(self, tls);
264        self.state.initialized.store(true, Ordering::SeqCst);
265        probe!(mmtk, collection_initialized);
266    }
267
268    /// Shut down all GC worker threads.
269    pub fn shutdown(&'static self) {
270        if self.state.is_initialized() {
271            self.scheduler.shutdown_gc_threads();
272            self.state.initialized.store(false, Ordering::SeqCst);
273        }
274    }
275
276    /// Prepare an MMTk instance for calling the `fork()` system call.
277    ///
278    /// The `fork()` system call is available on Linux and some UNIX variants, and may be emulated
279    /// on other platforms by libraries such as Cygwin.  The properties of the `fork()` system call
280    /// requires the users to do some preparation before calling it.
281    ///
282    /// -   **Multi-threading**:  If `fork()` is called when the process has multiple threads, it
283    ///     will only duplicate the current thread into the child process, and the child process can
284    ///     only call async-signal-safe functions, notably `exec()`.  For VMs that that use
285    ///     multi-process concurrency, it is imperative that when calling `fork()`, only one thread may
286    ///     exist in the process.
287    ///
288    /// -   **File descriptors**: The child process inherits copies of the parent's set of open
289    ///     file descriptors.  This may or may not be desired depending on use cases.
290    ///
291    /// This function helps VMs that use `fork()` for multi-process concurrency.  It instructs all
292    /// GC threads to save their contexts and return from their entry-point functions.  Currently,
293    /// such threads only include GC workers, and the entry point is
294    /// [`crate::memory_manager::start_worker`].  A subsequent call to `MMTK::after_fork()` will
295    /// re-spawn the threads using their saved contexts.  The VM must not allocate objects in the
296    /// MMTk heap before calling `MMTK::after_fork()`.
297    ///
298    /// TODO: Currently, the MMTk core does not keep any files open for a long time.  In the
299    /// future, this function and the `after_fork` function may be used for handling open file
300    /// descriptors across invocations of `fork()`.  One possible use case is logging GC activities
301    /// and statistics to files, such as performing heap dumps across multiple GCs.
302    ///
303    /// If a VM intends to execute another program by calling `fork()` and immediately calling
304    /// `exec`, it may skip this function because the state of the MMTk instance will be irrelevant
305    /// in that case.
306    ///
307    /// # Caution!
308    ///
309    /// This function sends an asynchronous message to GC threads and returns immediately, but it
310    /// is only safe for the VM to call `fork()` after the underlying **native threads** of the GC
311    /// threads have exited.  After calling this function, the VM should wait for their underlying
312    /// native threads to exit in VM-specific manner before calling `fork()`.
313    pub fn prepare_to_fork(&'static self) {
314        assert!(
315            self.state.is_initialized(),
316            "MMTk collection has not been initialized, yet (was initialize_collection() called before?)"
317        );
318        probe!(mmtk, prepare_to_fork);
319        self.scheduler.stop_gc_threads_for_forking();
320    }
321
322    /// Call this function after the VM called the `fork()` system call.
323    ///
324    /// This function will re-spawn MMTk threads from saved contexts.
325    ///
326    /// # Arguments
327    ///
328    /// *   `tls`: The thread that wants to respawn MMTk threads after forking. This value will be
329    ///     passed back to the VM in `Collection::spawn_gc_thread()` so that the VM knows the
330    ///     context.
331    pub fn after_fork(&'static self, tls: VMThread) {
332        assert!(
333            self.state.is_initialized(),
334            "MMTk collection has not been initialized, yet (was initialize_collection() called before?)"
335        );
336        probe!(mmtk, after_fork);
337        self.scheduler.respawn_gc_threads_after_forking(tls);
338    }
339
340    /// Generic hook to allow benchmarks to be harnessed. MMTk will trigger a GC
341    /// to clear any residual garbage and start collecting statistics for the benchmark.
342    /// This is usually called by the benchmark harness as its last step before the actual benchmark.
343    pub fn harness_begin(&self, tls: VMMutatorThread) {
344        probe!(mmtk, harness_begin);
345        self.handle_user_collection_request(tls, true, true);
346        self.state.inside_harness.store(true, Ordering::SeqCst);
347        self.stats.start_all();
348        self.scheduler.enable_stat();
349    }
350
351    /// Generic hook to allow benchmarks to be harnessed. MMTk will stop collecting
352    /// statistics, and print out the collected statistics in a defined format.
353    /// This is usually called by the benchmark harness right after the actual benchmark.
354    pub fn harness_end(&'static self) {
355        self.stats.stop_all(self);
356        self.state.inside_harness.store(false, Ordering::SeqCst);
357        probe!(mmtk, harness_end);
358    }
359
360    #[cfg(feature = "sanity")]
361    pub(crate) fn sanity_begin(&self) {
362        self.inside_sanity.store(true, Ordering::Relaxed)
363    }
364
365    #[cfg(feature = "sanity")]
366    pub(crate) fn sanity_end(&self) {
367        self.inside_sanity.store(false, Ordering::Relaxed)
368    }
369
370    #[cfg(feature = "sanity")]
371    #[allow(unused)]
372    pub(crate) fn is_in_sanity(&self) -> bool {
373        self.inside_sanity.load(Ordering::Relaxed)
374    }
375
376    pub(crate) fn set_gc_status(&self, s: GcStatus) {
377        let mut gc_status = self.state.gc_status.lock().unwrap();
378        if *gc_status == GcStatus::NotInGC {
379            self.state.stacks_prepared.store(false, Ordering::SeqCst);
380            // FIXME stats
381            self.stats.start_gc();
382        }
383        *gc_status = s;
384        if *gc_status == GcStatus::NotInGC {
385            // FIXME stats
386            if self.stats.get_gathering_stats() {
387                self.stats.end_gc();
388            }
389        }
390    }
391
392    /// Return true if a collection is in progress.
393    pub fn gc_in_progress(&self) -> bool {
394        *self.state.gc_status.lock().unwrap() != GcStatus::NotInGC
395    }
396
397    /// Return true if a collection is in progress and past the preparatory stage.
398    pub fn gc_in_progress_proper(&self) -> bool {
399        *self.state.gc_status.lock().unwrap() == GcStatus::GcProper
400    }
401
402    /// Return true if the current GC is an emergency GC.
403    ///
404    /// An emergency GC happens when a normal GC cannot reclaim enough memory to satisfy allocation
405    /// requests.  Plans may do full-heap GC, defragmentation, etc. during emergency GCs in order to
406    /// free up more memory.
407    ///
408    /// VM bindings can call this function during GC to check if the current GC is an emergency GC.
409    /// If it is, the VM binding is recommended to retain fewer objects than normal GCs, to the
410    /// extent allowed by the specification of the VM or the language.  For example, the VM binding
411    /// may choose not to retain objects used for caching.  Specifically, for Java virtual machines,
412    /// that means not retaining referents of [`SoftReference`][java-soft-ref] which is primarily
413    /// designed for implementing memory-sensitive caches.
414    ///
415    /// [java-soft-ref]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/SoftReference.html
416    pub fn is_emergency_collection(&self) -> bool {
417        self.state.is_emergency_collection()
418    }
419
420    /// Return true if the current GC is trigger manually by the user/binding.
421    pub fn is_user_triggered_collection(&self) -> bool {
422        self.state.is_user_triggered_collection()
423    }
424
425    /// The application code has requested a collection. This is just a GC hint, and
426    /// we may ignore it.
427    ///
428    /// Returns whether a GC was ran or not. If MMTk triggers a GC, this method will block the
429    /// calling thread and return true when the GC finishes. Otherwise, this method returns
430    /// false immediately.
431    ///
432    /// # Arguments
433    /// * `tls`: The mutator thread that requests the GC
434    /// * `force`: The request cannot be ignored (except for NoGC)
435    /// * `exhaustive`: The requested GC should be exhaustive. This is also a hint.
436    pub fn handle_user_collection_request(
437        &self,
438        tls: VMMutatorThread,
439        force: bool,
440        exhaustive: bool,
441    ) -> bool {
442        if self
443            .gc_trigger
444            .handle_user_collection_request(force, exhaustive)
445        {
446            use crate::vm::Collection;
447            VM::VMCollection::block_for_gc(tls);
448            true
449        } else {
450            false
451        }
452    }
453
454    /// MMTK has requested stop-the-world activity (e.g., stw within a concurrent gc).
455    #[allow(unused)]
456    pub fn trigger_internal_collection_request(&self) {
457        self.gc_trigger.trigger_internal_collection_request();
458    }
459
460    /// Get a reference to the plan.
461    pub fn get_plan(&self) -> &dyn Plan<VM = VM> {
462        unsafe { &**(self.plan.get()) }
463    }
464
465    /// Get the plan as mutable reference.
466    ///
467    /// # Safety
468    ///
469    /// This is unsafe because the caller must ensure that the plan is not used by other threads.
470    #[allow(clippy::mut_from_ref)]
471    pub unsafe fn get_plan_mut(&self) -> &mut dyn Plan<VM = VM> {
472        &mut **(self.plan.get())
473    }
474
475    /// Get the run time options.
476    pub fn get_options(&self) -> &Options {
477        &self.options
478    }
479
480    /// Enumerate objects in all spaces in this MMTK instance.
481    ///
482    /// The call-back function `f` is called for every object that has the valid object bit (VO
483    /// bit), i.e. objects that are allocated in the heap of this MMTK instance, but has not been
484    /// reclaimed, yet.
485    ///
486    /// # Notes about object initialization and finalization
487    ///
488    /// When this function visits an object, it only guarantees that its VO bit must have been set.
489    /// It is not guaranteed if the object has been "fully initialized" in the sense of the
490    /// programming language the VM is implementing.  For example, the object header and the type
491    /// information may not have been written.
492    ///
493    /// It will also visit objects that have been "finalized" in the sense of the programming
494    /// langauge the VM is implementing, as long as the object has not been reclaimed by the GC,
495    /// yet.  Be careful.  If the object header is destroyed, it may not be safe to access such
496    /// objects in the high-level language.
497    ///
498    /// # Interaction with allocation and GC
499    ///
500    /// This function does not mutate the heap.  It is safe if multiple threads execute this
501    /// function concurrently during mutator time.
502    ///
503    /// It has *undefined behavior* if allocation or GC happens while this function is being
504    /// executed.  The VM binding must ensure no threads are allocating and GC does not start while
505    /// executing this function.  One way to do this is stopping all mutators before calling this
506    /// function.
507    ///
508    /// Some high-level languages may provide an API that allows the user to allocate objects and
509    /// trigger GC while enumerating objects.  One example is [`ObjectSpace::each_object`][os_eo] in
510    /// Ruby.  The VM binding may use the callback of this function to save all visited object
511    /// references and let the user visit those references after this function returns.  Make sure
512    /// those saved references are in the root set or in an object that will live through GCs before
513    /// the high-level language finishes visiting the saved object references.
514    ///
515    /// [os_eo]: https://docs.ruby-lang.org/en/master/ObjectSpace.html#method-c-each_object
516    #[cfg(feature = "vo_bit")]
517    pub fn enumerate_objects<F>(&self, f: F)
518    where
519        F: FnMut(ObjectReference),
520    {
521        use crate::util::object_enum;
522
523        let mut enumerator = object_enum::ClosureObjectEnumerator::<_, VM>::new(f);
524        let plan = self.get_plan();
525        plan.for_each_space(&mut |space| {
526            space.enumerate_objects(&mut enumerator);
527        })
528    }
529
530    /// Aggregate a hash map of live bytes per space with the space stats to produce
531    /// a map of live bytes stats for the spaces.
532    pub(crate) fn aggregate_live_bytes_in_last_gc(
533        &self,
534        live_bytes_per_space: [usize; MAX_SPACES],
535    ) -> HashMap<&'static str, crate::LiveBytesStats> {
536        use crate::policy::space::Space;
537        let mut ret = HashMap::new();
538        self.get_plan().for_each_space(&mut |space: &dyn Space<VM>| {
539            let space_name = space.get_name();
540            let space_idx = space.get_descriptor().get_index();
541            let used_pages = space.reserved_pages();
542            if used_pages != 0 {
543                let used_bytes = crate::util::conversions::pages_to_bytes(used_pages);
544                let live_bytes = live_bytes_per_space[space_idx];
545                debug_assert!(
546                    live_bytes <= used_bytes,
547                    "Live bytes of objects in {} ({} bytes) is larger than used pages ({} bytes), something is wrong.",
548                    space_name, live_bytes, used_bytes
549                );
550                ret.insert(space_name, crate::LiveBytesStats {
551                    live_bytes,
552                    used_pages,
553                    used_bytes,
554                });
555            }
556        });
557        ret
558    }
559
560    /// Print VM maps.  It will print the memory ranges used by spaces as well as some attributes of
561    /// the spaces.
562    ///
563    /// -   "I": The space is immortal.  Its objects will never die.
564    /// -   "N": The space is non-movable.  Its objects will never move.
565    ///
566    /// Arguments:
567    /// *   `out`: the place to print the VM maps.
568    /// *   `space_name`: If `None`, print all spaces;
569    ///     if `Some(n)`, only print the space whose name is `n`.
570    pub fn debug_print_vm_maps(
571        &self,
572        out: &mut impl std::fmt::Write,
573        space_name: Option<&str>,
574    ) -> Result<(), std::fmt::Error> {
575        let mut result_so_far = Ok(());
576        self.get_plan().for_each_space(&mut |space| {
577            if result_so_far.is_ok()
578                && (space_name.is_none() || space_name == Some(space.get_name()))
579            {
580                result_so_far = crate::policy::space::print_vm_map(space, out);
581            }
582        });
583        result_so_far
584    }
585
586    /// Initialize object metadata for a VM space object.
587    /// Objects in the VM space are allocated/managed by the binding. This function provides a way for
588    /// the binding to set object metadata in MMTk for an object in the space.
589    #[cfg(feature = "vm_space")]
590    pub fn initialize_vm_space_object(&self, object: crate::util::ObjectReference) {
591        use crate::policy::sft::SFT;
592        let bytes = VM::VMObjectModel::get_current_size(object);
593        self.get_plan()
594            .base()
595            .vm_space
596            .initialize_object_metadata(object, bytes)
597    }
598}
599
600/// A non-mangled function to print object information for debugging purposes. This function can be directly
601/// called from a debugger.
602#[no_mangle]
603pub fn mmtk_debug_print_object(object: crate::util::ObjectReference) {
604    // If the address is unmapped, we cannot access its metadata. Just quit.
605    if !object.to_raw_address().is_mapped() {
606        println!("{} is not mapped in MMTk", object);
607        return;
608    }
609
610    // If the address is not aligned to the object reference size, it is not an object reference.
611    if !object
612        .to_raw_address()
613        .is_aligned_to(crate::util::ObjectReference::ALIGNMENT)
614    {
615        println!(
616            "{} is not properly aligned. It is not an object reference.",
617            object
618        );
619    }
620
621    // Forward to the space
622    let sft = SFT_MAP.get_checked(object.to_raw_address());
623    // Print the space name
624    println!("In {}:", sft.name());
625    // Print object information
626    sft.debug_print_object_info(object);
627}