4#include "internal/gc.h"
6typedef uint16_t attr_index_t;
7typedef uint32_t shape_id_t;
8#define SHAPE_ID_NUM_BITS 32
9#define SHAPE_ID_OFFSET_NUM_BITS 19
11STATIC_ASSERT(shape_id_num_bits, SHAPE_ID_NUM_BITS ==
sizeof(shape_id_t) * CHAR_BIT);
13#define SHAPE_BUFFER_SIZE (1 << SHAPE_ID_OFFSET_NUM_BITS)
14#define SHAPE_ID_OFFSET_MASK (SHAPE_BUFFER_SIZE - 1)
16#define SHAPE_ID_HEAP_INDEX_BITS 3
17#define SHAPE_ID_HEAP_INDEX_MAX ((1 << SHAPE_ID_HEAP_INDEX_BITS) - 1)
19#define SHAPE_ID_FL_USHIFT (SHAPE_ID_OFFSET_NUM_BITS + SHAPE_ID_HEAP_INDEX_BITS)
20#define SHAPE_ID_HEAP_INDEX_OFFSET SHAPE_ID_FL_USHIFT
34enum shape_id_fl_type {
35#define RBIMPL_SHAPE_ID_FL(n) (1<<(SHAPE_ID_FL_USHIFT+n))
37 SHAPE_ID_HEAP_INDEX_MASK = RBIMPL_SHAPE_ID_FL(0) | RBIMPL_SHAPE_ID_FL(1) | RBIMPL_SHAPE_ID_FL(2),
39 SHAPE_ID_FL_FROZEN = RBIMPL_SHAPE_ID_FL(3),
40 SHAPE_ID_FL_HAS_OBJECT_ID = RBIMPL_SHAPE_ID_FL(4),
41 SHAPE_ID_FL_TOO_COMPLEX = RBIMPL_SHAPE_ID_FL(5),
43 SHAPE_ID_FL_NON_CANONICAL_MASK = SHAPE_ID_FL_FROZEN | SHAPE_ID_FL_HAS_OBJECT_ID,
44 SHAPE_ID_FLAGS_MASK = SHAPE_ID_HEAP_INDEX_MASK | SHAPE_ID_FL_NON_CANONICAL_MASK | SHAPE_ID_FL_TOO_COMPLEX,
46#undef RBIMPL_SHAPE_ID_FL
52 SHAPE_ID_HAS_IVAR_MASK = SHAPE_ID_FL_TOO_COMPLEX | (SHAPE_ID_OFFSET_MASK - 1),
58#define SHAPE_ID_READ_ONLY_MASK (~(SHAPE_ID_FL_FROZEN | SHAPE_ID_HEAP_INDEX_MASK))
60typedef uint32_t redblack_id_t;
62#define SHAPE_MAX_FIELDS (attr_index_t)(-1)
63#define SHAPE_FLAG_SHIFT ((SIZEOF_VALUE * CHAR_BIT) - SHAPE_ID_NUM_BITS)
64#define SHAPE_FLAG_MASK (((VALUE)-1) >> SHAPE_ID_NUM_BITS)
66#define SHAPE_MAX_VARIATIONS 8
68#define INVALID_SHAPE_ID ((shape_id_t)-1)
69#define ATTR_INDEX_NOT_SET ((attr_index_t)-1)
71#define ROOT_SHAPE_ID 0x0
72#define ROOT_SHAPE_WITH_OBJ_ID 0x1
73#define ROOT_TOO_COMPLEX_SHAPE_ID (ROOT_SHAPE_ID | SHAPE_ID_FL_TOO_COMPLEX)
74#define ROOT_TOO_COMPLEX_WITH_OBJ_ID (ROOT_SHAPE_WITH_OBJ_ID | SHAPE_ID_FL_TOO_COMPLEX | SHAPE_ID_FL_HAS_OBJECT_ID)
75#define SPECIAL_CONST_SHAPE_ID (ROOT_SHAPE_ID | SHAPE_ID_FL_FROZEN)
84 attr_index_t next_field_index;
85 attr_index_t capacity;
105 SHAPE_FL_FROZEN = 1 << 0,
106 SHAPE_FL_HAS_OBJECT_ID = 1 << 1,
107 SHAPE_FL_TOO_COMPLEX = 1 << 2,
109 SHAPE_FL_NON_CANONICAL_MASK = SHAPE_FL_FROZEN | SHAPE_FL_HAS_OBJECT_ID,
116 const attr_index_t *capacities;
120 unsigned int cache_size;
123RUBY_SYMBOL_EXPORT_BEGIN
125RUBY_SYMBOL_EXPORT_END
127static inline shape_id_t
141static inline shape_id_t
142RBASIC_SHAPE_ID(
VALUE obj)
146#if RBASIC_SHAPE_ID_FIELD
147 return (shape_id_t)((
RBASIC(obj)->shape_id));
149 return (shape_id_t)((
RBASIC(obj)->flags) >> SHAPE_FLAG_SHIFT);
155static inline shape_id_t
156RBASIC_SHAPE_ID_FOR_READ(
VALUE obj)
158 return RBASIC_SHAPE_ID(obj) & SHAPE_ID_READ_ONLY_MASK;
162bool rb_shape_verify_consistency(
VALUE obj, shape_id_t shape_id);
166RBASIC_SET_SHAPE_ID(
VALUE obj, shape_id_t shape_id)
170#if RBASIC_SHAPE_ID_FIELD
174 RBASIC(obj)->flags &= SHAPE_FLAG_MASK;
175 RBASIC(obj)->flags |= ((
VALUE)(shape_id) << SHAPE_FLAG_SHIFT);
177 RUBY_ASSERT(rb_shape_verify_consistency(obj, shape_id));
180void rb_set_namespaced_class_shape_id(
VALUE obj, shape_id_t shape_id);
183RB_SET_SHAPE_ID(
VALUE obj, shape_id_t shape_id)
188 rb_set_namespaced_class_shape_id(obj, shape_id);
191 RBASIC_SET_SHAPE_ID(obj, shape_id);
197RSHAPE(shape_id_t shape_id)
199 uint32_t offset = (shape_id & SHAPE_ID_OFFSET_MASK);
202 return &rb_shape_tree.shape_list[offset];
205int32_t rb_shape_id_offset(
void);
207RUBY_FUNC_EXPORTED shape_id_t rb_obj_shape_id(
VALUE obj);
208shape_id_t rb_shape_get_next_iv_shape(shape_id_t shape_id,
ID id);
209bool rb_shape_get_iv_index(shape_id_t shape_id,
ID id, attr_index_t *value);
210bool rb_shape_get_iv_index_with_hint(shape_id_t shape_id,
ID id, attr_index_t *value, shape_id_t *shape_id_hint);
211bool rb_shape_find_ivar(shape_id_t shape_id,
ID id, shape_id_t *ivar_shape);
213typedef int rb_shape_foreach_transition_callback(shape_id_t shape_id,
void *data);
214bool rb_shape_foreach_field(shape_id_t shape_id, rb_shape_foreach_transition_callback func,
void *data);
216shape_id_t rb_shape_transition_frozen(
VALUE obj);
217shape_id_t rb_shape_transition_complex(
VALUE obj);
218shape_id_t rb_shape_transition_remove_ivar(
VALUE obj,
ID id, shape_id_t *removed_shape_id);
219shape_id_t rb_shape_transition_add_ivar(
VALUE obj,
ID id);
220shape_id_t rb_shape_transition_add_ivar_no_warnings(
VALUE obj,
ID id);
221shape_id_t rb_shape_transition_object_id(
VALUE obj);
222shape_id_t rb_shape_transition_heap(
VALUE obj,
size_t heap_index);
223shape_id_t rb_shape_object_id(shape_id_t original_shape_id);
225void rb_shape_free_all(
void);
227shape_id_t rb_shape_rebuild(shape_id_t initial_shape_id, shape_id_t dest_shape_id);
228void rb_shape_copy_fields(
VALUE dest,
VALUE *dest_buf, shape_id_t dest_shape_id,
VALUE *src_buf, shape_id_t src_shape_id);
229void rb_shape_copy_complex_ivars(
VALUE dest,
VALUE obj, shape_id_t src_shape_id,
st_table *fields_table);
232rb_shape_too_complex_p(shape_id_t shape_id)
234 return shape_id & SHAPE_ID_FL_TOO_COMPLEX;
238rb_shape_obj_too_complex_p(
VALUE obj)
244rb_shape_has_object_id(shape_id_t shape_id)
246 return shape_id & SHAPE_ID_FL_HAS_OBJECT_ID;
250rb_shape_canonical_p(shape_id_t shape_id)
252 return !(shape_id & SHAPE_ID_FL_NON_CANONICAL_MASK);
256rb_shape_heap_index(shape_id_t shape_id)
258 return (uint8_t)((shape_id & SHAPE_ID_HEAP_INDEX_MASK) >> SHAPE_ID_HEAP_INDEX_OFFSET);
261static inline shape_id_t
262rb_shape_root(
size_t heap_id)
264 shape_id_t heap_index = (shape_id_t)(heap_id + 1);
265 shape_id_t heap_flags = heap_index << SHAPE_ID_HEAP_INDEX_OFFSET;
267 RUBY_ASSERT((heap_flags & SHAPE_ID_HEAP_INDEX_MASK) == heap_flags);
268 RUBY_ASSERT(rb_shape_heap_index(heap_flags) == heap_index);
270 return ROOT_SHAPE_ID | heap_flags;
273static inline shape_id_t
274RSHAPE_PARENT(shape_id_t shape_id)
276 return RSHAPE(shape_id)->parent_id;
279static inline enum shape_type
280RSHAPE_TYPE(shape_id_t shape_id)
282 return RSHAPE(shape_id)->type;
286RSHAPE_TYPE_P(shape_id_t shape_id,
enum shape_type
type)
288 return RSHAPE_TYPE(shape_id) ==
type;
291static inline attr_index_t
292RSHAPE_EMBEDDED_CAPACITY(shape_id_t shape_id)
294 uint8_t heap_index = rb_shape_heap_index(shape_id);
296 return rb_shape_tree.capacities[heap_index - 1];
301static inline attr_index_t
302RSHAPE_CAPACITY(shape_id_t shape_id)
304 attr_index_t embedded_capacity = RSHAPE_EMBEDDED_CAPACITY(shape_id);
306 if (embedded_capacity > RSHAPE(shape_id)->capacity) {
307 return embedded_capacity;
310 return RSHAPE(shape_id)->capacity;
314static inline attr_index_t
315RSHAPE_LEN(shape_id_t shape_id)
317 return RSHAPE(shape_id)->next_field_index;
320static inline attr_index_t
321RSHAPE_INDEX(shape_id_t shape_id)
323 return RSHAPE_LEN(shape_id) - 1;
327RSHAPE_EDGE_NAME(shape_id_t shape_id)
329 return RSHAPE(shape_id)->edge_name;
332static inline uint32_t
333ROBJECT_FIELDS_CAPACITY(
VALUE obj)
339 return RSHAPE_CAPACITY(RBASIC_SHAPE_ID(obj));
343ROBJECT_FIELDS_HASH(
VALUE obj)
358static inline uint32_t
359ROBJECT_FIELDS_COUNT(
VALUE obj)
361 if (rb_shape_obj_too_complex_p(obj)) {
362 return (uint32_t)rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
367 return RSHAPE(RBASIC_SHAPE_ID(obj))->next_field_index;
371static inline uint32_t
372RBASIC_FIELDS_COUNT(
VALUE obj)
374 return RSHAPE(rb_obj_shape_id(obj))->next_field_index;
377bool rb_obj_set_shape_id(
VALUE obj, shape_id_t shape_id);
380rb_shape_obj_has_id(
VALUE obj)
382 return rb_shape_has_object_id(RBASIC_SHAPE_ID(obj));
386rb_shape_has_ivars(shape_id_t shape_id)
388 return shape_id & SHAPE_ID_HAS_IVAR_MASK;
392rb_shape_obj_has_ivars(
VALUE obj)
394 return rb_shape_has_ivars(RBASIC_SHAPE_ID(obj));
398rb_shape_has_fields(shape_id_t shape_id)
400 return shape_id & (SHAPE_ID_OFFSET_MASK | SHAPE_ID_FL_TOO_COMPLEX);
404rb_shape_obj_has_fields(
VALUE obj)
406 return rb_shape_has_fields(RBASIC_SHAPE_ID(obj));
410rb_obj_exivar_p(
VALUE obj)
422 return rb_shape_obj_has_fields(obj);
426RUBY_SYMBOL_EXPORT_BEGIN
427typedef void each_shape_callback(shape_id_t shape_id,
void *data);
428void rb_shape_each_shape_id(each_shape_callback callback,
void *data);
429size_t rb_shape_memsize(shape_id_t shape);
430size_t rb_shape_edges_count(shape_id_t shape_id);
431size_t rb_shape_depth(shape_id_t shape_id);
432RUBY_SYMBOL_EXPORT_END
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
#define RUBY_EXTERN
Declaration of externally visible global variables.
#define TYPE(_)
Old name of rb_type.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define T_NONE
Old name of RUBY_T_NONE.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBASIC(obj)
Convenient casting macro.
#define ROBJECT(obj)
Convenient casting macro.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.