5#include "internal/imemo.h"
6#include "vm_callinfo.h"
8size_t rb_iseq_memsize(
const rb_iseq_t *iseq);
9void rb_iseq_mark_and_move(
rb_iseq_t *iseq,
bool reference_updating);
13rb_imemo_name(
enum imemo_type
type)
17#define IMEMO_NAME(x) case imemo_##x: return #x;
19 IMEMO_NAME(callcache);
21 IMEMO_NAME(constcache);
28 IMEMO_NAME(parser_strterm);
30 IMEMO_NAME(throw_data);
34 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)
62rb_alloc_tmp_buffer_with_count(
volatile VALUE *store,
size_t size,
size_t cnt)
69 tmpbuf = rb_imemo_tmpbuf_new();
70 *store = (
VALUE)tmpbuf;
71 ptr = ruby_xmalloc(size);
79rb_alloc_tmp_buffer(
volatile VALUE *store,
long len)
83 if (
len < 0 || (cnt = (
long)roomof(
len,
sizeof(
VALUE))) < 0) {
84 rb_raise(rb_eArgError,
"negative buffer size (or size too big)");
87 return rb_alloc_tmp_buffer_with_count(store,
len, cnt);
91rb_free_tmp_buffer(
volatile VALUE *store)
95 void *ptr = ATOMIC_PTR_EXCHANGE(s->ptr, 0);
106 tmpbuf->next = old_heap;
117rb_imemo_memsize(
VALUE obj)
120 switch (imemo_type(obj)) {
122 rb_bug(
"imemo_ast is obsolete");
125 case imemo_callcache:
129 case imemo_constcache:
140 size += rb_iseq_memsize((
rb_iseq_t *)obj);
149 case imemo_parser_strterm:
153 case imemo_throw_data:
160 rb_bug(
"unreachable");
170static enum rb_id_table_iterator_result
171cc_table_mark_i(
VALUE ccs_ptr,
void *data)
174 VM_ASSERT(vm_ccs_p(ccs));
179 VM_ASSERT(rb_id_table_lookup(RCLASS_CC_TBL(klass), ccs->cme->called_id, &lookup_val));
180 VM_ASSERT(lookup_val == ccs_ptr);
183 if (METHOD_ENTRY_INVALIDATED(ccs->cme)) {
185 return ID_TABLE_DELETE;
188 rb_gc_mark_movable((
VALUE)ccs->cme);
190 for (
int i=0; i<ccs->len; i++) {
191 VM_ASSERT(klass == ccs->entries[i].cc->klass);
192 VM_ASSERT(vm_cc_check_cme(ccs->entries[i].cc, ccs->cme));
194 rb_gc_mark_movable((
VALUE)ccs->entries[i].cc);
196 return ID_TABLE_CONTINUE;
201rb_cc_table_mark(
VALUE klass)
205 rb_id_table_foreach_values(cc_tbl, cc_table_mark_i, (
void *)klass);
210moved_or_living_object_strictly_p(
VALUE obj)
220 rb_gc_mark_and_move(&ment->owner);
221 rb_gc_mark_and_move(&ment->defined_class);
225 case VM_METHOD_TYPE_ISEQ:
227 rb_gc_mark_and_move_ptr(&def->body.iseq.
iseqptr);
229 rb_gc_mark_and_move_ptr(&def->body.iseq.
cref);
231 if (!reference_updating) {
232 if (def->iseq_overload && ment->defined_class) {
235 rb_gc_mark((
VALUE)ment);
239 case VM_METHOD_TYPE_ATTRSET:
240 case VM_METHOD_TYPE_IVAR:
241 rb_gc_mark_and_move(&def->body.attr.location);
243 case VM_METHOD_TYPE_BMETHOD:
244 rb_gc_mark_and_move(&def->body.bmethod.proc);
245 if (!reference_updating) {
246 if (def->body.bmethod.hooks) rb_hook_list_mark(def->body.bmethod.hooks);
249 case VM_METHOD_TYPE_ALIAS:
250 rb_gc_mark_and_move_ptr(&def->body.alias.original_me);
252 case VM_METHOD_TYPE_REFINED:
253 rb_gc_mark_and_move_ptr(&def->body.refined.orig_me);
255 case VM_METHOD_TYPE_CFUNC:
256 case VM_METHOD_TYPE_ZSUPER:
257 case VM_METHOD_TYPE_MISSING:
258 case VM_METHOD_TYPE_OPTIMIZED:
259 case VM_METHOD_TYPE_UNDEF:
260 case VM_METHOD_TYPE_NOTIMPLEMENTED:
267rb_imemo_mark_and_move(
VALUE obj,
bool reference_updating)
269 switch (imemo_type(obj)) {
271 rb_bug(
"imemo_ast is obsolete");
274 case imemo_callcache: {
295 if (reference_updating) {
300 if (moved_or_living_object_strictly_p(cc->klass) &&
301 moved_or_living_object_strictly_p((
VALUE)cc->cme_)) {
302 *((
VALUE *)&cc->klass) = rb_gc_location(cc->klass);
307 vm_cc_invalidate(cc);
312 if (vm_cc_super_p(cc) || vm_cc_refinement_p(cc)) {
313 rb_gc_mark_movable((
VALUE)cc->cme_);
314 rb_gc_mark_movable((
VALUE)cc->klass);
322 case imemo_constcache: {
325 rb_gc_mark_and_move(&ice->value);
332 rb_gc_mark_and_move(&cref->klass_or_self);
333 rb_gc_mark_and_move_ptr(&cref->next);
334 rb_gc_mark_and_move(&cref->refinements);
341 if (LIKELY(env->ep)) {
343 RUBY_ASSERT(rb_gc_location(env->ep[VM_ENV_DATA_INDEX_ENV]) == rb_gc_location(obj));
344 RUBY_ASSERT(reference_updating || VM_ENV_ESCAPED_P(env->ep));
346 for (
unsigned int i = 0; i < env->env_size; i++) {
347 rb_gc_mark_and_move((
VALUE *)&env->env[i]);
350 rb_gc_mark_and_move_ptr(&env->iseq);
352 if (reference_updating) {
353 ((
VALUE *)env->ep)[VM_ENV_DATA_INDEX_ENV] = rb_gc_location(env->ep[VM_ENV_DATA_INDEX_ENV]);
356 if (!VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_WB_REQUIRED)) {
357 VM_ENV_FLAGS_SET(env->ep, VM_ENV_FLAG_WB_REQUIRED);
359 rb_gc_mark_movable( (
VALUE)rb_vm_env_prev_env(env));
368 if (!reference_updating) {
369 rb_gc_mark_maybe((
VALUE)ifunc->data);
375 rb_iseq_mark_and_move((
rb_iseq_t *)obj, reference_updating);
378 struct MEMO *memo = (
struct MEMO *)obj;
380 rb_gc_mark_and_move((
VALUE *)&memo->v1);
381 rb_gc_mark_and_move((
VALUE *)&memo->v2);
382 if (!reference_updating) {
383 rb_gc_mark_maybe(memo->u3.value);
391 case imemo_parser_strterm:
397 rb_gc_mark_and_move((
VALUE *)&svar->lastline);
398 rb_gc_mark_and_move((
VALUE *)&svar->backref);
399 rb_gc_mark_and_move((
VALUE *)&svar->others);
403 case imemo_throw_data: {
406 rb_gc_mark_and_move((
VALUE *)&throw_data->throw_obj);
413 if (!reference_updating) {
415 rb_gc_mark_locations(m->ptr, m->ptr + m->cnt);
416 }
while ((m = m->next) != NULL);
422 rb_bug(
"unreachable");
430static enum rb_id_table_iterator_result
431free_const_entry_i(
VALUE value,
void *data)
435 return ID_TABLE_CONTINUE;
441 rb_id_table_foreach_values(tbl, free_const_entry_i, 0);
442 rb_id_table_free(tbl);
450 for (
int i=0; i<ccs->len; i++) {
454 if (rb_gc_pointer_to_heap_p((
VALUE)cc) &&
455 !rb_objspace_garbage_object_p((
VALUE)cc) &&
456 IMEMO_TYPE_P(cc, imemo_callcache) &&
457 cc->klass == klass) {
465 VM_ASSERT(!vm_cc_super_p(cc) && !vm_cc_refinement_p(cc));
466 vm_cc_invalidate(cc);
468 ruby_xfree(ccs->entries);
476 RB_DEBUG_COUNTER_INC(ccs_free);
477 vm_ccs_free(ccs,
true,
Qundef);
480static enum rb_id_table_iterator_result
481cc_table_free_i(
VALUE ccs_ptr,
void *data)
485 VM_ASSERT(vm_ccs_p(ccs));
487 vm_ccs_free(ccs,
false, klass);
489 return ID_TABLE_CONTINUE;
493rb_cc_table_free(
VALUE klass)
498 rb_id_table_foreach_values(cc_tbl, cc_table_free_i, (
void *)klass);
499 rb_id_table_free(cc_tbl);
504rb_imemo_free(
VALUE obj)
506 switch (imemo_type(obj)) {
508 rb_bug(
"imemo_ast is obsolete");
511 case imemo_callcache:
512 RB_DEBUG_COUNTER_INC(obj_imemo_callcache);
515 case imemo_callinfo:{
520 if (ci->kwarg->references == 0)
xfree((
void *)ci->kwarg);
522 RB_DEBUG_COUNTER_INC(obj_imemo_callinfo);
526 case imemo_constcache:
527 RB_DEBUG_COUNTER_INC(obj_imemo_constcache);
531 RB_DEBUG_COUNTER_INC(obj_imemo_cref);
539 RB_DEBUG_COUNTER_INC(obj_imemo_env);
544 RB_DEBUG_COUNTER_INC(obj_imemo_ifunc);
548 RB_DEBUG_COUNTER_INC(obj_imemo_iseq);
552 RB_DEBUG_COUNTER_INC(obj_imemo_memo);
557 RB_DEBUG_COUNTER_INC(obj_imemo_ment);
560 case imemo_parser_strterm:
561 RB_DEBUG_COUNTER_INC(obj_imemo_parser_strterm);
565 RB_DEBUG_COUNTER_INC(obj_imemo_svar);
567 case imemo_throw_data:
568 RB_DEBUG_COUNTER_INC(obj_imemo_throw_data);
573 RB_DEBUG_COUNTER_INC(obj_imemo_tmpbuf);
577 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 FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#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.
int len
Length of the buffer.
VALUE type(ANYARGS)
ANYARGS-ed function type.
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.