Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature #20265] Deprecate and remove rb_newobj and rb_newobj_of #9964

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ See GitHub releases like [GitHub Releases of Logger](https://github.com/ruby/log

## C API updates

* `rb_newobj` and `rb_newobj_of` (and corresponding macros `RB_NEWOBJ`, `RB_NEWOBJ_OF`, `NEWOBJ`, `NEWOBJ_OF`) have been removed [[Feature #20265]].

## Implementation improvements

* `Array#each` is rewritten in Ruby for better performance [[Feature #20182]].
Expand All @@ -95,3 +97,4 @@ See GitHub releases like [GitHub Releases of Logger](https://github.com/ruby/log
[Feature #19117]: https://bugs.ruby-lang.org/issues/19117
[Bug #20064]: https://bugs.ruby-lang.org/issues/20064
[Feature #20182]: https://bugs.ruby-lang.org/issues/20182
[Feature #20265]: https://bugs.ruby-lang.org/issues/20265
19 changes: 0 additions & 19 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2952,25 +2952,6 @@ rb_wb_protected_newobj_of(rb_execution_context_t *ec, VALUE klass, VALUE flags,
return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
}

/* for compatibility */

VALUE
rb_newobj(void)
{
return newobj_of(GET_RACTOR(), 0, T_NONE, 0, 0, 0, FALSE, RVALUE_SIZE);
}

VALUE
rb_newobj_of(VALUE klass, VALUE flags)
{
if ((flags & RUBY_T_MASK) == T_OBJECT) {
return rb_class_allocate_instance(klass);
}
else {
return newobj_of(GET_RACTOR(), klass, flags & ~FL_WB_PROTECTED, 0, 0, 0, flags & FL_WB_PROTECTED, RVALUE_SIZE);
}
}

#define UNEXPECTED_NODE(func) \
rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
Expand Down
3 changes: 1 addition & 2 deletions include/ruby/internal/intern/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass);
* @return An allocated, not yet initialised instance of `klass`.
* @note It calls the allocator defined by rb_define_alloc_func(). You
* cannot use this function to define an allocator. Use
* rb_newobj_of(), #TypedData_Make_Struct or others, instead.
* TypedData_Make_Struct or others, instead.
* @note Usually prefer rb_class_new_instance() to rb_obj_alloc() and
* rb_obj_call_init().
* @see rb_class_new_instance()
* @see rb_obj_call_init()
* @see rb_define_alloc_func()
* @see rb_newobj_of()
* @see #TypedData_Make_Struct
*/
VALUE rb_obj_alloc(VALUE klass);
Expand Down
3 changes: 1 addition & 2 deletions include/ruby/internal/intern/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func);
* restrict creation of an instance of a class. For example it rarely makes
* sense for a DB adaptor class to allow programmers creating DB row objects
* without querying the DB itself. You can kill sporadic creation of such
* objects then, by nullifying the allocator function using this API. Your
* object shall be allocated using #RB_NEWOBJ_OF() directly.
* objects then, by nullifying the allocator function using this API.
*
* @param[out] klass The class to modify.
* @pre `klass` must be an instance of Class.
Expand Down
49 changes: 0 additions & 49 deletions include/ruby/internal/newobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,14 @@
#include "ruby/internal/value.h"
#include "ruby/assert.h"

/**
* Declares, allocates, then assigns a new object to the given variable.
*
* @param obj Variable name.
* @param type Variable type.
* @exception rb_eNoMemError No space left.
* @return An allocated object, not initialised.
* @note Modern programs tend to use #NEWOBJ_OF instead.
*
* @internal
*
* :FIXME: Should we deprecate it?
*/
#define RB_NEWOBJ(obj,type) type *(obj) = RBIMPL_CAST((type *)rb_newobj())

/**
* Identical to #RB_NEWOBJ, except it also accepts the allocating object's
* class and flags.
*
* @param obj Variable name.
* @param type Variable type.
* @param klass Object's class.
* @param flags Object's flags.
* @exception rb_eNoMemError No space left.
* @return An allocated object, filled with the arguments.
*/
#define RB_NEWOBJ_OF(obj,type,klass,flags) type *(obj) = RBIMPL_CAST((type *)rb_newobj_of(klass, flags))

#define NEWOBJ RB_NEWOBJ /**< @old{RB_NEWOBJ} */
#define NEWOBJ_OF RB_NEWOBJ_OF /**< @old{RB_NEWOBJ_OF} */
#define OBJSETUP rb_obj_setup /**< @old{rb_obj_setup} */
#define CLONESETUP rb_clone_setup /**< @old{rb_clone_setup} */
#define DUPSETUP rb_dup_setup /**< @old{rb_dup_setup} */

RBIMPL_SYMBOL_EXPORT_BEGIN()
/**
* This is the implementation detail of #RB_NEWOBJ.
*
* @exception rb_eNoMemError No space left.
* @return An allocated object, not initialised.
*/
VALUE rb_newobj(void);

/**
* This is the implementation detail of #RB_NEWOBJ_OF.
*
* @param klass Object's class.
* @param flags Object's flags.
* @exception rb_eNoMemError No space left.
* @return An allocated object, filled with the arguments.
*/
VALUE rb_newobj_of(VALUE klass, VALUE flags);

/**
* Fills common fields in the object.
*
* @note Prefer rb_newobj_of() to this function.
* @param[in,out] obj A Ruby object to be set up.
* @param[in] klass `obj` will belong to this class.
* @param[in] type One of ::ruby_value_type.
Expand Down
4 changes: 0 additions & 4 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
#undef RClass
#undef RCLASS_SUPER

/* internal/gc.h */
#undef NEWOBJ_OF
#undef RB_NEWOBJ_OF

/* internal/hash.h */
#undef RHASH_IFNONE
#undef RHASH_SIZE
Expand Down
5 changes: 0 additions & 5 deletions internal/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ size_t rb_size_pool_slot_size(unsigned char pool_id);
struct rb_execution_context_struct; /* in vm_core.h */
struct rb_objspace; /* in vm_core.h */

#ifdef NEWOBJ_OF
# undef NEWOBJ_OF
# undef RB_NEWOBJ_OF
#endif

#define NEWOBJ_OF(var, T, c, f, s, ec) \
T *(var) = (T *)(((f) & FL_WB_PROTECTED) ? \
rb_wb_protected_newobj_of((ec ? ec : GET_EC()), (c), (f) & ~FL_WB_PROTECTED, s) : \
Expand Down
12 changes: 2 additions & 10 deletions spec/ruby/optional/capi/ext/object_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,8 @@ static VALUE object_spec_rb_ivar_foreach(VALUE self, VALUE obj) {
}

static VALUE speced_allocator(VALUE klass) {
VALUE flags = 0;
VALUE instance;
if (RTEST(rb_class_inherited_p(klass, rb_cString))) {
flags = T_STRING;
} else if (RTEST(rb_class_inherited_p(klass, rb_cArray))) {
flags = T_ARRAY;
} else {
flags = T_OBJECT;
}
instance = rb_newobj_of(klass, flags);
VALUE super = rb_class_get_superclass(klass);
VALUE instance = rb_get_alloc_func(super)(klass);
rb_iv_set(instance, "@from_custom_allocator", Qtrue);
return instance;
}
Expand Down