mmtk/policy/sft.rs
1use crate::plan::tracing::OptionObjectQueue;
2use crate::scheduler::GCWorker;
3use crate::util::*;
4use crate::vm::VMBinding;
5use std::marker::PhantomData;
6
7/// Space Function Table (SFT).
8///
9/// This trait captures functions that reflect _space-specific per-object
10/// semantics_. These functions are implemented for each object via a special
11/// space-based dynamic dispatch mechanism where the semantics are _not_
12/// determined by the object's _type_, but rather, are determined by the _space_
13/// that the object is in.
14///
15/// The underlying mechanism exploits the fact that spaces use the address space
16/// at an MMTk chunk granularity with the consequence that each chunk maps to
17/// exactluy one space, so knowing the chunk for an object reveals its space.
18/// The dispatch then works by performing simple address arithmetic on the object
19/// reference to find a chunk index which is used to index a table which returns
20/// the space. The relevant function is then dispatched against that space
21/// object.
22///
23/// We use the SFT trait to simplify typing for Rust, so our table is a
24/// table of SFT rather than Space.
25pub trait SFT {
26 /// The space name
27 fn name(&self) -> &'static str;
28
29 /// Get forwarding pointer if the object is forwarded.
30 fn get_forwarded_object(&self, _object: ObjectReference) -> Option<ObjectReference> {
31 None
32 }
33
34 /// Is the object live, determined by the policy?
35 fn is_live(&self, object: ObjectReference) -> bool;
36
37 /// Is the object reachable, determined by the policy?
38 /// Note: Objects in ImmortalSpace may have `is_live = true` but are actually unreachable.
39 fn is_reachable(&self, object: ObjectReference) -> bool {
40 self.is_live(object)
41 }
42
43 // Functions for pinning/unpining and checking if an object is pinned
44 // For non moving policies, all the objects are considered as forever pinned,
45 // thus attempting to pin or unpin them will not succeed and will always return false.
46 // For policies where moving is compusory, pin/unpin is impossible and will panic (is_object_pinned will return false).
47 // For policies that support pinning (eg. Immix), pin/unpin will return a boolean indicating that the
48 // pinning/unpinning action has been performed by the function, and is_object_pinned will return whether the object
49 // is currently pinned.
50 #[cfg(feature = "object_pinning")]
51 fn pin_object(&self, object: ObjectReference) -> bool;
52 #[cfg(feature = "object_pinning")]
53 fn unpin_object(&self, object: ObjectReference) -> bool;
54 #[cfg(feature = "object_pinning")]
55 fn is_object_pinned(&self, object: ObjectReference) -> bool;
56
57 /// Is the object movable, determined by the policy? E.g. the policy is non-moving,
58 /// or the object is pinned.
59 fn is_movable(&self) -> bool;
60
61 /// Is the object sane? A policy should return false if there is any abnormality about
62 /// object - the sanity checker will fail if an object is not sane.
63 #[cfg(feature = "sanity")]
64 fn is_sane(&self) -> bool;
65
66 /// Is the object managed by MMTk? For most cases, if we find the sft for an object, that means
67 /// the object is in the space and managed by MMTk. However, for some spaces, like MallocSpace,
68 /// we mark the entire chunk in the SFT table as a malloc space, but only some of the addresses
69 /// in the space contain actual MMTk objects. So they need a further check.
70 fn is_in_space(&self, _object: ObjectReference) -> bool {
71 true
72 }
73
74 /// Is `addr` a valid object reference to an object allocated in this space?
75 /// This default implementation works for all spaces that use MMTk's mapper to allocate memory.
76 /// Some spaces, like `MallocSpace`, use third-party libraries to allocate memory.
77 /// Such spaces needs to override this method.
78 #[cfg(feature = "vo_bit")]
79 fn is_mmtk_object(&self, addr: Address) -> Option<ObjectReference>;
80
81 #[cfg(feature = "vo_bit")]
82 fn find_object_from_internal_pointer(
83 &self,
84 ptr: Address,
85 max_search_bytes: usize,
86 ) -> Option<ObjectReference>;
87
88 /// Initialize object metadata (in the header, or in the side metadata).
89 ///
90 /// This method is called after an object is allocated. Specifically,
91 /// - The VM binding calls [`crate::MMTK::initialize_vm_space_object`] which calls this method
92 /// to set the metadata for the VM space.
93 /// - Objects in other spaces are allocated by mutators using an MMTk allocator.
94 /// `Mutator::post_alloc` will call this method after allocation.
95 fn initialize_object_metadata(&self, object: ObjectReference, _bytes: usize);
96
97 /// Trace objects through SFT. This along with [`crate::plan::tracing::SFTTrace`]
98 /// provides an easy way for most plans to trace objects without the need to implement any plan-specific
99 /// code. However, tracing objects for some policies are more complicated, and they do not provide an
100 /// implementation of this method. For example, mark compact space requires trace twice in each GC.
101 /// Immix has defrag trace and fast trace.
102 fn sft_trace_object(
103 &self,
104 // We use `OptionObjectQueue`, the simplest `ObjectQueue` implementation, for `queue`
105 // because SFT doesn't support generic parameters. The generic `SFTTrace::trace_object`
106 // method wraps `SFT::sft_trace_object` and forwards the enqueued object to the actual
107 // queue.
108 queue: &mut OptionObjectQueue,
109 object: ObjectReference,
110 worker: GCWorkerMutRef,
111 ) -> ObjectReference;
112
113 /// Print debug info for the object. The implementer should print one line at a time so in case of an unexpected error,
114 /// we still print something.
115 fn debug_print_object_info(&self, _object: ObjectReference) {
116 println!("This policy does not implement debug_print_object_info.");
117 }
118}
119
120// Create erased VM refs for these types that will be used in `sft_trace_object()`.
121// In this way, we can store the refs with <VM> in SFT (which cannot have parameters with generic type parameters)
122
123use crate::util::erase_vm::define_erased_vm_mut_ref;
124define_erased_vm_mut_ref!(GCWorkerMutRef = GCWorker<VM>);
125
126/// Print debug info for SFT. Should be false when committed.
127pub const DEBUG_SFT: bool = cfg!(debug_assertions) && false;
128
129/// An empty entry for SFT.
130#[derive(Debug)]
131pub struct EmptySpaceSFT {}
132
133pub const EMPTY_SFT_NAME: &str = "empty";
134pub const EMPTY_SPACE_SFT: EmptySpaceSFT = EmptySpaceSFT {};
135
136impl SFT for EmptySpaceSFT {
137 fn name(&self) -> &'static str {
138 EMPTY_SFT_NAME
139 }
140 fn is_live(&self, object: ObjectReference) -> bool {
141 panic!(
142 "Called is_live() on {:x}, which maps to an empty space",
143 object
144 )
145 }
146 #[cfg(feature = "sanity")]
147 fn is_sane(&self) -> bool {
148 warn!("Object in empty space!");
149 false
150 }
151 #[cfg(feature = "object_pinning")]
152 fn pin_object(&self, _object: ObjectReference) -> bool {
153 panic!("Cannot pin/unpin objects of EmptySpace.")
154 }
155 #[cfg(feature = "object_pinning")]
156 fn unpin_object(&self, _object: ObjectReference) -> bool {
157 panic!("Cannot pin/unpin objects of EmptySpace.")
158 }
159 #[cfg(feature = "object_pinning")]
160 fn is_object_pinned(&self, _object: ObjectReference) -> bool {
161 false
162 }
163 fn is_movable(&self) -> bool {
164 /*
165 * FIXME steveb I think this should panic (ie the function should not
166 * be invoked on an empty space). However, JikesRVM currently does
167 * call this in an unchecked way and expects 'false' for out of bounds
168 * addresses. So until that is fixed upstream, we'll return false here.
169 *
170 * panic!("called is_movable() on empty space")
171 */
172 false
173 }
174 fn is_in_space(&self, _object: ObjectReference) -> bool {
175 false
176 }
177 #[cfg(feature = "vo_bit")]
178 fn is_mmtk_object(&self, _addr: Address) -> Option<ObjectReference> {
179 None
180 }
181 #[cfg(feature = "vo_bit")]
182 fn find_object_from_internal_pointer(
183 &self,
184 _ptr: Address,
185 _max_search_bytes: usize,
186 ) -> Option<ObjectReference> {
187 None
188 }
189
190 fn initialize_object_metadata(&self, object: ObjectReference, _bytes: usize) {
191 panic!(
192 "Called initialize_object_metadata() on {:x}, which maps to an empty space",
193 object
194 )
195 }
196
197 fn sft_trace_object(
198 &self,
199 _queue: &mut OptionObjectQueue,
200 object: ObjectReference,
201 _worker: GCWorkerMutRef,
202 ) -> ObjectReference {
203 // We do not have the `VM` type parameter here, so we cannot forward the call to the VM.
204 panic!(
205 "Call trace_object() on {}, which maps to an empty space. SFTTrace does not support the fallback to vm_trace_object().",
206 object,
207 )
208 }
209}