Skip to content

Commit

Permalink
Add RCLASS_SUBCLASSES Macro
Browse files Browse the repository at this point in the history
  • Loading branch information
eightbitraptor authored and tenderlove committed Feb 1, 2021
1 parent 7341b01 commit e0f999a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
20 changes: 10 additions & 10 deletions class.c
Expand Up @@ -53,14 +53,14 @@ rb_class_subclass_add(VALUE super, VALUE klass)
entry->klass = klass;
entry->next = NULL;

head = RCLASS_EXT(super)->subclasses;
head = RCLASS_SUBCLASSES(super);
if (head) {
entry->next = head;
RCLASS_PARENT_SUBCLASSES(head->klass) = &entry->next;
}

RCLASS_EXT(super)->subclasses = entry;
RCLASS_PARENT_SUBCLASSES(klass) = &RCLASS_EXT(super)->subclasses;
RCLASS_SUBCLASSES(super) = entry;
RCLASS_PARENT_SUBCLASSES(klass) = &RCLASS_SUBCLASSES(super);
}
}

Expand All @@ -73,14 +73,14 @@ rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
entry->klass = iclass;
entry->next = NULL;

head = RCLASS_EXT(module)->subclasses;
head = RCLASS_SUBCLASSES(module);
if (head) {
entry->next = head;
RCLASS_MODULE_SUBCLASSES(head->klass) = &entry->next;
}

RCLASS_EXT(module)->subclasses = entry;
RCLASS_MODULE_SUBCLASSES(iclass) = &RCLASS_EXT(module)->subclasses;
RCLASS_SUBCLASSES(module) = entry;
RCLASS_MODULE_SUBCLASSES(iclass) = &RCLASS_SUBCLASSES(module);
}

void
Expand Down Expand Up @@ -123,7 +123,7 @@ rb_class_remove_from_module_subclasses(VALUE klass)
void
rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE arg)
{
rb_subclass_entry_t *cur = RCLASS_EXT(klass)->subclasses;
rb_subclass_entry_t *cur = RCLASS_SUBCLASSES(klass);

/* do not be tempted to simplify this loop into a for loop, the order of
operations is important here if `f` modifies the linked list */
Expand Down Expand Up @@ -181,7 +181,7 @@ class_alloc(VALUE flags, VALUE klass)
RCLASS_M_TBL(obj) = 0;
RCLASS_IV_INDEX_TBL(obj) = 0;
RCLASS_SET_SUPER((VALUE)obj, 0);
RCLASS_EXT(obj)->subclasses = NULL;
RCLASS_SUBCLASSES(obj) = NULL;
RCLASS_PARENT_SUBCLASSES(obj) = NULL;
RCLASS_MODULE_SUBCLASSES(obj) = NULL;
*/
Expand Down Expand Up @@ -979,7 +979,7 @@ rb_include_module(VALUE klass, VALUE module)
rb_raise(rb_eArgError, "cyclic include detected");

if (RB_TYPE_P(klass, T_MODULE)) {
rb_subclass_entry_t *iclass = RCLASS_EXT(klass)->subclasses;
rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
int do_include = 1;
while (iclass) {
VALUE check_class = iclass->klass;
Expand Down Expand Up @@ -1190,7 +1190,7 @@ rb_prepend_module(VALUE klass, VALUE module)
rb_vm_check_redefinition_by_prepend(klass);
}
if (RB_TYPE_P(klass, T_MODULE)) {
rb_subclass_entry_t *iclass = RCLASS_EXT(klass)->subclasses;
rb_subclass_entry_t *iclass = RCLASS_SUBCLASSES(klass);
VALUE klass_origin = RCLASS_ORIGIN(klass);
struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
Expand Down
8 changes: 4 additions & 4 deletions gc.c
Expand Up @@ -2836,14 +2836,14 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
if (RCLASS_IV_INDEX_TBL(obj)) {
iv_index_tbl_free(RCLASS_IV_INDEX_TBL(obj));
}
if (RCLASS_EXT(obj)->subclasses) {
if (RCLASS_SUBCLASSES(obj)) {
if (BUILTIN_TYPE(obj) == T_MODULE) {
rb_class_detach_module_subclasses(obj);
}
else {
rb_class_detach_subclasses(obj);
}
RCLASS_EXT(obj)->subclasses = NULL;
RCLASS_SUBCLASSES(obj) = NULL;
}
rb_class_remove_from_module_subclasses(obj);
rb_class_remove_from_super_subclasses(obj);
Expand Down Expand Up @@ -3008,9 +3008,9 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
rb_id_table_free(RCLASS_CALLABLE_M_TBL(obj));
}
if (RCLASS_EXT(obj)->subclasses) {
if (RCLASS_SUBCLASSES(obj)) {
rb_class_detach_subclasses(obj);
RCLASS_EXT(obj)->subclasses = NULL;
RCLASS_SUBCLASSES(obj) = NULL;
}
cc_table_free(objspace, obj, FALSE);
rb_class_remove_from_module_subclasses(obj);
Expand Down
1 change: 1 addition & 0 deletions internal/class.h
Expand Up @@ -95,6 +95,7 @@ typedef struct rb_classext_struct rb_classext_t;
#define RCLASS_PARENT_SUBCLASSES(c) (RCLASS_EXT(c)->parent_subclasses)
#define RCLASS_MODULE_SUBCLASSES(c) (RCLASS_EXT(c)->module_subclasses)
#define RCLASS_ALLOCATOR(c) (RCLASS_EXT(c)->allocator)
#define RCLASS_SUBCLASSES(c) (RCLASS_EXT(c)->subclasses)

#define RICLASS_IS_ORIGIN FL_USER5
#define RCLASS_CLONED FL_USER6
Expand Down
2 changes: 1 addition & 1 deletion vm_method.c
Expand Up @@ -153,7 +153,7 @@ clear_method_cache_by_id_in_class(VALUE klass, ID mid)
VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_ICLASS));
if (rb_objspace_garbage_object_p(klass)) return;

if (LIKELY(RCLASS_EXT(klass)->subclasses == NULL)) {
if (LIKELY(RCLASS_SUBCLASSES(klass) == NULL)) {
// no subclasses
// check only current class

Expand Down

0 comments on commit e0f999a

Please sign in to comment.