Skip to content

Commit

Permalink
Remove NULL ObjectReference (#265)
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 589c9ee commit 3d98c8b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 67 deletions.
90 changes: 49 additions & 41 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 @@ -32,7 +32,7 @@ memoffset = "0.9.0"
# - change branch
# - 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
# mmtk = { path = "../repos/mmtk-core" }

Expand Down
10 changes: 3 additions & 7 deletions mmtk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use mmtk::memory_manager;
use mmtk::plan::BarrierSelector;
use mmtk::scheduler::GCWorker;
use mmtk::util::alloc::AllocatorSelector;
use mmtk::util::api_util::NullableObjectReference;
use mmtk::util::opaque_pointer::*;
use mmtk::util::{Address, ObjectReference};
use mmtk::AllocationSemantics;
Expand Down Expand Up @@ -492,13 +493,8 @@ pub extern "C" fn add_finalizer(object: ObjectReference) {
}

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

thread_local! {
Expand Down
48 changes: 34 additions & 14 deletions mmtk/src/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,37 +127,57 @@ impl<const COMPRESSED: bool> OpenJDKEdge<COMPRESSED> {

/// encode an object pointer to its u32 compressed form
fn compress(o: ObjectReference) -> u32 {
if o.is_null() {
0u32
} else {
((o.to_raw_address() - BASE.load(Ordering::Relaxed)) >> SHIFT.load(Ordering::Relaxed))
as u32
}
((o.to_raw_address() - BASE.load(Ordering::Relaxed)) >> SHIFT.load(Ordering::Relaxed))
as u32
}

/// decode an object pointer from its u32 compressed form
fn decompress(v: u32) -> ObjectReference {
fn decompress(v: u32) -> Option<ObjectReference> {
if v == 0 {
ObjectReference::NULL
None
} else {
// Note on `unsafe`: `v` must be positive here, so the result must be positive.
let objref = unsafe {
ObjectReference::from_raw_address_unchecked(
BASE.load(Ordering::Relaxed) + ((v as usize) << SHIFT.load(Ordering::Relaxed)),
)
};
Some(objref)
}
}

/// Store a null reference in the slot.
pub fn store_null(&self) {
if cfg!(any(target_arch = "x86", target_arch = "x86_64")) {
if COMPRESSED {
if self.is_compressed() {
self.x86_write_unaligned::<u32, true>(0)
} else {
self.x86_write_unaligned::<Address, true>(Address::ZERO)
}
} else {
self.x86_write_unaligned::<Address, false>(Address::ZERO)
}
} else {
ObjectReference::from_raw_address(
BASE.load(Ordering::Relaxed) + ((v as usize) << SHIFT.load(Ordering::Relaxed)),
)
debug_assert!(!COMPRESSED);
unsafe { self.addr.store(0) }
}
}
}

impl<const COMPRESSED: bool> Edge for OpenJDKEdge<COMPRESSED> {
fn load(&self) -> ObjectReference {
fn load(&self) -> Option<ObjectReference> {
if cfg!(any(target_arch = "x86", target_arch = "x86_64")) {
if COMPRESSED {
if self.is_compressed() {
Self::decompress(self.x86_read_unaligned::<u32, true>())
} else {
self.x86_read_unaligned::<ObjectReference, true>()
let addr = self.x86_read_unaligned::<Address, true>();
ObjectReference::from_raw_address(addr)
}
} else {
self.x86_read_unaligned::<ObjectReference, false>()
let addr = self.x86_read_unaligned::<Address, false>();
ObjectReference::from_raw_address(addr)
}
} else {
debug_assert!(!COMPRESSED);
Expand Down
10 changes: 7 additions & 3 deletions mmtk/src/object_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ impl<const COMPRESSED: bool> ObjectModel<OpenJDK<COMPRESSED>> for VMObjectModel<
) -> ObjectReference {
let bytes = unsafe { Oop::from(from).size::<COMPRESSED>() };
let dst = copy_context.alloc_copy(from, bytes, ::std::mem::size_of::<usize>(), 0, copy);
debug_assert!(!dst.is_zero());
// Copy
let src = from.to_raw_address();
unsafe { std::ptr::copy_nonoverlapping::<u8>(src.to_ptr(), dst.to_mut_ptr(), bytes) }
let to_obj = ObjectReference::from_raw_address(dst);
// Note on onsafe: `alloc_copy` never returns 0.
let to_obj = unsafe { ObjectReference::from_raw_address_unchecked(dst) };
copy_context.post_copy(to_obj, bytes, copy);
to_obj
}
Expand All @@ -56,7 +58,8 @@ impl<const COMPRESSED: bool> ObjectModel<OpenJDK<COMPRESSED>> for VMObjectModel<
}

fn get_reference_when_copied_to(_from: ObjectReference, to: Address) -> ObjectReference {
ObjectReference::from_raw_address(to)
debug_assert!(!to.is_zero());
unsafe { ObjectReference::from_raw_address_unchecked(to) }
}

fn get_current_size(object: ObjectReference) -> usize {
Expand Down Expand Up @@ -93,7 +96,8 @@ impl<const COMPRESSED: bool> ObjectModel<OpenJDK<COMPRESSED>> for VMObjectModel<
}

fn address_to_ref(address: Address) -> ObjectReference {
ObjectReference::from_raw_address(address)
debug_assert!(!address.is_zero());
unsafe { ObjectReference::from_raw_address_unchecked(address) }
}

fn dump_object(object: ObjectReference) {
Expand Down
Loading

0 comments on commit 3d98c8b

Please sign in to comment.