Ruby 4.1.0dev (2026-12-03 revision da234ef6cade3309d163e0cbeaaf616f3367b377)
shape.c (da234ef6cade3309d163e0cbeaaf616f3367b377)
1#include "vm_core.h"
2#include "vm_sync.h"
3#include "shape.h"
4#include "id_table.h"
5#include "internal/class.h"
6#include "internal/error.h"
7#include "internal/gc.h"
8#include "internal/object.h"
9#include "internal/symbol.h"
10#include "internal/variable.h"
11#include "symbol.h"
12#include "variable.h"
13#include <stdbool.h>
14
15#ifndef _WIN32
16#include <sys/mman.h>
17#endif
18
19#ifndef SHAPE_DEBUG
20#define SHAPE_DEBUG (VM_CHECK_MODE > 0)
21#endif
22
23#define REDBLACK_CACHE_SIZE (SHAPE_BUFFER_SIZE * 32)
24
25/* This depends on that the allocated memory by Ruby's allocator or
26 * mmap is not located at an odd address. */
27#define SINGLE_CHILD_TAG 0x1
28#define TAG_SINGLE_CHILD(x) (VALUE)((uintptr_t)(x) | SINGLE_CHILD_TAG)
29#define SINGLE_CHILD_MASK (~((uintptr_t)SINGLE_CHILD_TAG))
30#define SINGLE_CHILD_P(x) ((uintptr_t)(x) & SINGLE_CHILD_TAG)
31#define SINGLE_CHILD(x) (rb_shape_t *)((uintptr_t)(x) & SINGLE_CHILD_MASK)
32#define ANCESTOR_CACHE_THRESHOLD 10
33#define MAX_SHAPE_ID (INVALID_SHAPE_ID - 1)
34#define ANCESTOR_SEARCH_MAX_DEPTH 2
35
36// Should be on its own cache line
37static RUBY_ALIGNAS(128) rb_atomic_t redblack_cache_size;
38
39struct redblack_node {
40 rb_id_serial_t key;
41 shape_id_t value; // The color bit is stored in the shape_id flags.
42 redblack_id_t l;
43 redblack_id_t r;
44};
45typedef struct redblack_node redblack_node_t;
46
47static redblack_node_t *redblack_cache;
48
49#define LEAF 0
50#define BLACK 0x0
51#define RED (1<<(SHAPE_ID_FL_USHIFT))
52
53static inline redblack_node_t *
54redblack_node(redblack_id_t id)
55{
56 return id ? &redblack_cache[id - 1] : LEAF;
57}
58
59static redblack_node_t *
60redblack_left(redblack_node_t *node)
61{
62 if (node->l == LEAF) {
63 return LEAF;
64 }
65 else {
66 RUBY_ASSERT(node->l < redblack_cache_size);
67 redblack_node_t *left = redblack_node(node->l);
68 return left;
69 }
70}
71
72static redblack_node_t *
73redblack_right(redblack_node_t *node)
74{
75 if (node->r == LEAF) {
76 return LEAF;
77 }
78 else {
79 RUBY_ASSERT(node->r < redblack_cache_size);
80 redblack_node_t *right = redblack_node(node->r);
81 return right;
82 }
83}
84
85static redblack_node_t *
86redblack_find0(redblack_node_t *tree, ID key)
87{
88 if (tree == LEAF) {
89 return LEAF;
90 }
91 else {
92 RUBY_ASSERT(redblack_left(tree) == LEAF || redblack_left(tree)->key < tree->key);
93 RUBY_ASSERT(redblack_right(tree) == LEAF || redblack_right(tree)->key > tree->key);
94
95 if (tree->key == key) {
96 return tree;
97 }
98 else {
99 if (key < tree->key) {
100 return redblack_find0(redblack_left(tree), key);
101 }
102 else {
103 return redblack_find0(redblack_right(tree), key);
104 }
105 }
106 }
107}
108
109static redblack_node_t *
110redblack_find(redblack_id_t tree_id, ID key)
111{
112 return redblack_find0(redblack_node(tree_id), key);
113}
114
115static inline shape_id_t
116redblack_value(redblack_node_t *node)
117{
118 // Color is stored in flag bits the shape_id.
119 // Mask away the bit so we get the actual pointer back
120 return node->value & ~RED;
121}
122
123#ifdef HAVE_MMAP
124static inline shape_id_t
125redblack_color(redblack_node_t *node)
126{
127 if (node) {
128 return node->value & RED;
129 }
130 return BLACK;
131}
132
133static inline bool
134redblack_red_p(redblack_node_t *node)
135{
136 return redblack_color(node) == RED;
137}
138
139static redblack_id_t
140redblack_id_for(redblack_node_t *node)
141{
142 RUBY_ASSERT(node || node == LEAF);
143 if (node == LEAF) {
144 return 0;
145 }
146 else {
147 redblack_node_t *redblack_nodes = redblack_cache;
148 redblack_id_t id = (redblack_id_t)(node - redblack_nodes);
149 return id + 1;
150 }
151}
152
153static redblack_node_t *
154redblack_new(shape_id_t color, rb_id_serial_t key, shape_id_t value, redblack_node_t *left, redblack_node_t *right)
155{
156 if (redblack_cache_size + 1 >= REDBLACK_CACHE_SIZE) {
157 // We're out of cache, just quit
158 return LEAF;
159 }
160
161 RUBY_ASSERT(left == LEAF || left->key < key);
162 RUBY_ASSERT(right == LEAF || right->key > key);
163
164 redblack_node_t *redblack_nodes = redblack_cache;
165 redblack_node_t *node = &redblack_nodes[RUBY_ATOMIC_FETCH_ADD(redblack_cache_size, 1)];
166 node->key = key;
167 node->value = value | color;
168 node->l = redblack_id_for(left);
169 node->r = redblack_id_for(right);
170 return node;
171}
172
173static redblack_node_t *
174redblack_balance(shape_id_t color, rb_id_serial_t key, shape_id_t value, redblack_node_t *left, redblack_node_t *right)
175{
176 if (color == BLACK) {
177 rb_id_serial_t new_key, new_left_key, new_right_key;
178 shape_id_t new_value, new_left_value, new_right_value;
179 redblack_node_t *new_left_left, *new_left_right, *new_right_left, *new_right_right;
180
181 if (redblack_red_p(left) && redblack_red_p(redblack_left(left))) {
182 new_right_key = key;
183 new_right_value = value;
184 new_right_right = right;
185
186 new_key = left->key;
187 new_value = redblack_value(left);
188 new_right_left = redblack_right(left);
189
190 new_left_key = redblack_left(left)->key;
191 new_left_value = redblack_value(redblack_left(left));
192
193 new_left_left = redblack_left(redblack_left(left));
194 new_left_right = redblack_right(redblack_left(left));
195 }
196 else if (redblack_red_p(left) && redblack_red_p(redblack_right(left))) {
197 new_right_key = key;
198 new_right_value = value;
199 new_right_right = right;
200
201 new_left_key = left->key;
202 new_left_value = redblack_value(left);
203 new_left_left = redblack_left(left);
204
205 new_key = redblack_right(left)->key;
206 new_value = redblack_value(redblack_right(left));
207 new_left_right = redblack_left(redblack_right(left));
208 new_right_left = redblack_right(redblack_right(left));
209 }
210 else if (redblack_red_p(right) && redblack_red_p(redblack_left(right))) {
211 new_left_key = key;
212 new_left_value = value;
213 new_left_left = left;
214
215 new_right_key = right->key;
216 new_right_value = redblack_value(right);
217 new_right_right = redblack_right(right);
218
219 new_key = redblack_left(right)->key;
220 new_value = redblack_value(redblack_left(right));
221 new_left_right = redblack_left(redblack_left(right));
222 new_right_left = redblack_right(redblack_left(right));
223 }
224 else if (redblack_red_p(right) && redblack_red_p(redblack_right(right))) {
225 new_left_key = key;
226 new_left_value = value;
227 new_left_left = left;
228
229 new_key = right->key;
230 new_value = redblack_value(right);
231 new_left_right = redblack_left(right);
232
233 new_right_key = redblack_right(right)->key;
234 new_right_value = redblack_value(redblack_right(right));
235 new_right_left = redblack_left(redblack_right(right));
236 new_right_right = redblack_right(redblack_right(right));
237 }
238 else {
239 return redblack_new(color, key, value, left, right);
240 }
241
242 RUBY_ASSERT(new_left_key < new_key);
243 RUBY_ASSERT(new_right_key > new_key);
244 RUBY_ASSERT(new_left_left == LEAF || new_left_left->key < new_left_key);
245 RUBY_ASSERT(new_left_right == LEAF || new_left_right->key > new_left_key);
246 RUBY_ASSERT(new_left_right == LEAF || new_left_right->key < new_key);
247 RUBY_ASSERT(new_right_left == LEAF || new_right_left->key < new_right_key);
248 RUBY_ASSERT(new_right_left == LEAF || new_right_left->key > new_key);
249 RUBY_ASSERT(new_right_right == LEAF || new_right_right->key > new_right_key);
250
251 return redblack_new(
252 RED, new_key, new_value,
253 redblack_new(BLACK, new_left_key, new_left_value, new_left_left, new_left_right),
254 redblack_new(BLACK, new_right_key, new_right_value, new_right_left, new_right_right));
255 }
256
257 return redblack_new(color, key, value, left, right);
258}
259
260static redblack_node_t *
261redblack_insert_aux(redblack_node_t *tree, rb_id_serial_t key, shape_id_t value)
262{
263 if (tree == LEAF) {
264 return redblack_new(RED, key, value, LEAF, LEAF);
265 }
266 else {
267 redblack_node_t *left, *right;
268 if (key < tree->key) {
269 left = redblack_insert_aux(redblack_left(tree), key, value);
270 RUBY_ASSERT(left != LEAF);
271 right = redblack_right(tree);
272 RUBY_ASSERT(right == LEAF || right->key > tree->key);
273 }
274 else if (key > tree->key) {
275 left = redblack_left(tree);
276 RUBY_ASSERT(left == LEAF || left->key < tree->key);
277 right = redblack_insert_aux(redblack_right(tree), key, value);
278 RUBY_ASSERT(right != LEAF);
279 }
280 else {
281 return tree;
282 }
283
284 return redblack_balance(
285 redblack_color(tree),
286 tree->key,
287 redblack_value(tree),
288 left,
289 right
290 );
291 }
292}
293
294static redblack_node_t *
295redblack_force_black(redblack_node_t *node)
296{
297 node->value = redblack_value(node);
298 return node;
299}
300
301static redblack_id_t
302redblack_insert(redblack_node_t *tree, rb_id_serial_t key, shape_id_t value)
303{
304 redblack_node_t *root = redblack_insert_aux(tree, key, value);
305
306 if (redblack_red_p(root)) {
307 return redblack_id_for(redblack_force_black(root));
308 }
309 else {
310 return redblack_id_for(root);
311 }
312}
313#endif
314
315static VALUE shape_tree_obj = Qfalse;
316rb_shape_tree_t rb_shape_tree = { 0 };
317
318// Should be on its own cache line
319static RUBY_ALIGNAS(128) rb_atomic_t shape_next_id;
320
321static rb_shape_t *
322rb_shape_get_root_shape(void)
323{
324 return rb_shape_tree.shape_list;
325}
326
327static void
328shape_tree_mark_and_move(void *data)
329{
330 rb_shape_t *cursor = rb_shape_get_root_shape();
331 rb_shape_t *end = RSHAPE(shape_next_id - 1);
332 while (cursor <= end) {
333 if (cursor->edges && !SINGLE_CHILD_P(cursor->edges)) {
334 rb_gc_mark_and_move(&cursor->edges);
335 }
336 cursor++;
337 }
338}
339
340size_t
341rb_shapes_cache_size(void)
342{
343 return redblack_cache ? redblack_cache_size : 0;
344}
345
346size_t
347rb_shapes_count(void)
348{
349 return (size_t)RUBY_ATOMIC_LOAD(shape_next_id);
350}
351
352static size_t
353shape_tree_memsize(const void *data)
354{
355 if (redblack_cache) {
356 return redblack_cache_size * sizeof(redblack_node_t);
357 }
358 return 0;
359}
360
361static const rb_data_type_t shape_tree_type = {
362 .wrap_struct_name = "VM/shape_tree",
363 .function = {
364 .dmark = shape_tree_mark_and_move,
365 .dfree = NULL, // Nothing to free, done at VM exit in rb_shape_free_all,
366 .dsize = shape_tree_memsize,
367 .dcompact = shape_tree_mark_and_move,
368 },
369 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
370};
371
372
373/*
374 * Shape getters
375 */
376
377static inline shape_id_t
378SHAPE_OFFSET(rb_shape_t *shape)
379{
380 RUBY_ASSERT(shape);
381 return (shape_id_t)(shape - rb_shape_tree.shape_list);
382}
383
384static inline shape_id_t
385SHAPE_ID(rb_shape_t *shape, shape_id_t previous_shape_id)
386{
387 RUBY_ASSERT(shape);
388 return SHAPE_OFFSET(shape) | RSHAPE_FLAGS(previous_shape_id);
389}
390
391void
392rb_shape_each_shape_id(each_shape_callback callback, void *data)
393{
394 rb_shape_t *start = rb_shape_get_root_shape();
395 rb_shape_t *cursor = start;
396 rb_shape_t *end = RSHAPE(RUBY_ATOMIC_LOAD(shape_next_id));
397 while (cursor < end) {
398 callback((shape_id_t)(cursor - start), data);
399 cursor += 1;
400 }
401}
402
403RUBY_FUNC_EXPORTED shape_id_t
404rb_obj_shape_id(VALUE obj)
405{
406 if (RB_SPECIAL_CONST_P(obj)) {
407 rb_bug("rb_obj_shape_id: called on a special constant");
408 }
409
410 if (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE) {
411 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
412 shape_id_t base = ROOT_SHAPE_ID;
413 if (fields_obj) {
414 // Remove the layout and capacity from the fields object. We want to
415 // combine the shape tree state of the fields object with the layout
416 // and object slot capacity of the class / module object.
417 base = RBASIC_SHAPE_ID(fields_obj) & ~(SHAPE_ID_LAYOUT_MASK | SHAPE_ID_CAPACITY_MASK);
418 }
419 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
420 return rb_shape_layout(shape_id) | (shape_id & SHAPE_ID_CAPACITY_MASK) | base;
421 }
422 return RBASIC_SHAPE_ID(obj);
423}
424
425size_t
426rb_shape_depth(shape_id_t shape_id)
427{
428 size_t depth = 1;
429 rb_shape_t *shape = RSHAPE(shape_id);
430
431 while (shape->parent_offset != INVALID_SHAPE_ID) {
432 depth++;
433 shape = RSHAPE(shape->parent_offset);
434 }
435
436 return depth;
437}
438
439static rb_shape_t *
440shape_alloc(void)
441{
442 shape_id_t current, new_id;
443
444 do {
445 current = RUBY_ATOMIC_LOAD(shape_next_id);
446 if (current > MAX_SHAPE_ID) {
447 return NULL; // Out of shapes
448 }
449 new_id = current + 1;
450 } while (current != RUBY_ATOMIC_CAS(shape_next_id, current, new_id));
451
452 return &rb_shape_tree.shape_list[current];
453}
454
455static rb_shape_t *
456rb_shape_alloc_with_parent_offset(rb_id_serial_t edge_serial, shape_id_t parent_offset)
457{
458 rb_shape_t *shape = shape_alloc();
459 if (!shape) return NULL;
460
461 shape->edge_serial = edge_serial;
462 shape->next_field_index = 0;
463 shape->parent_offset = parent_offset;
464 shape->edges = 0;
465
466 return shape;
467}
468
469static rb_shape_t *
470rb_shape_alloc(rb_id_serial_t edge_serial, rb_shape_t *parent, enum shape_type type)
471{
472 rb_shape_t *shape = rb_shape_alloc_with_parent_offset(edge_serial, SHAPE_OFFSET(parent));
473 if (!shape) return NULL;
474
475 shape->type = (uint8_t)type;
476 shape->capacity = parent->capacity;
477 shape->edges = 0;
478 return shape;
479}
480
481#ifdef HAVE_MMAP
482static redblack_node_t *
483redblack_cache_ancestors(rb_shape_t *shape)
484{
485 if (!(shape->ancestor_index || shape->parent_offset == INVALID_SHAPE_ID)) {
486 redblack_node_t *parent_index_node = redblack_cache_ancestors(RSHAPE(shape->parent_offset));
487
488 if (shape->type == SHAPE_IVAR) {
489 shape->ancestor_index = redblack_insert(parent_index_node, shape->edge_serial, SHAPE_OFFSET(shape));
490
491#if RUBY_DEBUG
492 if (shape->ancestor_index) {
493 redblack_node_t *inserted_node = redblack_find(shape->ancestor_index, shape->edge_serial);
494 RUBY_ASSERT(inserted_node);
495 RUBY_ASSERT(redblack_value(inserted_node) == SHAPE_OFFSET(shape));
496 }
497#endif
498 }
499 else {
500 shape->ancestor_index = redblack_id_for(parent_index_node);
501 }
502 }
503
504 return redblack_node(shape->ancestor_index);
505}
506#else
507static redblack_node_t *
508redblack_cache_ancestors(rb_shape_t *shape)
509{
510 return LEAF;
511}
512#endif
513
514static attr_index_t
515shape_grow_capa(attr_index_t current_capa)
516{
517 size_t next_size = rb_obj_embedded_size(current_capa + 1);
518 if (UNLIKELY(!rb_gc_size_allocatable_p(next_size))) {
519 return rb_shape_max_capacity();
520 }
521
522 attr_index_t next_capa = rb_shape_capacity_for_slot_size(rb_gc_size_slot_size(next_size));
523 RUBY_ASSERT(next_capa > current_capa);
524 return next_capa;
525}
526
527static rb_shape_t *
528rb_shape_alloc_new_child(rb_id_serial_t serial, rb_shape_t *shape, enum shape_type shape_type)
529{
530 rb_shape_t *new_shape = rb_shape_alloc(serial, shape, shape_type);
531 if (!new_shape) return NULL;
532
533 switch (shape_type) {
534 case SHAPE_OBJ_ID:
535 case SHAPE_IVAR:
536 if (UNLIKELY(shape->next_field_index >= shape->capacity)) {
537 RUBY_ASSERT(shape->next_field_index == shape->capacity);
538 new_shape->capacity = shape_grow_capa(shape->capacity);
539 }
540
541 RUBY_ASSERT(new_shape->capacity > shape->next_field_index);
542 new_shape->next_field_index = shape->next_field_index + 1;
543 if (new_shape->next_field_index > ANCESTOR_CACHE_THRESHOLD) {
544 RB_VM_LOCKING() {
545 redblack_cache_ancestors(new_shape);
546 }
547 }
548 break;
549 case SHAPE_ROOT:
550 rb_bug("Unreachable");
551 break;
552 }
553
554 return new_shape;
555}
556
557static rb_shape_t *
558get_next_shape_internal_atomic(rb_shape_t *shape, rb_id_serial_t serial, enum shape_type shape_type, bool *variation_created, bool new_variations_allowed)
559{
560 rb_shape_t *res = NULL;
561
562 *variation_created = false;
563 VALUE edges_table;
564
565retry:
566 edges_table = RUBY_ATOMIC_VALUE_LOAD(shape->edges);
567
568 // If the current shape has children
569 if (edges_table) {
570 // Check if it only has one child
571 if (SINGLE_CHILD_P(edges_table)) {
572 rb_shape_t *child = SINGLE_CHILD(edges_table);
573 // If the one child has a matching edge name, then great,
574 // we found what we want.
575 if (child->edge_serial == serial) {
576 res = child;
577 }
578 }
579 else {
580 // If it has more than one child, do a hash lookup to find it.
581 VALUE lookup_result;
582 if (rb_managed_id_table_lookup_serial(edges_table, serial, &lookup_result)) {
583 res = (rb_shape_t *)lookup_result;
584 }
585 }
586 }
587
588 // If we didn't find the shape we're looking for and we're allowed more variations we create it.
589 if (!res && new_variations_allowed) {
590 VALUE new_edges = 0;
591
592 rb_shape_t *new_shape = rb_shape_alloc_new_child(serial, shape, shape_type);
593
594 // If we're out of shapes, return NULL
595 if (new_shape) {
596 if (!edges_table) {
597 // If the shape had no edge yet, we can directly set the new child
598 new_edges = TAG_SINGLE_CHILD(new_shape);
599 }
600 else {
601 // If the edge was single child we need to allocate a table.
602 if (SINGLE_CHILD_P(edges_table)) {
603 rb_shape_t *old_child = SINGLE_CHILD(edges_table);
604 new_edges = rb_managed_id_table_new(2);
605 rb_managed_id_table_insert(new_edges, rb_id_serial_to_id(old_child->edge_serial), (VALUE)old_child);
606 }
607 else {
608 new_edges = rb_managed_id_table_dup(edges_table);
609 }
610
611 rb_managed_id_table_insert(new_edges, rb_id_serial_to_id(new_shape->edge_serial), (VALUE)new_shape);
612 *variation_created = true;
613 }
614
615 if (edges_table != RUBY_ATOMIC_VALUE_CAS(shape->edges, edges_table, new_edges)) {
616 // Another thread updated the table;
617 goto retry;
618 }
619 RB_OBJ_WRITTEN(shape_tree_obj, Qundef, new_edges);
620 res = new_shape;
621 RB_GC_GUARD(new_edges);
622 }
623 }
624
625 return res;
626}
627
628static rb_shape_t *
629get_next_shape_internal(rb_shape_t *shape, rb_id_serial_t serial, enum shape_type shape_type, bool *variation_created, bool new_variations_allowed)
630{
631 if (rb_multi_ractor_p()) {
632 return get_next_shape_internal_atomic(shape, serial, shape_type, variation_created, new_variations_allowed);
633 }
634
635 rb_shape_t *res = NULL;
636 *variation_created = false;
637
638 VALUE edges_table = shape->edges;
639
640 // If the current shape has children
641 if (edges_table) {
642 // Check if it only has one child
643 if (SINGLE_CHILD_P(edges_table)) {
644 rb_shape_t *child = SINGLE_CHILD(edges_table);
645 // If the one child has a matching edge name, then great,
646 // we found what we want.
647 if (child->edge_serial == serial) {
648 res = child;
649 }
650 }
651 else {
652 // If it has more than one child, do a hash lookup to find it.
653 VALUE lookup_result;
654 if (rb_managed_id_table_lookup_serial(edges_table, serial, &lookup_result)) {
655 res = (rb_shape_t *)lookup_result;
656 }
657 }
658 }
659
660 // If we didn't find the shape we're looking for we create it.
661 if (!res) {
662 // If we're not allowed to create a new variation, of if we're out of shapes
663 // we return COMPLEX_SHAPE.
664 if (!new_variations_allowed || rb_shapes_count() > MAX_SHAPE_ID) {
665 res = NULL;
666 }
667 else {
668 rb_shape_t *new_shape = rb_shape_alloc_new_child(serial, shape, shape_type);
669
670 if (!edges_table) {
671 // If the shape had no edge yet, we can directly set the new child
672 shape->edges = TAG_SINGLE_CHILD(new_shape);
673 }
674 else {
675 // If the edge was single child we need to allocate a table.
676 if (SINGLE_CHILD_P(edges_table)) {
677 rb_shape_t *old_child = SINGLE_CHILD(edges_table);
678 VALUE new_edges = rb_managed_id_table_new(2);
679 rb_managed_id_table_insert_serial(new_edges, old_child->edge_serial, (VALUE)old_child);
680 RB_OBJ_WRITE(shape_tree_obj, &shape->edges, new_edges);
681 }
682
683 rb_managed_id_table_insert_serial(shape->edges, new_shape->edge_serial, (VALUE)new_shape);
684 *variation_created = true;
685 }
686
687 res = new_shape;
688 }
689 }
690
691 return res;
692}
693
694shape_id_t
695rb_shape_transition_object_id(shape_id_t original_shape_id)
696{
697 RUBY_ASSERT(!rb_shape_has_object_id(original_shape_id));
698
699 rb_shape_t *original_shape = RSHAPE(original_shape_id);
700
701 bool dont_care;
702 rb_shape_t *shape = NULL;
703 if (LIKELY(original_shape->next_field_index < rb_shape_max_capacity())) {
704 shape = get_next_shape_internal(original_shape, rb_shape_tree.serial_object_id, SHAPE_OBJ_ID, &dont_care, true);
705 }
706 if (!shape) {
707 return rb_shape_layout(original_shape_id) | ROOT_COMPLEX_WITH_OBJ_ID | RSHAPE_FLAGS(original_shape_id);
708 }
709
710 RUBY_ASSERT(shape);
711 return SHAPE_ID(shape, original_shape_id) | SHAPE_ID_FL_HAS_OBJECT_ID;
712}
713
714bool
715rb_shape_object_id_child_exists_p(shape_id_t shape_id)
716{
717 RUBY_ASSERT(!rb_shape_has_object_id(shape_id));
718
719 bool dont_care;
720 return get_next_shape_internal(RSHAPE(shape_id), rb_shape_tree.serial_object_id, SHAPE_OBJ_ID, &dont_care, false) != NULL;
721}
722
723shape_id_t
724rb_shape_object_id(shape_id_t original_shape_id)
725{
726 RUBY_ASSERT(rb_shape_has_object_id(original_shape_id));
727
728 rb_shape_t *shape = RSHAPE(original_shape_id);
729 while (shape->type != SHAPE_OBJ_ID) {
730 if (UNLIKELY(shape->parent_offset == INVALID_SHAPE_ID)) {
731 rb_bug("Missing object_id in shape tree");
732 }
733 shape = RSHAPE(shape->parent_offset);
734 }
735
736 return SHAPE_ID(shape, original_shape_id) | SHAPE_ID_FL_HAS_OBJECT_ID;
737}
738
739#if RUBY_DEBUG
740static bool
741shape_get_iv_index(rb_shape_t *shape, rb_id_serial_t serial, attr_index_t *value)
742{
743 while (shape->parent_offset != INVALID_SHAPE_ID) {
744 if (shape->edge_serial == serial) {
745 enum shape_type shape_type;
746 shape_type = (enum shape_type)shape->type;
747
748 switch (shape_type) {
749 case SHAPE_IVAR:
750 RUBY_ASSERT(shape->next_field_index > 0);
751 *value = shape->next_field_index - 1;
752 return true;
753 case SHAPE_ROOT:
754 return false;
755 case SHAPE_OBJ_ID:
756 rb_bug("Ivar should not exist on transition");
757 }
758 }
759
760 shape = RSHAPE(shape->parent_offset);
761 }
762
763 return false;
764}
765#endif
766
767static inline rb_shape_t *
768shape_get_next(rb_shape_t *shape, enum shape_type shape_type, VALUE klass, rb_id_serial_t serial, bool emit_warnings)
769{
770 RUBY_ASSERT(!is_instance_id(rb_id_serial_to_id(serial)) || RTEST(rb_sym2str(ID2SYM(rb_id_serial_to_id(serial)))));
771
772#if RUBY_DEBUG
773 attr_index_t index;
774 if (shape_get_iv_index(shape, serial, &index)) {
775 rb_bug("rb_shape_get_next: trying to create ivar that already exists at index %u", index);
776 }
777#endif
778
779 RUBY_ASSERT(SHAPE_ID_CAPACITY_MAX > 0);
780 RUBY_ASSERT(rb_shape_max_capacity() > 0);
781 if (UNLIKELY(shape->next_field_index >= rb_shape_max_capacity())) {
782 return NULL;
783 }
784
785 bool allow_new_shape = RCLASS_VARIATION_COUNT(klass) < SHAPE_MAX_VARIATIONS;
786 bool variation_created = false;
787 rb_shape_t *new_shape = get_next_shape_internal(shape, serial, shape_type, &variation_created, allow_new_shape);
788
789 if (!new_shape) {
790 // We could create a new variation, transitioning to COMPLEX.
791 return NULL;
792 }
793
794 // Check if we should update max_iv_count on the object's class
795 if (new_shape->next_field_index > RCLASS_MAX_IV_COUNT(klass) && !RCLASS_EXPECT_NO_IVAR(klass)) {
796 RCLASS_SET_MAX_IV_COUNT(klass, new_shape->next_field_index);
797 }
798
799 if (variation_created) {
800 RCLASS_VARIATION_COUNT(klass)++;
801
802 if (emit_warnings && rb_warning_category_enabled_p(RB_WARN_CATEGORY_PERFORMANCE)) {
803 if (RCLASS_VARIATION_COUNT(klass) >= SHAPE_MAX_VARIATIONS) {
806 "The class %"PRIsVALUE" reached %d shape variations, instance variables accesses will be slower and memory usage increased.\n"
807 "It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method.",
808 rb_class_path(klass),
809 SHAPE_MAX_VARIATIONS
810 );
811 }
812 }
813 }
814
815 return new_shape;
816}
817
818static VALUE
819obj_get_owner_class(VALUE obj)
820{
821 VALUE klass;
822 if (IMEMO_TYPE_P(obj, imemo_fields)) {
823 VALUE owner = rb_imemo_fields_owner(obj);
824 switch (BUILTIN_TYPE(owner)) {
825 case T_CLASS:
826 case T_MODULE:
827 klass = rb_singleton_class(owner);
828 break;
829 default:
830 klass = rb_obj_class(owner);
831 break;
832 }
833 }
834 else {
835 klass = rb_obj_class(obj);
836 }
837 return klass;
838}
839
840static rb_shape_t *
841remove_shape_recursive(VALUE obj, rb_shape_t *shape, rb_id_serial_t serial, rb_shape_t **removed_shape)
842{
843 if (shape->parent_offset == INVALID_SHAPE_ID) {
844 // We've hit the top of the shape tree and couldn't find the
845 // IV we wanted to remove, so return NULL
846 *removed_shape = NULL;
847 return NULL;
848 }
849 else {
850 if (shape->type == SHAPE_IVAR && shape->edge_serial == serial) {
851 *removed_shape = shape;
852
853 return RSHAPE(shape->parent_offset);
854 }
855 else {
856 // This isn't the IV we want to remove, keep walking up.
857 rb_shape_t *new_parent = remove_shape_recursive(obj, RSHAPE(shape->parent_offset), serial, removed_shape);
858
859 // We found a new parent. Create a child of the new parent that
860 // has the same attributes as this shape.
861 if (new_parent) {
862 VALUE klass = obj_get_owner_class(obj);
863 rb_shape_t *new_child = shape_get_next(new_parent, shape->type, klass, shape->edge_serial, true);
864 RUBY_ASSERT(!new_child || new_child->capacity <= shape->capacity);
865 return new_child;
866 }
867 else {
868 // We went all the way to the top of the shape tree and couldn't
869 // find an IV to remove so return NULL.
870 return NULL;
871 }
872 }
873 }
874}
875
876shape_id_t
877rb_obj_shape_transition_remove_ivar(VALUE obj, ID id, shape_id_t *removed_shape_id)
878{
879 shape_id_t original_shape_id = RBASIC_SHAPE_ID(obj);
880 RUBY_ASSERT(!rb_shape_frozen_p(original_shape_id));
881
882 if (rb_shape_complex_p(original_shape_id)) {
883 return original_shape_id;
884 }
885
886 rb_shape_t *removed_shape = NULL;
887 rb_shape_t *new_shape = remove_shape_recursive(obj, RSHAPE(original_shape_id), rb_id_to_serial(id), &removed_shape);
888
889 if (removed_shape) {
890 *removed_shape_id = SHAPE_OFFSET(removed_shape);
891 }
892
893 if (new_shape) {
894 return SHAPE_ID(new_shape, original_shape_id);
895 }
896 else if (removed_shape) {
897 // We found the shape to remove, but couldn't create a new variation.
898 // We must transition to COMPLEX.
899 shape_id_t next_shape_id = rb_shape_transition_complex(original_shape_id);
900 RUBY_ASSERT(rb_shape_has_object_id(next_shape_id) == rb_shape_has_object_id(original_shape_id));
901 return next_shape_id;
902 }
903 return original_shape_id;
904}
905
906shape_id_t
907rb_obj_shape_transition_add_ivar(VALUE obj, ID id)
908{
909 shape_id_t original_shape_id = RBASIC_SHAPE_ID(obj);
910 RUBY_ASSERT(!rb_shape_frozen_p(original_shape_id));
911
912 VALUE klass = obj_get_owner_class(obj);
913 rb_shape_t *next_shape = shape_get_next(RSHAPE(original_shape_id), SHAPE_IVAR, klass, rb_id_to_serial(id), true);
914 if (next_shape) {
915 return SHAPE_ID(next_shape, original_shape_id);
916 }
917 else {
918 return rb_shape_transition_complex(original_shape_id);
919 }
920}
921
922shape_id_t
923rb_shape_transition_add_ivar_no_warnings(shape_id_t original_shape_id, ID id, VALUE klass)
924{
925 RUBY_ASSERT(!rb_shape_frozen_p(original_shape_id));
926
927 rb_shape_t *next_shape = shape_get_next(RSHAPE(original_shape_id), SHAPE_IVAR, klass, rb_id_to_serial(id), false);
928 if (next_shape) {
929 return SHAPE_ID(next_shape, original_shape_id);
930 }
931 else {
932 return rb_shape_transition_complex(original_shape_id);
933 }
934}
935
936// Same as rb_shape_get_iv_index, but uses a provided valid shape id and index
937// to return a result faster if branches of the shape tree are closely related.
938bool
939rb_shape_get_iv_index_with_hint(shape_id_t shape_id, ID id, attr_index_t *value, shape_id_t *shape_id_hint)
940{
941 attr_index_t index_hint = *value;
942
943 if (*shape_id_hint == INVALID_SHAPE_ID) {
944 *shape_id_hint = shape_id;
945 return rb_shape_get_iv_index(shape_id, id, value);
946 }
947
948 rb_shape_t *shape = RSHAPE(shape_id);
949 rb_shape_t *initial_shape = shape;
950 rb_shape_t *shape_hint = RSHAPE(*shape_id_hint);
951
952 // We assume it's likely shape_id_hint and shape_id have a close common
953 // ancestor, so we check up to ANCESTOR_SEARCH_MAX_DEPTH ancestors before
954 // eventually using the index, as in case of a match it will be faster.
955 // However if the shape doesn't have an index, we walk the entire tree.
956 int depth = INT_MAX;
957 if (shape->ancestor_index && shape->next_field_index >= ANCESTOR_CACHE_THRESHOLD) {
958 depth = ANCESTOR_SEARCH_MAX_DEPTH;
959 }
960
961 rb_id_serial_t serial = rb_id_to_serial(id);
962 while (depth > 0 && shape->next_field_index > index_hint) {
963 while (shape_hint->next_field_index > shape->next_field_index) {
964 shape_hint = RSHAPE(shape_hint->parent_offset);
965 }
966
967 if (shape_hint == shape) {
968 // We've found a common ancestor so use the index hint
969 *value = index_hint;
970 *shape_id_hint = SHAPE_OFFSET(shape);
971 return true;
972 }
973 if (shape->edge_serial == serial) {
974 // We found the matching id before a common ancestor
975 *value = shape->next_field_index - 1;
976 *shape_id_hint = SHAPE_OFFSET(shape);
977 return true;
978 }
979
980 shape = RSHAPE(shape->parent_offset);
981 depth--;
982 }
983
984 // If the original shape had an index but its ancestor doesn't
985 // we switch back to the original one as it will be faster.
986 if (!shape->ancestor_index && initial_shape->ancestor_index) {
987 shape = initial_shape;
988 }
989 *shape_id_hint = shape_id;
990 return rb_shape_get_iv_index(SHAPE_ID(shape, shape_id), id, value);
991}
992
993static bool
994shape_cache_find_ivar(rb_shape_t *shape, rb_id_serial_t serial, rb_shape_t **ivar_shape)
995{
996 if (shape->ancestor_index && shape->next_field_index >= ANCESTOR_CACHE_THRESHOLD) {
997 redblack_node_t *node = redblack_find(shape->ancestor_index, serial);
998 if (node) {
999 *ivar_shape = RSHAPE(redblack_value(node));
1000
1001 return true;
1002 }
1003 }
1004
1005 return false;
1006}
1007
1008static bool
1009shape_find_ivar(rb_shape_t *shape, rb_id_serial_t serial, rb_shape_t **ivar_shape)
1010{
1011 while (shape->parent_offset != INVALID_SHAPE_ID) {
1012 if (shape->edge_serial == serial) {
1013 RUBY_ASSERT(shape->type == SHAPE_IVAR);
1014 *ivar_shape = shape;
1015 return true;
1016 }
1017
1018 shape = RSHAPE(shape->parent_offset);
1019 }
1020
1021 return false;
1022}
1023
1024bool
1025rb_shape_find_ivar(shape_id_t current_shape_id, ID id, shape_id_t *ivar_shape_id)
1026{
1027 RUBY_ASSERT(!rb_shape_complex_p(current_shape_id));
1028
1029 rb_shape_t *shape = RSHAPE(current_shape_id);
1030 rb_shape_t *ivar_shape;
1031 rb_id_serial_t serial = rb_id_to_serial(id);
1032
1033 if (!shape_cache_find_ivar(shape, serial, &ivar_shape)) {
1034 // If it wasn't in the ancestor cache, then don't do a linear search
1035 if (shape->ancestor_index && shape->next_field_index >= ANCESTOR_CACHE_THRESHOLD) {
1036 return false;
1037 }
1038 else {
1039 if (!shape_find_ivar(shape, serial, &ivar_shape)) {
1040 return false;
1041 }
1042 }
1043 }
1044
1045 *ivar_shape_id = SHAPE_ID(ivar_shape, current_shape_id);
1046
1047 return true;
1048}
1049
1050bool
1051rb_shape_get_iv_index(shape_id_t shape_id, ID id, attr_index_t *value)
1052{
1053 // It doesn't make sense to ask for the index of an IV that's stored
1054 // on an object that is "too complex" as it uses a hash for storing IVs
1055 RUBY_ASSERT(!rb_shape_complex_p(shape_id));
1056
1057 shape_id_t ivar_shape_id;
1058 if (rb_shape_find_ivar(shape_id, id, &ivar_shape_id)) {
1059 *value = RSHAPE_INDEX(ivar_shape_id);
1060 return true;
1061 }
1062 return false;
1063}
1064
1065int32_t
1066rb_shape_id_offset(void)
1067{
1068 return sizeof(uintptr_t) - SHAPE_ID_NUM_BITS / sizeof(uintptr_t);
1069}
1070
1071// Rebuild a similar shape with the same ivars but without "non-canonical"
1072// edges such as SHAPE_OBJ_ID.
1073static rb_shape_t *
1074shape_rebuild(rb_shape_t *initial_shape, rb_shape_t *dest_shape)
1075{
1076 rb_shape_t *midway_shape;
1077
1078 RUBY_ASSERT(initial_shape->type == SHAPE_ROOT);
1079
1080 if (dest_shape->type != initial_shape->type) {
1081 midway_shape = shape_rebuild(initial_shape, RSHAPE(dest_shape->parent_offset));
1082 if (UNLIKELY(!midway_shape)) {
1083 return NULL;
1084 }
1085 }
1086 else {
1087 midway_shape = initial_shape;
1088 }
1089
1090 switch ((enum shape_type)dest_shape->type) {
1091 case SHAPE_IVAR: {
1092 bool dont_care;
1093 midway_shape = get_next_shape_internal(midway_shape, dest_shape->edge_serial, SHAPE_IVAR, &dont_care, true);
1094 break;
1095 }
1096 case SHAPE_OBJ_ID:
1097 case SHAPE_ROOT:
1098 break;
1099 }
1100
1101 return midway_shape;
1102}
1103
1104// Rebuild `dest_shape_id` starting from `initial_shape_id`, and keep only SHAPE_IVAR transitions.
1105// SHAPE_OBJ_ID and frozen status are lost.
1106shape_id_t
1107rb_shape_rebuild(shape_id_t initial_shape_id, shape_id_t dest_shape_id)
1108{
1109 RUBY_ASSERT(RSHAPE_TYPE_P(initial_shape_id, SHAPE_ROOT));
1110
1111 if (RB_UNLIKELY(rb_shape_complex_p(initial_shape_id))) {
1112 // The class has been marked as too complex.
1113 return initial_shape_id;
1114 }
1115
1116 if (RB_UNLIKELY(rb_shape_complex_p(dest_shape_id))) {
1117 return rb_shape_transition_complex(initial_shape_id);
1118 }
1119
1120 shape_id_t next_shape_id;
1121 // The shape has a SHAPE_OBJ_ID edge, it needs to be rebuilt.
1122 if (dest_shape_id & SHAPE_ID_FL_HAS_OBJECT_ID) {
1123 rb_shape_t *next_shape = shape_rebuild(RSHAPE(initial_shape_id), RSHAPE(dest_shape_id));
1124 if (next_shape) {
1125 next_shape_id = SHAPE_ID(next_shape, initial_shape_id & ~SHAPE_ID_FL_NON_CANONICAL_MASK);
1126 }
1127 else {
1128 return rb_shape_transition_complex(initial_shape_id | (dest_shape_id & ~SHAPE_ID_FL_NON_CANONICAL_MASK));
1129 }
1130 }
1131 else {
1132 // Happy path, we have nothing to do other than change the flags.
1133 next_shape_id = RSHAPE_OFFSET(dest_shape_id) | RSHAPE_FLAGS(initial_shape_id);
1134 }
1135 return next_shape_id;
1136}
1137
1138void
1139rb_shape_copy_fields(VALUE dest, VALUE *dest_buf, shape_id_t dest_shape_id, VALUE *src_buf, shape_id_t src_shape_id)
1140{
1141 rb_shape_t *dest_shape = RSHAPE(dest_shape_id);
1142 rb_shape_t *src_shape = RSHAPE(src_shape_id);
1143
1144 if (src_shape->next_field_index == dest_shape->next_field_index) {
1145 // Happy path, we can just memcpy the ivptr content
1146 MEMCPY(dest_buf, src_buf, VALUE, dest_shape->next_field_index);
1147
1148 // Fire write barriers
1149 for (uint32_t i = 0; i < dest_shape->next_field_index; i++) {
1150 RB_OBJ_WRITTEN(dest, Qundef, dest_buf[i]);
1151 }
1152 }
1153 else {
1154 while (src_shape->parent_offset != INVALID_SHAPE_ID) {
1155 if (src_shape->type == SHAPE_IVAR) {
1156 while (dest_shape->edge_serial != src_shape->edge_serial) {
1157 if (UNLIKELY(dest_shape->parent_offset == INVALID_SHAPE_ID)) {
1158 rb_bug("Lost field %s", rb_id2name(rb_id_serial_to_id(src_shape->edge_serial)));
1159 }
1160 dest_shape = RSHAPE(dest_shape->parent_offset);
1161 }
1162
1163 RB_OBJ_WRITE(dest, &dest_buf[dest_shape->next_field_index - 1], src_buf[src_shape->next_field_index - 1]);
1164 }
1165 src_shape = RSHAPE(src_shape->parent_offset);
1166 }
1167 }
1168}
1169
1170size_t
1171rb_shape_edges_count(shape_id_t shape_id)
1172{
1173 rb_shape_t *shape = RSHAPE(shape_id);
1174 if (shape->edges) {
1175 if (SINGLE_CHILD_P(shape->edges)) {
1176 return 1;
1177 }
1178 else {
1179 return rb_managed_id_table_size(shape->edges);
1180 }
1181 }
1182 return 0;
1183}
1184
1185size_t
1186rb_shape_memsize(shape_id_t shape_id)
1187{
1188 rb_shape_t *shape = RSHAPE(shape_id);
1189
1190 size_t memsize = sizeof(rb_shape_t);
1191 if (shape->edges && !SINGLE_CHILD_P(shape->edges)) {
1192 memsize += rb_managed_id_table_size(shape->edges);
1193 }
1194 return memsize;
1195}
1196
1197bool
1198rb_shape_foreach_field(shape_id_t initial_shape_id, rb_shape_foreach_transition_callback func, void *data)
1199{
1200 RUBY_ASSERT(!rb_shape_complex_p(initial_shape_id));
1201
1202 rb_shape_t *shape = RSHAPE(initial_shape_id);
1203 if (shape->type == SHAPE_ROOT) {
1204 return true;
1205 }
1206
1207 shape_id_t parent_offset = SHAPE_ID(RSHAPE(shape->parent_offset), initial_shape_id);
1208 if (rb_shape_foreach_field(parent_offset, func, data)) {
1209 switch (func(SHAPE_ID(shape, initial_shape_id), data)) {
1210 case ST_STOP:
1211 return false;
1212 case ST_CHECK:
1213 case ST_CONTINUE:
1214 break;
1215 default:
1216 rb_bug("unreachable");
1217 }
1218 }
1219 return true;
1220}
1221
1222#if RUBY_DEBUG
1223/*
1224 * Get the layout of this object. The "layout" indicates what strategy
1225 * we should use for fetching instance variables from `obj`. It's based
1226 * on the C struct layout for each particular object.
1227 *
1228 * TODO: make Struct have a similar layout to RDATA
1229 */
1230static shape_id_t
1231rb_shape_expected_layout(VALUE obj)
1232{
1233 switch (BUILTIN_TYPE(obj)) {
1234 case T_OBJECT: {
1235 return SHAPE_ID_LAYOUT_ROBJECT;
1236 }
1237
1238 case T_CLASS:
1239 case T_MODULE:
1240 if (FL_TEST_RAW(obj, RCLASS_BOXABLE)) {
1241 return SHAPE_ID_LAYOUT_OTHER;
1242 }
1243 return SHAPE_ID_LAYOUT_RCLASS;
1244
1245 case T_STRUCT:
1246 case T_DATA:
1247 return SHAPE_ID_LAYOUT_EXTENDED;
1248
1249 case T_IMEMO:
1250 if (IMEMO_TYPE_P(obj, imemo_fields)) {
1251 return SHAPE_ID_LAYOUT_ROBJECT;
1252 }
1253 return SHAPE_ID_LAYOUT_OTHER;
1254
1255 default:
1256 return SHAPE_ID_LAYOUT_OTHER;
1257 }
1258}
1259
1260static const char *
1261shape_layout_name(shape_id_t shape_id)
1262{
1263 switch (rb_shape_layout(shape_id)) {
1264 case SHAPE_ID_LAYOUT_ROBJECT:
1265 return "robject";
1266 case SHAPE_ID_LAYOUT_RCLASS:
1267 return "rclass";
1268 case SHAPE_ID_LAYOUT_EXTENDED:
1269 return "extended (or RData)";
1270 case SHAPE_ID_LAYOUT_OTHER:
1271 return "other";
1272 default:
1273 return "invalid";
1274 }
1275}
1276
1277bool
1278rb_shape_verify_capacity_consistency_p(VALUE obj)
1279{
1280 switch (BUILTIN_TYPE(obj)) {
1281 case T_IMEMO:
1282 return IMEMO_TYPE_P(obj, imemo_fields);
1283 case T_STRING:
1284 return !FL_TEST_RAW(obj, FL_USER19); // STR_FAKESTR
1285 case T_ARRAY:
1286 return !FL_TEST_RAW(obj, RARRAY_FAKEARY);
1287 default:
1288 return true;
1289 }
1290}
1291
1292bool
1293rb_shape_verify_consistency(VALUE obj, shape_id_t shape_id)
1294{
1295 if (shape_id == INVALID_SHAPE_ID) {
1296 rb_bug("Can't set INVALID_SHAPE_ID on an object");
1297 }
1298
1299 shape_id_t actual_layout = rb_shape_layout(rb_obj_shape_id(obj));
1300 shape_id_t expected_layout = rb_shape_expected_layout(obj);
1301 if (actual_layout != expected_layout) {
1302 if (!(RB_TYPE_P(obj, T_OBJECT) && actual_layout == SHAPE_ID_LAYOUT_EXTENDED)) {
1303 rb_bug("shape_id layout mismatch: expected=%s actual=%s shape_id=%u obj=%s",
1304 shape_layout_name(expected_layout), shape_layout_name(actual_layout), shape_id, rb_obj_info(obj));
1305 }
1306 }
1307
1308 if (shape_id == ROOT_SHAPE_ID) {
1309 return true;
1310 }
1311
1312 rb_shape_t *shape = RSHAPE(shape_id);
1313
1314 // Make sure SHAPE_ID_HAS_IVAR_MASK is valid.
1315 if (rb_shape_complex_p(shape_id)) {
1316 RUBY_ASSERT(shape_id & SHAPE_ID_HAS_IVAR_MASK);
1317
1318 // Ensure complex object don't appear as embedded
1319 if (RB_TYPE_P(obj, T_OBJECT)) {
1320 RUBY_ASSERT(rb_obj_shape_extended_p(obj));
1321 }
1322 else if (IMEMO_TYPE_P(obj, imemo_fields)) {
1323 RUBY_ASSERT(rb_obj_shape_embedded_p(obj));
1324 }
1325 }
1326 else {
1327 bool has_object_id = false;
1328 while (shape->parent_offset != INVALID_SHAPE_ID) {
1329 if (shape->type == SHAPE_OBJ_ID) {
1330 has_object_id = true;
1331 break;
1332 }
1333 shape = RSHAPE(shape->parent_offset);
1334 }
1335
1336 if (rb_shape_has_object_id(shape_id)) {
1337 if (!has_object_id) {
1338 rb_bug("shape_id claim having obj_id but doesn't shape_id=%u, obj=%s", shape_id, rb_obj_info(obj));
1339 }
1340 }
1341 else {
1342 if (has_object_id) {
1343 rb_bug("shape_id claim not having obj_id but it does shape_id=%u, obj=%s", shape_id, rb_obj_info(obj));
1344 }
1345 }
1346
1347 attr_index_t ivar_count = RSHAPE_LEN(shape_id);
1348 if (has_object_id) {
1349 ivar_count--;
1350 }
1351 if (ivar_count) {
1352 RUBY_ASSERT(shape_id & SHAPE_ID_HAS_IVAR_MASK);
1353 }
1354 else {
1355 RUBY_ASSERT(!(shape_id & SHAPE_ID_HAS_IVAR_MASK));
1356 }
1357 }
1358
1359 if (rb_shape_verify_capacity_consistency_p(obj)) {
1360 attr_index_t shape_id_capacity = rb_shape_embedded_capacity(shape_id);
1361
1362 size_t shape_id_slot_size = shape_id_capacity * sizeof(VALUE) + sizeof(struct RBasic);
1363 size_t actual_slot_size = rb_gc_obj_slot_size(obj);
1364
1365 if (shape_id_capacity == SHAPE_ID_CAPACITY_MAX) {
1366 if (actual_slot_size < SHAPE_ID_CAPACITY_MAX) {
1367 rb_bug("shape_id_capacity is SHAPE_ID_CAPACITY_MAX, but actual slot size is only %zu", actual_slot_size);
1368 }
1369 }
1370 else {
1371 if (shape_id_slot_size != actual_slot_size) {
1372 rb_bug("shape_id capacity flags mismatch: shape_id_slot_size=%zu, gc_slot_size=%zu\n", shape_id_slot_size, actual_slot_size);
1373 }
1374 }
1375 }
1376
1377 return true;
1378}
1379#endif
1380
1381#if SHAPE_DEBUG
1382
1383/*
1384 * Exposing Shape to Ruby via RubyVM::Shape.of(object)
1385 */
1386
1387static VALUE
1388shape_complex(VALUE self)
1389{
1390 shape_id_t shape_id = NUM2INT(rb_struct_getmember(self, rb_intern("id")));
1391 return RBOOL(rb_shape_complex_p(shape_id));
1392}
1393
1394static VALUE
1395shape_frozen(VALUE self)
1396{
1397 shape_id_t shape_id = NUM2INT(rb_struct_getmember(self, rb_intern("id")));
1398 return RBOOL(shape_id & SHAPE_ID_FL_FROZEN);
1399}
1400
1401static VALUE
1402shape_has_object_id_p(VALUE self)
1403{
1404 shape_id_t shape_id = NUM2INT(rb_struct_getmember(self, rb_intern("id")));
1405 return RBOOL(rb_shape_has_object_id(shape_id));
1406}
1407
1408static VALUE
1409shape_layout(VALUE self)
1410{
1411 shape_id_t shape_id = NUM2UINT(rb_struct_getmember(self, rb_intern("id")));
1412
1413 switch (rb_shape_layout(shape_id)) {
1414 case SHAPE_ID_LAYOUT_ROBJECT:
1415 return ID2SYM(rb_intern("robject"));
1416 case SHAPE_ID_LAYOUT_RCLASS:
1417 return ID2SYM(rb_intern("rclass"));
1418 case SHAPE_ID_LAYOUT_EXTENDED:
1419 return ID2SYM(rb_intern("extended_or_rdata"));
1420 case SHAPE_ID_LAYOUT_OTHER:
1421 return ID2SYM(rb_intern("other"));
1422 default:
1423 rb_bug("unknown shape layout: %u", rb_shape_layout(shape_id));
1424 }
1425}
1426
1427static VALUE
1428parse_key(ID key)
1429{
1430 if (is_instance_id(key)) {
1431 return ID2SYM(key);
1432 }
1433 return LONG2NUM(key);
1434}
1435
1436static VALUE rb_shape_edge_name(rb_shape_t *shape);
1437
1438static VALUE
1439shape_id_t_to_rb_cShape(shape_id_t shape_id)
1440{
1441 VALUE rb_cShape = rb_const_get(rb_cRubyVM, rb_intern("Shape"));
1442 rb_shape_t *shape = RSHAPE(shape_id);
1443
1444 VALUE obj = rb_struct_new(rb_cShape,
1445 INT2NUM(shape_id),
1446 INT2NUM(RSHAPE_OFFSET(shape_id)),
1447 INT2NUM(shape->parent_offset),
1448 rb_shape_edge_name(shape),
1449 INT2NUM(shape->next_field_index),
1450 INT2NUM(rb_shape_embedded_capacity(shape_id)),
1451 INT2NUM(shape->type),
1452 INT2NUM(RSHAPE_CAPACITY(shape_id)));
1453 rb_obj_freeze(obj);
1454 return obj;
1455}
1456
1457static enum rb_id_table_iterator_result
1458rb_edges_to_hash(ID key, VALUE value, void *ref)
1459{
1460 rb_hash_aset(*(VALUE *)ref, parse_key(key), shape_id_t_to_rb_cShape(SHAPE_OFFSET((rb_shape_t *)value)));
1461 return ID_TABLE_CONTINUE;
1462}
1463
1464static VALUE
1465rb_shape_edges(VALUE self)
1466{
1467 rb_shape_t *shape = RSHAPE(NUM2INT(rb_struct_getmember(self, rb_intern("id"))));
1468
1469 VALUE hash = rb_hash_new();
1470
1471 if (shape->edges) {
1472 if (SINGLE_CHILD_P(shape->edges)) {
1473 rb_shape_t *child = SINGLE_CHILD(shape->edges);
1474 rb_edges_to_hash(rb_id_serial_to_id(child->edge_serial), (VALUE)child, &hash);
1475 }
1476 else {
1477 VALUE edges = shape->edges;
1478 rb_managed_id_table_foreach(edges, rb_edges_to_hash, &hash);
1479 RB_GC_GUARD(edges);
1480 }
1481 }
1482
1483 return hash;
1484}
1485
1486static VALUE
1487rb_shape_edge_name(rb_shape_t *shape)
1488{
1489 if (shape->edge_serial) {
1490 ID id = rb_id_serial_to_id(shape->edge_serial);
1491 if (is_instance_id(id)) {
1492 return ID2SYM(id);
1493 }
1494 return INT2NUM(shape->capacity);
1495 }
1496 return Qnil;
1497}
1498
1499static VALUE
1500rb_shape_export_depth(VALUE self)
1501{
1502 shape_id_t shape_id = NUM2INT(rb_struct_getmember(self, rb_intern("id")));
1503 return SIZET2NUM(rb_shape_depth(shape_id));
1504}
1505
1506static VALUE
1507rb_shape_parent(VALUE self)
1508{
1509 rb_shape_t *shape;
1510 shape = RSHAPE(NUM2INT(rb_struct_getmember(self, rb_intern("id"))));
1511 if (shape->parent_offset != INVALID_SHAPE_ID) {
1512 return shape_id_t_to_rb_cShape(shape->parent_offset);
1513 }
1514 else {
1515 return Qnil;
1516 }
1517}
1518
1519static VALUE
1520rb_shape_debug_shape(VALUE self, VALUE obj)
1521{
1522 if (RB_SPECIAL_CONST_P(obj)) {
1523 rb_raise(rb_eArgError, "Can't get shape of special constant");
1524 }
1525 return shape_id_t_to_rb_cShape(rb_obj_shape_id(obj));
1526}
1527
1528static VALUE
1529rb_shape_root_shape(VALUE self)
1530{
1531 return shape_id_t_to_rb_cShape(ROOT_SHAPE_ID);
1532}
1533
1534static VALUE
1535rb_shape_shapes_available(VALUE self)
1536{
1537 return ULL2NUM(MAX_SHAPE_ID - (rb_shapes_count() - 1));
1538}
1539
1540static VALUE
1541rb_shape_exhaust(int argc, VALUE *argv, VALUE self)
1542{
1543 rb_check_arity(argc, 0, 1);
1544 int offset = argc == 1 ? NUM2INT(argv[0]) : 0;
1545 RUBY_ATOMIC_SET(shape_next_id, MAX_SHAPE_ID - offset + 1);
1546 return Qnil;
1547}
1548
1549static VALUE
1550rb_shape_class_max_iv_count(VALUE self, VALUE klass)
1551{
1552 return INT2NUM(RCLASS_MAX_IV_COUNT(klass));
1553}
1554
1555static VALUE shape_to_h(rb_shape_t *shape);
1556
1557static enum rb_id_table_iterator_result collect_keys_and_values(ID key, VALUE value, void *ref)
1558{
1559 rb_hash_aset(*(VALUE *)ref, parse_key(key), shape_to_h((rb_shape_t *)value));
1560 return ID_TABLE_CONTINUE;
1561}
1562
1563static VALUE edges(VALUE edges)
1564{
1565 VALUE hash = rb_hash_new();
1566 if (edges) {
1567 if (SINGLE_CHILD_P(edges)) {
1568 rb_shape_t *child = SINGLE_CHILD(edges);
1569 collect_keys_and_values(rb_id_serial_to_id(child->edge_serial), (VALUE)child, &hash);
1570 }
1571 else {
1572 rb_managed_id_table_foreach(edges, collect_keys_and_values, &hash);
1573 }
1574 }
1575 return hash;
1576}
1577
1578static VALUE
1579shape_to_h(rb_shape_t *shape)
1580{
1582
1583 rb_hash_aset(rb_shape, ID2SYM(rb_intern("id")), INT2NUM(SHAPE_OFFSET(shape)));
1584 rb_hash_aset(rb_shape, ID2SYM(rb_intern("edges")), edges(shape->edges));
1585
1586 if (shape == rb_shape_get_root_shape()) {
1587 rb_hash_aset(rb_shape, ID2SYM(rb_intern("parent_offset")), INT2NUM(ROOT_SHAPE_ID));
1588 }
1589 else {
1590 rb_hash_aset(rb_shape, ID2SYM(rb_intern("parent_offset")), INT2NUM(shape->parent_offset));
1591 }
1592
1593 rb_hash_aset(rb_shape, ID2SYM(rb_intern("edge_name")), rb_id2str(rb_id_serial_to_id(shape->edge_serial)));
1594 return rb_shape;
1595}
1596
1597static VALUE
1598shape_transition_tree(VALUE self)
1599{
1600 return shape_to_h(rb_shape_get_root_shape());
1601}
1602
1603static VALUE
1604rb_shape_find_by_id(VALUE mod, VALUE id)
1605{
1606 shape_id_t shape_id = NUM2UINT(id);
1607 if (shape_id >= rb_shapes_count()) {
1608 rb_raise(rb_eArgError, "Shape ID %d is out of bounds\n", shape_id);
1609 }
1610 return shape_id_t_to_rb_cShape(shape_id);
1611}
1612#endif
1613
1614#ifdef HAVE_MMAP
1615#include <sys/mman.h>
1616#endif
1617
1618void
1619Init_default_shapes(void)
1620{
1621 attr_index_t max_capacity = (attr_index_t)((rb_gc_max_allocation_size() - sizeof(struct RBasic)) / sizeof(VALUE));
1622 if (max_capacity > SHAPE_ID_CAPACITY_MAX) max_capacity = SHAPE_ID_CAPACITY_MAX;
1623 rb_shape_tree.max_capacity = max_capacity;
1624
1625#ifdef HAVE_MMAP
1626 size_t shape_list_mmap_size = rb_size_mul_or_raise(SHAPE_BUFFER_SIZE, sizeof(rb_shape_t), rb_eRuntimeError);
1627 rb_shape_tree.shape_list = (rb_shape_t *)mmap(NULL, shape_list_mmap_size,
1628 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1629 if (rb_shape_tree.shape_list == MAP_FAILED) {
1630 rb_shape_tree.shape_list = 0;
1631 }
1632 else {
1633 ruby_annotate_mmap(rb_shape_tree.shape_list, shape_list_mmap_size, "Ruby:Init_default_shapes:shape_list");
1634 }
1635#else
1636 rb_shape_tree.shape_list = xcalloc(SHAPE_BUFFER_SIZE, sizeof(rb_shape_t));
1637#endif
1638
1639 if (!rb_shape_tree.shape_list) {
1640 rb_memerror();
1641 }
1642
1643 rb_shape_tree.id_object_id = rb_make_internal_id();
1644 rb_shape_tree.serial_object_id = rb_id_to_serial(rb_shape_tree.id_object_id);
1645
1646#ifdef HAVE_MMAP
1647 size_t shape_cache_mmap_size = rb_size_mul_or_raise(REDBLACK_CACHE_SIZE, sizeof(redblack_node_t), rb_eRuntimeError);
1648 redblack_cache = (redblack_node_t *)mmap(NULL, shape_cache_mmap_size,
1649 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1650 redblack_cache_size = 0;
1651
1652 // If mmap fails, then give up on the redblack tree cache.
1653 // We set the cache size such that the redblack node allocators think
1654 // the cache is full.
1655 if (redblack_cache == MAP_FAILED) {
1656 redblack_cache = NULL;
1657 redblack_cache_size = REDBLACK_CACHE_SIZE;
1658 }
1659 else {
1660 ruby_annotate_mmap(redblack_cache, shape_cache_mmap_size, "Ruby:Init_default_shapes:shape_cache");
1661 }
1662#endif
1663
1664 rb_gc_register_address(&shape_tree_obj);
1665
1666 // The shape tree's data lives in the `rb_shape_tree` global variable, so there's no struct to wrap here.
1667 // We can't use NULL, because then the GC would skip the mark/compact callbacks,
1668 // so instead we pass a fake non-NULL pointer that will never be dereferenced.
1669 shape_tree_obj = TypedData_Wrap_Struct(0, &shape_tree_type, (void *)1);
1670
1671 // Root shape
1672 rb_shape_t *root = rb_shape_alloc_with_parent_offset(0, INVALID_SHAPE_ID);
1673 root->capacity = 0;
1674 root->type = SHAPE_ROOT;
1675 RUBY_ASSERT(SHAPE_OFFSET(root) == ROOT_SHAPE_ID);
1676 RUBY_ASSERT(!(SHAPE_OFFSET(root) & SHAPE_ID_HAS_IVAR_MASK));
1677
1678 bool dontcare;
1679 rb_shape_t *root_with_obj_id = get_next_shape_internal(root, rb_shape_tree.serial_object_id, SHAPE_OBJ_ID, &dontcare, true);
1680 RUBY_ASSERT(root_with_obj_id);
1681 RUBY_ASSERT(SHAPE_OFFSET(root_with_obj_id) == ROOT_SHAPE_WITH_OBJ_ID);
1682 RUBY_ASSERT(root_with_obj_id->type == SHAPE_OBJ_ID);
1683 RUBY_ASSERT(root_with_obj_id->edge_serial == rb_shape_tree.serial_object_id);
1684 RUBY_ASSERT(root_with_obj_id->next_field_index == 1);
1685 RUBY_ASSERT(!(SHAPE_OFFSET(root_with_obj_id) & SHAPE_ID_HAS_IVAR_MASK));
1686 (void)root_with_obj_id;
1687}
1688
1689void
1690Init_shape(void)
1691{
1692#if SHAPE_DEBUG
1693 /* Document-class: RubyVM::Shape
1694 * :nodoc: */
1695 VALUE rb_cShape = rb_struct_define_under(rb_cRubyVM, "Shape",
1696 "id",
1697 "offset",
1698 "parent_offset",
1699 "edge_name",
1700 "next_field_index",
1701 "embedded_capacity",
1702 "type",
1703 "capacity",
1704 NULL);
1705
1706 rb_define_method(rb_cShape, "parent", rb_shape_parent, 0);
1707 rb_define_method(rb_cShape, "edges", rb_shape_edges, 0);
1708 rb_define_method(rb_cShape, "depth", rb_shape_export_depth, 0);
1709 rb_define_method(rb_cShape, "complex?", shape_complex, 0);
1710 rb_define_method(rb_cShape, "shape_frozen?", shape_frozen, 0);
1711 rb_define_method(rb_cShape, "has_object_id?", shape_has_object_id_p, 0);
1712 rb_define_method(rb_cShape, "layout", shape_layout, 0);
1713
1714 rb_define_const(rb_cShape, "SHAPE_ROOT", INT2NUM(SHAPE_ROOT));
1715 rb_define_const(rb_cShape, "SHAPE_IVAR", INT2NUM(SHAPE_IVAR));
1716 rb_define_const(rb_cShape, "SHAPE_ID_NUM_BITS", INT2NUM(SHAPE_ID_NUM_BITS));
1717 rb_define_const(rb_cShape, "SHAPE_FLAG_SHIFT", INT2NUM(SHAPE_FLAG_SHIFT));
1718 rb_define_const(rb_cShape, "SHAPE_MAX_VARIATIONS", INT2NUM(SHAPE_MAX_VARIATIONS));
1719 rb_define_const(rb_cShape, "SHAPE_MAX_FIELDS", INT2NUM(rb_shape_max_capacity()));
1720 rb_define_const(rb_cShape, "SIZEOF_RB_SHAPE_T", INT2NUM(sizeof(rb_shape_t)));
1721 rb_define_const(rb_cShape, "SIZEOF_REDBLACK_NODE_T", INT2NUM(sizeof(redblack_node_t)));
1722 rb_define_const(rb_cShape, "SHAPE_BUFFER_SIZE", INT2NUM(sizeof(rb_shape_t) * SHAPE_BUFFER_SIZE));
1723 rb_define_const(rb_cShape, "REDBLACK_CACHE_SIZE", INT2NUM(sizeof(redblack_node_t) * REDBLACK_CACHE_SIZE));
1724
1725 rb_define_singleton_method(rb_cShape, "transition_tree", shape_transition_tree, 0);
1726 rb_define_singleton_method(rb_cShape, "find_by_id", rb_shape_find_by_id, 1);
1727 rb_define_singleton_method(rb_cShape, "of", rb_shape_debug_shape, 1);
1728 rb_define_singleton_method(rb_cShape, "root_shape", rb_shape_root_shape, 0);
1729 rb_define_singleton_method(rb_cShape, "shapes_available", rb_shape_shapes_available, 0);
1730 rb_define_singleton_method(rb_cShape, "exhaust_shapes", rb_shape_exhaust, -1);
1731 rb_define_singleton_method(rb_cShape, "class_max_iv_count", rb_shape_class_max_iv_count, 1);
1732#endif
1733}
#define RUBY_ATOMIC_VALUE_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
Definition atomic.h:416
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
Definition atomic.h:165
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
Definition atomic.h:118
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
Definition atomic.h:175
#define RUBY_ATOMIC_SET(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type.
Definition atomic.h:185
#define RUBY_ALIGNAS
Wraps (or simulates) alignas.
Definition stdalign.h:27
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:3074
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define FL_USER19
Old name of RUBY_FL_USER19.
Definition fl_type.h:88
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define NUM2UINT
Old name of RB_NUM2UINT.
Definition int.h:45
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#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 xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:474
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
Definition error.h:54
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:230
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
Definition object.c:1305
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:504
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
void rb_memerror(void)
Triggers out-of-memory error.
Definition gc.c:6685
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1656
VALUE rb_struct_define_under(VALUE space, const char *name,...)
Identical to rb_struct_define(), except it defines the class under the specified namespace instead of...
Definition struct.c:558
VALUE rb_struct_new(VALUE klass,...)
Creates an instance of the given struct.
Definition struct.c:963
VALUE rb_struct_getmember(VALUE self, ID key)
Identical to rb_struct_aref(), except it takes ID instead of VALUE.
Definition struct.c:259
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3552
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:397
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1162
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:375
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:557
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:239
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
C99 shim for <stdbool.h>.
Ruby object's base components.
Definition rbasic.h:69
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