14#include "ruby/internal/config.h"
19#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
20# include "wasm/setjmp.h"
21# include "wasm/machine.h"
29#ifndef HAVE_MALLOC_USABLE_SIZE
31# define HAVE_MALLOC_USABLE_SIZE
32# define malloc_usable_size(a) _msize(a)
33# elif defined HAVE_MALLOC_SIZE
34# define HAVE_MALLOC_USABLE_SIZE
35# define malloc_usable_size(a) malloc_size(a)
39#ifdef HAVE_MALLOC_USABLE_SIZE
40# ifdef RUBY_ALTERNATIVE_MALLOC_HEADER
42# elif defined(HAVE_MALLOC_H)
44# elif defined(HAVE_MALLOC_NP_H)
45# include <malloc_np.h>
46# elif defined(HAVE_MALLOC_MALLOC_H)
47# include <malloc/malloc.h>
57#ifdef HAVE_SYS_RESOURCE_H
58# include <sys/resource.h>
61#if defined _WIN32 || defined __CYGWIN__
63#elif defined(HAVE_POSIX_MEMALIGN)
64#elif defined(HAVE_MEMALIGN)
71#include <emscripten.h>
75#ifdef HAVE_SYS_PRCTL_H
83#include "debug_counter.h"
84#include "eval_intern.h"
88#include "internal/class.h"
89#include "internal/compile.h"
90#include "internal/complex.h"
91#include "internal/concurrent_set.h"
92#include "internal/cont.h"
93#include "internal/error.h"
94#include "internal/eval.h"
95#include "internal/gc.h"
96#include "internal/hash.h"
97#include "internal/imemo.h"
98#include "internal/io.h"
99#include "internal/numeric.h"
100#include "internal/object.h"
101#include "internal/proc.h"
102#include "internal/rational.h"
103#include "internal/re.h"
104#include "internal/sanitizers.h"
105#include "internal/struct.h"
106#include "internal/symbol.h"
107#include "internal/thread.h"
108#include "internal/variable.h"
109#include "internal/warnings.h"
119#include "ruby_assert.h"
120#include "ruby_atomic.h"
125#include "vm_callinfo.h"
126#include "ractor_core.h"
136rb_gc_print_backtrace(
void)
138 rb_print_backtrace(stderr);
142rb_gc_vm_lock(
const char *file,
int line)
144 unsigned int lev = 0;
145 rb_vm_lock_enter(&lev, file, line);
150rb_gc_vm_unlock(
unsigned int lev,
const char *file,
int line)
152 rb_vm_lock_leave(&lev, file, line);
156rb_gc_cr_lock(
const char *file,
int line)
159 rb_vm_lock_enter_cr(GET_RACTOR(), &lev, file, line);
164rb_gc_cr_unlock(
unsigned int lev,
const char *file,
int line)
166 rb_vm_lock_leave_cr(GET_RACTOR(), &lev, file, line);
170rb_gc_vm_lock_no_barrier(
const char *file,
int line)
172 unsigned int lev = 0;
173 rb_vm_lock_enter_nb(&lev, file, line);
178rb_gc_vm_unlock_no_barrier(
unsigned int lev,
const char *file,
int line)
180 rb_vm_lock_leave_nb(&lev, file, line);
184rb_gc_vm_barrier(
void)
190rb_gc_get_ractor_newobj_cache(
void)
192 return GET_RACTOR()->newobj_cache;
199 context->ec = GET_EC();
205 return ruby_vm_event_flags & event;
211 if (LIKELY(!rb_gc_event_hook_required_p(event)))
return;
214 if (!ec->cfp)
return;
217 bool gc_thread_p =
false;
221# ifdef RB_THREAD_LOCAL_SPECIFIER
222 rb_current_ec_set(ec);
224 native_tls_set(ruby_current_ec_key, ec);
229 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
233# ifdef RB_THREAD_LOCAL_SPECIFIER
234 rb_current_ec_set(NULL);
236 native_tls_set(ruby_current_ec_key, NULL);
243rb_gc_get_objspace(
void)
245 return GET_VM()->gc.objspace;
249rb_gc_ractor_newobj_cache_foreach(
void (*func)(
void *cache,
void *data),
void *data)
252 if (RB_LIKELY(ruby_single_main_ractor)) {
254 ccan_list_empty(&GET_VM()->ractor.set) ||
255 (ccan_list_top(&GET_VM()->ractor.set,
rb_ractor_t, vmlr_node) == ruby_single_main_ractor &&
256 ccan_list_tail(&GET_VM()->ractor.set,
rb_ractor_t, vmlr_node) == ruby_single_main_ractor)
259 func(ruby_single_main_ractor->newobj_cache, data);
262 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
263 func(r->newobj_cache, data);
269rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
280#define RESTORE_FINALIZER() (\
281 ec->cfp = saved.cfp, \
282 ec->cfp->sp = saved.sp, \
283 ec->errinfo = saved.errinfo)
285 saved.errinfo = ec->errinfo;
287 saved.sp = ec->cfp->sp;
291 ASSERT_vm_unlocking();
292 rb_ractor_ignore_belonging(
true);
294 enum ruby_tag_type state = EC_EXEC_TAG();
295 if (state != TAG_NONE) {
298 VALUE failed_final = saved.final;
301 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
302 rb_ec_error_print(ec, ec->errinfo);
306 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
307 saved.final = callback(i, data);
311 rb_ractor_ignore_belonging(
false);
312#undef RESTORE_FINALIZER
316rb_gc_set_pending_interrupt(
void)
319 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
323rb_gc_unset_pending_interrupt(
void)
326 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
330rb_gc_multi_ractor_p(
void)
332 return rb_multi_ractor_p();
336rb_gc_shutdown_call_finalizer_p(
VALUE obj)
340 if (!ruby_free_at_exit_p() && (!
DATA_PTR(obj) || !
RDATA(obj)->dfree))
return false;
341 if (rb_obj_is_thread(obj))
return false;
342 if (rb_obj_is_mutex(obj))
return false;
343 if (rb_obj_is_fiber(obj))
return false;
344 if (rb_ractor_p(obj))
return false;
345 if (rb_obj_is_fstring_table(obj))
return false;
346 if (rb_obj_is_symbol_table(obj))
return false;
360 return ruby_free_at_exit_p();
365rb_gc_obj_changed_pool(
VALUE obj,
size_t heap_id)
369 RBASIC_SET_SHAPE_ID(obj, rb_obj_shape_transition_heap(obj, heap_id));
372void rb_vm_update_references(
void *ptr);
374#define rb_setjmp(env) RUBY_SETJMP(env)
375#define rb_jmp_buf rb_jmpbuf_t
376#undef rb_data_object_wrap
378#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
379#define MAP_ANONYMOUS MAP_ANON
382#define unless_objspace(objspace) \
384 rb_vm_t *unless_objspace_vm = GET_VM(); \
385 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
388#define RMOVED(obj) ((struct RMoved *)(obj))
390#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
391 if (gc_object_moved_p_internal((_objspace), (VALUE)(_thing))) { \
392 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
396#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
398#if RUBY_MARK_FREE_DEBUG
399int ruby_gc_debug_indent = 0;
402#ifndef RGENGC_OBJ_INFO
403# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
406#ifndef CALC_EXACT_MALLOC_SIZE
407# define CALC_EXACT_MALLOC_SIZE 0
412static size_t malloc_offset = 0;
413#if defined(HAVE_MALLOC_USABLE_SIZE)
415gc_compute_malloc_offset(
void)
426 for (offset = 0; offset <= 16; offset += 8) {
427 size_t allocated = (64 - offset);
428 void *test_ptr = malloc(allocated);
429 size_t wasted = malloc_usable_size(test_ptr) - allocated;
440gc_compute_malloc_offset(
void)
448rb_malloc_grow_capa(
size_t current,
size_t type_size)
450 size_t current_capacity = current;
451 if (current_capacity < 4) {
452 current_capacity = 4;
454 current_capacity *= type_size;
457 size_t new_capacity = (current_capacity * 2);
460 if (rb_popcount64(new_capacity) != 1) {
461 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
464 new_capacity -= malloc_offset;
465 new_capacity /= type_size;
466 if (current > new_capacity) {
467 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
473static inline struct rbimpl_size_overflow_tag
474size_mul_add_overflow(size_t x, size_t y, size_t z)
476 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
477 struct rbimpl_size_overflow_tag u = rbimpl_size_add_overflow(t.result, z);
478 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed, u.result };
481static inline struct rbimpl_size_overflow_tag
482size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
484 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
485 struct rbimpl_size_overflow_tag u = rbimpl_size_mul_overflow(z, w);
486 struct rbimpl_size_overflow_tag v = rbimpl_size_add_overflow(t.result, u.result);
487 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed || v.overflowed, v.result };
490PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
493size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
495 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
496 if (LIKELY(!t.overflowed)) {
499 else if (rb_during_gc()) {
505 "integer overflow: %"PRIuSIZE
508 x, y, (
size_t)SIZE_MAX);
513rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
515 return size_mul_or_raise(x, y, exc);
519size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
521 struct rbimpl_size_overflow_tag t = size_mul_add_overflow(x, y, z);
522 if (LIKELY(!t.overflowed)) {
525 else if (rb_during_gc()) {
531 "integer overflow: %"PRIuSIZE
535 x, y, z, (
size_t)SIZE_MAX);
540rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
542 return size_mul_add_or_raise(x, y, z, exc);
546size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
548 struct rbimpl_size_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
549 if (LIKELY(!t.overflowed)) {
552 else if (rb_during_gc()) {
558 "integer overflow: %"PRIdSIZE
563 x, y, z, w, (
size_t)SIZE_MAX);
567#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
569volatile VALUE rb_gc_guarded_val;
571rb_gc_guarded_ptr_val(
volatile VALUE *ptr,
VALUE val)
573 rb_gc_guarded_val = val;
579static const char *obj_type_name(
VALUE obj);
581#include "gc/default/default.c"
583#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
584# error "Modular GC requires dlopen"
588typedef struct gc_function_map {
590 void *(*objspace_alloc)(void);
591 void (*objspace_init)(
void *objspace_ptr);
592 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
593 void (*set_params)(
void *objspace_ptr);
595 size_t *(*heap_sizes)(
void *objspace_ptr);
597 void (*shutdown_free_objects)(
void *objspace_ptr);
598 void (*objspace_free)(
void *objspace_ptr);
599 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
601 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
602 bool (*during_gc_p)(
void *objspace_ptr);
603 void (*prepare_heap)(
void *objspace_ptr);
604 void (*gc_enable)(
void *objspace_ptr);
605 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
606 bool (*gc_enabled_p)(
void *objspace_ptr);
607 VALUE (*config_get)(
void *objpace_ptr);
608 void (*config_set)(
void *objspace_ptr,
VALUE hash);
609 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
610 VALUE (*stress_get)(
void *objspace_ptr);
613 VALUE (*new_obj)(
void *objspace_ptr,
void *cache_ptr,
VALUE klass,
VALUE flags,
bool wb_protected,
size_t alloc_size);
614 size_t (*obj_slot_size)(
VALUE obj);
615 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
616 bool (*size_allocatable_p)(
size_t size);
618 void *(*malloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
619 void *(*calloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
620 void *(*realloc)(
void *objspace_ptr,
void *ptr,
size_t new_size,
size_t old_size,
bool gc_allowed);
621 void (*free)(
void *objspace_ptr,
void *ptr,
size_t old_size);
622 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
624 void (*mark)(
void *objspace_ptr,
VALUE obj);
625 void (*mark_and_move)(
void *objspace_ptr,
VALUE *ptr);
626 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
627 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
629 void (*declare_weak_references)(
void *objspace_ptr,
VALUE obj);
630 bool (*handle_weak_references_alive_p)(
void *objspace_ptr,
VALUE obj);
632 void (*register_pinning_obj)(
void *objspace_ptr,
VALUE obj);
633 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
634 VALUE (*location)(
void *objspace_ptr,
VALUE value);
636 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
637 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
638 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
640 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
641 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
643 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
645 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
646 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
647 void (*shutdown_call_finalizer)(
void *objspace_ptr);
649 void (*before_fork)(
void *objspace_ptr);
650 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
652 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
653 bool (*get_measure_total_time)(
void *objspace_ptr);
654 unsigned long long (*get_total_time)(
void *objspace_ptr);
655 size_t (*gc_count)(
void *objspace_ptr);
656 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
657 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
658 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
659 const char *(*active_gc_name)(void);
662 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *ptr);
663 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
664 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
665 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
667 bool modular_gc_loaded_p;
668} rb_gc_function_map_t;
670static rb_gc_function_map_t rb_gc_functions;
672# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
673# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
676ruby_modular_gc_init(
void)
681 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
683 rb_gc_function_map_t gc_functions = { 0 };
685 char *gc_so_path = NULL;
690 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
691 char c = gc_so_file[i];
692 if (isalnum(c))
continue;
698 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
703 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
706 size_t prefix_len = 0;
707 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
708 const char *base = strrchr(dli.dli_fname,
'/');
711# define end_with_p(lit) \
712 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
713 memcmp(base - tail, lit, tail) == 0)
715 prefix_len = base - dli.dli_fname;
716 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
719 prefix_len += MODULAR_GC_DIR[0] !=
'/';
720 gc_so_path_size += prefix_len;
724 gc_so_path = alloca(gc_so_path_size);
726 size_t gc_so_path_idx = 0;
727#define GC_SO_PATH_APPEND(str) do { \
728 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
731 if (prefix_len > 0) {
732 memcpy(gc_so_path, dli.dli_fname, prefix_len);
733 gc_so_path_idx = prefix_len;
736 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
737 GC_SO_PATH_APPEND(gc_so_file);
738 GC_SO_PATH_APPEND(DLEXT);
739 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
740#undef GC_SO_PATH_APPEND
743 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
745 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
749 gc_functions.modular_gc_loaded_p =
true;
752 unsigned int err_count = 0;
754# define load_modular_gc_func(name) do { \
756 const char *func_name = "rb_gc_impl_" #name; \
757 gc_functions.name = dlsym(handle, func_name); \
758 if (!gc_functions.name) { \
759 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
764 gc_functions.name = rb_gc_impl_##name; \
769 load_modular_gc_func(objspace_alloc);
770 load_modular_gc_func(objspace_init);
771 load_modular_gc_func(ractor_cache_alloc);
772 load_modular_gc_func(set_params);
773 load_modular_gc_func(init);
774 load_modular_gc_func(heap_sizes);
776 load_modular_gc_func(shutdown_free_objects);
777 load_modular_gc_func(objspace_free);
778 load_modular_gc_func(ractor_cache_free);
780 load_modular_gc_func(start);
781 load_modular_gc_func(during_gc_p);
782 load_modular_gc_func(prepare_heap);
783 load_modular_gc_func(gc_enable);
784 load_modular_gc_func(gc_disable);
785 load_modular_gc_func(gc_enabled_p);
786 load_modular_gc_func(config_set);
787 load_modular_gc_func(config_get);
788 load_modular_gc_func(stress_set);
789 load_modular_gc_func(stress_get);
790 load_modular_gc_func(get_vm_context);
792 load_modular_gc_func(new_obj);
793 load_modular_gc_func(obj_slot_size);
794 load_modular_gc_func(heap_id_for_size);
795 load_modular_gc_func(size_allocatable_p);
797 load_modular_gc_func(malloc);
798 load_modular_gc_func(calloc);
799 load_modular_gc_func(realloc);
800 load_modular_gc_func(free);
801 load_modular_gc_func(adjust_memory_usage);
803 load_modular_gc_func(mark);
804 load_modular_gc_func(mark_and_move);
805 load_modular_gc_func(mark_and_pin);
806 load_modular_gc_func(mark_maybe);
808 load_modular_gc_func(declare_weak_references);
809 load_modular_gc_func(handle_weak_references_alive_p);
811 load_modular_gc_func(register_pinning_obj);
812 load_modular_gc_func(object_moved_p);
813 load_modular_gc_func(location);
815 load_modular_gc_func(writebarrier);
816 load_modular_gc_func(writebarrier_unprotect);
817 load_modular_gc_func(writebarrier_remember);
819 load_modular_gc_func(each_objects);
820 load_modular_gc_func(each_object);
822 load_modular_gc_func(make_zombie);
823 load_modular_gc_func(define_finalizer);
824 load_modular_gc_func(undefine_finalizer);
825 load_modular_gc_func(copy_finalizer);
826 load_modular_gc_func(shutdown_call_finalizer);
828 load_modular_gc_func(before_fork);
829 load_modular_gc_func(after_fork);
831 load_modular_gc_func(set_measure_total_time);
832 load_modular_gc_func(get_measure_total_time);
833 load_modular_gc_func(get_total_time);
834 load_modular_gc_func(gc_count);
835 load_modular_gc_func(latest_gc_info);
836 load_modular_gc_func(stat);
837 load_modular_gc_func(stat_heap);
838 load_modular_gc_func(active_gc_name);
840 load_modular_gc_func(object_metadata);
841 load_modular_gc_func(pointer_to_heap_p);
842 load_modular_gc_func(garbage_object_p);
843 load_modular_gc_func(set_event_hook);
844 load_modular_gc_func(copy_attributes);
847 fprintf(stderr,
"ruby_modular_gc_init: found %u missing exports in library %s\n", err_count, gc_so_path);
851# undef load_modular_gc_func
853 rb_gc_functions = gc_functions;
857# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
858# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
859# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
860# define rb_gc_impl_set_params rb_gc_functions.set_params
861# define rb_gc_impl_init rb_gc_functions.init
862# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
864# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
865# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
866# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
868# define rb_gc_impl_start rb_gc_functions.start
869# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
870# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
871# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
872# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
873# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
874# define rb_gc_impl_config_get rb_gc_functions.config_get
875# define rb_gc_impl_config_set rb_gc_functions.config_set
876# define rb_gc_impl_stress_set rb_gc_functions.stress_set
877# define rb_gc_impl_stress_get rb_gc_functions.stress_get
878# define rb_gc_impl_get_vm_context rb_gc_functions.get_vm_context
880# define rb_gc_impl_new_obj rb_gc_functions.new_obj
881# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
882# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
883# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
885# define rb_gc_impl_malloc rb_gc_functions.malloc
886# define rb_gc_impl_calloc rb_gc_functions.calloc
887# define rb_gc_impl_realloc rb_gc_functions.realloc
888# define rb_gc_impl_free rb_gc_functions.free
889# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
891# define rb_gc_impl_mark rb_gc_functions.mark
892# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
893# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
894# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
896# define rb_gc_impl_declare_weak_references rb_gc_functions.declare_weak_references
897# define rb_gc_impl_handle_weak_references_alive_p rb_gc_functions.handle_weak_references_alive_p
899# define rb_gc_impl_register_pinning_obj rb_gc_functions.register_pinning_obj
900# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
901# define rb_gc_impl_location rb_gc_functions.location
903# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
904# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
905# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
907# define rb_gc_impl_each_objects rb_gc_functions.each_objects
908# define rb_gc_impl_each_object rb_gc_functions.each_object
910# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
911# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
912# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
913# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
914# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
916# define rb_gc_impl_before_fork rb_gc_functions.before_fork
917# define rb_gc_impl_after_fork rb_gc_functions.after_fork
919# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
920# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
921# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
922# define rb_gc_impl_gc_count rb_gc_functions.gc_count
923# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
924# define rb_gc_impl_stat rb_gc_functions.stat
925# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
926# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
928# define rb_gc_impl_object_metadata rb_gc_functions.object_metadata
929# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
930# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
931# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
932# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
935#ifdef RUBY_ASAN_ENABLED
937asan_death_callback(
void)
940 rb_bug_without_die(
"ASAN error");
948rb_objspace_alloc(
void)
951 ruby_modular_gc_init();
954 void *
objspace = rb_gc_impl_objspace_alloc();
955 ruby_current_vm_ptr->gc.objspace =
objspace;
957 rb_gc_impl_stress_set(
objspace, initial_stress);
959#ifdef RUBY_ASAN_ENABLED
960 __sanitizer_set_death_callback(asan_death_callback);
973rb_gc_obj_slot_size(
VALUE obj)
975 return rb_gc_impl_obj_slot_size(obj);
979gc_validate_pc(
VALUE obj)
987 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && CFP_PC(cfp)) {
988 const VALUE *iseq_encoded = ISEQ_BODY(CFP_ISEQ(cfp))->iseq_encoded;
989 const VALUE *iseq_encoded_end = iseq_encoded + ISEQ_BODY(CFP_ISEQ(cfp))->iseq_size;
990 RUBY_ASSERT(CFP_PC(cfp) >= iseq_encoded,
"PC not set when allocating, breaking tracing");
991 RUBY_ASSERT(CFP_PC(cfp) <= iseq_encoded_end,
"PC not set when allocating, breaking tracing");
996NOINLINE(
static void gc_newobj_hook(
VALUE obj));
998gc_newobj_hook(
VALUE obj)
1000 int lev = RB_GC_VM_LOCK_NO_BARRIER();
1002 size_t slot_size = rb_gc_obj_slot_size(obj);
1003 memset((
char *)obj +
sizeof(
struct RBasic), 0, slot_size -
sizeof(
struct RBasic));
1010 bool gc_disabled =
RTEST(rb_gc_disable_no_rest());
1014 if (!gc_disabled) rb_gc_enable();
1016 RB_GC_VM_UNLOCK_NO_BARRIER(lev);
1024 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, wb_protected, size);
1026#if RACTOR_CHECK_MODE
1027 void rb_ractor_setup_belonging(
VALUE obj);
1028 rb_ractor_setup_belonging(obj);
1031 RBASIC_SET_SHAPE_ID_NO_CHECKS(obj, shape_id);
1033 gc_validate_pc(obj);
1036 gc_newobj_hook(obj);
1039#if RGENGC_CHECK_MODE
1040# ifndef GC_DEBUG_SLOT_FILL_SPECIAL_VALUE
1041# define GC_DEBUG_SLOT_FILL_SPECIAL_VALUE 255
1045 (
void *)(obj +
sizeof(
struct RBasic)),
1046 GC_DEBUG_SLOT_FILL_SPECIAL_VALUE,
1047 rb_gc_obj_slot_size(obj) -
sizeof(
struct RBasic)
1057 return rb_newobj(ec, klass, flags, ROOT_SHAPE_ID,
true, size);
1061rb_newobj_of_with_shape(
VALUE klass,
VALUE flags, shape_id_t shape_id,
size_t size)
1063 return rb_newobj(GET_EC(), klass, flags, shape_id,
true, size);
1067rb_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
1069 return rb_newobj(GET_EC(), klass, flags, ROOT_SHAPE_ID,
true, size);
1073VALUE class_allocate_complex_instance(
VALUE klass, uint32_t capacity)
1075 shape_id_t initial_shape_id = rb_shape_root(rb_gc_heap_id_for_size(
sizeof(
struct RObject)));
1076 VALUE obj = rb_newobj_of_with_shape(klass,
T_OBJECT, initial_shape_id,
sizeof(
struct RObject));
1077 rb_obj_init_complex(obj, rb_st_init_numtable_with_size(capacity));
1082rb_class_allocate_instance(
VALUE klass)
1084 uint32_t index_tbl_num_entries = RCLASS_MAX_IV_COUNT(klass);
1089 if (RB_UNLIKELY(index_tbl_num_entries > rb_shape_tree.max_capacity)) {
1090 obj = class_allocate_complex_instance(klass, index_tbl_num_entries);
1093 size_t size = rb_obj_embedded_size(index_tbl_num_entries);
1094 if (!rb_gc_size_allocatable_p(size)) {
1095 size =
sizeof(
struct RObject);
1100 obj = rb_newobj_of_with_shape(klass,
T_OBJECT, rb_shape_root(rb_gc_heap_id_for_size(size)), size);
1104 size_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
1105 for (
size_t i = fields_count; i < ROBJECT_FIELDS_CAPACITY(obj); i++) {
1113 rb_bug(
"Expected rb_class_allocate_instance to set the class correctly");
1121rb_gc_register_pinning_obj(
VALUE obj)
1123 rb_gc_impl_register_pinning_obj(rb_gc_get_objspace(), obj);
1126#define UNEXPECTED_NODE(func) \
1127 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1128 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1131rb_data_object_check(
VALUE klass)
1136 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1144 if (klass) rb_data_object_check(klass);
1147 rb_gc_register_pinning_obj(obj);
1165#define RTYPEDDATA_EMBEDDED_P rbimpl_typeddata_embedded_p
1166#define RB_DATA_TYPE_EMBEDDABLE_P(type) ((type)->flags & RUBY_TYPED_EMBEDDABLE)
1167#define RTYPEDDATA_EMBEDDABLE_P(obj) RB_DATA_TYPE_EMBEDDABLE_P(RTYPEDDATA_TYPE(obj))
1172 RBIMPL_NONNULL_ARG(
type);
1173 if (klass) rb_data_object_check(klass);
1175 VALUE obj = rb_newobj(GET_EC(), klass,
T_DATA | RUBY_TYPED_FL_IS_TYPED_DATA, ROOT_SHAPE_ID, wb_protected, size);
1177 rb_gc_register_pinning_obj(obj);
1190 if (UNLIKELY(RB_DATA_TYPE_EMBEDDABLE_P(
type))) {
1191 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1194 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
1200 if (RB_DATA_TYPE_EMBEDDABLE_P(
type)) {
1202 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1206 if (rb_gc_size_allocatable_p(embed_size)) {
1207 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1208 memset((
char *)obj + offsetof(
struct RTypedData,
data), 0, size);
1219ruby_xmalloc_usable_size(
void *ptr)
1221#ifdef HAVE_MALLOC_USABLE_SIZE
1222#if CALC_EXACT_MALLOC_SIZE
1226 return malloc_usable_size(ptr);
1234rb_objspace_data_type_memsize(
VALUE obj)
1238 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1241 if (RTYPEDDATA_EMBEDDABLE_P(obj) && !RTYPEDDATA_EMBEDDED_P(obj)) {
1242 size += ruby_xmalloc_usable_size((
void *)ptr);
1246 if (
type->function.dsize) {
1247 size +=
type->function.dsize(ptr);
1256rb_objspace_data_type_name(
VALUE obj)
1267rb_gc_declare_weak_references(
VALUE obj)
1269 rb_gc_impl_declare_weak_references(rb_gc_get_objspace(), obj);
1273rb_gc_handle_weak_references_alive_p(
VALUE obj)
1277 return rb_gc_impl_handle_weak_references_alive_p(rb_gc_get_objspace(), obj);
1281rb_gc_handle_weak_references(
VALUE obj)
1288 if (
type->function.handle_weak_references) {
1289 (
type->function.handle_weak_references)(RTYPEDDATA_GET_DATA(obj));
1293 "rb_gc_handle_weak_references: TypedData %s does not implement handle_weak_references",
1299 rb_bug(
"rb_gc_handle_weak_references: unknown T_DATA");
1304 switch (imemo_type(obj)) {
1305 case imemo_callcache: {
1307 if (cc->klass !=
Qundef &&
1308 (!rb_gc_handle_weak_references_alive_p(cc->klass) ||
1309 !rb_gc_handle_weak_references_alive_p((
VALUE)cc->cme_))) {
1310 vm_cc_invalidate(cc);
1314 case imemo_subclasses: {
1316 VALUE *entries = rb_imemo_subclasses_entries(obj);
1317 for (uint32_t i = 0; i < subs->count; i++) {
1318 if (entries[i] && !rb_gc_handle_weak_references_alive_p(entries[i])) {
1325 rb_bug(
"rb_gc_handle_weak_references: unexpected imemo type");
1331 rb_bug(
"rb_gc_handle_weak_references: type not supported\n");
1336rb_gc_imemo_needs_cleanup_p(
VALUE obj)
1338 switch (imemo_type(obj)) {
1339 case imemo_constcache:
1344 case imemo_callcache:
1345 case imemo_throw_data:
1346 case imemo_cvar_entry:
1352 case imemo_callinfo:
1355 case imemo_subclasses:
1362 return FL_TEST_RAW(obj, OBJ_FIELD_HEAP) || (id2ref_tbl && rb_obj_shape_has_id(obj));
1381rb_gc_obj_needs_cleanup_p(
VALUE obj)
1389 return rb_gc_imemo_needs_cleanup_p(obj);
1413 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1414 if (id2ref_tbl && rb_shape_has_object_id(shape_id))
return true;
1418 if (flags & ROBJECT_HEAP)
return true;
1422 if (flags & RUBY_TYPED_FL_IS_TYPED_DATA) {
1424 if (
type & TYPED_DATA_EMBEDDED) {
1434 if (flags & (RSTRING_NOEMBED | RSTRING_FSTR))
return true;
1435 return rb_shape_has_fields(shape_id);
1438 if (!(flags & RARRAY_EMBED_FLAG))
return true;
1439 return rb_shape_has_fields(shape_id);
1442 if (flags & RHASH_ST_TABLE_FLAG)
return true;
1443 return rb_shape_has_fields(shape_id);
1446 if ((flags & (RMATCH_ONIG | RMATCH_OFFSETS_EXTERNAL)) || USE_DEBUG_COUNTER)
return true;
1447 return rb_shape_has_fields(shape_id);
1450 if (!(flags & BIGNUM_EMBED_FLAG))
return true;
1451 return rb_shape_has_fields(shape_id);
1454 if (!(flags & RSTRUCT_EMBED_LEN_MASK))
return true;
1455 if (flags & RSTRUCT_GEN_FIELDS)
return rb_shape_has_fields(shape_id);
1461 return rb_shape_has_fields(shape_id);
1469io_fptr_finalize(
void *fptr)
1478 rb_gc_impl_make_zombie(
objspace, obj, io_fptr_finalize, fptr);
1486 int free_immediately =
false;
1487 void (*dfree)(
void *);
1494 dfree =
RDATA(obj)->dfree;
1499 if (!
RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1501 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1504 else if (free_immediately) {
1506 if (RTYPEDDATA_EMBEDDABLE_P(obj) && !RTYPEDDATA_EMBEDDED_P(obj)) {
1510 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1513 rb_gc_impl_make_zombie(
objspace, obj, dfree, data);
1514 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1519 RB_DEBUG_COUNTER_INC(obj_data_empty);
1536 rb_class_classext_free(args->klass, ext, is_prime);
1544 rb_iclass_classext_free(args->klass, ext, is_prime);
1552 RB_DEBUG_COUNTER_INC(obj_free);
1559 rb_bug(
"obj_free() called for broken object");
1568 if (rb_obj_shape_complex_p(obj)) {
1569 RB_DEBUG_COUNTER_INC(obj_obj_complex);
1570 st_free_table(ROBJECT_FIELDS_HASH(obj));
1573 SIZED_FREE_N(
ROBJECT(obj)->as.heap.fields, ROBJECT_FIELDS_CAPACITY(obj));
1574 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1578 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1584 rb_zjit_klass_free(obj);
1587 rb_class_classext_foreach(obj, classext_free, (
void *)&args);
1588 if (RCLASS_CLASSEXT_TBL(obj)) {
1589 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1601#if USE_DEBUG_COUNTER
1604 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1607 RB_DEBUG_COUNTER_INC(obj_hash_1);
1610 RB_DEBUG_COUNTER_INC(obj_hash_2);
1613 RB_DEBUG_COUNTER_INC(obj_hash_3);
1616 RB_DEBUG_COUNTER_INC(obj_hash_4);
1622 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1626 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1629 if (RHASH_AR_TABLE_P(obj)) {
1630 if (RHASH_AR_TABLE(obj) == NULL) {
1631 RB_DEBUG_COUNTER_INC(obj_hash_null);
1634 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1638 RB_DEBUG_COUNTER_INC(obj_hash_st);
1647 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1651 if (!rb_data_free(
objspace, obj))
return false;
1656#if USE_DEBUG_COUNTER
1658 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1661 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1664 RB_DEBUG_COUNTER_INC(obj_match_under4);
1668 onig_region_free(&rm->
as.onig, 0);
1672 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1676 if (
RFILE(obj)->fptr) {
1678 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1683 RB_DEBUG_COUNTER_INC(obj_rational);
1686 RB_DEBUG_COUNTER_INC(obj_complex);
1693 rb_class_classext_foreach(obj, classext_iclass_free, (
void *)&args);
1694 if (RCLASS_CLASSEXT_TBL(obj)) {
1695 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1698 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1702 RB_DEBUG_COUNTER_INC(obj_float);
1706 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1707 SIZED_FREE_N(BIGNUM_DIGITS(obj), BIGNUM_LEN(obj));
1708 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1711 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1716 UNEXPECTED_NODE(obj_free);
1720 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1721 RSTRUCT(obj)->
as.heap.ptr == NULL) {
1722 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1725 SIZED_FREE_N(RSTRUCT(obj)->
as.heap.ptr, RSTRUCT(obj)->
as.heap.len);
1726 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1731 RB_DEBUG_COUNTER_INC(obj_symbol);
1735 rb_imemo_free((
VALUE)obj);
1739 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1744 rb_gc_impl_make_zombie(
objspace, obj, 0, 0);
1755 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1759internal_object_p(
VALUE obj)
1761 void *ptr = asan_unpoison_object_temporary(obj);
1763 if (
RBASIC(obj)->flags) {
1766 UNEXPECTED_NODE(internal_object_p);
1775 if (obj == rb_mRubyVMFrozenCore)
1779 if (RCLASS_SINGLETON_P(obj)) {
1780 return rb_singleton_class_internal_p(obj);
1784 if (!
RBASIC(obj)->klass)
break;
1788 if (ptr || !
RBASIC(obj)->flags) {
1789 rb_asan_poison_object(obj);
1795rb_objspace_internal_object_p(
VALUE obj)
1797 return internal_object_p(obj);
1806os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1811 for (; v != (
VALUE)vend; v += stride) {
1812 if (!internal_object_p(v)) {
1832 rb_objspace_each_objects(os_obj_of_i, &oes);
1883 return os_obj_of(of);
1897 return rb_undefine_finalizer(obj);
1903 rb_check_frozen(obj);
1905 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1911should_be_callable(
VALUE block)
1914 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
1920should_be_finalizable(
VALUE obj)
1923 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
1926 rb_check_frozen(obj);
1932 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
2012 if (rb_callable_receiver(block) == obj) {
2013 rb_warn(
"finalizer references object to be finalized");
2016 return rb_define_finalizer(obj, block);
2022 should_be_finalizable(obj);
2023 should_be_callable(block);
2025 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
2033rb_objspace_call_finalizer(
void)
2035 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
2039rb_objspace_free_objects(
void *
objspace)
2041 rb_gc_impl_shutdown_free_objects(
objspace);
2045rb_objspace_garbage_object_p(
VALUE obj)
2047 return !
SPECIAL_CONST_P(obj) && rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
2051rb_gc_pointer_to_heap_p(
VALUE obj)
2053 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj);
2056#define OBJ_ID_INCREMENT (RUBY_IMMEDIATE_MASK + 1)
2057#define LAST_OBJECT_ID() (object_id_counter * OBJ_ID_INCREMENT)
2058static VALUE id2ref_value = 0;
2060#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
2061static size_t object_id_counter = 1;
2063static unsigned long long object_id_counter = 1;
2067generate_next_object_id(
void)
2069#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
2073 unsigned int lock_lev = RB_GC_VM_LOCK();
2074 VALUE id =
ULL2NUM(++object_id_counter * OBJ_ID_INCREMENT);
2075 RB_GC_VM_UNLOCK(lock_lev);
2081rb_gc_obj_id_moved(
VALUE obj)
2083 if (UNLIKELY(id2ref_tbl)) {
2084 st_insert(id2ref_tbl, (st_data_t)rb_obj_id(obj), (st_data_t)obj);
2089object_id_cmp(st_data_t x, st_data_t y)
2092 return !rb_big_eql(x, y);
2100object_id_hash(st_data_t n)
2105static const struct st_hash_type object_id_hash_type = {
2110static void gc_mark_tbl_no_pin(
st_table *table);
2113id2ref_tbl_mark(
void *data)
2125id2ref_tbl_memsize(
const void *data)
2127 return rb_st_memsize(data);
2131id2ref_tbl_free(
void *data)
2135 st_free_table(table);
2141 .dmark = id2ref_tbl_mark,
2142 .dfree = id2ref_tbl_free,
2143 .dsize = id2ref_tbl_memsize,
2151class_object_id(
VALUE klass)
2153 VALUE id = RUBY_ATOMIC_VALUE_LOAD(
RCLASS(klass)->object_id);
2155 unsigned int lock_lev = RB_GC_VM_LOCK();
2156 id = generate_next_object_id();
2161 else if (RB_UNLIKELY(id2ref_tbl)) {
2162 st_insert(id2ref_tbl,
id, klass);
2164 RB_GC_VM_UNLOCK(lock_lev);
2170object_id_get(
VALUE obj, shape_id_t shape_id)
2173 if (rb_shape_complex_p(shape_id)) {
2174 id = rb_obj_field_get(obj, ROOT_COMPLEX_WITH_OBJ_ID);
2177 id = rb_obj_field_get(obj, rb_shape_object_id(shape_id));
2183 rb_bug(
"Object's shape includes object_id, but it's missing %s", rb_obj_info(obj));
2191object_id0(
VALUE obj)
2194 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2196 if (rb_shape_has_object_id(shape_id)) {
2197 return object_id_get(obj, shape_id);
2200 shape_id_t object_id_shape_id = rb_obj_shape_transition_object_id(obj);
2202 id = generate_next_object_id();
2203 rb_obj_field_set(obj, object_id_shape_id, 0,
id);
2205 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == object_id_shape_id);
2208 if (RB_UNLIKELY(id2ref_tbl)) {
2210 st_insert(id2ref_tbl, (st_data_t)
id, (st_data_t)obj);
2225 return class_object_id(obj);
2234 unsigned int lock_lev = RB_GC_VM_LOCK();
2235 VALUE id = object_id0(obj);
2236 RB_GC_VM_UNLOCK(lock_lev);
2240 return object_id0(obj);
2244build_id2ref_i(
VALUE obj,
void *data)
2252 if (
RCLASS(obj)->object_id) {
2253 st_insert(id2ref_tbl,
RCLASS(obj)->object_id, obj);
2258 if (IMEMO_TYPE_P(obj, imemo_fields) && rb_obj_shape_has_id(obj)) {
2259 st_insert(id2ref_tbl, rb_obj_id(obj), rb_imemo_fields_owner(obj));
2264 if (rb_obj_shape_has_id(obj)) {
2265 st_insert(id2ref_tbl, rb_obj_id(obj), obj);
2275object_id_to_ref(
void *objspace_ptr,
VALUE object_id)
2279 unsigned int lev = RB_GC_VM_LOCK();
2287 st_table *tmp_id2ref_tbl = st_init_table(&object_id_hash_type);
2293 bool gc_disabled =
RTEST(rb_gc_disable());
2295 id2ref_tbl = tmp_id2ref_tbl;
2296 id2ref_value = tmp_id2ref_value;
2298 rb_gc_impl_each_object(
objspace, build_id2ref_i, (
void *)id2ref_tbl);
2300 if (!gc_disabled) rb_gc_enable();
2304 bool found = st_lookup(id2ref_tbl, object_id, &obj) && !rb_gc_impl_garbage_object_p(
objspace, obj);
2306 RB_GC_VM_UNLOCK(lev);
2321obj_free_object_id(
VALUE obj)
2324 if (RB_UNLIKELY(id2ref_tbl)) {
2328 obj_id =
RCLASS(obj)->object_id;
2331 if (!IMEMO_TYPE_P(obj, imemo_fields)) {
2337 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2338 if (rb_shape_has_object_id(shape_id)) {
2339 obj_id = object_id_get(obj, shape_id);
2348 if (RB_UNLIKELY(obj_id)) {
2351 if (!st_delete(id2ref_tbl, (st_data_t *)&obj_id, NULL)) {
2355 rb_bug(
"Object ID seen, but not in _id2ref table: object_id=%llu object=%s",
NUM2ULL(obj_id), rb_obj_info(obj));
2363rb_gc_obj_free_vm_weak_references(
VALUE obj)
2366 obj_free_object_id(obj);
2368 if (rb_obj_gen_fields_p(obj)) {
2375 rb_gc_free_fstring(obj);
2379 rb_gc_free_dsymbol(obj);
2382 switch (imemo_type(obj)) {
2383 case imemo_callinfo:
2419 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
2430 if (rb_static_id_valid_p(
SYM2ID(ptr))) {
2434 rb_raise(
rb_eRangeError,
"%p is not a symbol id value", (
void *)ptr);
2438 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is not an id value", rb_int2str(objid, 10));
2442 VALUE obj = object_id_to_ref(rb_gc_get_objspace(), objid);
2447 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is the id of an unshareable object on multi-ractor", rb_int2str(objid, 10));
2456 return id2ref(objid);
2463#if SIZEOF_LONG == SIZEOF_VOIDP
2470 return get_heap_object_id(obj);
2474nonspecial_obj_id(
VALUE obj)
2476#if SIZEOF_LONG == SIZEOF_VOIDP
2478#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2481# error not supported
2488 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
2525 return rb_find_object_id(rb_gc_get_objspace(), obj, object_id);
2529rb_obj_id_p(
VALUE obj)
2541rb_gc_before_updating_jit_code(
void)
2544 rb_yjit_mark_all_writeable();
2547 rb_zjit_mark_all_writable();
2557rb_gc_after_updating_jit_code(
void)
2560 rb_yjit_mark_all_executable();
2563 rb_zjit_mark_all_executable();
2570 size_t *size = (
size_t *)arg;
2573 if (RCLASSEXT_M_TBL(ext)) {
2574 s += rb_id_table_memsize(RCLASSEXT_M_TBL(ext));
2576 if (RCLASSEXT_CONST_TBL(ext)) {
2577 s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
2579 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2580 s += (RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1) *
sizeof(
VALUE);
2589classext_superclasses_memsize(
rb_classext_t *ext,
bool prime,
VALUE box_value,
void *arg)
2591 size_t *size = (
size_t *)arg;
2593 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2595 array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
2596 *size += array_size *
sizeof(
VALUE);
2601rb_obj_memsize_of(
VALUE obj)
2612 if (rb_obj_shape_complex_p(obj)) {
2613 size += rb_st_memsize(ROBJECT_FIELDS_HASH(obj));
2616 size += ROBJECT_FIELDS_CAPACITY(obj) *
sizeof(
VALUE);
2622 rb_class_classext_foreach(obj, classext_memsize, (
void *)&size);
2623 rb_class_classext_foreach(obj, classext_superclasses_memsize, (
void *)&size);
2626 if (RICLASS_OWNS_M_TBL_P(obj)) {
2627 if (RCLASS_M_TBL(obj)) {
2628 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
2633 size += rb_str_memsize(obj);
2636 size += rb_ary_memsize(obj);
2639 if (RHASH_ST_TABLE_P(obj)) {
2640 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
2642 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(
st_table);
2651 size += rb_objspace_data_type_memsize(obj);
2657 size += onig_region_memsize(&rm->
as.onig);
2663 if (
RFILE(obj)->fptr) {
2664 size += rb_io_memsize(
RFILE(obj)->fptr);
2671 size += rb_imemo_memsize(obj);
2679 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2680 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
2685 UNEXPECTED_NODE(obj_memsize_of);
2689 if (RSTRUCT_EMBED_LEN(obj) == 0) {
2690 size +=
sizeof(
VALUE) * RSTRUCT_LEN_RAW(obj);
2699 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
2703 return size + rb_gc_obj_slot_size(obj);
2707set_zero(st_data_t key, st_data_t val, st_data_t arg)
2711 rb_hash_aset(hash, k,
INT2FIX(0));
2722count_objects_i(
VALUE obj,
void *d)
2726 if (
RBASIC(obj)->flags) {
2777count_objects(
int argc,
VALUE *argv,
VALUE os)
2789 for (
size_t i = 0; i <=
T_MASK; i++) {
2793 types[i] = type_sym(i);
2800 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2803 hash = rb_hash_new();
2806 rb_hash_stlike_foreach(hash, set_zero, hash);
2808 rb_hash_aset(hash, total,
SIZET2NUM(data.total));
2809 rb_hash_aset(hash, free,
SIZET2NUM(data.freed));
2811 for (
size_t i = 0; i <=
T_MASK; i++) {
2812 if (data.counts[i]) {
2813 rb_hash_aset(hash, types[i],
SIZET2NUM(data.counts[i]));
2820#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2822#define STACK_START (ec->machine.stack_start)
2823#define STACK_END (ec->machine.stack_end)
2824#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2826#if STACK_GROW_DIRECTION < 0
2827# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2828#elif STACK_GROW_DIRECTION > 0
2829# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2831# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2832 : (size_t)(STACK_END - STACK_START + 1))
2834#if !STACK_GROW_DIRECTION
2835int ruby_stack_grow_direction;
2837ruby_get_stack_grow_direction(
volatile VALUE *addr)
2840 SET_MACHINE_STACK_END(&end);
2842 if (end > addr)
return ruby_stack_grow_direction = 1;
2843 return ruby_stack_grow_direction = -1;
2852 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2853 return STACK_LENGTH;
2856#define PREVENT_STACK_OVERFLOW 1
2857#ifndef PREVENT_STACK_OVERFLOW
2858#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2859# define PREVENT_STACK_OVERFLOW 1
2861# define PREVENT_STACK_OVERFLOW 0
2864#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2870 size_t length = STACK_LENGTH;
2871 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2873 return length > maximum_length;
2876#define stack_check(ec, water_mark) FALSE
2879#define STACKFRAME_FOR_CALL_CFUNC 2048
2884 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2890 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2895#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2896 if (!RB_SPECIAL_CONST_P(obj)) { \
2897 rb_vm_t *vm = GET_VM(); \
2898 void *objspace = vm->gc.objspace; \
2899 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2900 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2901 (func)(objspace, (obj_or_ptr)); \
2903 else if (check_obj ? \
2904 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2905 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2907 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2908 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2909 vm->gc.mark_func_data = NULL; \
2910 mark_func_data->mark_func((obj), mark_func_data->data); \
2911 vm->gc.mark_func_data = mark_func_data; \
2917gc_mark_internal(
VALUE obj)
2919 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2925 gc_mark_internal(obj);
2929rb_gc_mark_and_move(
VALUE *ptr)
2931 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr,
false);
2935gc_mark_and_pin_internal(
VALUE obj)
2937 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2943 gc_mark_and_pin_internal(obj);
2947gc_mark_maybe_internal(
VALUE obj)
2949 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2955 gc_mark_maybe_internal(obj);
2958ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2960each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2971each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2973 if (end <= start)
return;
2974 each_location(start, end - start, cb, data);
2978gc_mark_maybe_each_location(
VALUE obj,
void *data)
2980 gc_mark_maybe_internal(obj);
2986 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2990rb_gc_mark_values(
long n,
const VALUE *values)
2992 for (
long i = 0; i < n; i++) {
2993 gc_mark_internal(values[i]);
2998rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
3000 for (
long i = 0; i < n; i++) {
3001 gc_mark_and_pin_internal(values[i]);
3006mark_key(st_data_t key, st_data_t value, st_data_t data)
3008 gc_mark_and_pin_internal((
VALUE)key);
3018 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
3022mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
3024 gc_mark_internal((
VALUE)key);
3025 gc_mark_internal((
VALUE)value);
3031pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
3033 gc_mark_and_pin_internal((
VALUE)key);
3034 gc_mark_and_pin_internal((
VALUE)value);
3040pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
3042 gc_mark_and_pin_internal((
VALUE)key);
3043 gc_mark_internal((
VALUE)value);
3049mark_hash(
VALUE hash)
3051 if (rb_hash_compare_by_id_p(hash)) {
3052 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
3055 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
3058 gc_mark_internal(RHASH(hash)->ifnone);
3066 st_foreach(tbl, pin_key_pin_value, 0);
3069static enum rb_id_table_iterator_result
3072 gc_mark_internal(me);
3074 return ID_TABLE_CONTINUE;
3081 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
3085static enum rb_id_table_iterator_result
3090 if (!rb_gc_checking_shareable()) {
3091 gc_mark_internal(ce->value);
3092 gc_mark_internal(ce->file);
3094 return ID_TABLE_CONTINUE;
3101 rb_id_table_foreach_values(tbl, mark_const_entry_i,
objspace);
3104#if STACK_GROW_DIRECTION < 0
3105#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
3106#elif STACK_GROW_DIRECTION > 0
3107#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
3109#define GET_STACK_BOUNDS(start, end, appendix) \
3110 ((STACK_END < STACK_START) ? \
3111 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
3115gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
3117 gc_mark_maybe_internal(obj);
3119#ifdef RUBY_ASAN_ENABLED
3121 void *fake_frame_start;
3122 void *fake_frame_end;
3123 bool is_fake_frame = asan_get_fake_stack_extents(
3124 ec->machine.asan_fake_stack_handle, obj,
3125 ec->machine.stack_start, ec->machine.stack_end,
3126 &fake_frame_start, &fake_frame_end
3128 if (is_fake_frame) {
3129 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
3141 return rb_gc_impl_object_moved_p(
objspace, obj);
3151 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)value));
3153 return rb_gc_impl_location(
objspace, value);
3159 return gc_location_internal(rb_gc_get_objspace(), value);
3162#if defined(__wasm__)
3165static VALUE *rb_stack_range_tmp[2];
3168rb_mark_locations(
void *begin,
void *end)
3170 rb_stack_range_tmp[0] = begin;
3171 rb_stack_range_tmp[1] = end;
3175rb_gc_save_machine_context(
void)
3180# if defined(__EMSCRIPTEN__)
3185 emscripten_scan_stack(rb_mark_locations);
3186 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3188 emscripten_scan_registers(rb_mark_locations);
3189 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3196 VALUE *stack_start, *stack_end;
3198 GET_STACK_BOUNDS(stack_start, stack_end, 1);
3199 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
3201 rb_wasm_scan_locals(rb_mark_locations);
3202 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3210rb_gc_save_machine_context(
void)
3214 RB_VM_SAVE_MACHINE_CONTEXT(thread);
3221 rb_gc_mark_machine_context(ec);
3228 VALUE *stack_start, *stack_end;
3230 GET_STACK_BOUNDS(stack_start, stack_end, 0);
3231 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
3234#ifdef RUBY_ASAN_ENABLED
3241 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
3242 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
3243 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
3247rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
3249 gc_mark_and_pin_internal((
VALUE)value);
3257 if (!tbl || tbl->num_entries == 0)
return;
3259 st_foreach(tbl, rb_mark_tbl_i, 0);
3265 if (!tbl || tbl->num_entries == 0)
return;
3267 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
3273 gc_mark_tbl_no_pin(tbl);
3279 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
3285 void *
objspace = rb_gc_get_objspace();
3287 if (RB_LIKELY(rb_gc_impl_during_gc_p(
objspace))) {
3288 return rb_gc_impl_get_vm_context(
objspace)->ec;
3296rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
3299 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3301#define MARK_CHECKPOINT(category) do { \
3302 if (categoryp) *categoryp = category; \
3305 MARK_CHECKPOINT(
"vm");
3308 MARK_CHECKPOINT(
"end_proc");
3311 MARK_CHECKPOINT(
"global_tbl");
3312 rb_gc_mark_global_tbl();
3315 void rb_yjit_root_mark(
void);
3317 if (rb_yjit_enabled_p) {
3318 MARK_CHECKPOINT(
"YJIT");
3319 rb_yjit_root_mark();
3324 void rb_zjit_root_mark(
void);
3325 if (rb_zjit_enabled_p) {
3326 MARK_CHECKPOINT(
"ZJIT");
3327 rb_zjit_root_mark();
3331 MARK_CHECKPOINT(
"machine_context");
3332 mark_current_machine_context(ec);
3334 MARK_CHECKPOINT(
"global_symbols");
3335 rb_sym_global_symbols_mark_and_move();
3337 MARK_CHECKPOINT(
"finish");
3339#undef MARK_CHECKPOINT
3353 if (RCLASSEXT_SUPER(ext)) {
3354 gc_mark_internal(RCLASSEXT_SUPER(ext));
3356 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3358 if (!rb_gc_checking_shareable()) {
3360 gc_mark_internal(RCLASSEXT_FIELDS_OBJ(ext));
3361 gc_mark_internal(RCLASSEXT_CVC_TBL(ext));
3364 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
3365 mark_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
3367 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3368 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3369 if (RCLASSEXT_SUBCLASSES(ext)) {
3370 gc_mark_internal(RCLASSEXT_SUBCLASSES(ext));
3372 gc_mark_internal(RCLASSEXT_CLASSPATH(ext));
3381 if (RCLASSEXT_SUPER(ext)) {
3382 gc_mark_internal(RCLASSEXT_SUPER(ext));
3384 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
3385 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3387 if (RCLASSEXT_INCLUDER(ext)) {
3388 gc_mark_internal(RCLASSEXT_INCLUDER(ext));
3390 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3391 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3392 if (RCLASSEXT_SUBCLASSES(ext)) {
3393 gc_mark_internal(RCLASSEXT_SUBCLASSES(ext));
3397#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA_TYPE(d)->function.dmark
3400rb_gc_move_obj_during_marking(
VALUE from,
VALUE to)
3402 if (rb_obj_using_gen_fields_table_p(to)) {
3403 rb_mark_generic_ivar(from);
3412 if (rb_obj_using_gen_fields_table_p(obj)) {
3413 rb_mark_generic_ivar(obj);
3423 rb_bug(
"rb_gc_mark() called for broken object");
3427 UNEXPECTED_NODE(rb_gc_mark);
3431 rb_imemo_mark_and_move(obj,
false);
3438 gc_mark_internal(
RBASIC(obj)->klass);
3443 !rb_gc_checking_shareable()) {
3444 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
3449 foreach_args.obj = obj;
3450 rb_class_classext_foreach(obj, gc_mark_classext_module, (
void *)&foreach_args);
3451 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3452 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3458 foreach_args.obj = obj;
3459 rb_class_classext_foreach(obj, gc_mark_classext_iclass, (
void *)&foreach_args);
3460 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3461 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3466 if (ARY_SHARED_P(obj)) {
3467 VALUE root = ARY_SHARED_ROOT(obj);
3468 gc_mark_internal(root);
3473 for (
long i = 0; i <
len; i++) {
3474 gc_mark_internal(ptr[i]);
3484 gc_mark_internal(RSYMBOL(obj)->fstr);
3488 if (STR_SHARED_P(obj)) {
3489 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
3494 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
3497 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
3504 void *
const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) :
DATA_PTR(obj);
3507 gc_mark_internal(
RTYPEDDATA(obj)->fields_obj);
3512 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3514 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3515 gc_mark_internal(*(
VALUE *)((
char *)ptr + offset));
3522 if (mark_func) (*mark_func)(ptr);
3531 if (rb_obj_shape_complex_p(obj)) {
3532 gc_mark_tbl_no_pin(ROBJECT_FIELDS_HASH(obj));
3533 len = ROBJECT_FIELDS_COUNT_COMPLEX(obj);
3538 len = ROBJECT_FIELDS_COUNT_NOT_COMPLEX(obj);
3539 for (uint32_t i = 0; i <
len; i++) {
3540 gc_mark_internal(ptr[i]);
3547 if (
RFILE(obj)->fptr) {
3548 gc_mark_internal(
RFILE(obj)->fptr->self);
3549 gc_mark_internal(
RFILE(obj)->fptr->pathv);
3550 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
3551 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
3552 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
3553 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
3554 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
3555 gc_mark_internal(
RFILE(obj)->fptr->timeout);
3556 gc_mark_internal(
RFILE(obj)->fptr->wakeup_mutex);
3561 gc_mark_internal(
RREGEXP(obj)->src);
3565 gc_mark_internal(
RMATCH(obj)->regexp);
3567 gc_mark_internal(
RMATCH(obj)->str);
3572 gc_mark_internal(RRATIONAL(obj)->num);
3573 gc_mark_internal(RRATIONAL(obj)->den);
3577 gc_mark_internal(RCOMPLEX(obj)->real);
3578 gc_mark_internal(RCOMPLEX(obj)->imag);
3583 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
3585 for (
long i = 0; i <
len; i++) {
3586 gc_mark_internal(ptr[i]);
3589 if (rb_obj_shape_has_fields(obj) && !
FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
3590 gc_mark_internal(RSTRUCT_FIELDS_OBJ(obj));
3600 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
3602 rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)obj) ?
"corrupted object" :
"non object");
3607rb_gc_obj_optimal_size(
VALUE obj)
3612 size_t size = rb_ary_size_as_embedded(obj);
3613 if (rb_gc_size_allocatable_p(size)) {
3617 return sizeof(
struct RArray);
3622 if (rb_obj_shape_complex_p(obj)) {
3623 return sizeof(
struct RObject);
3626 size_t size = rb_obj_embedded_size(ROBJECT_FIELDS_CAPACITY(obj));
3627 if (rb_gc_size_allocatable_p(size)) {
3631 return sizeof(
struct RObject);
3637 size_t size = rb_str_size_as_embedded(obj);
3638 if (rb_gc_size_allocatable_p(size)) {
3642 return sizeof(
struct RString);
3651 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(
st_table) : sizeof(ar_table));
3660rb_gc_writebarrier(VALUE a, VALUE b)
3662 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
3668 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
3675rb_gc_writebarrier_remember(
VALUE obj)
3677 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
3683 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
3687rb_gc_modular_gc_loaded_p(
void)
3690 return rb_gc_functions.modular_gc_loaded_p;
3697rb_gc_active_gc_name(
void)
3699 const char *gc_name = rb_gc_impl_active_gc_name();
3701 const size_t len = strlen(gc_name);
3702 if (
len > RB_GC_MAX_NAME_LEN) {
3703 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
3704 RB_GC_MAX_NAME_LEN,
len, gc_name);
3711rb_gc_object_metadata(
VALUE obj)
3713 return rb_gc_impl_object_metadata(rb_gc_get_objspace(), obj);
3721 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
3725rb_gc_ractor_cache_free(
void *cache)
3727 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3733 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
3736 rb_vm_register_global_object(obj);
3748 tmp->next = vm->global_object_list;
3750 vm->global_object_list = tmp;
3759 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
3761 rb_print_backtrace(stderr);
3771 tmp = vm->global_object_list;
3772 if (tmp->varptr == addr) {
3773 vm->global_object_list = tmp->next;
3778 if (tmp->next->varptr == addr) {
3781 tmp->next = tmp->next->next;
3794 rb_gc_register_address(var);
3800 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
3844rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
3846 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3852 if (ARY_SHARED_P(v)) {
3853 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
3857 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
3859 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3860 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
3861 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
3862 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
3870 for (
long i = 0; i <
len; i++) {
3875 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3876 if (rb_ary_embeddable_p(v)) {
3877 rb_ary_make_embedded(v);
3889 if (rb_obj_shape_complex_p(v)) {
3890 gc_ref_update_table_values_only(ROBJECT_FIELDS_HASH(v));
3894 size_t slot_size = rb_gc_obj_slot_size(v);
3895 size_t embed_size = rb_obj_embedded_size(ROBJECT_FIELDS_CAPACITY(v));
3896 if (slot_size >= embed_size) {
3898 memcpy(
ROBJECT(v)->as.ary, ptr,
sizeof(
VALUE) * ROBJECT_FIELDS_COUNT(v));
3899 SIZED_FREE_N(ptr, ROBJECT_FIELDS_CAPACITY(v));
3905 for (uint32_t i = 0; i < ROBJECT_FIELDS_COUNT(v); i++) {
3911rb_gc_ref_update_table_values_only(
st_table *tbl)
3913 gc_ref_update_table_values_only(tbl);
3920 gc_update_table_refs(ptr);
3926 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
3932 for (
long i = 0; i < n; i++) {
3933 UPDATE_IF_MOVED(
objspace, values[i]);
3938rb_gc_update_values(
long n,
VALUE *values)
3940 gc_update_values(rb_gc_get_objspace(), n, values);
3943static enum rb_id_table_iterator_result
3944check_id_table_move(
VALUE value,
void *data)
3949 return ID_TABLE_REPLACE;
3952 return ID_TABLE_CONTINUE;
3956rb_gc_prepare_heap_process_object(
VALUE obj)
3962 rb_enc_str_coderange(obj);
3970rb_gc_prepare_heap(
void)
3972 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3976rb_gc_heap_id_for_size(
size_t size)
3978 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3982rb_gc_size_allocatable_p(
size_t size)
3984 return rb_gc_impl_size_allocatable_p(size);
3987static enum rb_id_table_iterator_result
3988update_id_table(
VALUE *value,
void *data,
int existing)
3996 return ID_TABLE_CONTINUE;
4003 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
4007static enum rb_id_table_iterator_result
4012 if (gc_object_moved_p_internal(
objspace, ce->value)) {
4013 ce->value = gc_location_internal(
objspace, ce->value);
4016 if (gc_object_moved_p_internal(
objspace, ce->file)) {
4017 ce->file = gc_location_internal(
objspace, ce->file);
4020 return ID_TABLE_CONTINUE;
4027 rb_id_table_foreach_values(tbl, update_const_tbl_i,
objspace);
4033 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
4034 size_t array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
4035 for (
size_t i = 0; i < array_size; i++) {
4036 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPERCLASSES(ext)[i]);
4044 UPDATE_IF_MOVED(
objspace, RCLASSEXT_ORIGIN(ext));
4045 UPDATE_IF_MOVED(
objspace, RCLASSEXT_REFINED_CLASS(ext));
4046 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CLASSPATH(ext));
4048 UPDATE_IF_MOVED(
objspace, RCLASSEXT_INCLUDER(ext));
4058 if (RCLASSEXT_SUPER(ext)) {
4059 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
4062 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
4064 UPDATE_IF_MOVED(
objspace, ext->fields_obj);
4065 if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
4066 update_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
4068 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
4069 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
4070 update_superclasses(
objspace, ext);
4071 if (RCLASSEXT_SUBCLASSES(ext)) {
4072 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUBCLASSES(ext));
4075 update_classext_values(
objspace, ext,
false);
4084 if (RCLASSEXT_SUPER(ext)) {
4085 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
4087 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
4088 update_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
4089 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
4090 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
4091 if (RCLASSEXT_SUBCLASSES(ext)) {
4092 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUBCLASSES(ext));
4095 update_classext_values(
objspace, ext,
true);
4099 vm_table_foreach_callback_func callback;
4100 vm_table_update_callback_func update_callback;
4106vm_weak_table_foreach_weak_key(st_data_t key, st_data_t value, st_data_t data,
int error)
4110 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4112 if (!iter_data->weak_only) {
4113 if (ret != ST_CONTINUE)
return ret;
4115 ret = iter_data->callback((
VALUE)value, iter_data->data);
4122vm_weak_table_foreach_update_weak_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
4126 int ret = iter_data->update_callback((
VALUE *)key, iter_data->data);
4128 if (!iter_data->weak_only) {
4129 if (ret != ST_CONTINUE)
return ret;
4131 ret = iter_data->update_callback((
VALUE *)value, iter_data->data);
4138vm_weak_table_sym_set_foreach(
VALUE *sym_ptr,
void *data)
4140 VALUE sym = *sym_ptr;
4145 int ret = iter_data->callback(sym, iter_data->data);
4147 if (ret == ST_REPLACE) {
4148 ret = iter_data->update_callback(sym_ptr, iter_data->data);
4154struct st_table *rb_generic_fields_tbl_get(
void);
4157vm_weak_table_id2ref_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
4162 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4163 if (ret != ST_CONTINUE)
return ret;
4166 return iter_data->callback((
VALUE)value, iter_data->data);
4170vm_weak_table_id2ref_foreach_update(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
4174 iter_data->update_callback((
VALUE *)value, iter_data->data);
4177 iter_data->update_callback((
VALUE *)key, iter_data->data);
4184vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
4188 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4198 RBASIC_SET_SHAPE_ID((
VALUE)key, ROOT_SHAPE_ID);
4202 ret = iter_data->update_callback(&new_key, iter_data->data);
4203 if (key != new_key) {
4210 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ret);
4213 if (!iter_data->weak_only) {
4214 int ivar_ret = iter_data->callback(new_value, iter_data->data);
4220 iter_data->update_callback(&new_value, iter_data->data);
4224 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ivar_ret);
4228 if (key != new_key || value != new_value) {
4229 DURING_GC_COULD_MALLOC_REGION_START();
4231 st_insert(rb_generic_fields_tbl_get(), (st_data_t)new_key, new_value);
4233 DURING_GC_COULD_MALLOC_REGION_END();
4240vm_weak_table_frozen_strings_foreach(
VALUE *str,
void *data)
4244 int retval = iter_data->callback(*str, iter_data->data);
4246 if (retval == ST_REPLACE) {
4247 retval = iter_data->update_callback(str, iter_data->data);
4250 if (retval == ST_DELETE) {
4257void rb_fstring_foreach_with_replace(
int (*callback)(
VALUE *str,
void *data),
void *data);
4259rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
4260 vm_table_update_callback_func update_callback,
4263 enum rb_gc_vm_weak_tables table)
4268 .callback = callback,
4269 .update_callback = update_callback,
4271 .weak_only = weak_only,
4275 case RB_GC_VM_CI_TABLE: {
4276 st_foreach_with_replace(
4278 vm_weak_table_foreach_weak_key,
4279 vm_weak_table_foreach_update_weak_key,
4280 (st_data_t)&foreach_data
4284 case RB_GC_VM_OVERLOADED_CME_TABLE: {
4285 st_foreach_with_replace(
4286 &vm->overloaded_cme_table,
4287 vm_weak_table_foreach_weak_key,
4288 vm_weak_table_foreach_update_weak_key,
4289 (st_data_t)&foreach_data
4293 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
4294 rb_sym_global_symbol_table_foreach_weak_reference(
4295 vm_weak_table_sym_set_foreach,
4300 case RB_GC_VM_ID2REF_TABLE: {
4302 st_foreach_with_replace(
4304 vm_weak_table_id2ref_foreach,
4305 vm_weak_table_id2ref_foreach_update,
4306 (st_data_t)&foreach_data
4311 case RB_GC_VM_GENERIC_FIELDS_TABLE: {
4312 st_table *generic_fields_tbl = rb_generic_fields_tbl_get();
4313 if (generic_fields_tbl) {
4316 vm_weak_table_gen_fields_foreach,
4317 (st_data_t)&foreach_data
4322 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
4323 rb_fstring_foreach_with_replace(
4324 vm_weak_table_frozen_strings_foreach,
4329 case RB_GC_VM_WEAK_TABLE_COUNT:
4330 rb_bug(
"Unreachable");
4332 rb_bug(
"rb_gc_vm_weak_table_foreach: unknown table %d", table);
4337rb_gc_update_vm_references(
void *
objspace)
4340 rb_vm_t *vm = rb_ec_vm_ptr(ec);
4342 rb_vm_update_references(vm);
4343 rb_gc_update_global_tbl();
4344 rb_sym_global_symbols_mark_and_move();
4347 void rb_yjit_root_update_references(
void);
4349 if (rb_yjit_enabled_p) {
4350 rb_yjit_root_update_references();
4355 void rb_zjit_root_update_references(
void);
4357 if (rb_zjit_enabled_p) {
4358 rb_zjit_root_update_references();
4371 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
4377 rb_class_classext_foreach(obj, update_classext, (
void *)&args);
4382 rb_class_classext_foreach(obj, update_iclass_classext, (
void *)&args);
4386 rb_imemo_mark_and_move(obj,
true);
4398 gc_ref_update_array(
objspace, obj);
4403 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
4408 if (STR_SHARED_P(obj)) {
4414 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
4415 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
4416 rb_str_make_embedded(obj);
4426 void *
const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) :
DATA_PTR(obj);
4434 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
4436 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
4437 VALUE *ref = (
VALUE *)((
char *)ptr + offset);
4438 *ref = gc_location_internal(
objspace, *ref);
4441 else if (typed_data) {
4443 if (compact_func) (*compact_func)(ptr);
4450 gc_ref_update_object(
objspace, obj);
4454 if (
RFILE(obj)->fptr) {
4457 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
4458 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
4459 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
4471 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
4487 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
4488 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
4492 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
4493 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
4500 VALUE *ptr = (
VALUE *)RSTRUCT_CONST_PTR(obj);
4502 for (i = 0; i <
len; i++) {
4506 if (RSTRUCT_EMBED_LEN(obj)) {
4512 UPDATE_IF_MOVED(
objspace, RSTRUCT(obj)->as.heap.fields_obj);
4517 rb_bug(
"unreachable");
4534 unless_objspace(
objspace) {
return; }
4536 rb_gc_impl_start(
objspace,
true,
true,
true,
false);
4542 unless_objspace(
objspace) {
return FALSE; }
4544 return rb_gc_impl_during_gc_p(
objspace);
4550 return rb_gc_impl_gc_count(rb_gc_get_objspace());
4566 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
4569 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
4579 arg = rb_hash_new();
4585 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4590 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4603 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4608 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4623 arg = rb_hash_new();
4626 if (
NIL_P(heap_name)) {
4637 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
4640 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
4645 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4654 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
4655 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
4663 void *
objspace = rb_gc_get_objspace();
4665 rb_gc_impl_config_set(
objspace, hash);
4673 return rb_gc_impl_stress_get(rb_gc_get_objspace());
4679 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
4685rb_gc_initial_stress_set(
VALUE flag)
4687 initial_stress = flag;
4691rb_gc_heap_sizes(
void)
4693 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
4699 return rb_objspace_gc_enable(rb_gc_get_objspace());
4703rb_objspace_gc_enable(
void *
objspace)
4705 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4707 return RBOOL(disabled);
4713 return rb_gc_enable();
4719 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4720 rb_gc_impl_gc_disable(
objspace,
false);
4721 return RBOOL(disabled);
4725rb_gc_disable_no_rest(
void)
4727 return gc_disable_no_rest(rb_gc_get_objspace());
4733 return rb_objspace_gc_disable(rb_gc_get_objspace());
4737rb_objspace_gc_disable(
void *
objspace)
4739 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4740 rb_gc_impl_gc_disable(
objspace,
true);
4741 return RBOOL(disabled);
4747 return rb_gc_disable();
4752ruby_gc_set_params(
void)
4754 rb_gc_impl_set_params(rb_gc_get_objspace());
4758rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
4761 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
4765 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4766 struct gc_mark_func_data_struct mfd = {
4771 vm->gc.mark_func_data = &mfd;
4772 rb_gc_mark_children(rb_gc_get_objspace(), obj);
4773 vm->gc.mark_func_data = prev_mfd;
4779 const char *category;
4780 void (*func)(
const char *category,
VALUE,
void *);
4785root_objects_from(
VALUE obj,
void *ptr)
4788 (*data->func)(data->category, obj, data->data);
4792rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
4794 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_gc_impl_objspace_reachable_objects_from_root() is not supported while during GC");
4800 .data = passing_data,
4803 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4804 struct gc_mark_func_data_struct mfd = {
4805 .mark_func = root_objects_from,
4809 vm->gc.mark_func_data = &mfd;
4810 rb_gc_save_machine_context();
4811 rb_gc_mark_roots(vm->gc.objspace, &data.category);
4812 vm->gc.mark_func_data = prev_mfd;
4823#define TYPE_NAME(t) case (t): return #t;
4850 if (obj && rb_objspace_data_type_name(obj)) {
4851 return rb_objspace_data_type_name(obj);
4860obj_type_name(
VALUE obj)
4862 return type_name(
TYPE(obj), obj);
4866rb_method_type_name(rb_method_type_t
type)
4869 case VM_METHOD_TYPE_ISEQ:
return "iseq";
4870 case VM_METHOD_TYPE_ATTRSET:
return "attrset";
4871 case VM_METHOD_TYPE_IVAR:
return "ivar";
4872 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
4873 case VM_METHOD_TYPE_ALIAS:
return "alias";
4874 case VM_METHOD_TYPE_REFINED:
return "refined";
4875 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
4876 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
4877 case VM_METHOD_TYPE_MISSING:
return "missing";
4878 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
4879 case VM_METHOD_TYPE_UNDEF:
return "undef";
4880 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
4882 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
4886rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
4888 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
4889 VALUE path = rb_iseq_path(iseq);
4890 int n = ISEQ_BODY(iseq)->location.first_lineno;
4891 snprintf(buff, buff_size,
" %s@%s:%d",
4892 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4893 RSTRING_PTR(path), n);
4898str_len_no_raise(
VALUE str)
4900 long len = RSTRING_LEN(str);
4901 if (
len < 0)
return 0;
4902 if (
len > INT_MAX)
return INT_MAX;
4906#define BUFF_ARGS buff + pos, buff_size - pos
4907#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4908#define APPEND_S(s) do { \
4909 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4913 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4916#define C(c, s) ((c) != 0 ? (s) : " ")
4919rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
4924 APPEND_F(
"%s", obj_type_name(obj));
4930 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
4936 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
4937 APPEND_F(
"%p %s/", (
void *)obj, obj_type_name(obj));
4956 if (internal_object_p(obj)) {
4959 else if (
RBASIC(obj)->klass == 0) {
4960 APPEND_S(
"(temporary internal)");
4964 if (!
NIL_P(class_path)) {
4965 APPEND_F(
"%s ", RSTRING_PTR(class_path));
4974const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
4977rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
4984 UNEXPECTED_NODE(rb_raw_obj_info);
4987 if (ARY_SHARED_P(obj)) {
4988 APPEND_S(
"shared -> ");
4989 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4992 APPEND_F(
"[%s%s%s] ",
4993 C(ARY_EMBED_P(obj),
"E"),
4994 C(ARY_SHARED_P(obj),
"S"),
4995 C(ARY_SHARED_ROOT_P(obj),
"R"));
4997 if (ARY_EMBED_P(obj)) {
4998 APPEND_F(
"len: %ld (embed)",
5002 APPEND_F(
"len: %ld, capa:%ld ptr:%p",
5004 RARRAY(obj)->as.heap.aux.capa,
5011 C(
FL_TEST(obj, RSTRING_FSTR),
"F"),
5014 if (STR_SHARED_P(obj)) {
5015 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
5018 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
5020 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
5022 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
5026 VALUE fstr = RSYMBOL(obj)->fstr;
5027 ID id = RSYMBOL(obj)->id;
5029 APPEND_F(
":%s id:%d", RSTRING_PTR(fstr), (
unsigned int)
id);
5032 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
5037 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
5041 APPEND_F(
"[%c] %"PRIdSIZE,
5042 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
5050 if (!
NIL_P(class_path)) {
5051 APPEND_F(
"%s", RSTRING_PTR(class_path));
5061 if (!
NIL_P(class_path)) {
5062 APPEND_F(
"src:%s", RSTRING_PTR(class_path));
5069 if (rb_obj_shape_complex_p(obj)) {
5070 size_t hash_len = rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
5071 APPEND_F(
"(complex) len:%zu", hash_len);
5074 APPEND_F(
"(embed) len:%d capa:%d", RSHAPE_LEN(RBASIC_SHAPE_ID(obj)), ROBJECT_FIELDS_CAPACITY(obj));
5078 APPEND_F(
"len:%d capa:%d ptr:%p", RSHAPE_LEN(RBASIC_SHAPE_ID(obj)), ROBJECT_FIELDS_CAPACITY(obj), (
void *)
ROBJECT_FIELDS(obj));
5086 (block = vm_proc_block(obj)) != NULL &&
5087 (vm_block_type(block) == block_type_iseq) &&
5088 (iseq = vm_block_iseq(block)) != NULL) {
5089 rb_raw_iseq_info(BUFF_ARGS, iseq);
5091 else if (rb_ractor_p(obj)) {
5094 APPEND_F(
"r:%d", r->pub.id);
5100 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
5102 switch (imemo_type(obj)) {
5107 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
5108 rb_id2name(me->called_id),
5109 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
5110 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
5111 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
5112 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
5113 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
5114 me->def ? rb_method_type_name(me->def->
type) :
"NULL",
5115 me->def ? me->def->aliased : -1,
5117 (void *)me->defined_class);
5120 switch (me->def->type) {
5121 case VM_METHOD_TYPE_ISEQ:
5122 APPEND_S(
" (iseq:");
5123 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
5135 rb_raw_iseq_info(BUFF_ARGS, iseq);
5138 case imemo_callinfo:
5141 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
5142 rb_id2name(vm_ci_mid(ci)),
5145 vm_ci_kwarg(ci) ?
"available" :
"NULL");
5148 case imemo_callcache:
5154 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
5155 NIL_P(class_path) ? (vm_cc_valid(cc) ?
"??" :
"<NULL>") : RSTRING_PTR(class_path),
5156 cme ? rb_id2name(cme->called_id) :
"<NULL>",
5157 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
5159 (void *)(uintptr_t)vm_cc_call(cc));
5177#ifdef RUBY_ASAN_ENABLED
5179rb_asan_poison_object(
VALUE obj)
5181 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5182 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
5186rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
5188 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5189 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
5193rb_asan_poisoned_object_p(
VALUE obj)
5195 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5196 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
5201raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
5203 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
5204 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
5205 if (pos >= buff_size) {}
5209rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
5211 void *
objspace = rb_gc_get_objspace();
5214 raw_obj_info(buff, buff_size, obj);
5216 else if (!rb_gc_impl_pointer_to_heap_p(
objspace, (
const void *)obj)) {
5217 snprintf(buff, buff_size,
"out-of-heap:%p", (
void *)obj);
5220 else if (0 && rb_gc_impl_garbage_object_p(
objspace, obj)) {
5221 snprintf(buff, buff_size,
"garbage:%p", (
void *)obj);
5225 asan_unpoisoning_object(obj) {
5226 raw_obj_info(buff, buff_size, obj);
5242 if (RB_UNLIKELY(oldval >= maxval - 1)) {
5253 if (RGENGC_OBJ_INFO) {
5256 char buffers[10][0x100];
5259 rb_atomic_t index = atomic_inc_wraparound(&info.index, numberof(info.buffers));
5260 char *
const buff = info.buffers[index];
5261 return rb_raw_obj_info(buff,
sizeof(info.buffers[0]), obj);
5263 return obj_type_name(obj);
5280 rb_vraise(argv->exc, argv->fmt, *argv->ap);
5285gc_raise(
VALUE exc,
const char *fmt, ...)
5299 fprintf(stderr,
"%s",
"[FATAL] ");
5300 vfprintf(stderr, fmt, ap);
5307NORETURN(
static void negative_size_allocation_error(
const char *));
5309negative_size_allocation_error(
const char *msg)
5315ruby_memerror_body(
void *dummy)
5321NORETURN(
static void ruby_memerror(
void));
5326 if (ruby_thread_has_gvl_p()) {
5335 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
5353 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
5356 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
5357 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
5358 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
5361 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
5362 rb_ec_raised_clear(ec);
5365 rb_ec_raised_set(ec, RAISED_NOMEMORY);
5366 exc = ruby_vm_special_exception_copy(exc);
5369 EC_JUMP_TAG(ec, TAG_RAISE);
5373rb_memerror_reentered(
void)
5376 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
5380handle_malloc_failure(
void *ptr)
5391static void *ruby_xmalloc_body(
size_t size);
5394ruby_xmalloc(
size_t size)
5396 return handle_malloc_failure(ruby_xmalloc_body(size));
5400malloc_gc_allowed(
void)
5404 return r == NULL || !r->malloc_gc_disabled;
5408ruby_xmalloc_body(
size_t size)
5410 if ((ssize_t)size < 0) {
5411 negative_size_allocation_error(
"too large allocation size");
5414 return rb_gc_impl_malloc(rb_gc_get_objspace(), size, malloc_gc_allowed());
5418ruby_malloc_size_overflow(
size_t count,
size_t elsize)
5420 rb_raise(rb_eArgError,
5421 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
5426ruby_malloc_add_size_overflow(
size_t x,
size_t y)
5428 rb_raise(rb_eArgError,
5429 "malloc: possible integer overflow (%"PRIuSIZE
"+%"PRIuSIZE
")",
5433static void *ruby_xmalloc2_body(
size_t n,
size_t size);
5436ruby_xmalloc2(
size_t n,
size_t size)
5438 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
5442ruby_xmalloc2_body(
size_t n,
size_t size)
5444 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
5447static void *ruby_xcalloc_body(
size_t n,
size_t size);
5450ruby_xcalloc(
size_t n,
size_t size)
5452 return handle_malloc_failure(ruby_xcalloc_body(n, size));
5456ruby_xcalloc_body(
size_t n,
size_t size)
5458 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
5461static void *ruby_xrealloc_sized_body(
void *ptr,
size_t new_size,
size_t old_size);
5463#ifdef ruby_xrealloc_sized
5464#undef ruby_xrealloc_sized
5467ruby_xrealloc_sized(
void *ptr,
size_t new_size,
size_t old_size)
5469 return handle_malloc_failure(ruby_xrealloc_sized_body(ptr, new_size, old_size));
5473ruby_xrealloc_sized_body(
void *ptr,
size_t new_size,
size_t old_size)
5475 if ((ssize_t)new_size < 0) {
5476 negative_size_allocation_error(
"too large allocation size");
5479 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size, malloc_gc_allowed());
5483ruby_xrealloc(
void *ptr,
size_t new_size)
5485 return ruby_xrealloc_sized(ptr, new_size, 0);
5488static void *ruby_xrealloc2_sized_body(
void *ptr,
size_t n,
size_t size,
size_t old_n);
5490#ifdef ruby_xrealloc2_sized
5491#undef ruby_xrealloc2_sized
5494ruby_xrealloc2_sized(
void *ptr,
size_t n,
size_t size,
size_t old_n)
5496 return handle_malloc_failure(ruby_xrealloc2_sized_body(ptr, n, size, old_n));
5500ruby_xrealloc2_sized_body(
void *ptr,
size_t n,
size_t size,
size_t old_n)
5502 size_t len = xmalloc2_size(n, size);
5503 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr,
len, old_n * size, malloc_gc_allowed());
5507ruby_xrealloc2(
void *ptr,
size_t n,
size_t size)
5509 return ruby_xrealloc2_sized(ptr, n, size, 0);
5512#ifdef ruby_xfree_sized
5513#undef ruby_xfree_sized
5516ruby_xfree_sized(
void *x,
size_t size)
5522 if (LIKELY(GET_VM())) {
5523 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
5534 ruby_xfree_sized(x, 0);
5538rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
5540 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5541 return ruby_xmalloc(w);
5545rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
5547 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5548 return ruby_xcalloc(w, 1);
5552rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
5554 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5555 return ruby_xrealloc((
void *)p, w);
5559rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
5561 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5562 return ruby_xmalloc(u);
5566rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
5568 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5569 return ruby_xcalloc(u, 1);
5576ruby_mimmalloc(
size_t size)
5579#if CALC_EXACT_MALLOC_SIZE
5583#if CALC_EXACT_MALLOC_SIZE
5599ruby_mimcalloc(
size_t num,
size_t size)
5602#if CALC_EXACT_MALLOC_SIZE
5603 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(num, size);
5604 if (UNLIKELY(t.overflowed)) {
5608 mem = calloc1(size);
5620 mem = calloc(num, size);
5626ruby_mimfree(
void *ptr)
5628#if CALC_EXACT_MALLOC_SIZE
5636rb_gc_adjust_memory_usage(ssize_t diff)
5638 unless_objspace(
objspace) {
return; }
5640 rb_gc_impl_adjust_memory_usage(
objspace, diff);
5644rb_obj_info(
VALUE obj)
5646 return obj_info(obj);
5650rb_obj_info_dump(
VALUE obj)
5653 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
5657rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
5660 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
5664rb_gc_before_fork(
void)
5666 rb_gc_impl_before_fork(rb_gc_get_objspace());
5670rb_gc_after_fork(rb_pid_t pid)
5672 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
5676rb_gc_obj_shareable_p(
VALUE obj)
5693check_shareable_i(
const VALUE child,
void *ptr)
5697 if (!rb_gc_obj_shareable_p(child)) {
5698 fprintf(stderr,
"(a) ");
5699 rb_gc_rp(data->parent);
5700 fprintf(stderr,
"(b) ");
5702 fprintf(stderr,
"check_shareable_i: shareable (a) -> unshareable (b)\n");
5705 rb_bug(
"!! violate shareable constraint !!");
5709static bool gc_checking_shareable =
false;
5718 unsigned int lev = RB_GC_VM_LOCK();
5720 gc_checking_shareable =
true;
5721 rb_objspace_reachable_objects_from(obj, check_shareable_i, (
void *)data);
5722 gc_checking_shareable =
false;
5724 RB_GC_VM_UNLOCK(lev);
5729rb_gc_verify_shareable(
VALUE obj)
5736 gc_verify_shareable(
objspace, obj, &data);
5738 if (data.err_count > 0) {
5739 rb_bug(
"rb_gc_verify_shareable");
5744rb_gc_checking_shareable(
void)
5746 return gc_checking_shareable;
5800 rb_gc_register_address(&id2ref_value);
5802 malloc_offset = gc_compute_malloc_offset();
5815 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
5838ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
5840#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
5843 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (
unsigned long)addr, size, name);
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ATOMIC_VALUE_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
#define RUBY_ATOMIC_SIZE_FETCH_ADD(var, val)
Identical to RUBY_ATOMIC_FETCH_ADD, except it expects its arguments to be size_t.
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#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...
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
VALUE rb_define_module(const char *name)
Defines a top-level module.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define T_FILE
Old name of RUBY_T_FILE.
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define T_MASK
Old name of RUBY_T_MASK.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define T_DATA
Old name of RUBY_T_DATA.
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define LL2NUM
Old name of RB_LL2NUM.
#define CLASS_OF
Old name of rb_class_of.
#define T_NONE
Old name of RUBY_T_NONE.
#define T_NODE
Old name of RUBY_T_NODE.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_ABLE
Old name of RB_FL_ABLE.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define T_UNDEF
Old name of RUBY_T_UNDEF.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_MATCH
Old name of RUBY_T_MATCH.
#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_TEST
Old name of RB_FL_TEST.
#define xcalloc
Old name of ruby_xcalloc.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
int ruby_stack_check(void)
Checks for stack overflow.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMemError
NoMemoryError exception.
VALUE rb_eRangeError
RangeError exception.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_mKernel
Kernel module.
VALUE rb_cObject
Object class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Defines RBIMPL_HAS_BUILTIN.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
void rb_str_free(VALUE str)
Destroys the given string for no reason.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
int rb_io_fptr_finalize(rb_io_t *fptr)
Destroys the given IO.
int len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
VALUE rb_yield(VALUE val)
Yields the block.
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY(obj)
Convenient casting macro.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS(obj)
Convenient casting macro.
VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
This is the primitive way to wrap an existing C struct into RData.
#define DATA_PTR(obj)
Convenient getter macro.
VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
Identical to rb_data_object_wrap(), except it allocates a new data region internally instead of takin...
#define RDATA(obj)
Convenient casting macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
#define RUBY_NEVER_FREE
This is a value you can set to RData::dfree.
#define RFILE(obj)
Convenient casting macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RMATCH(obj)
Convenient casting macro.
#define ROBJECT(obj)
Convenient casting macro.
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
#define RREGEXP(obj)
Convenient casting macro.
#define RREGEXP_PTR(obj)
Convenient accessor macro.
#define RSTRING(obj)
Convenient casting macro.
static long RSTRUCT_LEN(VALUE st)
Returns the number of struct members.
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
This is the primitive way to wrap an existing C struct into RTypedData.
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
Identical to rb_data_typed_object_wrap(), except it allocates a new data region internally instead of...
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
static const rb_data_type_t * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define RTYPEDDATA(obj)
Convenient casting macro.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
void rb_p(VALUE obj)
Inspects an object.
#define errno
Ractor-aware version of errno.
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
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.
#define _(args)
This was a transition path from K&R to ANSI.
Ruby object's base components.
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
void * data
Pointer to the actual C level struct that you want to wrap.
Regular expression execution context.
union RMatch::@55 as
"Registers" of a match.
struct rmatch_offset * char_offset
Capture group offsets, in C array.
int char_offset_num_allocated
Number of rmatch_offset that ::rmatch::char_offset holds.
int num_regs
Number of capture-group registers.
void * data
Pointer to the actual C level struct that you want to wrap.
VALUE fields_obj
Direct reference to the slots that holds instance variables, if any.
This is the struct that holds necessary info for a struct.
struct rb_data_type_struct::@62 function
Function pointers.
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
RUBY_DATA_FUNC dcompact
This function is called when the object is relocated.
const char * wrap_struct_name
Name of structs of this kind.
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
VALUE flags
Type-specific behavioural characteristics.
Ruby's IO, metadata and buffers.
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Represents the region of a capture group.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
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.
ruby_value_type
C-level type of an object.
@ RUBY_T_MASK
Bitmask of ruby_value_type.