Skip to content

Commit

Permalink
Remove NULL ObjectReference (#169)
Browse files Browse the repository at this point in the history
Upstream PR: mmtk/mmtk-core#1064

---------

Co-authored-by: mmtkgc-bot <mmtkgc.bot@gmail.com>
  • Loading branch information
wks and mmtkgc-bot authored Apr 27, 2024
1 parent f60ecd6 commit 1b0959f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 48 deletions.
21 changes: 11 additions & 10 deletions mmtk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mmtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ log = {version = "0.4", features = ["max_level_trace", "release_max_level_off"]
# - change branch/rev
# - change repo name
# But other changes including adding/removing whitespaces in commented lines may break the CI.
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "e79e94e744660c486d5471f252ff05c4248bcea9" }
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "a02803b4104519ff2289234101a2dd8ceedd1bc7" }
# Uncomment the following to build locally - if you change the path locally, do not commit the change in a PR
# mmtk = { path = "../repos/mmtk-core" }

Expand Down
15 changes: 5 additions & 10 deletions mmtk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use libc::c_char;
use libc::c_void;
use mmtk::memory_manager;
use mmtk::scheduler::*;
use mmtk::util::api_util::NullableObjectReference;
use mmtk::util::opaque_pointer::*;
use mmtk::util::{Address, ObjectReference};

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

#[no_mangle]
pub extern "C" fn get_forwarded_object(object: ObjectReference) -> ObjectReference {
match object.get_forwarded_object::<JikesRVM>() {
Some(ref_obj) => ref_obj,
None => ObjectReference::NULL,
}
pub extern "C" fn get_forwarded_object(object: ObjectReference) -> NullableObjectReference {
object.get_forwarded_object::<JikesRVM>().into()
}

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

#[no_mangle]
pub extern "C" fn get_finalized_object() -> ObjectReference {
match memory_manager::get_finalized_object(&SINGLETON) {
Some(obj) => obj,
None => ObjectReference::NULL,
}
pub extern "C" fn get_finalized_object() -> NullableObjectReference {
memory_manager::get_finalized_object(&SINGLETON).into()
}

// Allocation slow path
Expand Down
52 changes: 31 additions & 21 deletions mmtk/src/object_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ use memory_manager_constants::*;
use tib_layout_constants::*;
use JikesRVM;

/// Used as a parameter of `move_object` to specify where to move an object to.
enum MoveTarget {
/// Move an object to the address returned from `alloc_copy`.
ToAddress(Address),
/// Move an object to an `ObjectReference` pointing to an object previously computed from
/// `get_reference_when_copied_to`.
ToObject(ObjectReference),
}

/** Should we gather stats on hash code state transitions for address-based hashing? */
const HASH_STATS: bool = false;
/** count number of Object.hashCode() operations */
Expand Down Expand Up @@ -131,7 +140,7 @@ impl ObjectModel<JikesRVM> for VMObjectModel {

let bytes = if copy {
let bytes = Self::bytes_required_when_copied(from, rvm_type);
Self::move_object(unsafe { Address::zero() }, from, to, bytes, rvm_type);
Self::move_object(from, MoveTarget::ToObject(to), bytes, rvm_type);
bytes
} else {
Self::bytes_used(from, rvm_type)
Expand All @@ -156,7 +165,8 @@ impl ObjectModel<JikesRVM> for VMObjectModel {
}
}

ObjectReference::from_raw_address(res + OBJECT_REF_OFFSET)
debug_assert!(!res.is_zero());
unsafe { ObjectReference::from_raw_address_unchecked(res + OBJECT_REF_OFFSET) }
}

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

#[inline(always)]
fn address_to_ref(addr: Address) -> ObjectReference {
ObjectReference::from_raw_address(addr + (-TIB_OFFSET))
debug_assert!(!addr.is_zero());
unsafe { ObjectReference::from_raw_address_unchecked(addr + (-TIB_OFFSET)) }
}

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

let to_obj = Self::move_object(region, from, ObjectReference::NULL, bytes, rvm_type);
let to_obj = Self::move_object(from, MoveTarget::ToAddress(region), bytes, rvm_type);
copy_context.post_copy(to_obj, bytes, copy);
to_obj
}
Expand All @@ -275,7 +286,7 @@ impl VMObjectModel {
let offset = Self::get_offset_for_alignment_array(from, rvm_type);
let region = copy_context.alloc_copy(from, bytes, align, offset, copy);

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

#[inline(always)]
fn move_object(
immut_to_address: Address,
from_obj: ObjectReference,
immut_to_obj: ObjectReference,
mut to: MoveTarget,
num_bytes: usize,
_rvm_type: Address,
) -> ObjectReference {
trace!("VMObjectModel.move_object");
let mut to_address = immut_to_address;
let mut to_obj = immut_to_obj;
debug_assert!(to_address.is_zero() || to_obj.to_raw_address().is_zero());

// Default values
let mut copy_bytes = num_bytes;
Expand All @@ -399,8 +406,8 @@ impl VMObjectModel {

if !DYNAMIC_HASH_OFFSET {
// The hashcode is the first word, so we copy to object one word higher
if to_obj.to_raw_address().is_zero() {
to_address += HASHCODE_BYTES;
if let MoveTarget::ToAddress(ref mut addr) = to {
*addr += HASHCODE_BYTES;
}
}
} else if !DYNAMIC_HASH_OFFSET && hash_state == HASH_STATE_HASHED_AND_MOVED {
Expand All @@ -410,9 +417,18 @@ impl VMObjectModel {
}
}

if !to_obj.to_raw_address().is_zero() {
to_address = to_obj.to_raw_address() + (-obj_ref_offset);
}
let (to_address, to_obj) = match to {
MoveTarget::ToAddress(addr) => {
let obj =
unsafe { ObjectReference::from_raw_address_unchecked(addr + obj_ref_offset) };
(addr, obj)
}
MoveTarget::ToObject(obj) => {
let addr = obj.to_raw_address() + (-obj_ref_offset);
debug_assert!(obj.to_raw_address() == addr + obj_ref_offset);
(addr, obj)
}
};

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

if to_obj.is_null() {
to_obj = ObjectReference::from_raw_address(to_address + obj_ref_offset);
} else {
debug_assert!(to_obj.to_raw_address() == to_address + obj_ref_offset);
}

// Do we need to copy the hash code?
if hash_state == HASH_STATE_HASHED {
unsafe {
Expand Down
23 changes: 17 additions & 6 deletions mmtk/src/reference_glue.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use mmtk::util::opaque_pointer::*;
use mmtk::util::Address;
use mmtk::util::ObjectReference;
use mmtk::vm::ReferenceGlue;

Expand All @@ -25,15 +26,14 @@ impl ReferenceGlue<JikesRVM> for VMReferenceGlue {
}
}

fn get_referent(object: ObjectReference) -> ObjectReference {
fn get_referent(object: ObjectReference) -> Option<ObjectReference> {
if cfg!(feature = "binding_side_ref_proc") {
panic!();
} else {
debug_assert!(!object.is_null());
unsafe {
(object.to_raw_address() + REFERENCE_REFERENT_FIELD_OFFSET)
.load::<ObjectReference>()
}
let addr = unsafe {
(object.to_raw_address() + REFERENCE_REFERENT_FIELD_OFFSET).load::<Address>()
};
ObjectReference::from_raw_address(addr)
}
}

Expand All @@ -52,4 +52,15 @@ impl ReferenceGlue<JikesRVM> for VMReferenceGlue {
}
}
}

fn clear_referent(new_reference: ObjectReference) {
if cfg!(feature = "binding_side_ref_proc") {
panic!();
} else {
unsafe {
(new_reference.to_raw_address() + REFERENCE_REFERENT_FIELD_OFFSET)
.store(Address::ZERO);
}
}
}
}

0 comments on commit 1b0959f

Please sign in to comment.