Skip to content

Commit 9903af4

Browse files
committed
Implement heap walking
1 parent c4c47bf commit 9903af4

File tree

8 files changed

+78
-9
lines changed

8 files changed

+78
-9
lines changed

gc/mmtk.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags
218218
if (alloc_size > 24) alloc_obj[3] = v2;
219219
if (alloc_size > 32) alloc_obj[4] = v3;
220220

221+
mmtk_post_alloc(cache_ptr, (void*)alloc_obj, alloc_size + 8, MMTK_ALLOCATION_SEMANTICS_DEFAULT);
221222

222223
if (rb_gc_shutdown_call_finalizer_p((VALUE)alloc_obj)) {
223224
mmtk_add_obj_free_candidate(alloc_obj);
@@ -337,7 +338,38 @@ void rb_gc_impl_writebarrier(void *objspace_ptr, VALUE a, VALUE b) { }
337338
void rb_gc_impl_writebarrier_unprotect(void *objspace_ptr, VALUE obj) { }
338339
void rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj) { }
339340
// Heap walking
340-
void rb_gc_impl_each_objects(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data) { }
341+
struct each_objects_data {
342+
bool stop;
343+
int (*callback)(void *, void *, size_t, void *);
344+
void *data;
345+
};
346+
347+
static void
348+
each_objects_i(MMTk_ObjectReference obj, void *d)
349+
{
350+
struct each_objects_data *data = d;
351+
352+
if (data->stop) return;
353+
354+
size_t slot_size = rb_gc_impl_obj_slot_size((VALUE)obj);
355+
356+
if (data->callback(obj, (void *)((char *)obj + slot_size), slot_size, data->data) != 0) {
357+
data->stop = true;
358+
}
359+
}
360+
361+
void
362+
rb_gc_impl_each_objects(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data)
363+
{
364+
struct each_objects_data each_objects_data = {
365+
.stop = false,
366+
.callback = callback,
367+
.data = data,
368+
};
369+
370+
mmtk_enumerate_objects(each_objects_i, &each_objects_data);
371+
}
372+
341373
void rb_gc_impl_each_object(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data) { }
342374
// Finalizers
343375
void

gc/mmtk.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,15 @@ MMTk_Address mmtk_alloc(MMTk_Mutator *mutator,
125125
size_t offset,
126126
MMTk_AllocationSemantics semantics);
127127

128+
void mmtk_post_alloc(MMTk_Mutator *mutator,
129+
MMTk_ObjectReference refer,
130+
size_t bytes,
131+
MMTk_AllocationSemantics semantics);
132+
128133
void mmtk_add_obj_free_candidate(MMTk_ObjectReference object);
129134

135+
void mmtk_enumerate_objects(void (*callback)(MMTk_ObjectReference, void*), void *data);
136+
130137
struct MMTk_RawVecOfObjRef mmtk_get_all_obj_free_candidates(void);
131138

132139
void mmtk_free_raw_vec_of_obj_ref(struct MMTk_RawVecOfObjRef raw_vec);

gc/mmtk/Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gc/mmtk/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ probe = "0.5"
2929
features = ["is_mmtk_object", "object_pinning", "sticky_immix_non_moving_nursery"]
3030

3131
# Uncomment the following lines to use mmtk-core from the official repository.
32-
git = "https://github.com/mmtk/mmtk-core.git"
33-
rev = "a3a72f8e5795678eff06fdc1524f0b429a62ccc0"
32+
git = "https://github.com/wks/mmtk-core.git"
33+
rev = "7d3f79d4e50dacec881252562c8c7946e2513e55"
3434

3535
# Uncomment the following line to use mmtk-core from a local repository.
3636
# path = "../../mmtk-core"

gc/mmtk/src/abi.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,10 @@ pub struct RubyUpcalls {
402402
}
403403

404404
unsafe impl Sync for RubyUpcalls {}
405+
406+
#[repr(C)]
407+
#[derive(Clone)]
408+
pub struct HeapBounds {
409+
pub start: *mut libc::c_void,
410+
pub end: *mut libc::c_void,
411+
}

gc/mmtk/src/api.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,34 @@ pub extern "C" fn mmtk_alloc(
7777
)
7878
}
7979

80+
#[no_mangle]
81+
pub extern "C" fn mmtk_post_alloc(
82+
mutator: *mut RubyMutator,
83+
refer: ObjectReference,
84+
bytes: usize,
85+
semantics: AllocationSemantics,
86+
) {
87+
memory_manager::post_alloc::<Ruby>(unsafe { &mut *mutator }, refer, bytes, semantics)
88+
}
89+
8090
// TODO: Replace with buffered mmtk_add_obj_free_candidates
8191
#[no_mangle]
8292
pub extern "C" fn mmtk_add_obj_free_candidate(object: ObjectReference) {
8393
binding().weak_proc.add_obj_free_candidate(object)
8494
}
8595

96+
// =============== Heap walking ===============
97+
98+
#[no_mangle]
99+
pub extern "C" fn mmtk_enumerate_objects(
100+
callback: extern "C" fn(ObjectReference, *mut libc::c_void),
101+
data: *mut libc::c_void,
102+
) {
103+
crate::mmtk().enumerate_objects(|object| {
104+
callback(object, data);
105+
})
106+
}
107+
86108
// =============== Finalizers ===============
87109

88110
#[no_mangle]

gc/mmtk/src/scanning.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Scanning<Ruby> for VMScanning {
2828
object_tracer: &mut OT,
2929
) {
3030
debug_assert!(
31-
mmtk::memory_manager::is_mmtk_object(object.to_raw_address()),
31+
mmtk::memory_manager::is_mmtk_object(object.to_raw_address()).is_some(),
3232
"Not an MMTk object: {object}",
3333
);
3434
let gc_tls = unsafe { GCThreadTLS::from_vwt_check(tls) };
@@ -40,7 +40,7 @@ impl Scanning<Ruby> for VMScanning {
4040
if pin { " pin" } else { "" }
4141
);
4242
debug_assert!(
43-
mmtk::memory_manager::is_mmtk_object(target_object.to_raw_address()),
43+
mmtk::memory_manager::is_mmtk_object(target_object.to_raw_address()).is_some(),
4444
"Destination is not an MMTk object. Src: {object} dst: {target_object}"
4545
);
4646
let forwarded_target = object_tracer.trace_object(target_object);
@@ -173,7 +173,7 @@ impl VMScanning {
173173
}
174174
);
175175
debug_assert!(
176-
mmtk::memory_manager::is_mmtk_object(object.to_raw_address()),
176+
mmtk::memory_manager::is_mmtk_object(object.to_raw_address()).is_some(),
177177
"Root does not point to MMTk object. object: {object}"
178178
);
179179
buffer.push(object);

gc/mmtk/src/weak_proc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ trait GlobalTableProcessingWork {
230230
// of `trace_object` due to the way it is used in `UPDATE_IF_MOVED`.
231231
let forward_object = |_worker, object: ObjectReference, _pin| {
232232
debug_assert!(
233-
mmtk::memory_manager::is_mmtk_object(object.to_address::<Ruby>()),
233+
mmtk::memory_manager::is_mmtk_object(object.to_address::<Ruby>()).is_some(),
234234
"{} is not an MMTk object",
235235
object
236236
);

0 commit comments

Comments
 (0)