diff --git a/gc/mmtk.c b/gc/mmtk.c index e5f5a673080a77..b8ad518cd06b4d 100644 --- a/gc/mmtk.c +++ b/gc/mmtk.c @@ -218,6 +218,7 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags if (alloc_size > 24) alloc_obj[3] = v2; if (alloc_size > 32) alloc_obj[4] = v3; + mmtk_post_alloc(cache_ptr, (void*)alloc_obj, alloc_size + 8, MMTK_ALLOCATION_SEMANTICS_DEFAULT); if (rb_gc_shutdown_call_finalizer_p((VALUE)alloc_obj)) { mmtk_add_obj_free_candidate(alloc_obj); @@ -337,7 +338,38 @@ void rb_gc_impl_writebarrier(void *objspace_ptr, VALUE a, VALUE b) { } void rb_gc_impl_writebarrier_unprotect(void *objspace_ptr, VALUE obj) { } void rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj) { } // Heap walking -void rb_gc_impl_each_objects(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data) { } +struct each_objects_data { + bool stop; + int (*callback)(void *, void *, size_t, void *); + void *data; +}; + +static void +each_objects_i(MMTk_ObjectReference obj, void *d) +{ + struct each_objects_data *data = d; + + if (data->stop) return; + + size_t slot_size = rb_gc_impl_obj_slot_size((VALUE)obj); + + if (data->callback(obj, (void *)((char *)obj + slot_size), slot_size, data->data) != 0) { + data->stop = true; + } +} + +void +rb_gc_impl_each_objects(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data) +{ + struct each_objects_data each_objects_data = { + .stop = false, + .callback = callback, + .data = data, + }; + + mmtk_enumerate_objects(each_objects_i, &each_objects_data); +} + void rb_gc_impl_each_object(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data) { } // Finalizers void diff --git a/gc/mmtk.h b/gc/mmtk.h index 347972fb243b4f..b317bda4631859 100644 --- a/gc/mmtk.h +++ b/gc/mmtk.h @@ -125,8 +125,15 @@ MMTk_Address mmtk_alloc(MMTk_Mutator *mutator, size_t offset, MMTk_AllocationSemantics semantics); +void mmtk_post_alloc(MMTk_Mutator *mutator, + MMTk_ObjectReference refer, + size_t bytes, + MMTk_AllocationSemantics semantics); + void mmtk_add_obj_free_candidate(MMTk_ObjectReference object); +void mmtk_enumerate_objects(void (*callback)(MMTk_ObjectReference, void*), void *data); + struct MMTk_RawVecOfObjRef mmtk_get_all_obj_free_candidates(void); void mmtk_free_raw_vec_of_obj_ref(struct MMTk_RawVecOfObjRef raw_vec); diff --git a/gc/mmtk/Cargo.lock b/gc/mmtk/Cargo.lock index 61e47a601e5689..ea0e528efbd6ce 100644 --- a/gc/mmtk/Cargo.lock +++ b/gc/mmtk/Cargo.lock @@ -432,7 +432,7 @@ dependencies = [ [[package]] name = "mmtk" version = "0.26.0" -source = "git+https://github.com/mmtk/mmtk-core.git?rev=a3a72f8e5795678eff06fdc1524f0b429a62ccc0#a3a72f8e5795678eff06fdc1524f0b429a62ccc0" +source = "git+https://github.com/wks/mmtk-core.git?rev=7d3f79d4e50dacec881252562c8c7946e2513e55#7d3f79d4e50dacec881252562c8c7946e2513e55" dependencies = [ "atomic", "atomic-traits", @@ -457,6 +457,7 @@ dependencies = [ "portable-atomic", "probe", "regex", + "rustversion", "spin", "static_assertions", "strum", @@ -467,7 +468,7 @@ dependencies = [ [[package]] name = "mmtk-macros" version = "0.26.0" -source = "git+https://github.com/mmtk/mmtk-core.git?rev=a3a72f8e5795678eff06fdc1524f0b429a62ccc0#a3a72f8e5795678eff06fdc1524f0b429a62ccc0" +source = "git+https://github.com/wks/mmtk-core.git?rev=7d3f79d4e50dacec881252562c8c7946e2513e55#7d3f79d4e50dacec881252562c8c7946e2513e55" dependencies = [ "proc-macro-error", "proc-macro2", diff --git a/gc/mmtk/Cargo.toml b/gc/mmtk/Cargo.toml index bf2cac26e030bc..c5c7b9987a3dbb 100644 --- a/gc/mmtk/Cargo.toml +++ b/gc/mmtk/Cargo.toml @@ -29,8 +29,8 @@ probe = "0.5" features = ["is_mmtk_object", "object_pinning", "sticky_immix_non_moving_nursery"] # Uncomment the following lines to use mmtk-core from the official repository. -git = "https://github.com/mmtk/mmtk-core.git" -rev = "a3a72f8e5795678eff06fdc1524f0b429a62ccc0" +git = "https://github.com/wks/mmtk-core.git" +rev = "7d3f79d4e50dacec881252562c8c7946e2513e55" # Uncomment the following line to use mmtk-core from a local repository. # path = "../../mmtk-core" diff --git a/gc/mmtk/src/abi.rs b/gc/mmtk/src/abi.rs index 33ccbb77d6e94c..b27c01454508c4 100644 --- a/gc/mmtk/src/abi.rs +++ b/gc/mmtk/src/abi.rs @@ -402,3 +402,10 @@ pub struct RubyUpcalls { } unsafe impl Sync for RubyUpcalls {} + +#[repr(C)] +#[derive(Clone)] +pub struct HeapBounds { + pub start: *mut libc::c_void, + pub end: *mut libc::c_void, +} diff --git a/gc/mmtk/src/api.rs b/gc/mmtk/src/api.rs index 88ba17fe517aac..6a8c116498453f 100644 --- a/gc/mmtk/src/api.rs +++ b/gc/mmtk/src/api.rs @@ -77,12 +77,34 @@ pub extern "C" fn mmtk_alloc( ) } +#[no_mangle] +pub extern "C" fn mmtk_post_alloc( + mutator: *mut RubyMutator, + refer: ObjectReference, + bytes: usize, + semantics: AllocationSemantics, +) { + memory_manager::post_alloc::(unsafe { &mut *mutator }, refer, bytes, semantics) +} + // TODO: Replace with buffered mmtk_add_obj_free_candidates #[no_mangle] pub extern "C" fn mmtk_add_obj_free_candidate(object: ObjectReference) { binding().weak_proc.add_obj_free_candidate(object) } +// =============== Heap walking =============== + +#[no_mangle] +pub extern "C" fn mmtk_enumerate_objects( + callback: extern "C" fn(ObjectReference, *mut libc::c_void), + data: *mut libc::c_void, +) { + crate::mmtk().enumerate_objects(|object| { + callback(object, data); + }) +} + // =============== Finalizers =============== #[no_mangle] diff --git a/gc/mmtk/src/scanning.rs b/gc/mmtk/src/scanning.rs index 991bb7a0b4664f..3fb8ca1fb90bde 100644 --- a/gc/mmtk/src/scanning.rs +++ b/gc/mmtk/src/scanning.rs @@ -28,7 +28,7 @@ impl Scanning for VMScanning { object_tracer: &mut OT, ) { debug_assert!( - mmtk::memory_manager::is_mmtk_object(object.to_raw_address()), + mmtk::memory_manager::is_mmtk_object(object.to_raw_address()).is_some(), "Not an MMTk object: {object}", ); let gc_tls = unsafe { GCThreadTLS::from_vwt_check(tls) }; @@ -40,7 +40,7 @@ impl Scanning for VMScanning { if pin { " pin" } else { "" } ); debug_assert!( - mmtk::memory_manager::is_mmtk_object(target_object.to_raw_address()), + mmtk::memory_manager::is_mmtk_object(target_object.to_raw_address()).is_some(), "Destination is not an MMTk object. Src: {object} dst: {target_object}" ); let forwarded_target = object_tracer.trace_object(target_object); @@ -173,7 +173,7 @@ impl VMScanning { } ); debug_assert!( - mmtk::memory_manager::is_mmtk_object(object.to_raw_address()), + mmtk::memory_manager::is_mmtk_object(object.to_raw_address()).is_some(), "Root does not point to MMTk object. object: {object}" ); buffer.push(object); diff --git a/gc/mmtk/src/weak_proc.rs b/gc/mmtk/src/weak_proc.rs index fb04f1d414128c..f231a7ddf51fc2 100644 --- a/gc/mmtk/src/weak_proc.rs +++ b/gc/mmtk/src/weak_proc.rs @@ -230,7 +230,7 @@ trait GlobalTableProcessingWork { // of `trace_object` due to the way it is used in `UPDATE_IF_MOVED`. let forward_object = |_worker, object: ObjectReference, _pin| { debug_assert!( - mmtk::memory_manager::is_mmtk_object(object.to_address::()), + mmtk::memory_manager::is_mmtk_object(object.to_address::()).is_some(), "{} is not an MMTk object", object );