Skip to content

Commit

Permalink
Fix object allocation during heap walking
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Oct 7, 2024
1 parent b61b205 commit b09d5fb
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions gc/mmtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "gc/gc_impl.h"
#include "gc/mmtk.h"

#include "darray.h"

struct objspace {
bool measure_gc_time;
bool gc_stress;
Expand Down Expand Up @@ -54,6 +56,7 @@ struct MMTk_final_job {
} kind;
union {
struct {
void (*func)(void *);
void *data;
} dfree;
struct {
Expand Down Expand Up @@ -746,36 +749,33 @@ rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj)
}

// Heap walking
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;
rb_darray(VALUE) *objs = 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;
}
rb_darray_append(objs, (VALUE)obj);
}

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,
};
rb_darray(VALUE) objs;
rb_darray_make(&objs, 0);

mmtk_enumerate_objects(each_objects_i, &objs);

VALUE *obj_ptr;
rb_darray_foreach(objs, i, obj_ptr) {
VALUE obj = *obj_ptr;
size_t slot_size = rb_gc_impl_obj_slot_size((VALUE)obj);

if (callback((void *)obj, (void *)((char *)obj + slot_size), slot_size, data) != 0) {
break;
}
}

mmtk_enumerate_objects(each_objects_i, &each_objects_data);
rb_darray_free(objs);
}

void rb_gc_impl_each_object(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data) { }
Expand Down Expand Up @@ -806,7 +806,6 @@ gc_run_finalizers(void *data)

switch (job->kind) {
case MMTK_FINAL_JOB_DFREE:
// TODO
job->as.dfree.func(job->as.dfree.data);
break;
case MMTK_FINAL_JOB_FINALIZE:
Expand Down

0 comments on commit b09d5fb

Please sign in to comment.