Skip to content

Commit

Permalink
Initial implementation of Mark & Sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Aug 26, 2024
1 parent becfabf commit cbe1a1a
Show file tree
Hide file tree
Showing 16 changed files with 806 additions and 274 deletions.
64 changes: 56 additions & 8 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ rb_gc_get_objspace(void)
return GET_VM()->objspace;
}

void *
rb_gc_get_ractor_newobj_cache(void)
{
return GET_RACTOR()->newobj_cache;
}

void
rb_gc_ractor_newobj_cache_foreach(void (*func)(void *cache, void *data), void *data)
{
Expand Down Expand Up @@ -324,6 +330,41 @@ rb_gc_rebuild_shape(VALUE obj, size_t size_pool_id)
return (uint32_t)rb_shape_id(new_shape);
}

struct st_table *generic_ivtbl_get(void);

struct st_table *
rb_gc_get_generic_ivar_table(void)
{
return generic_ivtbl_get();
}

struct st_table *
rb_gc_get_frozen_strings_table(void)
{
return rb_vm_fstring_table();
}

extern rb_symbols_t ruby_global_symbols;
#define global_symbols ruby_global_symbols

struct st_table *
rb_gc_get_global_symbols_table(void)
{
return global_symbols.str_sym;
}

struct st_table *
rb_gc_get_overloaded_cme_table(void)
{
return GET_VM()->overloaded_cme_table;
}

struct st_table *
rb_gc_get_ci_table(void)
{
return GET_VM()->ci_table;
}

void rb_vm_update_references(void *ptr);

#define rb_setjmp(env) RUBY_SETJMP(env)
Expand Down Expand Up @@ -577,7 +618,7 @@ typedef struct gc_function_map {
void *(*objspace_alloc)(void);
void (*objspace_init)(void *objspace_ptr);
void (*objspace_free)(void *objspace_ptr);
void *(*ractor_cache_alloc)(void *objspace_ptr);
void *(*ractor_cache_alloc)(void *objspace_ptr, void *ractor);
void (*ractor_cache_free)(void *objspace_ptr, void *cache);
void (*set_params)(void *objspace_ptr);
void (*init)(void);
Expand Down Expand Up @@ -2421,8 +2462,7 @@ mark_const_table_i(VALUE value, void *objspace)
void
rb_gc_mark_roots(void *objspace, const char **categoryp)
{
rb_execution_context_t *ec = GET_EC();
rb_vm_t *vm = rb_ec_vm_ptr(ec);
rb_vm_t *vm = GET_VM();

#define MARK_CHECKPOINT(category) do { \
if (categoryp) *categoryp = category; \
Expand All @@ -2432,9 +2472,6 @@ rb_gc_mark_roots(void *objspace, const char **categoryp)
rb_vm_mark(vm);
if (vm->self) rb_gc_impl_mark(objspace, vm->self);

MARK_CHECKPOINT("machine_context");
mark_current_machine_context(objspace, ec);

MARK_CHECKPOINT("end_proc");
rb_mark_end_proc();

Expand All @@ -2451,6 +2488,17 @@ rb_gc_mark_roots(void *objspace, const char **categoryp)
#endif

MARK_CHECKPOINT("finish");
}

void
rb_gc_mark_thread_roots(void *objspace, void *ractor, const char **categoryp)
{
if (ractor == NULL) ractor = GET_RACTOR();

rb_execution_context_t *ec = ((rb_ractor_t *)ractor)->threads.running_ec;

MARK_CHECKPOINT("machine_context");
mark_current_machine_context(objspace, ec);
#undef MARK_CHECKPOINT
}

Expand Down Expand Up @@ -2736,9 +2784,9 @@ rb_obj_gc_flags(VALUE obj, ID* flags, size_t max)
/* GC */

void *
rb_gc_ractor_cache_alloc(void)
rb_gc_ractor_cache_alloc(rb_ractor_t *ractor)
{
return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace());
return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
}

void
Expand Down
4 changes: 3 additions & 1 deletion gc/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -4839,6 +4839,8 @@ mark_roots(rb_objspace_t *objspace, const char **categoryp)
if (stress_to_class) rb_gc_mark(stress_to_class);

rb_gc_mark_roots(objspace, categoryp);

rb_gc_mark_thread_roots(objspace, NULL, categoryp);
}

static inline void
Expand Down Expand Up @@ -6450,7 +6452,7 @@ rb_gc_impl_obj_flags(void *objspace_ptr, VALUE obj, ID* flags, size_t max)
}

void *
rb_gc_impl_ractor_cache_alloc(void *objspace_ptr)
rb_gc_impl_ractor_cache_alloc(void *objspace_ptr, void *ractor)
{
rb_objspace_t *objspace = objspace_ptr;

Expand Down
7 changes: 7 additions & 0 deletions gc/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ void rb_gc_update_vm_references(void *objspace);
void rb_gc_reachable_objects_from_callback(VALUE obj);
void rb_gc_event_hook(VALUE obj, rb_event_flag_t event);
void *rb_gc_get_objspace(void);
void *rb_gc_get_ractor_newobj_cache(void);
size_t rb_size_mul_or_raise(size_t x, size_t y, VALUE exc);
void rb_gc_run_obj_finalizer(VALUE objid, long count, VALUE (*callback)(long i, void *data), void *data);
void rb_gc_set_pending_interrupt(void);
void rb_gc_unset_pending_interrupt(void);
bool rb_gc_obj_free(void *objspace, VALUE obj);
void rb_gc_mark_roots(void *objspace, const char **categoryp);
void rb_gc_mark_thread_roots(void *objspace, void *ractor, const char **categoryp);
void rb_gc_ractor_newobj_cache_foreach(void (*func)(void *cache, void *data), void *data);
bool rb_gc_multi_ractor_p(void);
void rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *passing_data);
Expand All @@ -43,6 +45,11 @@ uint32_t rb_gc_get_shape(VALUE obj);
void rb_gc_set_shape(VALUE obj, uint32_t shape_id);
uint32_t rb_gc_rebuild_shape(VALUE obj, size_t size_pool_id);
size_t rb_obj_memsize_of(VALUE obj);
struct st_table *rb_gc_get_generic_ivar_table(void);
struct st_table *rb_gc_get_frozen_strings_table(void);
struct st_table *rb_gc_get_global_symbols_table(void);
struct st_table *rb_gc_get_overloaded_cme_table(void);
struct st_table *rb_gc_get_ci_table(void);
RUBY_SYMBOL_EXPORT_END

void rb_ractor_finish_marking(void);
Expand Down
2 changes: 1 addition & 1 deletion gc/gc_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
GC_IMPL_FN void *rb_gc_impl_objspace_alloc(void);
GC_IMPL_FN void rb_gc_impl_objspace_init(void *objspace_ptr);
GC_IMPL_FN void rb_gc_impl_objspace_free(void *objspace_ptr);
GC_IMPL_FN void *rb_gc_impl_ractor_cache_alloc(void *objspace_ptr);
GC_IMPL_FN void *rb_gc_impl_ractor_cache_alloc(void *objspace_ptr, void *ractor);
GC_IMPL_FN void rb_gc_impl_ractor_cache_free(void *objspace_ptr, void *cache);
GC_IMPL_FN void rb_gc_impl_set_params(void *objspace_ptr);
GC_IMPL_FN void rb_gc_impl_init(void);
Expand Down
Loading

0 comments on commit cbe1a1a

Please sign in to comment.