Ruby 3.5.0dev (2025-05-16 revision 04f538c1441e65def90d5b4224010e7d4f4ffab3)
class.h
1#ifndef INTERNAL_CLASS_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_CLASS_H
11#include "id.h"
12#include "id_table.h" /* for struct rb_id_table */
13#include "internal/namespace.h" /* for rb_current_namespace */
14#include "internal/serial.h" /* for rb_serial_t */
15#include "internal/static_assert.h"
16#include "internal/variable.h" /* for rb_class_ivar_set */
17#include "ruby/internal/stdbool.h" /* for bool */
18#include "ruby/intern.h" /* for rb_alloc_func_t */
19#include "ruby/ruby.h" /* for struct RBasic */
20#include "shape.h"
21#include "ruby_assert.h"
22#include "vm_core.h"
23#include "vm_sync.h"
24#include "method.h" /* for rb_cref_t */
25
26#ifdef RCLASS_SUPER
27# undef RCLASS_SUPER
28#endif
29
31 long refcount;
32 struct st_table *tbl;
33};
35
36static inline long
37rb_ns_subclasses_ref_count(rb_ns_subclasses_t *ns_sub)
38{
39 return ns_sub->refcount;
40}
41
42static inline rb_ns_subclasses_t *
43rb_ns_subclasses_ref_inc(rb_ns_subclasses_t *ns_sub)
44{
45 ns_sub->refcount++;
46 return ns_sub;
47}
48
49static inline void
50rb_ns_subclasses_ref_dec(rb_ns_subclasses_t *ns_sub)
51{
52 ns_sub->refcount--;
53 if (ns_sub->refcount == 0) {
54 st_free_table(ns_sub->tbl);
55 xfree(ns_sub);
56 }
57}
58
60 rb_ns_subclasses_t *ns_subclasses;
61 struct rb_subclass_entry *head;
62};
64
66 VALUE klass;
67 struct rb_subclass_entry *next;
68 struct rb_subclass_entry *prev;
69};
71
73 uint32_t index;
74 rb_serial_t global_cvar_state;
75 const rb_cref_t * cref;
76 VALUE class_value;
77};
78
80 const rb_namespace_t *ns;
81 VALUE super;
82 VALUE *fields; // Fields are either ivar or other internal properties stored inline
83 struct rb_id_table *m_tbl;
84 struct rb_id_table *const_tbl;
85 struct rb_id_table *callable_m_tbl;
86 struct rb_id_table *cc_tbl; /* ID -> [[ci1, cc1], [ci2, cc2] ...] */
87 struct rb_id_table *cvc_tbl;
88 VALUE *superclasses;
108
109 const VALUE origin_;
110 const VALUE refined_class;
111 union {
112 struct {
113 rb_alloc_func_t allocator;
114 } class;
115 struct {
116 VALUE attached_object;
117 } singleton_class;
118 struct {
119 const VALUE includer;
120 } iclass;
121 } as;
122 attr_index_t max_iv_count;
123 uint16_t superclass_depth;
124 unsigned char variation_count;
125 bool permanent_classpath : 1;
126 bool cloned : 1;
127 bool shared_const_tbl : 1;
128 bool iclass_is_origin : 1;
129 bool iclass_origin_shared_mtbl : 1;
130 bool superclasses_owner : 1;
131 bool superclasses_with_self : 1;
132 VALUE classpath;
133};
134typedef struct rb_classext_struct rb_classext_t;
135
136STATIC_ASSERT(shape_max_variations, SHAPE_MAX_VARIATIONS < (1 << (sizeof(((rb_classext_t *)0)->variation_count) * CHAR_BIT)));
137
138struct RClass {
139 struct RBasic basic;
140 st_table *ns_classext_tbl; // ns_object -> (rb_classext_t *)
141 VALUE object_id;
142 /*
143 * If ns_classext_tbl is NULL, then the prime classext is readable (because no other classext exists).
144 * For the check whether writable or not, check flag RCLASS_PRIME_CLASSEXT_WRITABLE
145 */
146};
147
148// Assert that classes can be embedded in heaps[2] (which has 160B slot size)
149// On 32bit platforms there is no variable width allocation so it doesn't matter.
150// TODO: restore this assertion after shrinking rb_classext_t
151// STATIC_ASSERT(sizeof_rb_classext_t, sizeof(struct RClass) + sizeof(rb_classext_t) <= 4 * RVALUE_SIZE || SIZEOF_VALUE < SIZEOF_LONG_LONG);
152
154 struct RClass rclass;
155 rb_classext_t classext;
156};
157
158static const uint16_t RCLASS_MAX_SUPERCLASS_DEPTH = ((uint16_t)-1);
159
160static inline bool RCLASS_SINGLETON_P(VALUE klass);
161
162static inline bool RCLASS_PRIME_CLASSEXT_READABLE_P(VALUE obj);
163static inline bool RCLASS_PRIME_CLASSEXT_WRITABLE_P(VALUE obj);
164static inline void RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE obj, bool writable);
165
166#define RCLASS_EXT_PRIME(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
167#define RCLASS_EXT_PRIME_P(ext, c) (&((struct RClass_and_rb_classext_t*)(c))->classext == ext)
168
169static inline rb_classext_t * RCLASS_EXT_READABLE_IN_NS(VALUE obj, const rb_namespace_t *ns);
170static inline rb_classext_t * RCLASS_EXT_READABLE(VALUE obj);
171static inline rb_classext_t * RCLASS_EXT_WRITABLE_IN_NS(VALUE obj, const rb_namespace_t *ns);
172static inline rb_classext_t * RCLASS_EXT_WRITABLE(VALUE obj);
173
174// Raw accessor
175#define RCLASS_CLASSEXT_TBL(klass) (RCLASS(klass)->ns_classext_tbl)
176
177#define RCLASSEXT_NS(ext) (ext->ns)
178#define RCLASSEXT_SUPER(ext) (ext->super)
179#define RCLASSEXT_FIELDS(ext) (ext->fields)
180#define RCLASSEXT_M_TBL(ext) (ext->m_tbl)
181#define RCLASSEXT_CONST_TBL(ext) (ext->const_tbl)
182#define RCLASSEXT_CALLABLE_M_TBL(ext) (ext->callable_m_tbl)
183#define RCLASSEXT_CC_TBL(ext) (ext->cc_tbl)
184#define RCLASSEXT_CVC_TBL(ext) (ext->cvc_tbl)
185#define RCLASSEXT_SUPERCLASS_DEPTH(ext) (ext->superclass_depth)
186#define RCLASSEXT_SUPERCLASSES(ext) (ext->superclasses)
187#define RCLASSEXT_SUBCLASSES(ext) (ext->subclasses)
188#define RCLASSEXT_NS_SUPER_SUBCLASSES(ext) (ext->ns_super_subclasses)
189#define RCLASSEXT_NS_MODULE_SUBCLASSES(ext) (ext->ns_module_subclasses)
190#define RCLASSEXT_ORIGIN(ext) (ext->origin_)
191#define RCLASSEXT_REFINED_CLASS(ext) (ext->refined_class)
192// class.allocator/singleton_class.attached_object are not accessed directly via RCLASSEXT_*
193#define RCLASSEXT_INCLUDER(ext) (ext->as.iclass.includer)
194#define RCLASSEXT_MAX_IV_COUNT(ext) (ext->max_iv_count)
195#define RCLASSEXT_VARIATION_COUNT(ext) (ext->variation_count)
196#define RCLASSEXT_PERMANENT_CLASSPATH(ext) (ext->permanent_classpath)
197#define RCLASSEXT_CLONED(ext) (ext->cloned)
198#define RCLASSEXT_SHARED_CONST_TBL(ext) (ext->shared_const_tbl)
199#define RCLASSEXT_ICLASS_IS_ORIGIN(ext) (ext->iclass_is_origin)
200#define RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) (ext->iclass_origin_shared_mtbl)
201#define RCLASSEXT_SUPERCLASSES_OWNER(ext) (ext->superclasses_owner)
202#define RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) (ext->superclasses_with_self)
203#define RCLASSEXT_CLASSPATH(ext) (ext->classpath)
204
205static inline void RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE origin);
206static inline void RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer);
207
208/* Prime classext entry accessor for very specific reason */
209#define RCLASS_PRIME_NS(c) (RCLASS_EXT_PRIME(c)->ns)
210// To invalidate CC by inserting&invalidating method entry into tables containing the target cme
211// See clear_method_cache_by_id_in_class()
212#define RCLASS_PRIME_FIELDS(c) (RCLASS_EXT_PRIME(c)->fields)
213#define RCLASS_PRIME_M_TBL(c) (RCLASS_EXT_PRIME(c)->m_tbl)
214#define RCLASS_PRIME_CONST_TBL(c) (RCLASS_EXT_PRIME(c)->const_tbl)
215#define RCLASS_PRIME_CALLABLE_M_TBL(c) (RCLASS_EXT_PRIME(c)->callable_m_tbl)
216#define RCLASS_PRIME_CC_TBL(c) (RCLASS_EXT_PRIME(c)->cc_tbl)
217#define RCLASS_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->m_tbl != tbl)
218#define RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->callable_m_tbl != tbl)
219#define RCLASS_CC_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->cc_tbl != tbl)
220
221// Read accessor, regarding namespaces
222#define RCLASS_SUPER(c) (RCLASS_EXT_READABLE(c)->super)
223#define RCLASS_M_TBL(c) (RCLASS_EXT_READABLE(c)->m_tbl)
224#define RCLASS_CONST_TBL(c) (RCLASS_EXT_READABLE(c)->const_tbl)
225/*
226 * Both cc_tbl/callable_m_tbl are cache-like and always be changed when referreed,
227 * so always those should be writable.
228 */
229#define RCLASS_CVC_TBL(c) (RCLASS_EXT_READABLE(c)->cvc_tbl)
230#define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT_READABLE(c)->superclass_depth)
231#define RCLASS_SUPERCLASSES(c) (RCLASS_EXT_READABLE(c)->superclasses)
232#define RCLASS_SUPERCLASSES_WITH_SELF_P(c) (RCLASS_EXT_READABLE(c)->superclasses_with_self)
233#define RCLASS_SUBCLASSES_X(c) (RCLASS_EXT_READABLE(c)->subclasses)
234#define RCLASS_SUBCLASSES_FIRST(c) (RCLASS_EXT_READABLE(c)->subclasses->head->next)
235#define RCLASS_ORIGIN(c) (RCLASS_EXT_READABLE(c)->origin_)
236#define RICLASS_IS_ORIGIN_P(c) (RCLASS_EXT_READABLE(c)->iclass_is_origin)
237#define RCLASS_MAX_IV_COUNT(c) (RCLASS_EXT_READABLE(c)->max_iv_count)
238#define RCLASS_VARIATION_COUNT(c) (RCLASS_EXT_READABLE(c)->variation_count)
239#define RCLASS_PERMANENT_CLASSPATH_P(c) (RCLASS_EXT_READABLE(c)->permanent_classpath)
240#define RCLASS_CLONED_P(c) (RCLASS_EXT_READABLE(c)->cloned)
241#define RCLASS_CLASSPATH(c) (RCLASS_EXT_READABLE(c)->classpath)
242
243// namespaces don't make changes on these refined_class/attached_object/includer
244#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT_PRIME(c)->refined_class)
245#define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT_PRIME(c)->as.singleton_class.attached_object)
246#define RCLASS_INCLUDER(c) (RCLASS_EXT_PRIME(c)->as.iclass.includer)
247
248// Writable classext entries (instead of RCLASS_SET_*) because member data will be operated directly
249#define RCLASS_WRITABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->m_tbl)
250#define RCLASS_WRITABLE_CONST_TBL(c) (RCLASS_EXT_WRITABLE(c)->const_tbl)
251#define RCLASS_WRITABLE_CALLABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->callable_m_tbl)
252#define RCLASS_WRITABLE_CC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cc_tbl)
253#define RCLASS_WRITABLE_CVC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cvc_tbl)
254#define RCLASS_WRITABLE_SUBCLASSES(c) (RCLASS_EXT_WRITABLE(c)->subclasses)
255
256static inline void RCLASS_SET_SUPER(VALUE klass, VALUE super);
257static inline void RCLASS_WRITE_SUPER(VALUE klass, VALUE super);
258static inline st_table * RCLASS_FIELDS_HASH(VALUE obj);
259static inline st_table * RCLASS_WRITABLE_FIELDS_HASH(VALUE obj);
260static inline uint32_t RCLASS_FIELDS_COUNT(VALUE obj);
261static inline void RCLASS_SET_FIELDS_HASH(VALUE obj, const st_table *table);
262static inline void RCLASS_WRITE_FIELDS_HASH(VALUE obj, const st_table *table);
263// TODO: rename RCLASS_SET_M_TBL_WORKAROUND (and _WRITE_) to RCLASS_SET_M_TBL with write barrier
264static inline void RCLASS_SET_M_TBL_WORKAROUND(VALUE klass, struct rb_id_table *table, bool check_promoted);
265static inline void RCLASS_WRITE_M_TBL_WORKAROUND(VALUE klass, struct rb_id_table *table, bool check_promoted);
266static inline void RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared);
267static inline void RCLASS_WRITE_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared);
268static inline void RCLASS_WRITE_CALLABLE_M_TBL(VALUE klass, struct rb_id_table *table);
269static inline void RCLASS_WRITE_CC_TBL(VALUE klass, struct rb_id_table *table);
270static inline void RCLASS_SET_CVC_TBL(VALUE klass, struct rb_id_table *table);
271static inline void RCLASS_WRITE_CVC_TBL(VALUE klass, struct rb_id_table *table);
272
273static inline void RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool owns_it, bool with_self);
274static inline void RCLASS_SET_SUBCLASSES(VALUE klass, rb_subclass_anchor_t *anchor);
275static inline void RCLASS_WRITE_NS_SUPER_SUBCLASSES(VALUE klass, rb_ns_subclasses_t *ns_subclasses);
276static inline void RCLASS_WRITE_NS_MODULE_SUBCLASSES(VALUE klass, rb_ns_subclasses_t *ns_subclasses);
277
278static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
279static inline void RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin);
280static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
281static inline void RICLASS_WRITE_ORIGIN_SHARED_MTBL(VALUE iclass);
282static inline bool RICLASS_OWNS_M_TBL_P(VALUE iclass);
283
284static inline void RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined);
285static inline rb_alloc_func_t RCLASS_ALLOCATOR(VALUE klass);
286static inline void RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator);
287static inline VALUE RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object);
288
289static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
290static inline void RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count);
291static inline void RCLASS_WRITE_MAX_IV_COUNT(VALUE klass, attr_index_t count);
292static inline void RCLASS_SET_CLONED(VALUE klass, bool cloned);
293static inline void RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent);
294static inline void RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool permanent);
295
296#define RCLASS_IS_ROOT FL_USER0
297// 1 is for RUBY_FL_SINGLETON or RMODULE_ALLOCATED_BUT_NOT_INITIALIZED (see class.c)
298#define RCLASS_PRIME_CLASSEXT_WRITABLE FL_USER2
299// 3 is RMODULE_IS_REFINEMENT for RMODULE
300// 4-19: SHAPE_FLAG_MASK
301
302/* class.c */
303rb_classext_t * rb_class_duplicate_classext(rb_classext_t *orig, VALUE obj, const rb_namespace_t *ns);
304void rb_class_ensure_writable(VALUE obj);
305
306static inline int
307RCLASS_SET_NAMESPACE_CLASSEXT(VALUE obj, const rb_namespace_t *ns, rb_classext_t *ext)
308{
309 int first_set = 0;
310 st_table *tbl = RCLASS_CLASSEXT_TBL(obj);
311 VM_ASSERT(NAMESPACE_USER_P(ns)); // non-prime classext is only for user namespace, with ns_object
312 VM_ASSERT(ns->ns_object);
313 VM_ASSERT(RCLASSEXT_NS(ext) == ns);
314 if (!tbl) {
315 RCLASS_CLASSEXT_TBL(obj) = tbl = st_init_numtable_with_size(1);
316 }
317 if (rb_st_table_size(tbl) == 0) {
318 first_set = 1;
319 }
320 rb_st_insert(tbl, (st_data_t)ns->ns_object, (st_data_t)ext);
321 return first_set;
322}
323
324static inline bool
325RCLASS_PRIME_CLASSEXT_READABLE_P(VALUE klass)
326{
327 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
328 // if the lookup table exists, then it means the prime classext is NOT directly readable.
329 return RCLASS_CLASSEXT_TBL(klass) == NULL;
330}
331
332static inline bool
333RCLASS_PRIME_CLASSEXT_WRITABLE_P(VALUE klass)
334{
335 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
336 return FL_TEST(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
337}
338
339static inline void
340RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE klass, bool writable)
341{
342 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
343
344 if (writable) {
345 FL_SET(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
346 }
347 else {
348 FL_UNSET(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
349 }
350}
351
352static inline rb_classext_t *
353RCLASS_EXT_TABLE_LOOKUP_INTERNAL(VALUE obj, const rb_namespace_t *ns)
354{
355 st_data_t classext_ptr;
356 st_table *classext_tbl = RCLASS_CLASSEXT_TBL(obj);
357 if (classext_tbl) {
358 if (rb_st_lookup(classext_tbl, (st_data_t)ns->ns_object, &classext_ptr)) {
359 return (rb_classext_t *)classext_ptr;
360 }
361 }
362 return NULL;
363}
364
365static inline rb_classext_t *
366RCLASS_EXT_READABLE_LOOKUP(VALUE obj, const rb_namespace_t *ns)
367{
368 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, ns);
369 if (ext)
370 return ext;
371 // Classext for the ns not found. Refer the prime one instead.
372 return RCLASS_EXT_PRIME(obj);
373}
374
375static inline rb_classext_t *
376RCLASS_EXT_READABLE_IN_NS(VALUE obj, const rb_namespace_t *ns)
377{
378 if (!ns
379 || NAMESPACE_BUILTIN_P(ns)
380 || RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
381 return RCLASS_EXT_PRIME(obj);
382 }
383 return RCLASS_EXT_READABLE_LOOKUP(obj, ns);
384}
385
386static inline rb_classext_t *
387RCLASS_EXT_READABLE(VALUE obj)
388{
389 const rb_namespace_t *ns;
390 if (RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
391 return RCLASS_EXT_PRIME(obj);
392 }
393 // delay namespace loading to optimize for unmodified classes
394 ns = rb_current_namespace();
395 if (!ns || NAMESPACE_BUILTIN_P(ns)) {
396 return RCLASS_EXT_PRIME(obj);
397 }
398 return RCLASS_EXT_READABLE_LOOKUP(obj, ns);
399}
400
401static inline rb_classext_t *
402RCLASS_EXT_WRITABLE_LOOKUP(VALUE obj, const rb_namespace_t *ns)
403{
404 rb_classext_t *ext;
405 int first_set = 0;
406
407 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, ns);
408 if (ext)
409 return ext;
410
411 if (!rb_shape_obj_too_complex_p(obj)) {
412 rb_evict_ivars_to_hash(obj); // fallback to ivptr for ivars from shapes
413 }
414
415 RB_VM_LOCK_ENTER();
416 {
417 // re-check the classext is not created to avoid the multi-thread race
418 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, ns);
419 if (!ext) {
420 ext = rb_class_duplicate_classext(RCLASS_EXT_PRIME(obj), obj, ns);
421 first_set = RCLASS_SET_NAMESPACE_CLASSEXT(obj, ns, ext);
422 if (first_set) {
423 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(obj, false);
424 }
425 }
426 }
427 RB_VM_LOCK_LEAVE();
428 return ext;
429}
430
431static inline rb_classext_t *
432RCLASS_EXT_WRITABLE_IN_NS(VALUE obj, const rb_namespace_t *ns)
433{
434 if (!ns
435 || NAMESPACE_BUILTIN_P(ns)
436 || RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
437 return RCLASS_EXT_PRIME(obj);
438 }
439 return RCLASS_EXT_WRITABLE_LOOKUP(obj, ns);
440}
441
442static inline rb_classext_t *
443RCLASS_EXT_WRITABLE(VALUE obj)
444{
445 const rb_namespace_t *ns;
446 if (RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
447 return RCLASS_EXT_PRIME(obj);
448 }
449 // delay namespace loading to optimize for unmodified classes
450 ns = rb_current_namespace();
451 if (!ns || NAMESPACE_BUILTIN_P(ns)) {
452 // If no namespace is specified, Ruby VM is in bootstrap
453 // and the clean class definition is under construction.
454 return RCLASS_EXT_PRIME(obj);
455 }
456 return RCLASS_EXT_WRITABLE_LOOKUP(obj, ns);
457}
458
459static inline void
460RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE origin)
461{
462 RB_OBJ_WRITE(klass, &(RCLASSEXT_ORIGIN(ext)), origin);
463}
464
465static inline void
466RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer)
467{
469 RB_OBJ_WRITE(klass, &(RCLASSEXT_INCLUDER(ext)), includer);
470}
471
472/* class.c */
473typedef void rb_class_classext_foreach_callback_func(rb_classext_t *classext, bool is_prime, VALUE namespace, void *arg);
474void rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg);
475void rb_class_subclass_add(VALUE super, VALUE klass);
476void rb_class_remove_from_super_subclasses(VALUE);
477void rb_class_remove_from_module_subclasses(VALUE);
478void rb_class_classext_free_subclasses(rb_classext_t *, VALUE);
479void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
480void rb_class_detach_subclasses(VALUE);
481void rb_class_detach_module_subclasses(VALUE);
482void rb_class_update_superclasses(VALUE);
483size_t rb_class_superclasses_memsize(VALUE);
484void rb_class_remove_subclass_head(VALUE);
485int rb_singleton_class_internal_p(VALUE sklass);
486VALUE rb_class_set_super(VALUE klass, VALUE super);
488VALUE rb_class_s_alloc(VALUE klass);
489VALUE rb_module_s_alloc(VALUE klass);
490void rb_module_set_initialized(VALUE module);
491void rb_module_check_initializable(VALUE module);
492VALUE rb_make_metaclass(VALUE, VALUE);
493VALUE rb_include_class_new(VALUE, VALUE);
494VALUE rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super);
495VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
496VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
497VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
498VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
499VALUE rb_class_undefined_instance_methods(VALUE mod);
500VALUE rb_special_singleton_class(VALUE);
501VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
503void rb_undef_methods_from(VALUE klass, VALUE super);
505VALUE rb_keyword_error_new(const char *, VALUE);
506
507RUBY_SYMBOL_EXPORT_BEGIN
508
509/* for objspace */
510VALUE rb_class_super_of(VALUE klass);
511VALUE rb_class_singleton_p(VALUE klass);
512unsigned char rb_class_variation_count(VALUE klass);
513
514RUBY_SYMBOL_EXPORT_END
515
516static inline bool
517RCLASS_SINGLETON_P(VALUE klass)
518{
519 return RB_TYPE_P(klass, T_CLASS) && FL_TEST_RAW(klass, FL_SINGLETON);
520}
521
522static inline void
523RCLASS_SET_SUPER(VALUE klass, VALUE super)
524{
525 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_PRIME(klass)), super);
526}
527
528static inline void
529RCLASS_WRITE_SUPER(VALUE klass, VALUE super)
530{
531 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_WRITABLE(klass)), super);
532}
533
534static inline st_table *
535RCLASS_FIELDS_HASH(VALUE obj)
536{
538 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
539 return (st_table *)RCLASSEXT_FIELDS(RCLASS_EXT_READABLE(obj));
540}
541
542static inline st_table *
543RCLASS_WRITABLE_FIELDS_HASH(VALUE obj)
544{
546 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
547 return (st_table *)RCLASSEXT_FIELDS(RCLASS_EXT_WRITABLE(obj));
548}
549
550static inline void
551RCLASS_SET_FIELDS_HASH(VALUE obj, const st_table *tbl)
552{
554 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
555 RCLASSEXT_FIELDS(RCLASS_EXT_PRIME(obj)) = (VALUE *)tbl;
556}
557
558static inline void
559RCLASS_WRITE_FIELDS_HASH(VALUE obj, const st_table *tbl)
560{
562 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
563 RCLASSEXT_FIELDS(RCLASS_EXT_WRITABLE(obj)) = (VALUE *)tbl;
564}
565
566static inline uint32_t
567RCLASS_FIELDS_COUNT(VALUE obj)
568{
570 if (rb_shape_obj_too_complex_p(obj)) {
571 uint32_t count;
572
573 // "Too complex" classes could have their IV hash mutated in
574 // parallel, so lets lock around getting the hash size.
575 RB_VM_LOCK_ENTER();
576 {
577 count = (uint32_t)rb_st_table_size(RCLASS_FIELDS_HASH(obj));
578 }
579 RB_VM_LOCK_LEAVE();
580
581 return count;
582 }
583 else {
584 return RSHAPE(RCLASS_SHAPE_ID(obj))->next_field_index;
585 }
586}
587
588#define RCLASS_SET_M_TBL_EVEN_WHEN_PROMOTED(klass, table) RCLASS_SET_M_TBL_WORKAROUND(klass, table, false)
589#define RCLASS_SET_M_TBL(klass, table) RCLASS_SET_M_TBL_WORKAROUND(klass, table, true)
590
591static inline void
592RCLASS_SET_M_TBL_WORKAROUND(VALUE klass, struct rb_id_table *table, bool check_promoted)
593{
594 RUBY_ASSERT(!check_promoted || !RB_OBJ_PROMOTED(klass));
595 RCLASSEXT_M_TBL(RCLASS_EXT_PRIME(klass)) = table;
596}
597
598#define RCLASS_WRITE_M_TBL_EVEN_WHEN_PROMOTED(klass, table) RCLASS_WRITE_M_TBL_WORKAROUND(klass, table, false)
599#define RCLASS_WRITE_M_TBL(klass, table) RCLASS_WRITE_M_TBL_WORKAROUND(klass, table, true)
600
601static inline void
602RCLASS_WRITE_M_TBL_WORKAROUND(VALUE klass, struct rb_id_table *table, bool check_promoted)
603{
604 RUBY_ASSERT(!check_promoted || !RB_OBJ_PROMOTED(klass));
605 // TODO: add write barrier here to guard assigning m_tbl
606 // see commit 28a6e4ea9d9379a654a8f7c4b37fa33aa3ccd0b7
607 RCLASSEXT_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
608}
609
610static inline void
611RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
612{
613 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
614 RCLASSEXT_CONST_TBL(ext) = table;
615 if (shared)
616 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
617}
618
619static inline void
620RCLASS_WRITE_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
621{
622 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
623 RCLASSEXT_CONST_TBL(ext) = table;
624 if (shared)
625 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
626}
627
628static inline void
629RCLASS_WRITE_CALLABLE_M_TBL(VALUE klass, struct rb_id_table *table)
630{
631 RCLASSEXT_CALLABLE_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
632}
633
634static inline void
635RCLASS_WRITE_CC_TBL(VALUE klass, struct rb_id_table *table)
636{
637 RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
638}
639
640static inline void
641RCLASS_SET_CVC_TBL(VALUE klass, struct rb_id_table *table)
642{
643 RCLASSEXT_CVC_TBL(RCLASS_EXT_PRIME(klass)) = table;
644}
645
646static inline void
647RCLASS_WRITE_CVC_TBL(VALUE klass, struct rb_id_table *table)
648{
649 RCLASSEXT_CVC_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
650}
651
652static inline void
653RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined)
654{
655 RB_OBJ_WRITE(klass, &RCLASSEXT_REFINED_CLASS(RCLASS_EXT_PRIME(klass)), refined);
656}
657
658static inline rb_alloc_func_t
659RCLASS_ALLOCATOR(VALUE klass)
660{
661 if (RCLASS_SINGLETON_P(klass) || RB_TYPE_P(klass, T_ICLASS)) {
662 return 0;
663 }
664 return RCLASS_EXT_PRIME(klass)->as.class.allocator;
665}
666
667static inline void
668RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator)
669{
670 assert(!RCLASS_SINGLETON_P(klass));
671 RCLASS_EXT_PRIME(klass)->as.class.allocator = allocator; // Allocator is set only on the initial definition
672}
673
674static inline void
675RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
676{
677 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
678 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
679 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
680}
681
682static inline void
683RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin)
684{
685 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
686 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
687 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
688}
689
690static inline void
691RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
692{
693 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_PRIME(iclass)) = true;
694}
695
696static inline void
697RICLASS_WRITE_ORIGIN_SHARED_MTBL(VALUE iclass)
698{
699 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_WRITABLE(iclass)) = true;
700}
701
702static inline bool
703RICLASS_OWNS_M_TBL_P(VALUE iclass)
704{
705 rb_classext_t *ext = RCLASS_EXT_READABLE(iclass);
706 return RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext);
707}
708
709static inline void
710RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
711{
713 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
714}
715
716static inline void
717RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool owns_it, bool with_self)
718{
719 RUBY_ASSERT(depth <= RCLASS_MAX_SUPERCLASS_DEPTH);
720
721 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
722 RCLASSEXT_SUPERCLASS_DEPTH(ext) = depth;
723 RCLASSEXT_SUPERCLASSES(ext) = superclasses;
724 RCLASSEXT_SUPERCLASSES_OWNER(ext) = owns_it;
725 RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) = with_self;
726}
727
728static inline void
729RCLASS_SET_SUBCLASSES(VALUE klass, struct rb_subclass_anchor *anchor)
730{
731 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
732 RCLASSEXT_SUBCLASSES(ext) = anchor;
733}
734
735static inline void
736RCLASS_WRITE_NS_SUPER_SUBCLASSES(VALUE klass, rb_ns_subclasses_t *ns_subclasses)
737{
738 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
739 if (RCLASSEXT_NS_SUPER_SUBCLASSES(ext))
740 rb_ns_subclasses_ref_dec(RCLASSEXT_NS_SUPER_SUBCLASSES(ext));
741 RCLASSEXT_NS_SUPER_SUBCLASSES(ext) = rb_ns_subclasses_ref_inc(ns_subclasses);
742}
743
744static inline void
745RCLASS_WRITE_NS_MODULE_SUBCLASSES(VALUE klass, rb_ns_subclasses_t *ns_subclasses)
746{
747 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
748 if (RCLASSEXT_NS_MODULE_SUBCLASSES(ext))
749 rb_ns_subclasses_ref_dec(RCLASSEXT_NS_MODULE_SUBCLASSES(ext));
750 RCLASSEXT_NS_MODULE_SUBCLASSES(ext) = rb_ns_subclasses_ref_inc(ns_subclasses);
751}
752
753static inline void
754RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
755{
756 rb_classext_t *ext = RCLASS_EXT_READABLE(klass);
757 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
758 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
759
760 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
761 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
762}
763
764static inline void
765RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
766{
767 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
768 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
769 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
770
771 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
772 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
773}
774
775static inline VALUE
776RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
777{
778 assert(RCLASS_SINGLETON_P(klass));
779
780 RB_OBJ_WRITE(klass, &RCLASS_EXT_PRIME(klass)->as.singleton_class.attached_object, attached_object);
781 return attached_object;
782}
783
784static inline void
785RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count)
786{
787 RCLASSEXT_MAX_IV_COUNT(RCLASS_EXT_PRIME(klass)) = count;
788}
789
790static inline void
791RCLASS_WRITE_MAX_IV_COUNT(VALUE klass, attr_index_t count)
792{
793 RCLASSEXT_MAX_IV_COUNT(RCLASS_EXT_WRITABLE(klass)) = count;
794}
795
796static inline void
797RCLASS_SET_CLONED(VALUE klass, bool cloned)
798{
799 RCLASSEXT_CLONED(RCLASS_EXT_PRIME(klass)) = cloned;
800}
801
802#endif /* INTERNAL_CLASS_H */
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition class.c:761
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition class.c:1470
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
Definition class.c:2781
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:128
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:130
#define FL_UNSET
Old name of RB_FL_UNSET.
Definition fl_type.h:132
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
static bool RB_OBJ_PROMOTED(VALUE obj)
Tests if the object is "promoted" – that is, whether the object experienced one or more GC marks.
Definition gc.h:726
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Definition vm.h:216
C99 shim for <stdbool.h>
Ruby object's base components.
Definition rbasic.h:63
struct rb_subclass_anchor * subclasses
The head of subclasses is a blank (w/o klass) entry to be referred from anchor (and be never deleted)...
Definition class.h:93
rb_ns_subclasses_t * ns_module_subclasses
In the case that this is an ICLASS, ns_module_subclasses points to the link in the module's subclasse...
Definition class.h:107
rb_ns_subclasses_t * ns_super_subclasses
The ns_super_subclasses points the ns_subclasses struct to retreive the subclasses of the super class...
Definition class.h:101
CREF (Class REFerence)
Definition method.h:45
Definition class.h:72
Internal header for Namespace.
Definition namespace.h:14
Internal header for Class.
Definition class.h:30
Definition class.h:65
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
@ RUBY_T_MODULE
Definition value_type.h:118
@ RUBY_T_CLASS
Definition value_type.h:117