Skip to content

Commit 90e5ce6

Browse files
committed
Rename RCLASS_EXT() macro to RCLASS_EXT_PRIME() to prevent using it wrongly
The macro RCLASS_EXT() accesses the prime classext directly, but it can be valid only in a limited situation when namespace is enabled. So, to prevent using RCLASS_EXT() in the wrong way, rename the macro and let the developer check it is ok to access the prime classext or not.
1 parent ff790c7 commit 90e5ce6

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

class.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ duplicate_classext_subclasses(rb_classext_t *orig, rb_classext_t *copy)
256256
static void
257257
class_duplicate_iclass_classext(VALUE iclass, rb_classext_t *mod_ext, const rb_namespace_t *ns)
258258
{
259-
rb_classext_t *src = RCLASS_EXT(iclass);
259+
rb_classext_t *src = RCLASS_EXT_PRIME(iclass);
260260
rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, ns);
261261
int first_set = 0;
262262

@@ -416,7 +416,7 @@ rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *
416416
foreach_arg.callback_arg = arg;
417417
rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&foreach_arg);
418418
}
419-
func(RCLASS_EXT(klass), true, (VALUE)NULL, arg);
419+
func(RCLASS_EXT_PRIME(klass), true, (VALUE)NULL, arg);
420420
}
421421

422422
VALUE
@@ -545,7 +545,7 @@ debug_dump_super_chain(rb_classext_t *ext, const rb_namespace_t *ns)
545545
if (ns) {
546546
s = RCLASSEXT_SUPER(RCLASS_EXT_READABLE_IN_NS(s, ns));
547547
} else {
548-
s = RCLASSEXT_SUPER(RCLASS_EXT(s));
548+
s = RCLASSEXT_SUPER(RCLASS_EXT_PRIME(s));
549549
}
550550
} else {
551551
chaining = false;
@@ -835,9 +835,9 @@ rb_class_debug_dump_all_classext(VALUE klass)
835835
rb_str_cat_cstr(r, buf);
836836
rb_str_cat_cstr(r, "========================\n");
837837
rb_str_cat_cstr(r, "Namespace: ");
838-
rb_str_concat(r, debug_dump_inspect_or_return_type(rb_get_namespace_object((rb_namespace_t *)RCLASSEXT_NS(RCLASS_EXT(klass)))));
838+
rb_str_concat(r, debug_dump_inspect_or_return_type(rb_get_namespace_object((rb_namespace_t *)RCLASSEXT_NS(RCLASS_EXT_PRIME(klass)))));
839839
rb_str_cat_cstr(r, "\n");
840-
rb_str_concat(r, debug_dump_classext(RCLASS_EXT(klass), klass, (rb_namespace_t *)NULL));
840+
rb_str_concat(r, debug_dump_classext(RCLASS_EXT_PRIME(klass), klass, (rb_namespace_t *)NULL));
841841
rb_str_cat_cstr(r, "----\n");
842842
if (RCLASS(klass)->ns_classext_tbl) {
843843
rb_st_foreach(RCLASS(klass)->ns_classext_tbl, debug_dump_classext_i, (st_data_t)r);
@@ -1143,7 +1143,7 @@ class_alloc(VALUE flags, VALUE klass)
11431143
if (RGENGC_WB_PROTECTED_CLASS) flags |= FL_WB_PROTECTED;
11441144
NEWOBJ_OF(obj, struct RClass, klass, flags, alloc_size, 0);
11451145

1146-
memset(RCLASS_EXT(obj), 0, sizeof(rb_classext_t));
1146+
memset(RCLASS_EXT_PRIME(obj), 0, sizeof(rb_classext_t));
11471147

11481148
/* ZALLOC
11491149
RCLASS_CONST_TBL(obj) = 0;

gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3749,7 +3749,7 @@ update_classext(rb_classext_t *ext, bool is_prime, VALUE namespace, void *arg)
37493749
} else {
37503750
// Classext is not copied in this case
37513751
for (attr_index_t i = 0; i < RCLASS_FIELDS_COUNT(klass); i++) {
3752-
UPDATE_IF_MOVED(objspace, RCLASSEXT_FIELDS(RCLASS_EXT(klass))[i]);
3752+
UPDATE_IF_MOVED(objspace, RCLASSEXT_FIELDS(RCLASS_EXT_PRIME(klass))[i]);
37533753
}
37543754
}
37553755

internal/class.h

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static inline bool RCLASS_PRIME_CLASSEXT_READABLE_P(VALUE obj);
157157
static inline bool RCLASS_PRIME_CLASSEXT_WRITABLE_P(VALUE obj);
158158
static inline void RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE obj, bool writable);
159159

160-
#define RCLASS_EXT(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
160+
#define RCLASS_EXT_PRIME(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
161161
#define RCLASS_EXT_PRIME_P(ext, c) (&((struct RClass_and_rb_classext_t*)(c))->classext == ext)
162162

163163
static inline rb_classext_t * RCLASS_EXT_READABLE_IN_NS(VALUE obj, const rb_namespace_t *ns);
@@ -200,17 +200,17 @@ static inline void RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE o
200200
static inline void RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer);
201201

202202
/* Prime classext entry accessor for very specific reason */
203-
#define RCLASS_PRIME_NS(c) (RCLASS_EXT(c)->ns)
203+
#define RCLASS_PRIME_NS(c) (RCLASS_EXT_PRIME(c)->ns)
204204
// To invalidate CC by inserting&invalidating method entry into tables containing the target cme
205205
// See clear_method_cache_by_id_in_class()
206-
#define RCLASS_PRIME_FIELDS(c) (RCLASS_EXT(c)->fields)
207-
#define RCLASS_PRIME_M_TBL(c) (RCLASS_EXT(c)->m_tbl)
208-
#define RCLASS_PRIME_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
209-
#define RCLASS_PRIME_CALLABLE_M_TBL(c) (RCLASS_EXT(c)->callable_m_tbl)
210-
#define RCLASS_PRIME_CC_TBL(c) (RCLASS_EXT(c)->cc_tbl)
211-
#define RCLASS_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT(c)->m_tbl != tbl)
212-
#define RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT(c)->callable_m_tbl != tbl)
213-
#define RCLASS_CC_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT(c)->cc_tbl != tbl)
206+
#define RCLASS_PRIME_FIELDS(c) (RCLASS_EXT_PRIME(c)->fields)
207+
#define RCLASS_PRIME_M_TBL(c) (RCLASS_EXT_PRIME(c)->m_tbl)
208+
#define RCLASS_PRIME_CONST_TBL(c) (RCLASS_EXT_PRIME(c)->const_tbl)
209+
#define RCLASS_PRIME_CALLABLE_M_TBL(c) (RCLASS_EXT_PRIME(c)->callable_m_tbl)
210+
#define RCLASS_PRIME_CC_TBL(c) (RCLASS_EXT_PRIME(c)->cc_tbl)
211+
#define RCLASS_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->m_tbl != tbl)
212+
#define RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->callable_m_tbl != tbl)
213+
#define RCLASS_CC_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->cc_tbl != tbl)
214214

215215
// Read accessor, regarding namespaces
216216
#define RCLASS_SUPER(c) (RCLASS_EXT_READABLE(c)->super)
@@ -235,9 +235,9 @@ static inline void RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE
235235
#define RCLASS_CLASSPATH(c) (RCLASS_EXT_READABLE(c)->classpath)
236236

237237
// namespaces don't make changes on these refined_class/attached_object/includer
238-
#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)
239-
#define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT(c)->as.singleton_class.attached_object)
240-
#define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
238+
#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT_PRIME(c)->refined_class)
239+
#define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT_PRIME(c)->as.singleton_class.attached_object)
240+
#define RCLASS_INCLUDER(c) (RCLASS_EXT_PRIME(c)->includer)
241241

242242
// Writable classext entries (instead of RCLASS_SET_*) because member data will be operated directly
243243
#define RCLASS_WRITABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->m_tbl)
@@ -369,7 +369,7 @@ RCLASS_EXT_READABLE_LOOKUP(VALUE obj, const rb_namespace_t *ns)
369369
if (ext)
370370
return ext;
371371
// Classext for the ns not found. Refer the prime one instead.
372-
return RCLASS_EXT(obj);
372+
return RCLASS_EXT_PRIME(obj);
373373
}
374374

375375
static inline rb_classext_t *
@@ -378,7 +378,7 @@ RCLASS_EXT_READABLE_IN_NS(VALUE obj, const rb_namespace_t *ns)
378378
if (!ns
379379
|| NAMESPACE_BUILTIN_P(ns)
380380
|| RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
381-
return RCLASS_EXT(obj);
381+
return RCLASS_EXT_PRIME(obj);
382382
}
383383
return RCLASS_EXT_READABLE_LOOKUP(obj, ns);
384384
}
@@ -388,12 +388,12 @@ RCLASS_EXT_READABLE(VALUE obj)
388388
{
389389
const rb_namespace_t *ns;
390390
if (RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
391-
return RCLASS_EXT(obj);
391+
return RCLASS_EXT_PRIME(obj);
392392
}
393393
// delay namespace loading to optimize for unmodified classes
394394
ns = rb_current_namespace();
395395
if (!ns || NAMESPACE_BUILTIN_P(ns)) {
396-
return RCLASS_EXT(obj);
396+
return RCLASS_EXT_PRIME(obj);
397397
}
398398
return RCLASS_EXT_READABLE_LOOKUP(obj, ns);
399399
}
@@ -417,7 +417,7 @@ RCLASS_EXT_WRITABLE_LOOKUP(VALUE obj, const rb_namespace_t *ns)
417417
// re-check the classext is not created to avoid the multi-thread race
418418
ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, ns);
419419
if (!ext) {
420-
ext = rb_class_duplicate_classext(RCLASS_EXT(obj), obj, ns);
420+
ext = rb_class_duplicate_classext(RCLASS_EXT_PRIME(obj), obj, ns);
421421
first_set = RCLASS_SET_NAMESPACE_CLASSEXT(obj, ns, ext);
422422
if (first_set) {
423423
RCLASS_SET_PRIME_CLASSEXT_WRITABLE(obj, false);
@@ -434,7 +434,7 @@ RCLASS_EXT_WRITABLE_IN_NS(VALUE obj, const rb_namespace_t *ns)
434434
if (!ns
435435
|| NAMESPACE_BUILTIN_P(ns)
436436
|| RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
437-
return RCLASS_EXT(obj);
437+
return RCLASS_EXT_PRIME(obj);
438438
}
439439
return RCLASS_EXT_WRITABLE_LOOKUP(obj, ns);
440440
}
@@ -444,14 +444,14 @@ RCLASS_EXT_WRITABLE(VALUE obj)
444444
{
445445
const rb_namespace_t *ns;
446446
if (RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
447-
return RCLASS_EXT(obj);
447+
return RCLASS_EXT_PRIME(obj);
448448
}
449449
// delay namespace loading to optimize for unmodified classes
450450
ns = rb_current_namespace();
451451
if (!ns || NAMESPACE_BUILTIN_P(ns)) {
452452
// If no namespace is specified, Ruby VM is in bootstrap
453453
// and the clean class definition is under construction.
454-
return RCLASS_EXT(obj);
454+
return RCLASS_EXT_PRIME(obj);
455455
}
456456
return RCLASS_EXT_WRITABLE_LOOKUP(obj, ns);
457457
}
@@ -521,7 +521,7 @@ RCLASS_SINGLETON_P(VALUE klass)
521521
static inline void
522522
RCLASS_SET_SUPER(VALUE klass, VALUE super)
523523
{
524-
RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT(klass)), super);
524+
RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_PRIME(klass)), super);
525525
}
526526

527527
static inline void
@@ -551,7 +551,7 @@ RCLASS_SET_FIELDS_HASH(VALUE obj, const st_table *tbl)
551551
{
552552
RUBY_ASSERT(RB_TYPE_P(obj, RUBY_T_CLASS) || RB_TYPE_P(obj, RUBY_T_MODULE));
553553
RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
554-
RCLASSEXT_FIELDS(RCLASS_EXT(obj)) = (VALUE *)tbl;
554+
RCLASSEXT_FIELDS(RCLASS_EXT_PRIME(obj)) = (VALUE *)tbl;
555555
}
556556

557557
static inline void
@@ -591,7 +591,7 @@ static inline void
591591
RCLASS_SET_M_TBL_WORKAROUND(VALUE klass, struct rb_id_table *table, bool check_promoted)
592592
{
593593
RUBY_ASSERT(!check_promoted || !RB_OBJ_PROMOTED(klass));
594-
RCLASSEXT_M_TBL(RCLASS_EXT(klass)) = table;
594+
RCLASSEXT_M_TBL(RCLASS_EXT_PRIME(klass)) = table;
595595
}
596596

597597
#define RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(klass, table) RCLASS_WRITE_M_TBL_WORKAROUND(klass, table, false)
@@ -609,7 +609,7 @@ RCLASS_WRITE_M_TBL_WORKAROUND(VALUE klass, struct rb_id_table *table, bool check
609609
static inline void
610610
RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
611611
{
612-
rb_classext_t *ext = RCLASS_EXT(klass);
612+
rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
613613
RCLASSEXT_CONST_TBL(ext) = table;
614614
if (shared)
615615
RCLASSEXT_SHARED_CONST_TBL(ext) = true;
@@ -639,7 +639,7 @@ RCLASS_WRITE_CC_TBL(VALUE klass, struct rb_id_table *table)
639639
static inline void
640640
RCLASS_SET_CVC_TBL(VALUE klass, struct rb_id_table *table)
641641
{
642-
RCLASSEXT_CVC_TBL(RCLASS_EXT(klass)) = table;
642+
RCLASSEXT_CVC_TBL(RCLASS_EXT_PRIME(klass)) = table;
643643
}
644644

645645
static inline void
@@ -651,7 +651,7 @@ RCLASS_WRITE_CVC_TBL(VALUE klass, struct rb_id_table *table)
651651
static inline void
652652
RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined)
653653
{
654-
RB_OBJ_WRITE(klass, &RCLASSEXT_REFINED_CLASS(RCLASS_EXT(klass)), refined);
654+
RB_OBJ_WRITE(klass, &RCLASSEXT_REFINED_CLASS(RCLASS_EXT_PRIME(klass)), refined);
655655
}
656656

657657
static inline rb_alloc_func_t
@@ -660,20 +660,20 @@ RCLASS_ALLOCATOR(VALUE klass)
660660
if (RCLASS_SINGLETON_P(klass)) {
661661
return 0;
662662
}
663-
return RCLASS_EXT(klass)->as.class.allocator;
663+
return RCLASS_EXT_PRIME(klass)->as.class.allocator;
664664
}
665665

666666
static inline void
667667
RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator)
668668
{
669669
assert(!RCLASS_SINGLETON_P(klass));
670-
RCLASS_EXT(klass)->as.class.allocator = allocator; // Allocator is set only on the initial definition
670+
RCLASS_EXT_PRIME(klass)->as.class.allocator = allocator; // Allocator is set only on the initial definition
671671
}
672672

673673
static inline void
674674
RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
675675
{
676-
rb_classext_t *ext = RCLASS_EXT(klass);
676+
rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
677677
RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
678678
if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
679679
}
@@ -689,7 +689,7 @@ RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin)
689689
static inline void
690690
RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
691691
{
692-
RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT(iclass)) = true;
692+
RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_PRIME(iclass)) = true;
693693
}
694694

695695
static inline void
@@ -724,7 +724,7 @@ RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool o
724724
static inline void
725725
RCLASS_SET_SUBCLASSES(VALUE klass, struct rb_subclass_anchor *anchor)
726726
{
727-
rb_classext_t *ext = RCLASS_EXT(klass);
727+
rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
728728
RCLASSEXT_SUBCLASSES(ext) = anchor;
729729
}
730730

@@ -773,14 +773,14 @@ RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
773773
{
774774
assert(RCLASS_SINGLETON_P(klass));
775775

776-
RB_OBJ_WRITE(klass, &RCLASS_EXT(klass)->as.singleton_class.attached_object, attached_object);
776+
RB_OBJ_WRITE(klass, &RCLASS_EXT_PRIME(klass)->as.singleton_class.attached_object, attached_object);
777777
return attached_object;
778778
}
779779

780780
static inline void
781781
RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count)
782782
{
783-
RCLASSEXT_MAX_IV_COUNT(RCLASS_EXT(klass)) = count;
783+
RCLASSEXT_MAX_IV_COUNT(RCLASS_EXT_PRIME(klass)) = count;
784784
}
785785

786786
static inline void
@@ -792,7 +792,7 @@ RCLASS_WRITE_MAX_IV_COUNT(VALUE klass, attr_index_t count)
792792
static inline void
793793
RCLASS_SET_CLONED(VALUE klass, bool cloned)
794794
{
795-
RCLASSEXT_CLONED(RCLASS_EXT(klass)) = cloned;
795+
RCLASSEXT_CLONED(RCLASS_EXT_PRIME(klass)) = cloned;
796796
}
797797

798798
#endif /* INTERNAL_CLASS_H */

0 commit comments

Comments
 (0)