Skip to content

Commit cec348c

Browse files
committed
Extract gc_newobj_hook
1 parent a1143f1 commit cec348c

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

gc.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,29 @@ gc_validate_pc(VALUE obj)
10091009
#endif
10101010
}
10111011

1012+
NOINLINE(static void gc_newobj_hook(VALUE obj));
1013+
static void
1014+
gc_newobj_hook(VALUE obj)
1015+
{
1016+
int lev = RB_GC_VM_LOCK_NO_BARRIER();
1017+
{
1018+
size_t slot_size = rb_gc_obj_slot_size(obj);
1019+
memset((char *)obj + sizeof(struct RBasic), 0, slot_size - sizeof(struct RBasic));
1020+
1021+
/* We must disable GC here because the callback could call xmalloc
1022+
* which could potentially trigger a GC, and a lot of code is unsafe
1023+
* to trigger a GC right after an object has been allocated because
1024+
* they perform initialization for the object and assume that the
1025+
* GC does not trigger before then. */
1026+
bool gc_disabled = RTEST(rb_gc_disable_no_rest());
1027+
{
1028+
rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_NEWOBJ);
1029+
}
1030+
if (!gc_disabled) rb_gc_enable();
1031+
}
1032+
RB_GC_VM_UNLOCK_NO_BARRIER(lev);
1033+
}
1034+
10121035
static inline VALUE
10131036
newobj_of(rb_ractor_t *cr, VALUE klass, VALUE flags, shape_id_t shape_id, bool wb_protected, size_t size)
10141037
{
@@ -1018,23 +1041,7 @@ newobj_of(rb_ractor_t *cr, VALUE klass, VALUE flags, shape_id_t shape_id, bool w
10181041
gc_validate_pc(obj);
10191042

10201043
if (UNLIKELY(rb_gc_event_hook_required_p(RUBY_INTERNAL_EVENT_NEWOBJ))) {
1021-
int lev = RB_GC_VM_LOCK_NO_BARRIER();
1022-
{
1023-
size_t slot_size = rb_gc_obj_slot_size(obj);
1024-
memset((char *)obj + sizeof(struct RBasic), 0, slot_size - sizeof(struct RBasic));
1025-
1026-
/* We must disable GC here because the callback could call xmalloc
1027-
* which could potentially trigger a GC, and a lot of code is unsafe
1028-
* to trigger a GC right after an object has been allocated because
1029-
* they perform initialization for the object and assume that the
1030-
* GC does not trigger before then. */
1031-
bool gc_disabled = RTEST(rb_gc_disable_no_rest());
1032-
{
1033-
rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_NEWOBJ);
1034-
}
1035-
if (!gc_disabled) rb_gc_enable();
1036-
}
1037-
RB_GC_VM_UNLOCK_NO_BARRIER(lev);
1044+
gc_newobj_hook(obj);
10381045
}
10391046

10401047
#if RGENGC_CHECK_MODE

0 commit comments

Comments
 (0)