5#include "internal/imemo.h"
6#include "internal/object.h"
7#include "internal/st.h"
8#include "vm_callinfo.h"
10size_t rb_iseq_memsize(
const rb_iseq_t *iseq);
11void rb_iseq_mark_and_move(
rb_iseq_t *iseq,
bool reference_updating);
15rb_imemo_name(
enum imemo_type
type)
19#define IMEMO_NAME(x) case imemo_##x: return #x;
20 IMEMO_NAME(callcache);
22 IMEMO_NAME(constcache);
30 IMEMO_NAME(throw_data);
35 rb_bug(
"unreachable");
43rb_imemo_new(
enum imemo_type
type,
VALUE v0,
size_t size)
46 NEWOBJ_OF(obj,
void, v0, flags, size, 0);
52rb_imemo_tmpbuf_new(
void)
64rb_alloc_tmp_buffer_with_count(
volatile VALUE *store,
size_t size,
size_t cnt)
69 *store = (
VALUE)tmpbuf;
70 void *ptr = ruby_xmalloc(size);
78rb_alloc_tmp_buffer(
volatile VALUE *store,
long len)
82 if (
len < 0 || (cnt = (
long)roomof(
len,
sizeof(
VALUE))) < 0) {
83 rb_raise(rb_eArgError,
"negative buffer size (or size too big)");
86 return rb_alloc_tmp_buffer_with_count(store,
len, cnt);
90rb_free_tmp_buffer(
volatile VALUE *store)
94 void *ptr = ATOMIC_PTR_EXCHANGE(s->ptr, 0);
101imemo_fields_new(
VALUE owner,
size_t capa)
104 if (rb_gc_size_allocatable_p(embedded_size)) {
105 VALUE fields = rb_imemo_new(imemo_fields, owner, embedded_size);
110 VALUE fields = rb_imemo_new(imemo_fields, owner,
sizeof(
struct rb_fields));
118rb_imemo_fields_new(
VALUE owner,
size_t capa)
120 return imemo_fields_new(owner,
capa);
124imemo_fields_new_complex(
VALUE owner,
size_t capa)
126 VALUE fields = imemo_fields_new(owner, 1);
127 IMEMO_OBJ_FIELDS(fields)->as.complex.table = st_init_numtable_with_size(
capa);
133rb_imemo_fields_new_complex(
VALUE owner,
size_t capa)
135 return imemo_fields_new_complex(owner,
capa);
139imemo_fields_trigger_wb_i(st_data_t key, st_data_t value, st_data_t arg)
147imemo_fields_complex_wb_i(st_data_t key, st_data_t value, st_data_t arg)
157 IMEMO_OBJ_FIELDS(fields)->as.complex.table = tbl;
159 st_foreach(tbl, imemo_fields_trigger_wb_i, (st_data_t)fields);
164rb_imemo_fields_clone(
VALUE fields_obj)
166 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
169 if (rb_shape_too_complex_p(shape_id)) {
170 clone = rb_imemo_fields_new_complex(rb_imemo_fields_owner(fields_obj), 0);
171 RBASIC_SET_SHAPE_ID(clone, shape_id);
172 st_table *src_table = rb_imemo_fields_complex_tbl(fields_obj);
173 st_table *dest_table = rb_imemo_fields_complex_tbl(clone);
174 st_replace(dest_table, src_table);
175 st_foreach(dest_table, imemo_fields_complex_wb_i, (st_data_t)clone);
178 clone = imemo_fields_new(rb_imemo_fields_owner(fields_obj), RSHAPE_CAPACITY(shape_id));
179 RBASIC_SET_SHAPE_ID(clone, shape_id);
180 VALUE *fields = rb_imemo_fields_ptr(clone);
181 attr_index_t fields_count = RSHAPE_LEN(shape_id);
182 MEMCPY(fields, rb_imemo_fields_ptr(fields_obj),
VALUE, fields_count);
183 for (attr_index_t i = 0; i < fields_count; i++) {
192rb_imemo_fields_clear(
VALUE fields_obj)
196 if (rb_shape_obj_too_complex_p(fields_obj)) {
197 RBASIC_SET_SHAPE_ID(fields_obj, ROOT_TOO_COMPLEX_SHAPE_ID);
200 RBASIC_SET_SHAPE_ID(fields_obj, ROOT_SHAPE_ID);
203 RBASIC_CLEAR_CLASS(fields_obj);
211rb_imemo_memsize(
VALUE obj)
214 switch (imemo_type(obj)) {
215 case imemo_callcache:
219 case imemo_constcache:
230 size += rb_iseq_memsize((
rb_iseq_t *)obj);
241 case imemo_throw_data:
249 if (rb_shape_obj_too_complex_p(obj)) {
250 size += st_memsize(IMEMO_OBJ_FIELDS(obj)->as.complex.table);
253 size += RSHAPE_CAPACITY(RBASIC_SHAPE_ID(obj)) *
sizeof(
VALUE);
258 rb_bug(
"unreachable");
269moved_or_living_object_strictly_p(
VALUE obj)
279 rb_gc_mark_and_move(&ment->owner);
280 rb_gc_mark_and_move(&ment->defined_class);
284 case VM_METHOD_TYPE_ISEQ:
286 rb_gc_mark_and_move_ptr(&def->body.iseq.
iseqptr);
288 rb_gc_mark_and_move_ptr(&def->body.iseq.
cref);
290 if (!reference_updating) {
291 if (def->iseq_overload && ment->defined_class) {
294 rb_gc_mark((
VALUE)ment);
298 case VM_METHOD_TYPE_ATTRSET:
299 case VM_METHOD_TYPE_IVAR:
300 rb_gc_mark_and_move(&def->body.attr.location);
302 case VM_METHOD_TYPE_BMETHOD:
303 rb_gc_mark_and_move(&def->body.bmethod.proc);
304 if (def->body.bmethod.hooks) {
305 rb_hook_list_mark_and_move(def->body.bmethod.hooks);
308 case VM_METHOD_TYPE_ALIAS:
309 rb_gc_mark_and_move_ptr(&def->body.alias.original_me);
311 case VM_METHOD_TYPE_REFINED:
312 rb_gc_mark_and_move_ptr(&def->body.refined.orig_me);
314 case VM_METHOD_TYPE_CFUNC:
315 case VM_METHOD_TYPE_ZSUPER:
316 case VM_METHOD_TYPE_MISSING:
317 case VM_METHOD_TYPE_OPTIMIZED:
318 case VM_METHOD_TYPE_UNDEF:
319 case VM_METHOD_TYPE_NOTIMPLEMENTED:
326rb_imemo_mark_and_move(
VALUE obj,
bool reference_updating)
328 switch (imemo_type(obj)) {
329 case imemo_callcache: {
351 if (UNDEF_P(cc->klass)) {
356 else if (reference_updating) {
357 if (moved_or_living_object_strictly_p((
VALUE)cc->cme_)) {
358 *((
VALUE *)&cc->klass) = rb_gc_location(cc->klass);
366 vm_cc_invalidate(cc);
373 rb_gc_mark_weak((
VALUE *)&cc->klass);
374 if ((vm_cc_super_p(cc) || vm_cc_refinement_p(cc))) {
375 rb_gc_mark_movable((
VALUE)cc->cme_);
383 case imemo_constcache: {
386 rb_gc_mark_and_move(&ice->value);
393 rb_gc_mark_and_move(&cref->klass_or_self);
394 rb_gc_mark_and_move_ptr(&cref->next);
395 rb_gc_mark_and_move(&cref->refinements);
402 if (LIKELY(env->ep)) {
404 RUBY_ASSERT(rb_gc_location(env->ep[VM_ENV_DATA_INDEX_ENV]) == rb_gc_location(obj));
405 RUBY_ASSERT(reference_updating || VM_ENV_ESCAPED_P(env->ep));
407 for (
unsigned int i = 0; i < env->env_size; i++) {
408 rb_gc_mark_and_move((
VALUE *)&env->env[i]);
411 rb_gc_mark_and_move_ptr(&env->iseq);
413 if (reference_updating) {
414 ((
VALUE *)env->ep)[VM_ENV_DATA_INDEX_ENV] = rb_gc_location(env->ep[VM_ENV_DATA_INDEX_ENV]);
417 if (!VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_WB_REQUIRED)) {
418 VM_ENV_FLAGS_SET(env->ep, VM_ENV_FLAG_WB_REQUIRED);
420 rb_gc_mark_movable( (
VALUE)rb_vm_env_prev_env(env));
429 if (!reference_updating) {
430 rb_gc_mark_maybe((
VALUE)ifunc->data);
436 rb_iseq_mark_and_move((
rb_iseq_t *)obj, reference_updating);
439 struct MEMO *memo = (
struct MEMO *)obj;
441 rb_gc_mark_and_move((
VALUE *)&memo->v1);
442 rb_gc_mark_and_move((
VALUE *)&memo->v2);
443 if (!reference_updating) {
444 rb_gc_mark_maybe(memo->u3.value);
456 rb_gc_mark_and_move((
VALUE *)&svar->lastline);
457 rb_gc_mark_and_move((
VALUE *)&svar->backref);
458 rb_gc_mark_and_move((
VALUE *)&svar->others);
462 case imemo_throw_data: {
465 rb_gc_mark_and_move((
VALUE *)&throw_data->throw_obj);
472 if (!reference_updating) {
473 rb_gc_mark_locations(m->ptr, m->ptr + m->cnt);
481 if (rb_shape_obj_too_complex_p(obj)) {
482 st_table *tbl = rb_imemo_fields_complex_tbl(obj);
483 if (reference_updating) {
484 rb_gc_ref_update_table_values_only(tbl);
487 rb_mark_tbl_no_pin(tbl);
491 VALUE *fields = rb_imemo_fields_ptr(obj);
492 attr_index_t
len = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
493 for (attr_index_t i = 0; i <
len; i++) {
494 rb_gc_mark_and_move(&fields[i]);
500 rb_bug(
"unreachable");
508static enum rb_id_table_iterator_result
509free_const_entry_i(
VALUE value,
void *data)
513 return ID_TABLE_CONTINUE;
519 rb_id_table_foreach_values(tbl, free_const_entry_i, 0);
520 rb_id_table_free(tbl);
524imemo_fields_free(
struct rb_fields *fields)
527 if (rb_shape_obj_too_complex_p((
VALUE)fields)) {
528 st_free_table(fields->as.complex.table);
531 xfree(fields->as.external.ptr);
537rb_imemo_free(
VALUE obj)
539 switch (imemo_type(obj)) {
540 case imemo_callcache:
541 RB_DEBUG_COUNTER_INC(obj_imemo_callcache);
544 case imemo_callinfo:{
549 if (ci->kwarg->references == 0)
xfree((
void *)ci->kwarg);
551 RB_DEBUG_COUNTER_INC(obj_imemo_callinfo);
555 case imemo_constcache:
556 RB_DEBUG_COUNTER_INC(obj_imemo_constcache);
560 RB_DEBUG_COUNTER_INC(obj_imemo_cref);
568 RB_DEBUG_COUNTER_INC(obj_imemo_env);
573 RB_DEBUG_COUNTER_INC(obj_imemo_ifunc);
577 RB_DEBUG_COUNTER_INC(obj_imemo_iseq);
581 RB_DEBUG_COUNTER_INC(obj_imemo_memo);
586 RB_DEBUG_COUNTER_INC(obj_imemo_ment);
590 RB_DEBUG_COUNTER_INC(obj_imemo_svar);
593 case imemo_throw_data:
594 RB_DEBUG_COUNTER_INC(obj_imemo_throw_data);
599 RB_DEBUG_COUNTER_INC(obj_imemo_tmpbuf);
603 imemo_fields_free(IMEMO_OBJ_FIELDS(obj));
604 RB_DEBUG_COUNTER_INC(obj_imemo_fields);
607 rb_bug(
"unreachable");
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define T_MOVED
Old name of RUBY_T_MOVED.
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBASIC(obj)
Convenient casting macro.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
IFUNC (Internal FUNCtion)
const VALUE cref_or_me
class reference or rb_method_entry_t
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.