Move rb_class_allocate_instance from gc.c to object.c

This commit is contained in:
Peter Zhu 2024-02-13 10:21:22 -05:00
Родитель fc2c128e7e
Коммит 1d3b306753
6 изменённых файлов: 38 добавлений и 42 удалений

40
gc.c
Просмотреть файл

@ -2970,43 +2970,11 @@ rb_newobj(void)
return newobj_of(GET_RACTOR(), 0, T_NONE, 0, 0, 0, FALSE, RVALUE_SIZE); return newobj_of(GET_RACTOR(), 0, T_NONE, 0, 0, 0, FALSE, RVALUE_SIZE);
} }
static VALUE
rb_class_instance_allocate_internal(VALUE klass, VALUE flags, bool wb_protected)
{
GC_ASSERT((flags & RUBY_T_MASK) == T_OBJECT);
GC_ASSERT(flags & ROBJECT_EMBED);
size_t size;
uint32_t index_tbl_num_entries = RCLASS_EXT(klass)->max_iv_count;
size = rb_obj_embedded_size(index_tbl_num_entries);
if (!rb_gc_size_allocatable_p(size)) {
size = sizeof(struct RObject);
}
VALUE obj = newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, wb_protected, size);
RUBY_ASSERT(rb_shape_get_shape(obj)->type == SHAPE_ROOT);
// Set the shape to the specific T_OBJECT shape which is always
// SIZE_POOL_COUNT away from the root shape.
ROBJECT_SET_SHAPE_ID(obj, ROBJECT_SHAPE_ID(obj) + SIZE_POOL_COUNT);
#if RUBY_DEBUG
RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
VALUE *ptr = ROBJECT_IVPTR(obj);
for (size_t i = 0; i < ROBJECT_IV_CAPACITY(obj); i++) {
ptr[i] = Qundef;
}
#endif
return obj;
}
VALUE VALUE
rb_newobj_of(VALUE klass, VALUE flags) rb_newobj_of(VALUE klass, VALUE flags)
{ {
if ((flags & RUBY_T_MASK) == T_OBJECT) { if ((flags & RUBY_T_MASK) == T_OBJECT) {
return rb_class_instance_allocate_internal(klass, (flags | ROBJECT_EMBED) & ~FL_WB_PROTECTED, flags & FL_WB_PROTECTED); return rb_class_allocate_instance(klass);
} }
else { else {
return newobj_of(GET_RACTOR(), klass, flags & ~FL_WB_PROTECTED, 0, 0, 0, flags & FL_WB_PROTECTED, RVALUE_SIZE); return newobj_of(GET_RACTOR(), klass, flags & ~FL_WB_PROTECTED, 0, 0, 0, flags & FL_WB_PROTECTED, RVALUE_SIZE);
@ -3116,12 +3084,6 @@ rb_imemo_new_debug(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0,
} }
#endif #endif
VALUE
rb_class_allocate_instance(VALUE klass)
{
return rb_class_instance_allocate_internal(klass, T_OBJECT | ROBJECT_EMBED, RGENGC_WB_PROTECTED_OBJECT);
}
static inline void static inline void
rb_data_object_check(VALUE klass) rb_data_object_check(VALUE klass)
{ {

Просмотреть файл

@ -237,7 +237,6 @@ RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
static inline void *ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2)); static inline void *ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
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)); 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));
static inline void ruby_sized_xfree_inlined(void *ptr, size_t size); static inline void ruby_sized_xfree_inlined(void *ptr, size_t size);
VALUE rb_class_allocate_instance(VALUE klass);
void rb_gc_ractor_newobj_cache_clear(rb_ractor_newobj_cache_t *newobj_cache); void rb_gc_ractor_newobj_cache_clear(rb_ractor_newobj_cache_t *newobj_cache);
size_t rb_gc_obj_slot_size(VALUE obj); size_t rb_gc_obj_slot_size(VALUE obj);
bool rb_gc_size_allocatable_p(size_t size); bool rb_gc_size_allocatable_p(size_t size);

Просмотреть файл

@ -12,6 +12,7 @@
/* object.c */ /* object.c */
size_t rb_obj_embedded_size(uint32_t numiv); size_t rb_obj_embedded_size(uint32_t numiv);
VALUE rb_class_allocate_instance(VALUE klass);
VALUE rb_class_search_ancestor(VALUE klass, VALUE super); VALUE rb_class_search_ancestor(VALUE klass, VALUE super);
NORETURN(void rb_undefined_alloc(VALUE klass)); NORETURN(void rb_undefined_alloc(VALUE klass));
double rb_num_to_dbl(VALUE val); double rb_num_to_dbl(VALUE val);

Просмотреть файл

@ -117,6 +117,37 @@ rb_obj_reveal(VALUE obj, VALUE klass)
return obj; return obj;
} }
VALUE
rb_class_allocate_instance(VALUE klass)
{
uint32_t index_tbl_num_entries = RCLASS_EXT(klass)->max_iv_count;
size_t size = rb_obj_embedded_size(index_tbl_num_entries);
if (!rb_gc_size_allocatable_p(size)) {
size = sizeof(struct RObject);
}
NEWOBJ_OF(o, struct RObject, klass,
T_OBJECT | ROBJECT_EMBED | (RGENGC_WB_PROTECTED_OBJECT ? FL_WB_PROTECTED : 0), size, 0);
VALUE obj = (VALUE)o;
RUBY_ASSERT(rb_shape_get_shape(obj)->type == SHAPE_ROOT);
// Set the shape to the specific T_OBJECT shape which is always
// SIZE_POOL_COUNT away from the root shape.
ROBJECT_SET_SHAPE_ID(obj, ROBJECT_SHAPE_ID(obj) + SIZE_POOL_COUNT);
#if RUBY_DEBUG
RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
VALUE *ptr = ROBJECT_IVPTR(obj);
for (size_t i = 0; i < ROBJECT_IV_CAPACITY(obj); i++) {
ptr[i] = Qundef;
}
#endif
return obj;
}
VALUE VALUE
rb_obj_setup(VALUE obj, VALUE klass, VALUE type) rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
{ {

Просмотреть файл

@ -38,6 +38,7 @@ fn main() {
.clang_args(filtered_clang_args) .clang_args(filtered_clang_args)
.header("encindex.h") .header("encindex.h")
.header("internal.h") .header("internal.h")
.header("internal/object.h")
.header("internal/re.h") .header("internal/re.h")
.header("include/ruby/ruby.h") .header("include/ruby/ruby.h")
.header("shape.h") .header("shape.h")
@ -375,8 +376,10 @@ fn main() {
// From include/ruby/internal/intern/vm.h // From include/ruby/internal/intern/vm.h
.allowlist_function("rb_get_alloc_func") .allowlist_function("rb_get_alloc_func")
// From gc.h and internal/gc.h // From internal/object.h
.allowlist_function("rb_class_allocate_instance") .allowlist_function("rb_class_allocate_instance")
// From gc.h and internal/gc.h
.allowlist_function("rb_obj_info") .allowlist_function("rb_obj_info")
.allowlist_function("ruby_xfree") .allowlist_function("ruby_xfree")

Просмотреть файл

@ -1007,6 +1007,7 @@ extern "C" {
pub fn rb_ivar_defined(obj: VALUE, name: ID) -> VALUE; pub fn rb_ivar_defined(obj: VALUE, name: ID) -> VALUE;
pub fn rb_attr_get(obj: VALUE, name: ID) -> VALUE; pub fn rb_attr_get(obj: VALUE, name: ID) -> VALUE;
pub fn rb_obj_info_dump(obj: VALUE); pub fn rb_obj_info_dump(obj: VALUE);
pub fn rb_class_allocate_instance(klass: VALUE) -> VALUE;
pub fn rb_reg_new_ary(ary: VALUE, options: ::std::os::raw::c_int) -> VALUE; pub fn rb_reg_new_ary(ary: VALUE, options: ::std::os::raw::c_int) -> VALUE;
pub fn rb_ary_tmp_new_from_values( pub fn rb_ary_tmp_new_from_values(
arg1: VALUE, arg1: VALUE,
@ -1036,7 +1037,6 @@ extern "C" {
cfp: *const rb_control_frame_t, cfp: *const rb_control_frame_t,
) -> *const rb_callable_method_entry_t; ) -> *const rb_callable_method_entry_t;
pub fn rb_obj_info(obj: VALUE) -> *const ::std::os::raw::c_char; pub fn rb_obj_info(obj: VALUE) -> *const ::std::os::raw::c_char;
pub fn rb_class_allocate_instance(klass: VALUE) -> VALUE;
pub fn rb_ec_stack_check(ec: *mut rb_execution_context_struct) -> ::std::os::raw::c_int; pub fn rb_ec_stack_check(ec: *mut rb_execution_context_struct) -> ::std::os::raw::c_int;
pub fn rb_shape_id_offset() -> i32; pub fn rb_shape_id_offset() -> i32;
pub fn rb_shape_get_shape_by_id(shape_id: shape_id_t) -> *mut rb_shape_t; pub fn rb_shape_get_shape_by_id(shape_id: shape_id_t) -> *mut rb_shape_t;