Skip to content

Commit 1b0959f

Browse files
wksmmtkgc-bot
andauthored
Remove NULL ObjectReference (#169)
Upstream PR: mmtk/mmtk-core#1064 --------- Co-authored-by: mmtkgc-bot <[email protected]>
1 parent f60ecd6 commit 1b0959f

File tree

5 files changed

+65
-48
lines changed

5 files changed

+65
-48
lines changed

mmtk/Cargo.lock

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mmtk/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ log = {version = "0.4", features = ["max_level_trace", "release_max_level_off"]
2828
# - change branch/rev
2929
# - change repo name
3030
# But other changes including adding/removing whitespaces in commented lines may break the CI.
31-
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "e79e94e744660c486d5471f252ff05c4248bcea9" }
31+
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "a02803b4104519ff2289234101a2dd8ceedd1bc7" }
3232
# Uncomment the following to build locally - if you change the path locally, do not commit the change in a PR
3333
# mmtk = { path = "../repos/mmtk-core" }
3434

mmtk/src/api.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use libc::c_char;
55
use libc::c_void;
66
use mmtk::memory_manager;
77
use mmtk::scheduler::*;
8+
use mmtk::util::api_util::NullableObjectReference;
89
use mmtk::util::opaque_pointer::*;
910
use mmtk::util::{Address, ObjectReference};
1011

@@ -210,11 +211,8 @@ pub extern "C" fn add_phantom_candidate(reff: ObjectReference, referent: ObjectR
210211
}
211212

212213
#[no_mangle]
213-
pub extern "C" fn get_forwarded_object(object: ObjectReference) -> ObjectReference {
214-
match object.get_forwarded_object::<JikesRVM>() {
215-
Some(ref_obj) => ref_obj,
216-
None => ObjectReference::NULL,
217-
}
214+
pub extern "C" fn get_forwarded_object(object: ObjectReference) -> NullableObjectReference {
215+
object.get_forwarded_object::<JikesRVM>().into()
218216
}
219217

220218
#[no_mangle]
@@ -285,11 +283,8 @@ pub extern "C" fn add_finalizer(object: ObjectReference) {
285283
}
286284

287285
#[no_mangle]
288-
pub extern "C" fn get_finalized_object() -> ObjectReference {
289-
match memory_manager::get_finalized_object(&SINGLETON) {
290-
Some(obj) => obj,
291-
None => ObjectReference::NULL,
292-
}
286+
pub extern "C" fn get_finalized_object() -> NullableObjectReference {
287+
memory_manager::get_finalized_object(&SINGLETON).into()
293288
}
294289

295290
// Allocation slow path

mmtk/src/object_model.rs

+31-21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ use memory_manager_constants::*;
2121
use tib_layout_constants::*;
2222
use JikesRVM;
2323

24+
/// Used as a parameter of `move_object` to specify where to move an object to.
25+
enum MoveTarget {
26+
/// Move an object to the address returned from `alloc_copy`.
27+
ToAddress(Address),
28+
/// Move an object to an `ObjectReference` pointing to an object previously computed from
29+
/// `get_reference_when_copied_to`.
30+
ToObject(ObjectReference),
31+
}
32+
2433
/** Should we gather stats on hash code state transitions for address-based hashing? */
2534
const HASH_STATS: bool = false;
2635
/** count number of Object.hashCode() operations */
@@ -131,7 +140,7 @@ impl ObjectModel<JikesRVM> for VMObjectModel {
131140

132141
let bytes = if copy {
133142
let bytes = Self::bytes_required_when_copied(from, rvm_type);
134-
Self::move_object(unsafe { Address::zero() }, from, to, bytes, rvm_type);
143+
Self::move_object(from, MoveTarget::ToObject(to), bytes, rvm_type);
135144
bytes
136145
} else {
137146
Self::bytes_used(from, rvm_type)
@@ -156,7 +165,8 @@ impl ObjectModel<JikesRVM> for VMObjectModel {
156165
}
157166
}
158167

159-
ObjectReference::from_raw_address(res + OBJECT_REF_OFFSET)
168+
debug_assert!(!res.is_zero());
169+
unsafe { ObjectReference::from_raw_address_unchecked(res + OBJECT_REF_OFFSET) }
160170
}
161171

162172
fn get_current_size(object: ObjectReference) -> usize {
@@ -229,7 +239,8 @@ impl ObjectModel<JikesRVM> for VMObjectModel {
229239

230240
#[inline(always)]
231241
fn address_to_ref(addr: Address) -> ObjectReference {
232-
ObjectReference::from_raw_address(addr + (-TIB_OFFSET))
242+
debug_assert!(!addr.is_zero());
243+
unsafe { ObjectReference::from_raw_address_unchecked(addr + (-TIB_OFFSET)) }
233244
}
234245

235246
fn dump_object(_object: ObjectReference) {
@@ -256,7 +267,7 @@ impl VMObjectModel {
256267
let offset = Self::get_offset_for_alignment_class(from, rvm_type);
257268
let region = copy_context.alloc_copy(from, bytes, align, offset, copy);
258269

259-
let to_obj = Self::move_object(region, from, ObjectReference::NULL, bytes, rvm_type);
270+
let to_obj = Self::move_object(from, MoveTarget::ToAddress(region), bytes, rvm_type);
260271
copy_context.post_copy(to_obj, bytes, copy);
261272
to_obj
262273
}
@@ -275,7 +286,7 @@ impl VMObjectModel {
275286
let offset = Self::get_offset_for_alignment_array(from, rvm_type);
276287
let region = copy_context.alloc_copy(from, bytes, align, offset, copy);
277288

278-
let to_obj = Self::move_object(region, from, ObjectReference::NULL, bytes, rvm_type);
289+
let to_obj = Self::move_object(from, MoveTarget::ToAddress(region), bytes, rvm_type);
279290
copy_context.post_copy(to_obj, bytes, copy);
280291
// XXX: Do not sync icache/dcache because we do not support PowerPC
281292
to_obj
@@ -371,16 +382,12 @@ impl VMObjectModel {
371382

372383
#[inline(always)]
373384
fn move_object(
374-
immut_to_address: Address,
375385
from_obj: ObjectReference,
376-
immut_to_obj: ObjectReference,
386+
mut to: MoveTarget,
377387
num_bytes: usize,
378388
_rvm_type: Address,
379389
) -> ObjectReference {
380390
trace!("VMObjectModel.move_object");
381-
let mut to_address = immut_to_address;
382-
let mut to_obj = immut_to_obj;
383-
debug_assert!(to_address.is_zero() || to_obj.to_raw_address().is_zero());
384391

385392
// Default values
386393
let mut copy_bytes = num_bytes;
@@ -399,8 +406,8 @@ impl VMObjectModel {
399406

400407
if !DYNAMIC_HASH_OFFSET {
401408
// The hashcode is the first word, so we copy to object one word higher
402-
if to_obj.to_raw_address().is_zero() {
403-
to_address += HASHCODE_BYTES;
409+
if let MoveTarget::ToAddress(ref mut addr) = to {
410+
*addr += HASHCODE_BYTES;
404411
}
405412
}
406413
} else if !DYNAMIC_HASH_OFFSET && hash_state == HASH_STATE_HASHED_AND_MOVED {
@@ -410,9 +417,18 @@ impl VMObjectModel {
410417
}
411418
}
412419

413-
if !to_obj.to_raw_address().is_zero() {
414-
to_address = to_obj.to_raw_address() + (-obj_ref_offset);
415-
}
420+
let (to_address, to_obj) = match to {
421+
MoveTarget::ToAddress(addr) => {
422+
let obj =
423+
unsafe { ObjectReference::from_raw_address_unchecked(addr + obj_ref_offset) };
424+
(addr, obj)
425+
}
426+
MoveTarget::ToObject(obj) => {
427+
let addr = obj.to_raw_address() + (-obj_ref_offset);
428+
debug_assert!(obj.to_raw_address() == addr + obj_ref_offset);
429+
(addr, obj)
430+
}
431+
};
416432

417433
// Low memory word of source object
418434
let from_address = from_obj.to_raw_address() + (-obj_ref_offset);
@@ -422,12 +438,6 @@ impl VMObjectModel {
422438
Self::aligned_32_copy(to_address, from_address, copy_bytes);
423439
}
424440

425-
if to_obj.is_null() {
426-
to_obj = ObjectReference::from_raw_address(to_address + obj_ref_offset);
427-
} else {
428-
debug_assert!(to_obj.to_raw_address() == to_address + obj_ref_offset);
429-
}
430-
431441
// Do we need to copy the hash code?
432442
if hash_state == HASH_STATE_HASHED {
433443
unsafe {

mmtk/src/reference_glue.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use mmtk::util::opaque_pointer::*;
2+
use mmtk::util::Address;
23
use mmtk::util::ObjectReference;
34
use mmtk::vm::ReferenceGlue;
45

@@ -25,15 +26,14 @@ impl ReferenceGlue<JikesRVM> for VMReferenceGlue {
2526
}
2627
}
2728

28-
fn get_referent(object: ObjectReference) -> ObjectReference {
29+
fn get_referent(object: ObjectReference) -> Option<ObjectReference> {
2930
if cfg!(feature = "binding_side_ref_proc") {
3031
panic!();
3132
} else {
32-
debug_assert!(!object.is_null());
33-
unsafe {
34-
(object.to_raw_address() + REFERENCE_REFERENT_FIELD_OFFSET)
35-
.load::<ObjectReference>()
36-
}
33+
let addr = unsafe {
34+
(object.to_raw_address() + REFERENCE_REFERENT_FIELD_OFFSET).load::<Address>()
35+
};
36+
ObjectReference::from_raw_address(addr)
3737
}
3838
}
3939

@@ -52,4 +52,15 @@ impl ReferenceGlue<JikesRVM> for VMReferenceGlue {
5252
}
5353
}
5454
}
55+
56+
fn clear_referent(new_reference: ObjectReference) {
57+
if cfg!(feature = "binding_side_ref_proc") {
58+
panic!();
59+
} else {
60+
unsafe {
61+
(new_reference.to_raw_address() + REFERENCE_REFERENT_FIELD_OFFSET)
62+
.store(Address::ZERO);
63+
}
64+
}
65+
}
5566
}

0 commit comments

Comments
 (0)