Skip to content

Commit 01cd9c9

Browse files
committed
Add rb_gc_register_pinning_obj
1 parent 5614700 commit 01cd9c9

File tree

8 files changed

+34
-0
lines changed

8 files changed

+34
-0
lines changed

gc.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ typedef struct gc_function_map {
638638
void (*declare_weak_references)(void *objspace_ptr, VALUE obj);
639639
bool (*handle_weak_references_alive_p)(void *objspace_ptr, VALUE obj);
640640
// Compaction
641+
void (*register_pinning_obj)(void *objspace_ptr, VALUE obj);
641642
bool (*object_moved_p)(void *objspace_ptr, VALUE obj);
642643
VALUE (*location)(void *objspace_ptr, VALUE value);
643644
// Write barriers
@@ -813,6 +814,7 @@ ruby_modular_gc_init(void)
813814
load_modular_gc_func(declare_weak_references);
814815
load_modular_gc_func(handle_weak_references_alive_p);
815816
// Compaction
817+
load_modular_gc_func(register_pinning_obj);
816818
load_modular_gc_func(object_moved_p);
817819
load_modular_gc_func(location);
818820
// Write barriers
@@ -894,6 +896,7 @@ ruby_modular_gc_init(void)
894896
# define rb_gc_impl_declare_weak_references rb_gc_functions.declare_weak_references
895897
# define rb_gc_impl_handle_weak_references_alive_p rb_gc_functions.handle_weak_references_alive_p
896898
// Compaction
899+
# define rb_gc_impl_register_pinning_obj rb_gc_functions.register_pinning_obj
897900
# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
898901
# define rb_gc_impl_location rb_gc_functions.location
899902
// Write barriers
@@ -1049,6 +1052,12 @@ rb_wb_protected_newobj_of(rb_execution_context_t *ec, VALUE klass, VALUE flags,
10491052
return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, shape_id, TRUE, size);
10501053
}
10511054

1055+
void
1056+
rb_gc_register_pinning_obj(VALUE obj)
1057+
{
1058+
rb_gc_impl_register_pinning_obj(rb_gc_get_objspace(), obj);
1059+
}
1060+
10521061
#define UNEXPECTED_NODE(func) \
10531062
rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
10541063
BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
@@ -1069,6 +1078,8 @@ rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FU
10691078
if (klass) rb_data_object_check(klass);
10701079
VALUE obj = newobj_of(GET_RACTOR(), klass, T_DATA, ROOT_SHAPE_ID, !dmark, sizeof(struct RTypedData));
10711080

1081+
rb_gc_register_pinning_obj(obj);
1082+
10721083
struct RData *data = (struct RData *)obj;
10731084
data->dmark = dmark;
10741085
data->dfree = dfree;
@@ -1093,6 +1104,8 @@ typed_data_alloc(VALUE klass, VALUE typed_flag, void *datap, const rb_data_type_
10931104
bool wb_protected = (type->flags & RUBY_FL_WB_PROTECTED) || !type->function.dmark;
10941105
VALUE obj = newobj_of(GET_RACTOR(), klass, T_DATA | RUBY_TYPED_FL_IS_TYPED_DATA, ROOT_SHAPE_ID, wb_protected, size);
10951106

1107+
rb_gc_register_pinning_obj(obj);
1108+
10961109
struct RTypedData *data = (struct RTypedData *)obj;
10971110
data->fields_obj = 0;
10981111
*(VALUE *)&data->type = ((VALUE)type) | typed_flag;

gc/default/default.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7059,6 +7059,12 @@ gc_sort_heap_by_compare_func(rb_objspace_t *objspace, gc_compact_compare_func co
70597059
}
70607060
#endif
70617061

7062+
void
7063+
rb_gc_impl_register_pinning_obj(void *objspace_ptr, VALUE obj)
7064+
{
7065+
/* no-op */
7066+
}
7067+
70627068
bool
70637069
rb_gc_impl_object_moved_p(void *objspace_ptr, VALUE obj)
70647070
{

gc/gc_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ GC_IMPL_FN void rb_gc_impl_mark_maybe(void *objspace_ptr, VALUE obj);
8686
GC_IMPL_FN void rb_gc_impl_declare_weak_references(void *objspace_ptr, VALUE obj);
8787
GC_IMPL_FN bool rb_gc_impl_handle_weak_references_alive_p(void *objspace_ptr, VALUE obj);
8888
// Compaction
89+
GC_IMPL_FN void rb_gc_impl_register_pinning_obj(void *objspace_ptr, VALUE obj);
8990
GC_IMPL_FN bool rb_gc_impl_object_moved_p(void *objspace_ptr, VALUE obj);
9091
GC_IMPL_FN VALUE rb_gc_impl_location(void *objspace_ptr, VALUE value);
9192
// Write barriers

hash.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4673,6 +4673,8 @@ rb_hash_compare_by_id(VALUE hash)
46734673
RHASH_ST_CLEAR(tmp);
46744674
}
46754675

4676+
rb_gc_register_pinning_obj(hash);
4677+
46764678
return hash;
46774679
}
46784680

@@ -4702,6 +4704,7 @@ rb_ident_hash_new(void)
47024704
{
47034705
VALUE hash = rb_hash_new();
47044706
hash_st_table_init(hash, &identhash, 0);
4707+
rb_gc_register_pinning_obj(hash);
47054708
return hash;
47064709
}
47074710

@@ -4710,6 +4713,7 @@ rb_ident_hash_new_with_size(st_index_t size)
47104713
{
47114714
VALUE hash = rb_hash_new();
47124715
hash_st_table_init(hash, &identhash, size);
4716+
rb_gc_register_pinning_obj(hash);
47134717
return hash;
47144718
}
47154719

imemo.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ rb_imemo_tmpbuf_new(void)
5454
VALUE flags = T_IMEMO | (imemo_tmpbuf << FL_USHIFT);
5555
NEWOBJ_OF(obj, rb_imemo_tmpbuf_t, 0, flags, sizeof(rb_imemo_tmpbuf_t), NULL);
5656

57+
rb_gc_register_pinning_obj((VALUE)obj);
58+
5759
obj->ptr = NULL;
5860
obj->cnt = 0;
5961

@@ -101,6 +103,9 @@ struct MEMO *
101103
rb_imemo_memo_new(VALUE a, VALUE b, VALUE c)
102104
{
103105
struct MEMO *memo = IMEMO_NEW(struct MEMO, imemo_memo, 0);
106+
107+
rb_gc_register_pinning_obj((VALUE)memo);
108+
104109
*((VALUE *)&memo->v1) = a;
105110
*((VALUE *)&memo->v2) = b;
106111
*((VALUE *)&memo->u3.value) = c;

internal/gc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ static inline void *ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size
204204
static inline void *ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2, 3));
205205
static inline void ruby_sized_xfree_inlined(void *ptr, size_t size);
206206
void rb_gc_obj_id_moved(VALUE obj);
207+
void rb_gc_register_pinning_obj(VALUE obj);
207208

208209
void *rb_gc_ractor_cache_alloc(rb_ractor_t *ractor);
209210
void rb_gc_ractor_cache_free(void *cache);

proc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,9 @@ rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int m
898898
rb_execution_context_t *ec = GET_EC();
899899

900900
struct vm_ifunc *ifunc = IMEMO_NEW(struct vm_ifunc, imemo_ifunc, (VALUE)rb_vm_svar_lep(ec, ec->cfp));
901+
902+
rb_gc_register_pinning_obj((VALUE)ifunc);
903+
901904
ifunc->func = func;
902905
ifunc->data = data;
903906
ifunc->argc.min = min_argc;

string.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ str_enc_fastpath(VALUE str)
205205
RUBY_ASSERT(RSTRING_PTR(str) <= RSTRING_PTR(shared_str) + RSTRING_LEN(shared_str)); \
206206
RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
207207
FL_SET((str), STR_SHARED); \
208+
rb_gc_register_pinning_obj(str); \
208209
FL_SET((shared_str), STR_SHARED_ROOT); \
209210
if (RBASIC_CLASS((shared_str)) == 0) /* for CoW-friendliness */ \
210211
FL_SET_RAW((shared_str), STR_BORROWED); \

0 commit comments

Comments
 (0)