Skip to content

Commit a5035a2

Browse files
committed
Make ObjectReferenct non-nullable
1 parent ef2bd6d commit a5035a2

File tree

19 files changed

+97
-130
lines changed

19 files changed

+97
-130
lines changed

src/memory_manager.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,9 +708,6 @@ pub fn is_mmtk_object(addr: Address) -> bool {
708708
/// * `object`: The object reference to query.
709709
pub fn is_in_mmtk_spaces<VM: VMBinding>(object: ObjectReference) -> bool {
710710
use crate::mmtk::SFT_MAP;
711-
if object.is_null() {
712-
return false;
713-
}
714711
SFT_MAP
715712
.get_checked(object.to_address::<VM>())
716713
.is_in_space(object)

src/plan/generational/gc_work.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ impl<VM: VMBinding, P: GenerationalPlanExt<VM> + PlanTraceObject<VM>> ProcessEdg
3636
Self { plan, base }
3737
}
3838
fn trace_object(&mut self, object: ObjectReference) -> ObjectReference {
39-
debug_assert!(!object.is_null());
40-
4139
// We cannot borrow `self` twice in a call, so we extract `worker` as a local variable.
4240
let worker = self.worker();
4341
self.plan
4442
.trace_object_nursery(&mut self.base.nodes, object, worker)
4543
}
4644
fn process_edge(&mut self, slot: EdgeOf<Self>) {
47-
let object = slot.load();
48-
if object.is_null() {
45+
let Some(object) = slot.load() else {
46+
// Skip slots that are not holding an object reference.
4947
return;
50-
}
48+
};
5149
let new_object = self.trace_object(object);
5250
debug_assert!(!self.plan.is_object_in_nursery(new_object));
5351
// Note: If `object` is a mature object, `trace_object` will not call `space.trace_object`,

src/plan/tracing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'a, E: ProcessEdgesWork> EdgeVisitor<EdgeOf<E>> for ObjectsClosure<'a, E> {
117117
{
118118
use crate::vm::edge_shape::Edge;
119119
trace!(
120-
"(ObjectsClosure) Visit edge {:?} (pointing to {})",
120+
"(ObjectsClosure) Visit edge {:?} (pointing to {:?})",
121121
slot,
122122
slot.load()
123123
);

src/policy/copyspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ impl<VM: VMBinding> CopySpace<VM> {
204204
worker: &mut GCWorker<VM>,
205205
) -> ObjectReference {
206206
trace!("copyspace.trace_object(, {:?}, {:?})", object, semantics,);
207-
debug_assert!(!object.is_null());
208207

209208
// If this is not from space, we do not need to trace it (the object has been copied to the tosapce)
210209
if !self.is_from_space() {

src/policy/immix/immixspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ impl<VM: VMBinding> crate::policy::gc_work::PolicyTraceObject<VM> for ImmixSpace
184184
copy: Option<CopySemantics>,
185185
worker: &mut GCWorker<VM>,
186186
) -> ObjectReference {
187-
debug_assert!(!object.is_null());
188187
if KIND == TRACE_KIND_TRANSITIVE_PIN {
189188
self.trace_object_without_moving(queue, object)
190189
} else if KIND == TRACE_KIND_DEFRAG {

src/policy/immortalspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ impl<VM: VMBinding> ImmortalSpace<VM> {
187187
queue: &mut Q,
188188
object: ObjectReference,
189189
) -> ObjectReference {
190-
debug_assert!(!object.is_null());
191190
#[cfg(feature = "vo_bit")]
192191
debug_assert!(
193192
crate::util::metadata::vo_bit::is_vo_bit_set::<VM>(object),

src/policy/largeobjectspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ impl<VM: VMBinding> LargeObjectSpace<VM> {
189189
queue: &mut Q,
190190
object: ObjectReference,
191191
) -> ObjectReference {
192-
debug_assert!(!object.is_null());
193192
#[cfg(feature = "vo_bit")]
194193
debug_assert!(
195194
crate::util::metadata::vo_bit::is_vo_bit_set::<VM>(object),

src/policy/markcompactspace.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ impl<VM: VMBinding> SFT for MarkCompactSpace<VM> {
3737
}
3838

3939
fn get_forwarded_object(&self, object: ObjectReference) -> Option<ObjectReference> {
40-
let forwarding_pointer = Self::get_header_forwarding_pointer(object);
41-
if forwarding_pointer.is_null() {
42-
None
43-
} else {
44-
Some(forwarding_pointer)
45-
}
40+
Self::get_header_forwarding_pointer(object)
4641
}
4742

4843
fn is_live(&self, object: ObjectReference) -> bool {
@@ -130,7 +125,6 @@ impl<VM: VMBinding> crate::policy::gc_work::PolicyTraceObject<VM> for MarkCompac
130125
_copy: Option<CopySemantics>,
131126
_worker: &mut GCWorker<VM>,
132127
) -> ObjectReference {
133-
debug_assert!(!object.is_null());
134128
debug_assert!(
135129
KIND != TRACE_KIND_TRANSITIVE_PIN,
136130
"MarkCompact does not support transitive pin trace."
@@ -177,8 +171,9 @@ impl<VM: VMBinding> MarkCompactSpace<VM> {
177171
}
178172

179173
/// Get header forwarding pointer for an object
180-
fn get_header_forwarding_pointer(object: ObjectReference) -> ObjectReference {
181-
unsafe { Self::header_forwarding_pointer_address(object).load::<ObjectReference>() }
174+
fn get_header_forwarding_pointer(object: ObjectReference) -> Option<ObjectReference> {
175+
let addr = unsafe { Self::header_forwarding_pointer_address(object).load::<Address>() };
176+
ObjectReference::from_raw_address(addr)
182177
}
183178

184179
/// Store header forwarding pointer for an object
@@ -251,7 +246,9 @@ impl<VM: VMBinding> MarkCompactSpace<VM> {
251246
queue.enqueue(object);
252247
}
253248

254-
Self::get_header_forwarding_pointer(object)
249+
Self::get_header_forwarding_pointer(object).unwrap_or_else(|| {
250+
panic!("trace_forward_object called when an object is not forwarded, yet. object: {object}")
251+
})
255252
}
256253

257254
pub fn test_and_mark(object: ObjectReference) -> bool {
@@ -388,10 +385,9 @@ impl<VM: VMBinding> MarkCompactSpace<VM> {
388385
// clear the VO bit
389386
vo_bit::unset_vo_bit::<VM>(obj);
390387

391-
let forwarding_pointer = Self::get_header_forwarding_pointer(obj);
392-
393-
trace!("Compact {} to {}", obj, forwarding_pointer);
394-
if !forwarding_pointer.is_null() {
388+
let maybe_forwarding_pointer = Self::get_header_forwarding_pointer(obj);
389+
if let Some(forwarding_pointer) = maybe_forwarding_pointer {
390+
trace!("Compact {} to {}", obj, forwarding_pointer);
395391
let new_object = forwarding_pointer;
396392
Self::clear_header_forwarding_pointer(new_object);
397393

@@ -403,6 +399,8 @@ impl<VM: VMBinding> MarkCompactSpace<VM> {
403399
vo_bit::set_vo_bit::<VM>(new_object);
404400
to = new_object.to_object_start::<VM>() + copied_size;
405401
debug_assert_eq!(end_of_new_object, to);
402+
} else {
403+
trace!("Skipping dead object {}", obj);
406404
}
407405
}
408406
}

src/policy/marksweepspace/malloc_ms/global.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,6 @@ impl<VM: VMBinding> MallocSpace<VM> {
400400
queue: &mut Q,
401401
object: ObjectReference,
402402
) -> ObjectReference {
403-
debug_assert!(!object.is_null());
404-
405403
assert!(
406404
self.in_space(object),
407405
"Cannot mark an object {} that was not alloced by malloc.",

src/policy/marksweepspace/native_ms/block.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ impl Block {
287287
while cell + cell_size <= self.start() + Block::BYTES {
288288
// The invariants we checked earlier ensures that we can use cell and object reference interchangably
289289
// We may not really have an object in this cell, but if we do, this object reference is correct.
290-
let potential_object = ObjectReference::from_raw_address(cell);
290+
// About unsafe: We know `cell` is non-zero here.
291+
let potential_object = unsafe { ObjectReference::from_raw_address_unchecked(cell) };
291292

292293
if !VM::VMObjectModel::LOCAL_MARK_BIT_SPEC
293294
.is_marked::<VM>(potential_object, Ordering::SeqCst)
@@ -327,9 +328,12 @@ impl Block {
327328

328329
while cell + cell_size <= self.end() {
329330
// possible object ref
330-
let potential_object_ref = ObjectReference::from_raw_address(
331-
cursor + VM::VMObjectModel::OBJECT_REF_OFFSET_LOWER_BOUND,
332-
);
331+
let potential_object_ref = unsafe {
332+
// We know cursor plus an offset cannot be 0.
333+
ObjectReference::from_raw_address_unchecked(
334+
cursor + VM::VMObjectModel::OBJECT_REF_OFFSET_LOWER_BOUND,
335+
)
336+
};
333337
trace!(
334338
"{:?}: cell = {}, last cell in free list = {}, cursor = {}, potential object = {}",
335339
self,

0 commit comments

Comments
 (0)