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
82#include "debug_counter.h"
83#include "eval_intern.h"
87#include "internal/class.h"
88#include "internal/compile.h"
89#include "internal/complex.h"
90#include "internal/concurrent_set.h"
91#include "internal/cont.h"
92#include "internal/error.h"
93#include "internal/eval.h"
94#include "internal/gc.h"
95#include "internal/hash.h"
96#include "internal/imemo.h"
97#include "internal/io.h"
98#include "internal/numeric.h"
99#include "internal/object.h"
100#include "internal/proc.h"
101#include "internal/rational.h"
102#include "internal/re.h"
103#include "internal/sanitizers.h"
104#include "internal/struct.h"
105#include "internal/symbol.h"
106#include "internal/thread.h"
107#include "internal/variable.h"
108#include "internal/warnings.h"
118#include "ruby_assert.h"
119#include "ruby_atomic.h"
124#include "vm_callinfo.h"
125#include "ractor_core.h"
126#include "internal/ractor.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_vm_lock_no_barrier(
const char *file,
int line)
158 unsigned int lev = 0;
159 rb_vm_lock_enter_nb(&lev, file, line);
164rb_gc_vm_unlock_no_barrier(
unsigned int lev,
const char *file,
int line)
166 rb_vm_lock_leave_nb(&lev, file, line);
170rb_gc_vm_barrier(
void)
176rb_gc_get_ractor_newobj_cache(
void)
178 return GET_RACTOR()->newobj_cache;
184 context->ec = GET_EC();
190 return ruby_vm_event_flags & event;
196 if (LIKELY(!rb_gc_event_hook_required_p(event)))
return;
207 bool gc_thread_p =
false;
215# ifdef RB_THREAD_LOCAL_SPECIFIER
216 rb_current_ec_set(ec);
218 native_tls_set(ruby_current_ec_key, ec);
223 if (RB_LIKELY(ec->cfp != NULL)) {
224 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
229# ifdef RB_THREAD_LOCAL_SPECIFIER
230 rb_current_ec_set(NULL);
232 native_tls_set(ruby_current_ec_key, NULL);
243rb_gc_stash_cleanup_objspace(
void)
245 GET_VM()->gc.cleanup_objspace = rb_gc_get_objspace();
251 if (RB_UNLIKELY(ruby_vm_during_cleanup) && GET_VM()->gc.cleanup_objspace) {
252 return GET_VM()->gc.cleanup_objspace;
257 return GET_VM()->ractor.main_ractor->objspace;
265rb_gc_get_objspace(
void)
267 return gc_current_objspace_of(rb_current_ractor_raw(
false));
271rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
282#define RESTORE_FINALIZER() (\
283 ec->cfp = saved.cfp, \
284 ec->cfp->sp = saved.sp, \
285 ec->errinfo = saved.errinfo)
287 saved.errinfo = ec->errinfo;
289 saved.sp = ec->cfp->sp;
293 ASSERT_vm_unlocking();
294 rb_ractor_ignore_belonging(
true);
296 enum ruby_tag_type state = EC_EXEC_TAG();
297 if (state != TAG_NONE) {
300 VALUE failed_final = saved.final;
303 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
304 rb_ec_error_print(ec, ec->errinfo);
308 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
309 saved.final = callback(i, data);
313 rb_ractor_ignore_belonging(
false);
314#undef RESTORE_FINALIZER
318rb_gc_set_pending_interrupt(
void)
321 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
331 rb_ractor_t *
const cr = rb_current_ractor_raw(
false);
332 if (cr == NULL || cr->objspace !=
objspace) {
338 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
340 rb_postponed_job_trigger_for_ractor(pjob, r->pub.self);
351 rb_postponed_job_trigger_for_ractor(pjob, GET_VM()->ractor.main_ractor->pub.self);
355rb_gc_unset_pending_interrupt(
void)
358 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
362rb_gc_multi_ractor_p(
void)
364 return rb_multi_ractor_p();
368rb_gc_shutdown_call_finalizer_p(
VALUE obj)
372 if (!ruby_free_at_exit_p()) {
374 if (!rbimpl_typeddata_embedded_p(obj) && !
RTYPEDDATA(obj)->data)
return false;
376 if (rb_obj_is_thread(obj))
return false;
377 if (rb_obj_is_mutex(obj))
return false;
378 if (rb_obj_is_fiber(obj))
return false;
379 if (rb_ractor_p(obj))
return false;
380 if (rb_obj_is_fstring_table(obj))
return false;
381 if (rb_obj_is_symbol_table(obj))
return false;
395 return ruby_free_at_exit_p();
400rb_gc_obj_changed_slot_size(
VALUE obj,
size_t slot_size)
402 shape_id_t shape_id = rb_obj_shape_transition_capacity(obj, rb_shape_capacity_for_slot_size(slot_size));
403 RBASIC_SET_FULL_SHAPE_ID(obj, shape_id);
406void rb_vm_update_references(
void *ptr);
408#define rb_setjmp(env) RUBY_SETJMP(env)
409#define rb_jmp_buf rb_jmpbuf_t
411#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
412#define MAP_ANONYMOUS MAP_ANON
415#define unless_objspace(objspace) \
417 rb_vm_t *unless_objspace_vm = GET_VM(); \
418 if (unless_objspace_vm) objspace = rb_gc_get_objspace(); \
421#define RMOVED(obj) ((struct RMoved *)(obj))
423#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
424 if (gc_object_moved_p_internal((_objspace), (VALUE)(_thing))) { \
425 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
429#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
431#if RUBY_MARK_FREE_DEBUG
432int ruby_gc_debug_indent = 0;
435#ifndef RGENGC_OBJ_INFO
436# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
439#ifndef CALC_EXACT_MALLOC_SIZE
440# define CALC_EXACT_MALLOC_SIZE 0
445static size_t malloc_offset = 0;
446#if defined(HAVE_MALLOC_USABLE_SIZE)
448gc_compute_malloc_offset(
void)
459 for (offset = 0; offset <= 16; offset += 8) {
460 size_t allocated = (64 - offset);
461 void *test_ptr = malloc(allocated);
462 size_t wasted = malloc_usable_size(test_ptr) - allocated;
473gc_compute_malloc_offset(
void)
481rb_malloc_grow_capa(
size_t current,
size_t type_size)
483 size_t current_capacity = current;
484 if (current_capacity < 4) {
485 current_capacity = 4;
487 current_capacity *= type_size;
490 size_t new_capacity = (current_capacity * 2);
493 if (rb_popcount64(new_capacity) != 1) {
494 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
497 new_capacity -= malloc_offset;
498 new_capacity /= type_size;
499 if (current > new_capacity) {
500 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
506static inline struct rbimpl_size_overflow_tag
507size_mul_add_overflow(size_t x, size_t y, size_t z)
509 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
510 struct rbimpl_size_overflow_tag u = rbimpl_size_add_overflow(t.result, z);
511 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed, u.result };
514static inline struct rbimpl_size_overflow_tag
515size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
517 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
518 struct rbimpl_size_overflow_tag u = rbimpl_size_mul_overflow(z, w);
519 struct rbimpl_size_overflow_tag v = rbimpl_size_add_overflow(t.result, u.result);
520 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed || v.overflowed, v.result };
523PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
526size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
528 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
529 if (LIKELY(!t.overflowed)) {
532 else if (rb_during_gc()) {
538 "integer overflow: %"PRIuSIZE
541 x, y, (
size_t)SIZE_MAX);
546rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
548 return size_mul_or_raise(x, y, exc);
552size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
554 struct rbimpl_size_overflow_tag t = size_mul_add_overflow(x, y, z);
555 if (LIKELY(!t.overflowed)) {
558 else if (rb_during_gc()) {
564 "integer overflow: %"PRIuSIZE
568 x, y, z, (
size_t)SIZE_MAX);
573rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
575 return size_mul_add_or_raise(x, y, z, exc);
579size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
581 struct rbimpl_size_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
582 if (LIKELY(!t.overflowed)) {
585 else if (rb_during_gc()) {
591 "integer overflow: %"PRIdSIZE
596 x, y, z, w, (
size_t)SIZE_MAX);
600#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
602volatile VALUE rb_gc_guarded_val;
604rb_gc_guarded_ptr_val(
volatile VALUE *ptr,
VALUE val)
606 rb_gc_guarded_val = val;
612static const char *obj_type_name(
VALUE obj);
618rb_gc_atfork_global_locks(
void)
624#include "gc/default/default.c"
626#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
627# error "Modular GC requires dlopen"
631typedef struct gc_function_map {
633 void *(*objspace_alloc)(void);
634 void (*objspace_init)(
void *objspace_ptr);
635 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
636 void (*objspace_retire_gc)(
void *objspace_ptr);
637 void (*set_params)(
void *objspace_ptr);
640 void (*shutdown_free_objects)(
void *objspace_ptr);
641 void (*objspace_free)(
void *objspace_ptr);
642 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
644 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
645 bool (*during_gc_p)(
void *objspace_ptr);
646 void (*prepare_heap)(
void *objspace_ptr);
647 void (*gc_enable)(
void *objspace_ptr);
648 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
649 bool (*gc_enabled_p)(
void *objspace_ptr);
650 bool (*user_gc_disabled_set)(
void *objspace_ptr,
bool disable);
651 bool (*user_gc_disabled_p)(
void *objspace_ptr);
652 bool (*multi_objspace_p)(void);
653 bool (*during_global_gc_p)(
void *objspace_ptr);
654 bool (*during_postmortem_p)(
void *objspace_ptr);
655 bool (*obj_foreign_p)(
void *objspace_ptr,
VALUE obj);
656 bool (*shref_marked_p)(
void *objspace_ptr,
VALUE obj);
657 size_t (*heap_page_count)(
void *objspace_ptr);
658 void (*objspace_absorb)(
void *dst_ptr,
void *src_ptr);
659 void (*gc_rest)(
void *objspace_ptr);
660 VALUE (*config_get)(
void *objpace_ptr);
661 void (*config_set)(
void *objspace_ptr,
VALUE hash);
662 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
663 VALUE (*stress_get)(
void *objspace_ptr);
666 VALUE (*new_obj)(
void *objspace_ptr,
void *cache_ptr,
VALUE klass,
VALUE flags,
bool wb_protected,
size_t alloc_size,
size_t *actual_alloc_size);
668 size_t (*obj_slot_size)(
VALUE obj);
669 size_t (*size_slot_size)(
void *objspace_ptr,
size_t size);
670 bool (*size_allocatable_p)(
size_t size);
671 size_t (*max_allocation_size)(void);
673 void *(*malloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
674 void *(*calloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
675 void *(*realloc)(
void *objspace_ptr,
void *ptr,
size_t new_size,
size_t old_size,
bool gc_allowed);
676 void (*free)(
void *objspace_ptr,
void *ptr,
size_t old_size);
677 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
679 void (*mark)(
void *objspace_ptr,
VALUE obj);
680 void (*mark_and_move)(
void *objspace_ptr,
VALUE *ptr);
681 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
682 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
684 void (*declare_weak_references)(
void *objspace_ptr,
VALUE obj);
685 bool (*handle_weak_references_alive_p)(
void *objspace_ptr,
VALUE obj);
687 void (*register_pinning_obj)(
void *objspace_ptr,
VALUE obj);
688 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
689 bool (*pinned_p)(
void *objspace_ptr,
VALUE obj);
690 VALUE (*location)(
void *objspace_ptr,
VALUE value);
692 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
693 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
694 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
695 void (*obj_became_shareable)(
void *objspace_ptr,
VALUE obj);
697 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
698 void (*each_objects_shareable)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
699 void (*each_objects_foreign)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
700 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
702 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
704 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
705 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
706 void (*shutdown_call_finalizer)(
void *objspace_ptr);
708 void (*before_fork)(
void *objspace_ptr);
709 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
711 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
712 bool (*get_measure_total_time)(
void *objspace_ptr);
713 unsigned long long (*get_total_time)(
void *objspace_ptr);
714 size_t (*gc_count)(
void *objspace_ptr);
715 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
716 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
717 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
718 const char *(*active_gc_name)(void);
721 bool (*live_object_p)(
void *objspace_ptr,
const void *ptr);
722 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
723 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
724 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
726 bool modular_gc_loaded_p;
727} rb_gc_function_map_t;
729static rb_gc_function_map_t rb_gc_functions;
731# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
732# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
735ruby_modular_gc_init(
void)
740 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
742 rb_gc_function_map_t gc_functions = { 0 };
744 char *gc_so_path = NULL;
749 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
750 char c = gc_so_file[i];
751 if (isalnum(c))
continue;
757 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
762 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
765 size_t prefix_len = 0;
766 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
767 const char *base = strrchr(dli.dli_fname,
'/');
770# define end_with_p(lit) \
771 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
772 memcmp(base - tail, lit, tail) == 0)
774 prefix_len = base - dli.dli_fname;
775 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
778 prefix_len += MODULAR_GC_DIR[0] !=
'/';
779 gc_so_path_size += prefix_len;
783 gc_so_path = alloca(gc_so_path_size);
785 size_t gc_so_path_idx = 0;
786#define GC_SO_PATH_APPEND(str) do { \
787 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
790 if (prefix_len > 0) {
791 memcpy(gc_so_path, dli.dli_fname, prefix_len);
792 gc_so_path_idx = prefix_len;
795 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
796 GC_SO_PATH_APPEND(gc_so_file);
797 GC_SO_PATH_APPEND(DLEXT);
798 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
799#undef GC_SO_PATH_APPEND
802 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
804 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
808 gc_functions.modular_gc_loaded_p =
true;
811 unsigned int err_count = 0;
813# define load_modular_gc_func(name) do { \
815 const char *func_name = "rb_gc_impl_" #name; \
816 gc_functions.name = dlsym(handle, func_name); \
817 if (!gc_functions.name) { \
818 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
823 gc_functions.name = rb_gc_impl_##name; \
828 load_modular_gc_func(objspace_alloc);
829 load_modular_gc_func(objspace_init);
830 load_modular_gc_func(ractor_cache_alloc);
831 load_modular_gc_func(objspace_retire_gc);
832 load_modular_gc_func(set_params);
833 load_modular_gc_func(init);
835 load_modular_gc_func(shutdown_free_objects);
836 load_modular_gc_func(objspace_free);
837 load_modular_gc_func(ractor_cache_free);
839 load_modular_gc_func(start);
840 load_modular_gc_func(during_gc_p);
841 load_modular_gc_func(prepare_heap);
842 load_modular_gc_func(gc_enable);
843 load_modular_gc_func(gc_disable);
844 load_modular_gc_func(gc_enabled_p);
845 load_modular_gc_func(user_gc_disabled_set);
846 load_modular_gc_func(user_gc_disabled_p);
847 load_modular_gc_func(multi_objspace_p);
848 load_modular_gc_func(during_global_gc_p);
849 load_modular_gc_func(during_postmortem_p);
850 load_modular_gc_func(obj_foreign_p);
851 load_modular_gc_func(shref_marked_p);
852 load_modular_gc_func(heap_page_count);
853 load_modular_gc_func(objspace_absorb);
854 load_modular_gc_func(gc_rest);
855 load_modular_gc_func(config_set);
856 load_modular_gc_func(config_get);
857 load_modular_gc_func(stress_set);
858 load_modular_gc_func(stress_get);
859 load_modular_gc_func(get_vm_context);
861 load_modular_gc_func(new_obj);
862 load_modular_gc_func(zjit_new_obj_fastpath);
863 load_modular_gc_func(obj_slot_size);
864 load_modular_gc_func(size_slot_size);
865 load_modular_gc_func(size_allocatable_p);
866 load_modular_gc_func(max_allocation_size);
868 load_modular_gc_func(malloc);
869 load_modular_gc_func(calloc);
870 load_modular_gc_func(realloc);
871 load_modular_gc_func(free);
872 load_modular_gc_func(adjust_memory_usage);
874 load_modular_gc_func(mark);
875 load_modular_gc_func(mark_and_move);
876 load_modular_gc_func(mark_and_pin);
877 load_modular_gc_func(mark_maybe);
879 load_modular_gc_func(declare_weak_references);
880 load_modular_gc_func(handle_weak_references_alive_p);
882 load_modular_gc_func(register_pinning_obj);
883 load_modular_gc_func(object_moved_p);
884 load_modular_gc_func(pinned_p);
885 load_modular_gc_func(location);
887 load_modular_gc_func(writebarrier);
888 load_modular_gc_func(writebarrier_unprotect);
889 load_modular_gc_func(writebarrier_remember);
890 load_modular_gc_func(obj_became_shareable);
892 load_modular_gc_func(each_objects);
893 load_modular_gc_func(each_objects_shareable);
894 load_modular_gc_func(each_objects_foreign);
895 load_modular_gc_func(each_object);
897 load_modular_gc_func(make_zombie);
898 load_modular_gc_func(define_finalizer);
899 load_modular_gc_func(undefine_finalizer);
900 load_modular_gc_func(copy_finalizer);
901 load_modular_gc_func(shutdown_call_finalizer);
903 load_modular_gc_func(before_fork);
904 load_modular_gc_func(after_fork);
906 load_modular_gc_func(set_measure_total_time);
907 load_modular_gc_func(get_measure_total_time);
908 load_modular_gc_func(get_total_time);
909 load_modular_gc_func(gc_count);
910 load_modular_gc_func(latest_gc_info);
911 load_modular_gc_func(stat);
912 load_modular_gc_func(stat_heap);
913 load_modular_gc_func(active_gc_name);
915 load_modular_gc_func(object_metadata);
916 load_modular_gc_func(live_object_p);
917 load_modular_gc_func(garbage_object_p);
918 load_modular_gc_func(set_event_hook);
919 load_modular_gc_func(copy_attributes);
922 fprintf(stderr,
"ruby_modular_gc_init: found %u missing exports in library %s\n", err_count, gc_so_path);
926# undef load_modular_gc_func
928 rb_gc_functions = gc_functions;
932# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
933# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
934# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
935# define rb_gc_impl_objspace_retire_gc rb_gc_functions.objspace_retire_gc
936# define rb_gc_impl_set_params rb_gc_functions.set_params
937# define rb_gc_impl_init rb_gc_functions.init
939# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
940# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
941# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
943# define rb_gc_impl_start rb_gc_functions.start
944# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
945# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
946# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
947# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
948# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
949# define rb_gc_impl_user_gc_disabled_set rb_gc_functions.user_gc_disabled_set
950# define rb_gc_impl_user_gc_disabled_p rb_gc_functions.user_gc_disabled_p
951# define rb_gc_impl_multi_objspace_p rb_gc_functions.multi_objspace_p
952# define rb_gc_impl_during_global_gc_p rb_gc_functions.during_global_gc_p
953# define rb_gc_impl_during_postmortem_p rb_gc_functions.during_postmortem_p
954# define rb_gc_impl_obj_foreign_p rb_gc_functions.obj_foreign_p
955# define rb_gc_impl_shref_marked_p rb_gc_functions.shref_marked_p
956# define rb_gc_impl_heap_page_count rb_gc_functions.heap_page_count
957# define rb_gc_impl_objspace_absorb rb_gc_functions.objspace_absorb
958# define rb_gc_impl_gc_rest rb_gc_functions.gc_rest
959# define rb_gc_impl_config_get rb_gc_functions.config_get
960# define rb_gc_impl_config_set rb_gc_functions.config_set
961# define rb_gc_impl_stress_set rb_gc_functions.stress_set
962# define rb_gc_impl_stress_get rb_gc_functions.stress_get
963# define rb_gc_impl_get_vm_context rb_gc_functions.get_vm_context
965# define rb_gc_impl_new_obj rb_gc_functions.new_obj
966# define rb_gc_impl_zjit_new_obj_fastpath rb_gc_functions.zjit_new_obj_fastpath
967# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
968# define rb_gc_impl_size_slot_size rb_gc_functions.size_slot_size
969# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
970# define rb_gc_impl_max_allocation_size rb_gc_functions.max_allocation_size
972# define rb_gc_impl_malloc rb_gc_functions.malloc
973# define rb_gc_impl_calloc rb_gc_functions.calloc
974# define rb_gc_impl_realloc rb_gc_functions.realloc
975# define rb_gc_impl_free rb_gc_functions.free
976# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
978# define rb_gc_impl_mark rb_gc_functions.mark
979# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
980# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
981# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
983# define rb_gc_impl_declare_weak_references rb_gc_functions.declare_weak_references
984# define rb_gc_impl_handle_weak_references_alive_p rb_gc_functions.handle_weak_references_alive_p
986# define rb_gc_impl_register_pinning_obj rb_gc_functions.register_pinning_obj
987# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
988# define rb_gc_impl_pinned_p rb_gc_functions.pinned_p
989# define rb_gc_impl_location rb_gc_functions.location
991# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
992# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
993# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
994# define rb_gc_impl_obj_became_shareable rb_gc_functions.obj_became_shareable
996# define rb_gc_impl_each_objects rb_gc_functions.each_objects
997# define rb_gc_impl_each_objects_shareable rb_gc_functions.each_objects_shareable
998# define rb_gc_impl_each_objects_foreign rb_gc_functions.each_objects_foreign
999# define rb_gc_impl_each_object rb_gc_functions.each_object
1001# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
1002# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
1003# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
1004# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
1005# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
1007# define rb_gc_impl_before_fork rb_gc_functions.before_fork
1008# define rb_gc_impl_after_fork rb_gc_functions.after_fork
1010# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
1011# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
1012# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
1013# define rb_gc_impl_gc_count rb_gc_functions.gc_count
1014# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
1015# define rb_gc_impl_stat rb_gc_functions.stat
1016# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
1017# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
1019# define rb_gc_impl_object_metadata rb_gc_functions.object_metadata
1020# define rb_gc_impl_live_object_p rb_gc_functions.live_object_p
1021# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
1022# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
1023# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
1026#ifdef RUBY_ASAN_ENABLED
1028asan_death_callback(
void)
1031 rb_bug_without_die(
"ASAN error");
1039rb_gc_init_objspaces(
void)
1042 ruby_modular_gc_init();
1045 rb_vm_t *vm = ruby_current_vm_ptr;
1047 void *
objspace = rb_gc_impl_objspace_alloc();
1049 vm->ractor.main_ractor->objspace =
objspace;
1050 rb_gc_impl_objspace_init(
objspace);
1051 rb_gc_impl_stress_set(
objspace, initial_stress);
1053#ifdef RUBY_ASAN_ENABLED
1054 __sanitizer_set_death_callback(asan_death_callback);
1061static bool gc_ever_multi_ractor =
false;
1064rb_gc_ever_multi_ractor_p(
void)
1066 if (!gc_ever_multi_ractor && rb_multi_ractor_p()) gc_ever_multi_ractor =
true;
1067 return gc_ever_multi_ractor;
1073rb_gc_objspace_alloc(
void)
1075 gc_ever_multi_ractor =
true;
1076 if (!rb_gc_impl_multi_objspace_p()) {
1078 return rb_gc_get_objspace();
1080 void *
objspace = rb_gc_impl_objspace_alloc();
1081 rb_gc_impl_objspace_init(
objspace);
1089 rb_gc_impl_objspace_free(
objspace);
1093rb_gc_obj_slot_size(
VALUE obj)
1095 return rb_gc_impl_obj_slot_size(obj);
1099gc_validate_pc(
VALUE obj)
1107 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && CFP_PC(cfp)) {
1108 const VALUE *iseq_encoded = ISEQ_BODY(CFP_ISEQ(cfp))->iseq_encoded;
1109 const VALUE *iseq_encoded_end = iseq_encoded + ISEQ_BODY(CFP_ISEQ(cfp))->iseq_size;
1110 RUBY_ASSERT(CFP_PC(cfp) >= iseq_encoded,
"PC not set when allocating, breaking tracing");
1111 RUBY_ASSERT(CFP_PC(cfp) <= iseq_encoded_end,
"PC not set when allocating, breaking tracing");
1116NOINLINE(
static void gc_newobj_hook(
VALUE obj));
1118gc_newobj_hook(
VALUE obj)
1120 int lev = RB_GC_VM_LOCK_NO_BARRIER();
1122 size_t slot_size = rb_gc_obj_slot_size(obj);
1123 memset((
char *)obj +
sizeof(
struct RBasic), 0, slot_size -
sizeof(
struct RBasic));
1130 bool gc_disabled =
RTEST(rb_gc_local_disable_no_rest());
1134 if (!gc_disabled) rb_gc_local_enable();
1136 RB_GC_VM_UNLOCK_NO_BARRIER(lev);
1148 size_t actual_alloc_size;
1149 VALUE obj = rb_gc_impl_new_obj(
objspace, cr->newobj_cache, klass, flags, wb_protected, size, &actual_alloc_size);
1151 GC_ASSERT(actual_alloc_size >= size);
1152 shape_id = rb_shape_transition_slot_size(shape_id, actual_alloc_size);
1154 RBASIC_SET_FULL_SHAPE_ID_NO_CHECKS(obj, shape_id);
1156 gc_validate_pc(obj);
1159 gc_newobj_hook(obj);
1162 if (RUBY_DTRACE_GC_OBJ_NEW_ENABLED()) {
1163 RUBY_DTRACE_GC_OBJ_NEW((
void*)obj, flags);
1166#if RGENGC_CHECK_MODE
1167# ifndef GC_DEBUG_SLOT_FILL_SPECIAL_VALUE
1168# define GC_DEBUG_SLOT_FILL_SPECIAL_VALUE 255
1172 (
void *)(obj +
sizeof(
struct RBasic)),
1173 GC_DEBUG_SLOT_FILL_SPECIAL_VALUE,
1174 rb_gc_obj_slot_size(obj) -
sizeof(
struct RBasic)
1187 return newobj_body(cr, cr->objspace, klass, flags, shape_id, wb_protected, size);
1207 return newobj_body(cr,
objspace, klass, flags, shape_id, wb_protected, size);
1222 return newobj_body(cr, cr->objspace, klass, flags, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER,
true, size);
1226rb_newobj_of_with_shape(
VALUE klass,
VALUE flags, shape_id_t shape_id,
size_t size)
1228 return rb_newobj(GET_EC(), klass, flags, shape_id,
true, size);
1232rb_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
1234 return rb_newobj(GET_EC(), klass, flags, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER,
true, size);
1238VALUE class_allocate_complex_instance(
VALUE klass, uint32_t capacity)
1240 VALUE obj = rb_newobj_of_with_shape(klass,
T_OBJECT, rb_shape_transition_extended(ROOT_COMPLEX_SHAPE_ID),
sizeof(
struct RObject));
1244 VALUE fields_obj = rb_imemo_fields_new_complex(obj, ROOT_COMPLEX_SHAPE_ID, capacity,
false);
1245 ROBJECT_SET_EXTENDED(obj, fields_obj);
1250robject_embedded_size(uint32_t fields_count)
1252 size_t size = rb_obj_embedded_size(fields_count);
1253 if (!rb_gc_size_allocatable_p(size)) {
1254 size =
sizeof(
struct RObject);
1260rb_class_allocate_instance_capa(
VALUE klass, attr_index_t max_iv_count)
1266 if (RB_UNLIKELY(max_iv_count > rb_shape_max_capacity())) {
1267 obj = class_allocate_complex_instance(klass, max_iv_count);
1270 size_t size = robject_embedded_size(max_iv_count);
1274 obj = rb_newobj_of_with_shape(klass,
T_OBJECT, rb_shape_transition_robject(0), size);
1277 VALUE *ptr = ROBJECT_FIELDS(obj);
1278 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1279 attr_index_t fields_count = RSHAPE_LEN(shape_id);
1280 attr_index_t capacity = RSHAPE_CAPACITY(shape_id);
1282 for (attr_index_t i = fields_count; i < capacity; i++) {
1290 rb_bug(
"Expected rb_class_allocate_instance to set the class correctly");
1298rb_class_allocate_instance(
VALUE klass)
1300 return rb_class_allocate_instance_capa(klass, RCLASS_MAX_IV_COUNT(klass));
1305rb_zjit_class_allocate_instance_fastpath(
VALUE klass,
size_t *size_out,
VALUE *flags_out)
1307 uint32_t index_tbl_num_entries = RCLASS_MAX_IV_COUNT(klass);
1310 if (RB_UNLIKELY(index_tbl_num_entries > rb_shape_max_capacity())) {
1314 *size_out = robject_embedded_size(index_tbl_num_entries);
1315 *flags_out =
T_OBJECT | rb_shape_transition_robject(0);
1321rb_zjit_newobj_hook_enabled_p(
void)
1328rb_gc_register_pinning_obj(
VALUE obj)
1330 rb_gc_impl_register_pinning_obj(rb_gc_get_objspace(), obj);
1333#define UNEXPECTED_NODE(func) \
1334 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1335 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1338rb_data_object_check(
VALUE klass)
1343 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1347#define RTYPEDDATA_EMBEDDED_P rbimpl_typeddata_embedded_p
1348#define RB_DATA_TYPE_EMBEDDABLE_P(type) ((type)->flags & RUBY_TYPED_EMBEDDABLE)
1349#define RTYPEDDATA_EMBEDDABLE_P(obj) RB_DATA_TYPE_EMBEDDABLE_P(RTYPEDDATA_TYPE(obj))
1354 RBIMPL_NONNULL_ARG(
type);
1355 if (klass) rb_data_object_check(klass);
1357 VALUE obj = rb_newobj_in_objspace(GET_EC(),
objspace, klass,
T_DATA, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_RDATA, wb_protected, size);
1359 rb_gc_register_pinning_obj(obj);
1372 if (UNLIKELY(RB_DATA_TYPE_EMBEDDABLE_P(
type))) {
1373 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1382 return typed_data_wrap_in(rb_ec_ractor_ptr(GET_EC())->
objspace, klass, datap,
type);
1388 return typed_data_wrap_in(
objspace, klass, datap,
type);
1394 if (RB_DATA_TYPE_EMBEDDABLE_P(
type)) {
1396 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1404 if (rb_gc_size_allocatable_p(embed_size) && !rb_gc_data_type_deferred_free_p(
type)) {
1405 VALUE obj = typed_data_alloc_in(
objspace, klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1406 memset((
char *)obj + offsetof(
struct RTypedData,
data), 0, size);
1419 return typed_data_zalloc_in(rb_ec_ractor_ptr(GET_EC())->
objspace, klass, size,
type);
1425 return typed_data_zalloc_in(
objspace, klass, size,
type);
1429ruby_xmalloc_usable_size(
void *ptr)
1431#ifdef HAVE_MALLOC_USABLE_SIZE
1432#if CALC_EXACT_MALLOC_SIZE
1436 return malloc_usable_size(ptr);
1444rb_objspace_data_type_memsize(
VALUE obj)
1447 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1450 if (RTYPEDDATA_EMBEDDABLE_P(obj) && !RTYPEDDATA_EMBEDDED_P(obj)) {
1451 size += ruby_xmalloc_usable_size((
void *)ptr);
1455 if (
type->function.dsize) {
1456 size +=
type->function.dsize(ptr);
1464rb_objspace_data_type_name(
VALUE obj)
1470rb_gc_declare_weak_references(
VALUE obj)
1472 rb_gc_impl_declare_weak_references(rb_gc_get_objspace(), obj);
1476rb_gc_handle_weak_references_alive_p(
VALUE obj)
1480 return rb_gc_impl_handle_weak_references_alive_p(rb_gc_get_objspace(), obj);
1484rb_gc_handle_weak_references(
VALUE obj)
1491 if (
type->function.handle_weak_references) {
1492 (
type->function.handle_weak_references)(RTYPEDDATA_GET_DATA(obj));
1496 "rb_gc_handle_weak_references: TypedData %s does not implement handle_weak_references",
1504 switch (imemo_type(obj)) {
1505 case imemo_callcache: {
1507 if (cc->klass !=
Qundef &&
1508 (!rb_gc_handle_weak_references_alive_p(cc->klass) ||
1509 !rb_gc_handle_weak_references_alive_p((
VALUE)cc->cme_))) {
1510 vm_cc_invalidate(cc);
1514 case imemo_subclasses: {
1516 VALUE *entries = rb_imemo_subclasses_entries(obj);
1517 for (uint32_t i = 0; i < subs->count; i++) {
1518 if (entries[i] && !rb_gc_handle_weak_references_alive_p(entries[i])) {
1525 rb_bug(
"rb_gc_handle_weak_references: unexpected imemo type");
1531 rb_bug(
"rb_gc_handle_weak_references: type not supported\n");
1536rb_gc_imemo_needs_cleanup_p(
VALUE obj)
1538 switch (imemo_type(obj)) {
1539 case imemo_constcache:
1544 case imemo_callcache:
1545 case imemo_throw_data:
1546 case imemo_cvar_entry:
1552 case imemo_callinfo:
1556 case imemo_subclasses:
1563 return rb_obj_shape_complex_p(obj);
1581rb_gc_obj_needs_cleanup_p(
VALUE obj)
1588 return rb_gc_imemo_needs_cleanup_p(obj);
1594 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1595 if (rb_shape_has_fields(shape_id) && rb_shape_layout(shape_id) == SHAPE_ID_LAYOUT_OTHER) {
1620 if (
type & TYPED_DATA_EMBEDDED) {
1630 return (flags & (RSTRING_NOEMBED | RSTRING_FSTR));
1633 return !(flags & RARRAY_EMBED_FLAG);
1636 return (flags & RHASH_ST_TABLE_FLAG);
1639 return (flags & (RMATCH_ONIG | RMATCH_OFFSETS_EXTERNAL)) || USE_DEBUG_COUNTER;
1642 return !(flags & BIGNUM_EMBED_FLAG);
1645 return !(flags & RSTRUCT_EMBED_LEN_MASK);
1652io_fptr_finalize(
void *fptr)
1661 rb_gc_impl_make_zombie(
objspace, obj, io_fptr_finalize, fptr);
1667 void *data = RTYPEDDATA_GET_DATA(obj);
1670 void (*dfree)(
void *) =
type->function.dfree;
1673 bool embedded = RTYPEDDATA_EMBEDDED_P(obj);
1675 bool free_embeddable_data = RB_DATA_TYPE_EMBEDDABLE_P(
type) && !embedded;
1680 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1683 else if (free_immediately) {
1685 if (free_embeddable_data) {
1689 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1692 rb_gc_impl_make_zombie(
objspace, obj, dfree, data);
1693 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1698 RB_DEBUG_COUNTER_INC(obj_data_empty);
1715 rb_class_classext_free(args->klass, ext, is_prime);
1723 rb_iclass_classext_free(args->klass, ext, is_prime);
1731 RB_DEBUG_COUNTER_INC(obj_free);
1735 if (RUBY_DTRACE_GC_OBJ_FREE_ENABLED()) {
1736 RUBY_DTRACE_GC_OBJ_FREE((
void*)obj,
RBASIC(obj)->flags);
1739 switch (builtin_type) {
1744 rb_bug(
"obj_free() called for broken object");
1750 switch (builtin_type) {
1756 rb_zjit_klass_free(obj);
1759 rb_class_classext_foreach(obj, classext_free, (
void *)&args);
1760 if (RCLASS_CLASSEXT_TBL(obj)) {
1761 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1773#if USE_DEBUG_COUNTER
1776 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1779 RB_DEBUG_COUNTER_INC(obj_hash_1);
1782 RB_DEBUG_COUNTER_INC(obj_hash_2);
1785 RB_DEBUG_COUNTER_INC(obj_hash_3);
1788 RB_DEBUG_COUNTER_INC(obj_hash_4);
1794 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1798 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1801 if (RHASH_AR_TABLE_P(obj)) {
1802 if (RHASH_AR_TABLE(obj) == NULL) {
1803 RB_DEBUG_COUNTER_INC(obj_hash_null);
1806 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1810 RB_DEBUG_COUNTER_INC(obj_hash_st);
1819 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1823 if (!rb_data_free(
objspace, obj))
return false;
1828#if USE_DEBUG_COUNTER
1830 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1833 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1836 RB_DEBUG_COUNTER_INC(obj_match_under4);
1840 onig_region_free(&rm->
as.onig, 0);
1844 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1848 if (
RFILE(obj)->fptr) {
1849 bool closed = rb_io_fptr_finalize_closed(
RFILE(obj)->fptr);
1850 if (!closed) make_io_zombie(
objspace, obj);
1851 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1856 RB_DEBUG_COUNTER_INC(obj_rational);
1859 RB_DEBUG_COUNTER_INC(obj_complex);
1866 rb_class_classext_foreach(obj, classext_iclass_free, (
void *)&args);
1867 if (RCLASS_CLASSEXT_TBL(obj)) {
1868 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1871 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1875 RB_DEBUG_COUNTER_INC(obj_float);
1879 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1880 SIZED_FREE_N(BIGNUM_DIGITS(obj), BIGNUM_LEN(obj));
1881 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1884 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1889 UNEXPECTED_NODE(obj_free);
1893 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1894 RSTRUCT(obj)->
as.heap.ptr == NULL) {
1895 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1898 SIZED_FREE_N(RSTRUCT(obj)->
as.heap.ptr, RSTRUCT(obj)->
as.heap.len);
1899 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1904 RB_DEBUG_COUNTER_INC(obj_symbol);
1908 rb_imemo_free((
VALUE)obj);
1912 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1917 rb_gc_impl_make_zombie(
objspace, obj, 0, 0);
1932 const rb_ractor_t *
const cr = rb_current_ractor_raw(
false);
1933 if (cr != NULL && cr != GET_VM()->ractor.main_ractor) {
1934 e &= ~RUBY_INTERNAL_EVENT_FREEOBJ;
1936 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), e);
1940internal_object_p(
VALUE obj)
1942 void *ptr = asan_unpoison_object_temporary(obj);
1944 if (
RBASIC(obj)->flags) {
1947 UNEXPECTED_NODE(internal_object_p);
1956 if (obj == rb_mRubyVMFrozenCore)
1960 if (RCLASS_SINGLETON_P(obj)) {
1961 return rb_singleton_class_internal_p(obj);
1965 if (!
RBASIC(obj)->klass)
break;
1969 if (ptr || !
RBASIC(obj)->flags) {
1970 rb_asan_poison_object(obj);
1976rb_objspace_internal_object_p(
VALUE obj)
1978 return internal_object_p(obj);
1987os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1992 for (; v != (
VALUE)vend; v += stride) {
1993 if (!internal_object_p(v)) {
2013os_shareable_collect_i(
void *vstart,
void *vend,
size_t stride,
void *data)
2018 for (; v != (
VALUE)vend; v += stride) {
2032static void rb_gc_critical_disable(
void);
2033static void rb_gc_critical_enable(
void);
2046 rb_gc_impl_each_objects(rb_gc_get_objspace(), os_obj_of_i, &oes);
2051 if (rb_multi_ractor_p()) {
2056 rb_gc_critical_disable();
2060 void *self = rb_gc_get_objspace();
2063 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2064 if (r->objspace && r->objspace != self) {
2065 rb_gc_impl_each_objects_shareable(r->objspace, os_shareable_collect_i, &ocs);
2069 rb_gc_critical_enable();
2072 for (
long i = 0; i <
len; i++) {
2132 return os_obj_of(of);
2148 return rb_undefine_finalizer(obj);
2154 rb_check_frozen(obj);
2156 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
2162should_be_callable(
VALUE block)
2165 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
2171should_be_finalizable(
VALUE obj)
2174 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
2177 rb_check_frozen(obj);
2183 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
2263 if (rb_callable_receiver(block) == obj) {
2264 rb_warn(
"finalizer references object to be finalized");
2267 return rb_define_finalizer(obj, block);
2273 should_be_finalizable(obj);
2274 should_be_callable(block);
2276 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
2284rb_objspace_call_finalizer(
void)
2286 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
2290rb_objspace_free_objects(
void *
objspace)
2292 rb_gc_impl_shutdown_free_objects(
objspace);
2296rb_objspace_garbage_object_p(
VALUE obj)
2298 return !
SPECIAL_CONST_P(obj) && rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
2302rb_objspace_foreign_object_p(
VALUE obj)
2307#define OBJ_ID_INCREMENT (RUBY_IMMEDIATE_MASK + 1)
2308#define LAST_OBJECT_ID() (object_id_counter * OBJ_ID_INCREMENT)
2310#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
2311static size_t object_id_counter = 1;
2313static unsigned long long object_id_counter = 1;
2317generate_next_object_id(
void)
2319#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
2323 unsigned int lock_lev = RB_GC_VM_LOCK();
2324 VALUE id =
ULL2NUM(++object_id_counter * OBJ_ID_INCREMENT);
2325 RB_GC_VM_UNLOCK(lock_lev);
2330static void gc_mark_tbl_no_pin(
st_table *table);
2333class_object_id(
VALUE klass)
2335 VALUE id = RUBY_ATOMIC_VALUE_LOAD(
RCLASS(klass)->object_id);
2337 unsigned int lock_lev = RB_GC_VM_LOCK();
2338 id = generate_next_object_id();
2343 RB_GC_VM_UNLOCK(lock_lev);
2349object_id_get(
VALUE obj, shape_id_t shape_id)
2352 if (rb_shape_complex_p(shape_id)) {
2353 id = rb_obj_field_get(obj, ROOT_COMPLEX_WITH_OBJ_ID);
2356 id = rb_obj_field_get(obj, rb_shape_object_id(shape_id));
2362 rb_bug(
"Object's shape includes object_id, but it's missing %s", rb_obj_info(obj));
2370object_id0(
VALUE obj)
2373 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2375 if (rb_shape_has_object_id(shape_id)) {
2376 return object_id_get(obj, shape_id);
2379 shape_id_t object_id_shape_id = rb_obj_shape_transition_object_id(obj);
2381 id = generate_next_object_id();
2382 rb_obj_field_set(obj, object_id_shape_id, 0,
id);
2398 return class_object_id(obj);
2407 unsigned int lock_lev = RB_GC_VM_LOCK();
2408 VALUE id = object_id0(obj);
2409 RB_GC_VM_UNLOCK(lock_lev);
2413 return object_id0(obj);
2417rb_gc_obj_free_vm_weak_references(
VALUE obj)
2424 if (rb_obj_gen_fields_p(obj)) {
2431 rb_gc_free_fstring(obj);
2435 rb_gc_free_dsymbol(obj);
2438 switch (imemo_type(obj)) {
2439 case imemo_callinfo:
2458#if SIZEOF_LONG == SIZEOF_VOIDP
2465 return get_heap_object_id(obj);
2469nonspecial_obj_id(
VALUE obj)
2471#if SIZEOF_LONG == SIZEOF_VOIDP
2473#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2476# error not supported
2483 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
2520 return rb_find_object_id(rb_gc_get_objspace(), obj, object_id);
2524rb_obj_id_p(
VALUE obj)
2536rb_gc_before_updating_jit_code(
void)
2539 rb_yjit_mark_all_writeable();
2542 rb_zjit_mark_all_writable();
2552rb_gc_after_updating_jit_code(
void)
2555 rb_yjit_mark_all_executable();
2558 rb_zjit_mark_all_executable();
2565 size_t *size = (
size_t *)arg;
2568 if (RCLASSEXT_M_TBL(ext)) {
2569 s += rb_id_table_memsize(RCLASSEXT_M_TBL(ext));
2571 if (RCLASSEXT_CONST_TBL(ext)) {
2572 s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
2574 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2575 s += (RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1) *
sizeof(
VALUE);
2584classext_superclasses_memsize(
rb_classext_t *ext,
bool prime,
VALUE box_value,
void *arg)
2586 size_t *size = (
size_t *)arg;
2588 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2590 array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
2591 *size += array_size *
sizeof(
VALUE);
2596rb_obj_memsize_of(
VALUE obj)
2609 rb_class_classext_foreach(obj, classext_memsize, (
void *)&size);
2610 rb_class_classext_foreach(obj, classext_superclasses_memsize, (
void *)&size);
2613 if (RICLASS_OWNS_M_TBL_P(obj)) {
2614 if (RCLASS_M_TBL(obj)) {
2615 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
2620 size += rb_str_memsize(obj);
2623 size += rb_ary_memsize(obj);
2626 if (RHASH_ST_TABLE_P(obj)) {
2627 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
2629 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(
st_table);
2638 size += rb_objspace_data_type_memsize(obj);
2644 size += onig_region_memsize(&rm->
as.onig);
2650 if (
RFILE(obj)->fptr) {
2651 size += rb_io_memsize(
RFILE(obj)->fptr);
2658 size += rb_imemo_memsize(obj);
2666 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2667 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
2672 UNEXPECTED_NODE(obj_memsize_of);
2676 if (RSTRUCT_EMBED_LEN(obj) == 0) {
2677 size +=
sizeof(
VALUE) * RSTRUCT_LEN_RAW(obj);
2686 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
2690 return size + rb_gc_obj_slot_size(obj);
2694set_zero(st_data_t key, st_data_t val, st_data_t arg)
2698 rb_hash_aset(hash, k,
INT2FIX(0));
2709count_objects_i(
VALUE obj,
void *d)
2713 if (
RBASIC(obj)->flags) {
2764count_objects(
int argc,
VALUE *argv,
VALUE os)
2776 for (
size_t i = 0; i <=
T_MASK; i++) {
2780 types[i] = type_sym(i);
2787 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2790 hash = rb_hash_new_capa(2 +
T_MASK);
2793 rb_hash_stlike_foreach(hash, set_zero, hash);
2795 rb_hash_aset(hash, total,
SIZET2NUM(data.total));
2796 rb_hash_aset(hash, free,
SIZET2NUM(data.freed));
2798 for (
size_t i = 0; i <=
T_MASK; i++) {
2799 if (data.counts[i]) {
2800 rb_hash_aset(hash, types[i],
SIZET2NUM(data.counts[i]));
2807#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2809#define STACK_START (ec->machine.stack_start)
2810#define STACK_END (ec->machine.stack_end)
2811#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2813#if STACK_GROW_DIRECTION < 0
2814# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2815#elif STACK_GROW_DIRECTION > 0
2816# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2818# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2819 : (size_t)(STACK_END - STACK_START + 1))
2821#if !STACK_GROW_DIRECTION
2822int ruby_stack_grow_direction;
2824ruby_get_stack_grow_direction(
volatile VALUE *addr)
2827 SET_MACHINE_STACK_END(&end);
2829 if (end > addr)
return ruby_stack_grow_direction = 1;
2830 return ruby_stack_grow_direction = -1;
2839 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2840 return STACK_LENGTH;
2843#define PREVENT_STACK_OVERFLOW 1
2844#ifndef PREVENT_STACK_OVERFLOW
2845#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2846# define PREVENT_STACK_OVERFLOW 1
2848# define PREVENT_STACK_OVERFLOW 0
2851#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2857 size_t length = STACK_LENGTH;
2858 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2860 return length > maximum_length;
2863#define stack_check(ec, water_mark) FALSE
2866#define STACKFRAME_FOR_CALL_CFUNC 2048
2871 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2877 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2891 return cr != NULL ? &cr->mark_func_data : &GET_VM()->gc.mark_func_data;
2894 return &cr->mark_func_data;
2897#define GC_MARK_FUNC_DATA_SLOTP() gc_mark_func_data_slotp_of(rb_current_ractor_raw(false))
2901#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2902 if (!RB_SPECIAL_CONST_P(obj)) { \
2903 rb_ractor_t *const mark_cr = rb_current_ractor_raw(false); \
2904 struct gc_mark_func_data_struct **mfdp = gc_mark_func_data_slotp_of(mark_cr); \
2905 struct gc_mark_func_data_struct *mark_func_data = *mfdp; \
2906 void *objspace = gc_current_objspace_of(mark_cr); \
2907 if (LIKELY(mark_func_data == NULL)) { \
2908 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2909 (func)(objspace, (obj_or_ptr)); \
2911 else if (check_obj ? \
2912 rb_gc_impl_live_object_p(objspace, (const void *)obj) && \
2913 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2915 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2917 mark_func_data->mark_func((obj), mark_func_data->data); \
2918 *mfdp = mark_func_data; \
2924gc_mark_internal(
VALUE obj)
2926 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2932 gc_mark_internal(obj);
2936rb_gc_mark_and_move(
VALUE *ptr)
2938 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr,
false);
2942gc_mark_and_pin_internal(
VALUE obj)
2944 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2950 gc_mark_and_pin_internal(obj);
2954gc_mark_maybe_internal(
VALUE obj)
2956 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2962 gc_mark_maybe_internal(obj);
2965ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2967each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2978each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2980 if (end <= start)
return;
2981 each_location(start, end - start, cb, data);
2985gc_mark_maybe_each_location(
VALUE obj,
void *data)
2987 gc_mark_maybe_internal(obj);
2993 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2997rb_gc_mark_values(
long n,
const VALUE *values)
2999 for (
long i = 0; i < n; i++) {
3000 gc_mark_internal(values[i]);
3005rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
3007 for (
long i = 0; i < n; i++) {
3008 gc_mark_and_pin_internal(values[i]);
3013mark_key(st_data_t key, st_data_t value, st_data_t data)
3015 gc_mark_and_pin_internal((
VALUE)key);
3025 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
3029mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
3031 gc_mark_internal((
VALUE)key);
3032 gc_mark_internal((
VALUE)value);
3038pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
3040 gc_mark_and_pin_internal((
VALUE)key);
3041 gc_mark_and_pin_internal((
VALUE)value);
3047pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
3049 gc_mark_and_pin_internal((
VALUE)key);
3050 gc_mark_internal((
VALUE)value);
3056mark_hash(
VALUE hash)
3058 if (rb_hash_compare_by_id_p(hash)) {
3059 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
3062 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
3065 gc_mark_internal(RHASH(hash)->ifnone);
3073 st_foreach(tbl, pin_key_pin_value, 0);
3076static enum rb_id_table_iterator_result
3079 gc_mark_internal(me);
3081 return ID_TABLE_CONTINUE;
3088 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
3092static enum rb_id_table_iterator_result
3097 gc_mark_internal(ce->value);
3098 gc_mark_internal(ce->file);
3100 return ID_TABLE_CONTINUE;
3107 rb_id_table_foreach_values(tbl, mark_const_entry_i,
objspace);
3110#if STACK_GROW_DIRECTION < 0
3111#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
3112#elif STACK_GROW_DIRECTION > 0
3113#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
3115#define GET_STACK_BOUNDS(start, end, appendix) \
3116 ((STACK_END < STACK_START) ? \
3117 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
3121gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
3123 gc_mark_maybe_internal(obj);
3125#ifdef RUBY_ASAN_ENABLED
3127 void *fake_frame_start;
3128 void *fake_frame_end;
3129 bool is_fake_frame = asan_get_fake_stack_extents(
3130 ec->machine.asan_fake_stack_handle, obj,
3131 ec->machine.stack_start, ec->machine.stack_end,
3132 &fake_frame_start, &fake_frame_end
3134 if (is_fake_frame) {
3135 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
3147 return rb_gc_impl_object_moved_p(
objspace, obj);
3157 return rb_gc_impl_location(
objspace, value);
3163 return gc_location_internal(rb_gc_get_objspace(), value);
3169 VALUE destination = rb_gc_location(*ptr);
3170 if (destination != *ptr) {
3175#if defined(__wasm__)
3178static VALUE *rb_stack_range_tmp[2];
3181rb_mark_locations(
void *begin,
void *end)
3183 rb_stack_range_tmp[0] = begin;
3184 rb_stack_range_tmp[1] = end;
3188rb_gc_save_machine_context(
void)
3193# if defined(__EMSCRIPTEN__)
3198 emscripten_scan_stack(rb_mark_locations);
3199 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3201 emscripten_scan_registers(rb_mark_locations);
3202 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3209 VALUE *stack_start, *stack_end;
3211 GET_STACK_BOUNDS(stack_start, stack_end, 1);
3212 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
3214 rb_wasm_scan_locals(rb_mark_locations);
3215 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3223rb_gc_save_machine_context(
void)
3227 RB_VM_SAVE_MACHINE_CONTEXT(thread);
3234 rb_gc_mark_machine_context(ec);
3241 VALUE *stack_start, *stack_end;
3243 GET_STACK_BOUNDS(stack_start, stack_end, 0);
3244 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
3247#ifdef RUBY_ASAN_ENABLED
3254 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
3255 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
3256 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
3260rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
3262 gc_mark_and_pin_internal((
VALUE)value);
3270 if (!tbl || tbl->num_entries == 0)
return;
3272 st_foreach(tbl, rb_mark_tbl_i, 0);
3278 if (!tbl || tbl->num_entries == 0)
return;
3280 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
3286 gc_mark_tbl_no_pin(tbl);
3290rb_gc_mark_set_no_pin(
st_table *tbl)
3292 if (!tbl || tbl->num_entries == 0)
return;
3294 st_foreach(tbl, gc_mark_set_no_pin_i, 0);
3300 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
3306 void *
objspace = rb_gc_get_objspace();
3308 if (RB_LIKELY(rb_gc_impl_during_gc_p(
objspace))) {
3309 return rb_gc_impl_get_vm_context(
objspace)->ec;
3317rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
3320 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3322#define MARK_CHECKPOINT(category) do { \
3323 if (categoryp) *categoryp = category; \
3328 const bool global_gc = rb_gc_impl_during_global_gc_p(
objspace) ||
3329 !rb_gc_impl_multi_objspace_p();
3333 MARK_CHECKPOINT(
"ractor");
3336 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
3337 rb_ractor_mark_local_roots(r);
3342 if (vm->ractor.cnt == 0 && vm->ractor.main_ractor) {
3343 rb_ractor_mark_local_roots(vm->ractor.main_ractor);
3349 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
3350 rb_ractor_t *owner = vm->gc.zombie_objspaces[i].owner;
3352 rb_gc_mark_vm_stack_values((
long)owner->registered_marks_cnt,
3353 owner->registered_marks);
3360 if (!rb_gc_impl_multi_objspace_p()) {
3363 ccan_list_for_each(&vm->ractor.terminated_set,
tr, vmlr_node) {
3364 rb_gc_mark_vm_stack_values((
long)
tr->registered_marks_cnt,
3365 tr->registered_marks);
3371 rb_ractor_mark_local_roots(rb_ec_ractor_ptr(ec));
3377 MARK_CHECKPOINT(
"registered_globals");
3379 for (
size_t i = 0; i < vm->gc.registered_globals.addrs_cnt; i++) {
3380 rb_gc_mark_maybe(*vm->gc.registered_globals.addrs[i]);
3387 MARK_CHECKPOINT(
"trap_list");
3388 rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
3392 if (global_gc ||
objspace == vm->ractor.main_ractor->objspace) {
3395 MARK_CHECKPOINT(
"end_proc");
3398 MARK_CHECKPOINT(
"vm");
3402 const bool vm_mark_needs_lock = rb_multi_ractor_p() && !global_gc;
3403 unsigned int vm_mark_lock_lev = 0;
3404 if (vm_mark_needs_lock) vm_mark_lock_lev = RB_GC_VM_LOCK_NO_BARRIER();
3407 MARK_CHECKPOINT(
"global_tbl");
3408 rb_gc_mark_global_tbl();
3411 void rb_yjit_root_mark(
void);
3413 if (rb_yjit_enabled_p) {
3414 MARK_CHECKPOINT(
"YJIT");
3415 rb_yjit_root_mark();
3420 void rb_zjit_root_mark(
void);
3421 if (rb_zjit_enabled_p) {
3422 MARK_CHECKPOINT(
"ZJIT");
3423 rb_zjit_root_mark();
3426 if (vm_mark_needs_lock) RB_GC_VM_UNLOCK_NO_BARRIER(vm_mark_lock_lev);
3428 if (global_gc || rb_gc_single_objspace_p()) {
3429 MARK_CHECKPOINT(
"global_symbols");
3430 rb_sym_global_symbols_mark_and_move();
3438 if (!rb_gc_impl_during_postmortem_p(
objspace)) {
3439 MARK_CHECKPOINT(
"machine_context");
3440 mark_current_machine_context(ec);
3443 MARK_CHECKPOINT(
"finish");
3445#undef MARK_CHECKPOINT
3459 if (RCLASSEXT_SUPER(ext)) {
3460 gc_mark_internal(RCLASSEXT_SUPER(ext));
3462 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3464 gc_mark_internal(RCLASSEXT_FIELDS_OBJ(ext));
3465 gc_mark_internal(RCLASSEXT_CVC_TBL(ext));
3467 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
3468 mark_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
3470 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3471 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3472 if (RCLASSEXT_SUBCLASSES(ext)) {
3473 gc_mark_internal(RCLASSEXT_SUBCLASSES(ext));
3475 gc_mark_internal(RCLASSEXT_CLASSPATH(ext));
3484 if (RCLASSEXT_SUPER(ext)) {
3485 gc_mark_internal(RCLASSEXT_SUPER(ext));
3487 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
3488 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3490 if (RCLASSEXT_INCLUDER(ext)) {
3491 gc_mark_internal(RCLASSEXT_INCLUDER(ext));
3493 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3494 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3495 if (RCLASSEXT_SUBCLASSES(ext)) {
3496 gc_mark_internal(RCLASSEXT_SUBCLASSES(ext));
3500#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA_TYPE(d)->function.dmark
3503rb_obj_using_gen_fields_table_p(
VALUE obj)
3514 return rb_obj_gen_fields_p(obj);
3518rb_gc_move_obj_during_marking(
VALUE from,
VALUE to)
3520 if (rb_obj_using_gen_fields_table_p(to)) {
3521 rb_mark_generic_ivar(from);
3530 if (rb_obj_using_gen_fields_table_p(obj)) {
3531 rb_mark_generic_ivar(obj);
3541 rb_bug(
"rb_gc_mark() called for broken object");
3545 UNEXPECTED_NODE(rb_gc_mark);
3549 rb_imemo_mark_and_move(obj,
false);
3556 gc_mark_internal(
RBASIC(obj)->klass);
3561 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
3566 foreach_args.obj = obj;
3567 rb_class_classext_foreach(obj, gc_mark_classext_module, (
void *)&foreach_args);
3568 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3569 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3575 foreach_args.obj = obj;
3576 rb_class_classext_foreach(obj, gc_mark_classext_iclass, (
void *)&foreach_args);
3577 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3578 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3583 if (ARY_SHARED_P(obj)) {
3584 VALUE root = ARY_SHARED_ROOT(obj);
3586 gc_mark_internal(root);
3592 gc_mark_and_pin_internal(root);
3598 for (
long i = 0; i <
len; i++) {
3599 gc_mark_internal(ptr[i]);
3609 gc_mark_internal(RSYMBOL(obj)->fstr);
3613 if (STR_SHARED_P(obj)) {
3614 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
3619 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
3622 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
3628 void *
const ptr = RTYPEDDATA_GET_DATA(obj);
3630 gc_mark_internal(
RTYPEDDATA(obj)->fields_obj);
3634 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3636 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3637 gc_mark_internal(*(
VALUE *)((
char *)ptr + offset));
3642 if (mark_func) (*mark_func)(ptr);
3650 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
3651 if (rb_shape_embedded_p(shape_id)) {
3652 uint32_t
len = RSHAPE_LEN(shape_id);
3655 for (uint32_t i = 0; i <
len; i++) {
3656 gc_mark_internal(ptr[i]);
3660 if (!rb_gc_checking_shareable()) {
3661 gc_mark_internal(
ROBJECT(obj)->as.extended);
3668 if (
RFILE(obj)->fptr) {
3669 gc_mark_internal(
RFILE(obj)->fptr->self);
3670 gc_mark_internal(
RFILE(obj)->fptr->pathv);
3671 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
3672 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
3673 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
3674 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
3675 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
3676 gc_mark_internal(
RFILE(obj)->fptr->timeout);
3677 gc_mark_internal(
RFILE(obj)->fptr->wakeup_mutex);
3682 gc_mark_internal(
RREGEXP(obj)->src);
3686 gc_mark_internal(
RMATCH(obj)->regexp);
3688 gc_mark_internal(
RMATCH(obj)->str);
3693 gc_mark_internal(RRATIONAL(obj)->num);
3694 gc_mark_internal(RRATIONAL(obj)->den);
3698 gc_mark_internal(RCOMPLEX(obj)->real);
3699 gc_mark_internal(RCOMPLEX(obj)->imag);
3704 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
3706 for (
long i = 0; i <
len; i++) {
3707 gc_mark_internal(ptr[i]);
3710 gc_mark_internal(RSTRUCT_FIELDS_OBJ(obj));
3719 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
3721 rb_gc_impl_live_object_p(
objspace, (
void *)obj) ?
"corrupted object" :
"non object");
3726rb_gc_obj_optimal_size(
VALUE obj)
3731 size_t size = rb_ary_size_as_embedded(obj);
3732 if (rb_gc_size_allocatable_p(size)) {
3736 return sizeof(
struct RArray);
3741 if (rb_obj_shape_complex_p(obj)) {
3742 return sizeof(
struct RObject);
3745 size_t size = rb_obj_embedded_size(RSHAPE_CAPACITY(RBASIC_SHAPE_ID(obj)));
3746 if (rb_gc_size_allocatable_p(size)) {
3750 return sizeof(
struct RObject);
3756 size_t size = rb_str_size_as_embedded(obj);
3757 if (rb_gc_size_allocatable_p(size)) {
3761 return sizeof(
struct RString);
3767 if (RHASH_AR_TABLE_P(obj)) {
3768 const unsigned bound = RHASH_AR_TABLE_BOUND(obj);
3769 const size_t ar_size = RHASH_AR_SLOT_SIZE(bound);
3770 if (ar_size > RHASH_ST_SLOT_SIZE ||
OBJ_FROZEN(obj)) {
3775 return RHASH_ST_SLOT_SIZE;
3786 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
3792 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
3799rb_gc_writebarrier_remember(
VALUE obj)
3801 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
3807rb_gc_obj_became_shareable(
VALUE obj)
3809 rb_gc_impl_obj_became_shareable(rb_gc_get_objspace(), obj);
3817 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
3822rb_gc_modular_gc_loaded_p(
void)
3824 return rb_gc_functions.modular_gc_loaded_p;
3828rb_gc_active_gc_name(
void)
3830 const char *gc_name = rb_gc_impl_active_gc_name();
3832 const size_t len = strlen(gc_name);
3833 if (
len > RB_GC_MAX_NAME_LEN) {
3834 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
3835 RB_GC_MAX_NAME_LEN,
len, gc_name);
3843rb_gc_object_metadata(
VALUE obj)
3845 return rb_gc_impl_object_metadata(rb_gc_get_objspace(), obj);
3853 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
3857rb_gc_ractor_cache_free(
void *cache)
3859 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3865#if defined(RUBY_ASAN_ENABLED)
3866 (void)rb_gc_impl_zjit_new_obj_fastpath;
3869 return rb_gc_impl_zjit_new_obj_fastpath(rb_gc_get_objspace(), alloc_size, flags, klass, fastpath);
3880 if (rb_gc_impl_live_object_p(rb_gc_get_objspace(), (
void *)obj)) {
3881 rb_vm_register_global_object(obj);
3892 if (vm->gc.registered_globals.addrs_cnt == vm->gc.registered_globals.addrs_capa) {
3893 size_t nc = vm->gc.registered_globals.addrs_capa ? vm->gc.registered_globals.addrs_capa * 2 : 64;
3894 VALUE **p = realloc(vm->gc.registered_globals.addrs, nc *
sizeof(
VALUE *));
3895 if (!p) rb_bug(
"rb_gc_register_address: out of memory");
3896 vm->gc.registered_globals.addrs = p;
3897 vm->gc.registered_globals.addrs_capa = nc;
3899 vm->gc.registered_globals.addrs[vm->gc.registered_globals.addrs_cnt++] = addr;
3915 for (
size_t i = 0; i < vm->gc.registered_globals.addrs_cnt; i++) {
3916 if (vm->gc.registered_globals.addrs[i] == addr) {
3917 MEMMOVE(&vm->gc.registered_globals.addrs[i], &vm->gc.registered_globals.addrs[i + 1],
3918 VALUE *, vm->gc.registered_globals.addrs_cnt - i - 1);
3919 vm->gc.registered_globals.addrs_cnt--;
3929 rb_gc_register_address(var);
3935 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
3942 int (*callback)(
void *,
void *, size_t,
void *);
3947each_objects_foreign_i(
void *
objspace,
void *arg)
3951 rb_gc_impl_each_objects_foreign(
objspace, a->callback, a->data);
3999rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
4004 void *self = rb_gc_get_objspace();
4005 rb_gc_impl_each_objects(self, callback, data);
4012 rb_gc_vm_each_objspace(each_objects_foreign_i, &arg);
4020rb_gc_vm_each_objspace(
void (*func)(
void *
objspace,
void *data),
void *data)
4022 ASSERT_vm_locking();
4026 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4028 func(r->objspace, data);
4033 if (r->creating_child_objspace) {
4034 func(r->creating_child_objspace, data);
4037 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4038 func(vm->gc.zombie_objspaces[i].objspace, data);
4046static void gc_orphan_merge_job(
void *unused);
4054 ASSERT_vm_locking();
4055 if (vm->gc.zombie_objspaces_count == vm->gc.zombie_objspaces_capa) {
4056 size_t new_capa = vm->gc.zombie_objspaces_capa ? vm->gc.zombie_objspaces_capa * 2 : 16;
4057 struct rb_objspace_zombie *grown =
4058 realloc(vm->gc.zombie_objspaces, new_capa *
sizeof(
struct rb_objspace_zombie));
4059 if (grown == NULL) rb_bug(
"zombie_objspaces_push: out of memory");
4060 vm->gc.zombie_objspaces = grown;
4061 vm->gc.zombie_objspaces_capa = new_capa;
4063 size_t pages = rb_gc_impl_heap_page_count(
objspace);
4064 vm->gc.zombie_objspaces[vm->gc.zombie_objspaces_count++] = (
struct rb_objspace_zombie){
4066 .owner_slot = owner_slot,
4070 vm->gc.zombie_total_pages += pages;
4081gc_orphan_merge_pjob_ensure(
void)
4083 if (GET_VM()->gc.orphan_merge_pjob == POSTPONED_JOB_HANDLE_INVALID) {
4085 if (GET_VM()->gc.orphan_merge_pjob == POSTPONED_JOB_HANDLE_INVALID) {
4086 rb_bug(
"Could not preregister postponed job for GC");
4093rb_gc_objspace_retire_gc(
void)
4095 rb_gc_impl_objspace_retire_gc(rb_gc_get_objspace());
4099rb_gc_objspace_retire(
void **objspace_slot)
4103 if (!rb_gc_impl_multi_objspace_p()) {
4105 *objspace_slot = NULL;
4111 if (rb_gc_impl_user_gc_disabled_set(*objspace_slot,
false)) {
4116 gc_orphan_merge_pjob_ensure();
4122 zombie_objspaces_push(vm, *objspace_slot, objspace_slot, owner);
4130rb_gc_objspace_disown(
void *
objspace)
4132 if (!rb_gc_impl_multi_objspace_p())
return;
4133 ASSERT_vm_locking();
4137 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4138 if (vm->gc.zombie_objspaces[i].objspace ==
objspace) {
4139 vm->gc.zombie_objspaces[i].owner_slot = NULL;
4142 vm->gc.zombie_objspaces[i].owner = NULL;
4148 zombie_objspaces_push(vm,
objspace, NULL, NULL);
4153 gc_orphan_merge_pjob_ensure();
4154 rb_postponed_job_trigger_for_ractor(GET_VM()->gc.orphan_merge_pjob, vm->ractor.main_ractor->pub.self);
4160rb_gc_during_global_gc_p(
void)
4162 return rb_gc_impl_during_global_gc_p(rb_gc_get_objspace());
4166rb_gc_vm_forget_zombie(
void *
objspace)
4168 ASSERT_vm_locking();
4170 size_t n = vm->gc.zombie_objspaces_count;
4171 for (
size_t i = 0; i < n; i++) {
4172 if (vm->gc.zombie_objspaces[i].objspace ==
objspace) {
4173 vm->gc.zombie_total_pages -= vm->gc.zombie_objspaces[i].pages;
4174 vm->gc.zombie_objspaces[i] = vm->gc.zombie_objspaces[n - 1];
4175 vm->gc.zombie_objspaces_count = n - 1;
4185rb_gc_vm_zombie_total_pages(
void)
4187 return GET_VM()->gc.zombie_total_pages;
4192rb_gc_vm_ractor_count(
void)
4194 return GET_VM()->ractor.cnt;
4199rb_gc_vm_refresh_zombie_pages(
void)
4203 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4204 size_t pages = rb_gc_impl_heap_page_count(vm->gc.zombie_objspaces[i].objspace);
4205 vm->gc.zombie_objspaces[i].pages = pages;
4208 vm->gc.zombie_total_pages = total;
4217 rb_gc_impl_gc_rest(rb_gc_get_objspace());
4224static int gc_absorbing_zombie = 0;
4229static bool gc_absorbed_since_global_gc =
false;
4232rb_gc_reset_absorbed_since_global_gc(
void)
4234 gc_absorbed_since_global_gc =
false;
4247rb_gc_multi_objspace_p(
void)
4249 return rb_gc_impl_multi_objspace_p();
4255rb_gc_obj_foreign_p(
VALUE obj)
4257 return rb_gc_impl_obj_foreign_p(rb_gc_get_objspace(), obj);
4261rb_gc_single_objspace_p(
void)
4263 if (!rb_gc_impl_multi_objspace_p())
return true;
4267 return (ruby_single_main_ractor != NULL || vm->ractor.cnt == 1) &&
4268 vm->gc.zombie_objspaces_count == 0 && gc_absorbing_zombie == 0 &&
4269 !gc_absorbed_since_global_gc &&
4270 (vm->ractor.main_ractor == NULL ||
4271 vm->ractor.main_ractor->creating_child_objspace == NULL);
4278objspace_absorb_merge(
void *dst,
void *src)
4280 ASSERT_vm_locking();
4281 rb_gc_impl_objspace_absorb(dst, src);
4282 gc_absorbed_since_global_gc =
true;
4289rb_gc_objspace_postmortem_self(
void)
4291 if (!rb_gc_impl_multi_objspace_p())
return;
4293 rb_gc_impl_objspace_retire_gc(rb_gc_get_objspace());
4297rb_gc_objspace_absorb_into_current(
void **objspace_slot)
4299 if (!rb_gc_impl_multi_objspace_p()) {
4300 *objspace_slot = NULL;
4306 *objspace_slot = NULL;
4307 gc_absorbing_zombie++;
4309 objspace_absorb_merge(rb_gc_get_objspace(),
objspace);
4310 gc_absorbing_zombie--;
4319objspace_absorb_disowned_zombies(
void)
4325 while (i < vm->gc.zombie_objspaces_count) {
4326 if (vm->gc.zombie_objspaces[i].owner_slot == NULL) {
4327 void *zombie = vm->gc.zombie_objspaces[i].objspace;
4331 gc_absorbing_zombie++;
4332 rb_gc_vm_forget_zombie(zombie);
4333 objspace_absorb_merge(rb_gc_get_objspace(), zombie);
4334 gc_absorbing_zombie--;
4344gc_orphan_merge_job(
void *unused)
4347 objspace_absorb_disowned_zombies();
4355rb_gc_disable_holders_atfork(
void)
4358 rb_gc_impl_user_gc_disabled_p(rb_gc_get_objspace()) ? 1 : 0);
4362rb_gc_zombie_objspaces_atfork(
void)
4366 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4367 if (vm->gc.zombie_objspaces[i].owner_slot == NULL) {
4368 rb_postponed_job_trigger_for_ractor(GET_VM()->gc.orphan_merge_pjob, vm->ractor.main_ractor->pub.self);
4378rb_gc_objspace_absorb_all_zombies(
void)
4384 objspace_absorb_disowned_zombies();
4386 while (vm->gc.zombie_objspaces_count > 0) {
4387 size_t before = vm->gc.zombie_objspaces_count;
4388 GC_ASSERT(vm->gc.zombie_objspaces[0].owner_slot != NULL);
4391 rb_ractor_t *owner = vm->gc.zombie_objspaces[0].owner;
4393 rb_ractor_absorb_registered_marks(GET_RACTOR(), owner);
4395 rb_gc_objspace_absorb_into_current(vm->gc.zombie_objspaces[0].owner_slot);
4396 if (vm->gc.zombie_objspaces_count >= before) {
4397 rb_bug(
"rb_gc_objspace_absorb_all_zombies: zombie list did not shrink");
4405 if (ARY_SHARED_P(v)) {
4406 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
4410 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
4414 if (
RB_TYPE_P(new_root,
T_ARRAY) && ARY_EMBED_P(new_root) && new_root != old_root) {
4415 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
4416 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
4417 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
4425 for (
long i = 0; i <
len; i++) {
4430 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
4434 if (rb_ary_embeddable_p(v) && !rb_gc_impl_pinned_p(
objspace, v)) {
4435 rb_ary_make_embedded(v);
4444 RUBY_ASSERT(rb_gc_obj_slot_size(v) == rb_obj_shape_slot_size(v));
4445 shape_id_t shape_id = RBASIC_SHAPE_ID(v);
4447 if (!rb_shape_embedded_p(shape_id)) {
4450 if (!rb_shape_complex_p(shape_id) && rb_shape_embedded_capacity(shape_id) >= RSHAPE_LEN(shape_id)) {
4451 VALUE *embedded_fields = ROBJECT_EMBEDDED_FIELDS(v);
4452 VALUE *extended_fields = ROBJECT_FIELDS(v);
4453 MEMCPY(embedded_fields, extended_fields,
VALUE, RSHAPE_LEN(shape_id));
4454 shape_id = rb_shape_transition_robject(shape_id);
4455 RBASIC_SET_FULL_SHAPE_ID(v, shape_id);
4456 rb_gc_writebarrier_remember(v);
4463 VALUE *ptr = ROBJECT_FIELDS(v);
4464 attr_index_t
len = RSHAPE_LEN(shape_id);
4465 for (attr_index_t i = 0; i <
len; i++) {
4471rb_gc_ref_update_table_values_only(
st_table *tbl)
4473 gc_ref_update_table_values_only(tbl);
4480 gc_update_table_refs(ptr);
4484rb_gc_update_set_refs_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
4486 if (rb_gc_location((
VALUE)key) != (
VALUE)key) {
4494rb_gc_update_set_refs_replace_i(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
4496 rb_gc_update_moved((
VALUE *)key);
4502rb_gc_update_set_refs(
st_table *tbl)
4504 if (!tbl || tbl->num_entries == 0)
return;
4506 if (st_foreach_with_replace(tbl, rb_gc_update_set_refs_i, rb_gc_update_set_refs_replace_i, 0)) {
4514 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
4520 for (
long i = 0; i < n; i++) {
4521 UPDATE_IF_MOVED(
objspace, values[i]);
4526rb_gc_update_values(
long n,
VALUE *values)
4528 gc_update_values(rb_gc_get_objspace(), n, values);
4531static enum rb_id_table_iterator_result
4532check_id_table_move(
VALUE value,
void *data)
4537 return ID_TABLE_REPLACE;
4540 return ID_TABLE_CONTINUE;
4544rb_gc_prepare_heap_process_object(
VALUE obj)
4550 rb_enc_str_coderange(obj);
4558rb_gc_prepare_heap(
void)
4560 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
4564rb_gc_size_slot_size(
size_t size)
4566 return rb_gc_impl_size_slot_size(rb_gc_get_objspace(), size);
4570rb_gc_size_allocatable_p(
size_t size)
4572 return rb_gc_impl_size_allocatable_p(size);
4576rb_gc_max_allocation_size(
void)
4578 return rb_gc_impl_max_allocation_size();
4581static enum rb_id_table_iterator_result
4582update_id_table(
VALUE *value,
void *data,
int existing)
4590 return ID_TABLE_CONTINUE;
4597 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
4601static enum rb_id_table_iterator_result
4606 if (gc_object_moved_p_internal(
objspace, ce->value)) {
4607 ce->value = gc_location_internal(
objspace, ce->value);
4610 if (gc_object_moved_p_internal(
objspace, ce->file)) {
4611 ce->file = gc_location_internal(
objspace, ce->file);
4614 return ID_TABLE_CONTINUE;
4621 rb_id_table_foreach_values(tbl, update_const_tbl_i,
objspace);
4627 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
4628 size_t array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
4629 for (
size_t i = 0; i < array_size; i++) {
4630 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPERCLASSES(ext)[i]);
4638 UPDATE_IF_MOVED(
objspace, RCLASSEXT_ORIGIN(ext));
4639 UPDATE_IF_MOVED(
objspace, RCLASSEXT_REFINED_CLASS(ext));
4640 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CLASSPATH(ext));
4642 UPDATE_IF_MOVED(
objspace, RCLASSEXT_INCLUDER(ext));
4652 if (RCLASSEXT_SUPER(ext)) {
4653 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
4656 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
4658 UPDATE_IF_MOVED(
objspace, ext->fields_obj);
4659 if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
4660 update_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
4662 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
4663 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
4664 update_superclasses(
objspace, ext);
4665 if (RCLASSEXT_SUBCLASSES(ext)) {
4666 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUBCLASSES(ext));
4669 update_classext_values(
objspace, ext,
false);
4678 if (RCLASSEXT_SUPER(ext)) {
4679 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
4681 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
4682 update_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
4683 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
4684 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
4685 if (RCLASSEXT_SUBCLASSES(ext)) {
4686 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUBCLASSES(ext));
4689 update_classext_values(
objspace, ext,
true);
4693 vm_table_foreach_callback_func callback;
4694 vm_table_update_callback_func update_callback;
4699 struct st_table *gen_fields_current_tbl;
4703 size_t gf_deferred_cnt, gf_deferred_capa;
4707vm_weak_table_foreach_weak_key(st_data_t key, st_data_t value, st_data_t data,
int error)
4711 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4713 if (!iter_data->weak_only) {
4714 if (ret != ST_CONTINUE)
return ret;
4716 ret = iter_data->callback((
VALUE)value, iter_data->data);
4723vm_weak_table_foreach_update_weak_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
4727 int ret = iter_data->update_callback((
VALUE *)key, iter_data->data);
4729 if (!iter_data->weak_only) {
4730 if (ret != ST_CONTINUE)
return ret;
4732 ret = iter_data->update_callback((
VALUE *)value, iter_data->data);
4739vm_weak_table_sym_set_foreach(
VALUE *sym_ptr,
void *data)
4741 VALUE sym = *sym_ptr;
4746 int ret = iter_data->callback(sym, iter_data->data);
4748 if (ret == ST_REPLACE) {
4749 ret = iter_data->update_callback(sym_ptr, iter_data->data);
4755struct st_table *rb_generic_fields_tbl_get(
void);
4758vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
4762 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4777 RBASIC_SET_SHAPE_ID((
VALUE)key, ROOT_SHAPE_ID);
4782 ret = iter_data->update_callback(&new_key, iter_data->data);
4783 if (key != new_key) {
4790 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ret);
4793 if (!iter_data->weak_only) {
4794 int ivar_ret = iter_data->callback(new_value, iter_data->data);
4800 iter_data->update_callback(&new_value, iter_data->data);
4807 RBASIC_SET_SHAPE_ID((
VALUE)key, ROOT_SHAPE_ID);
4811 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ivar_ret);
4815 if (key != new_key) {
4817 if (iter_data->gf_deferred_cnt == iter_data->gf_deferred_capa) {
4818 size_t nc = iter_data->gf_deferred_capa ? iter_data->gf_deferred_capa * 2 : 64;
4819 struct gen_fields_deferred_insert *p =
4820 realloc(iter_data->gf_deferred, nc *
sizeof(*p));
4821 if (!p) rb_bug(
"vm_weak_table_gen_fields_foreach: out of memory");
4822 iter_data->gf_deferred = p;
4823 iter_data->gf_deferred_capa = nc;
4825 iter_data->gf_deferred[iter_data->gf_deferred_cnt++] =
4826 (
struct gen_fields_deferred_insert){ .k = (st_data_t)new_key, .v = (st_data_t)new_value };
4828 else if (value != new_value) {
4829 DURING_GC_COULD_MALLOC_REGION_START();
4832 st_insert(iter_data->gen_fields_current_tbl, (st_data_t)new_key, new_value);
4834 DURING_GC_COULD_MALLOC_REGION_END();
4841vm_weak_table_frozen_strings_foreach(
VALUE *str,
void *data)
4845 int retval = iter_data->callback(*str, iter_data->data);
4847 if (retval == ST_REPLACE) {
4848 retval = iter_data->update_callback(str, iter_data->data);
4851 if (retval == ST_DELETE) {
4858void rb_fstring_foreach_with_replace(
int (*callback)(
VALUE *str,
void *data),
void *data);
4864vm_weak_table_gen_fields_tbl_cb(
struct st_table *tbl,
void *arg)
4867 foreach_data->gen_fields_current_tbl = tbl;
4868 st_foreach(tbl, vm_weak_table_gen_fields_foreach, (st_data_t)foreach_data);
4872rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
4873 vm_table_update_callback_func update_callback,
4876 enum rb_gc_vm_weak_tables table)
4881 .callback = callback,
4882 .update_callback = update_callback,
4884 .weak_only = weak_only,
4888 case RB_GC_VM_CI_TABLE: {
4889 st_foreach_with_replace(
4891 vm_weak_table_foreach_weak_key,
4892 vm_weak_table_foreach_update_weak_key,
4893 (st_data_t)&foreach_data
4897 case RB_GC_VM_OVERLOADED_CME_TABLE: {
4898 st_foreach_with_replace(
4899 &vm->overloaded_cme_table,
4900 vm_weak_table_foreach_weak_key,
4901 vm_weak_table_foreach_update_weak_key,
4902 (st_data_t)&foreach_data
4906 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
4907 rb_sym_global_symbol_table_foreach_weak_reference(
4908 vm_weak_table_sym_set_foreach,
4913 case RB_GC_VM_GENERIC_FIELDS_TABLE: {
4918 if (rb_gc_during_global_gc_p()) {
4919 rb_generic_fields_tables_foreach(vm_weak_table_gen_fields_tbl_cb, (
void *)&foreach_data);
4921 else if (!weak_only) {
4922 rb_generic_fields_shared_table_foreach(vm_weak_table_gen_fields_tbl_cb, (
void *)&foreach_data);
4924 if (foreach_data.gf_deferred != NULL) {
4925 DURING_GC_COULD_MALLOC_REGION_START();
4927 for (
size_t i = 0; i < foreach_data.gf_deferred_cnt; i++) {
4928 struct gen_fields_deferred_insert *
const d = &foreach_data.gf_deferred[i];
4929 st_insert(foreach_data.gen_fields_current_tbl, d->k, d->v);
4932 DURING_GC_COULD_MALLOC_REGION_END();
4933 free(foreach_data.gf_deferred);
4937 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
4938 rb_fstring_foreach_with_replace(
4939 vm_weak_table_frozen_strings_foreach,
4944 case RB_GC_VM_WEAK_TABLE_COUNT:
4945 rb_bug(
"Unreachable");
4947 rb_bug(
"rb_gc_vm_weak_table_foreach: unknown table %d", table);
4959gf_mark_foreach_i(st_data_t key, st_data_t val, st_data_t data)
4962 return ctx->cb((
VALUE)key, (
VALUE)val, ctx->arg);
4966gf_mark_foreach_table_cb(
struct st_table *tbl,
void *arg)
4968 st_foreach(tbl, gf_mark_foreach_i, (st_data_t)arg);
4972rb_gc_vm_generic_fields_mark_foreach(
int (*cb)(
VALUE key,
VALUE val,
void *arg),
void *arg)
4975 rb_generic_fields_tables_foreach(gf_mark_foreach_table_cb, &ctx);
4979 bool (*is_dead)(
VALUE key);
4983gf_drain_i(st_data_t key, st_data_t val, st_data_t data)
4986 if (ctx->is_dead((
VALUE)key)) {
4996gf_drain_table_cb(
struct st_table *tbl,
void *arg)
4998 st_foreach(tbl, gf_drain_i, (st_data_t)arg);
5002rb_gc_vm_generic_fields_drain_dead(
bool (*is_dead)(
VALUE key))
5005 rb_generic_fields_tables_foreach(gf_drain_table_cb, &ctx);
5009rb_gc_vm_top_self(
void)
5011 return rb_vm_top_self();
5015rb_gc_update_vm_references(
void *
objspace)
5018 rb_vm_t *vm = rb_ec_vm_ptr(ec);
5020 rb_vm_update_references(vm);
5021 rb_gc_update_global_tbl();
5022 rb_sym_global_symbols_mark_and_move();
5025 void rb_yjit_root_update_references(
void);
5027 if (rb_yjit_enabled_p) {
5028 rb_yjit_root_update_references();
5033 void rb_zjit_root_update_references(
void);
5035 if (rb_zjit_enabled_p) {
5036 rb_zjit_root_update_references();
5049 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
5055 rb_class_classext_foreach(obj, update_classext, (
void *)&args);
5060 rb_class_classext_foreach(obj, update_iclass_classext, (
void *)&args);
5064 rb_imemo_mark_and_move(obj,
true);
5076 gc_ref_update_array(
objspace, obj);
5081 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
5086 if (STR_SHARED_P(obj)) {
5094 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
5095 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)
5096 && !rb_gc_impl_pinned_p(
objspace, obj)) {
5097 rb_str_make_embedded(obj);
5106 void *
const ptr = RTYPEDDATA_GET_DATA(obj);
5112 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
5114 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
5115 VALUE *ref = (
VALUE *)((
char *)ptr + offset);
5116 *ref = gc_location_internal(
objspace, *ref);
5121 if (compact_func) (*compact_func)(ptr);
5128 gc_ref_update_object(
objspace, obj);
5132 if (
RFILE(obj)->fptr) {
5135 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
5136 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
5137 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
5149 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
5165 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
5166 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
5170 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
5171 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
5178 VALUE *ptr = (
VALUE *)RSTRUCT_CONST_PTR(obj);
5180 for (i = 0; i <
len; i++) {
5184 UPDATE_IF_MOVED(
objspace, RSTRUCT(obj)->fields_obj);
5188 rb_bug(
"unreachable");
5205 unless_objspace(
objspace) {
return; }
5207 rb_gc_impl_start(
objspace,
true,
true,
true,
false);
5213 unless_objspace(
objspace) {
return FALSE; }
5215 return rb_gc_impl_during_gc_p(
objspace);
5221 return rb_gc_impl_gc_count(rb_gc_get_objspace());
5237 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
5240 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
5250 arg = rb_hash_new();
5256 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
5261 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
5274 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
5279 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
5294 arg = rb_hash_new();
5297 if (
NIL_P(heap_name)) {
5308 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
5311 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
5316 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
5325 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
5326 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
5334 void *
objspace = rb_gc_get_objspace();
5336 rb_gc_impl_config_set(
objspace, hash);
5344 return rb_gc_impl_stress_get(rb_gc_get_objspace());
5350 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
5356rb_gc_initial_stress_set(
VALUE flag)
5358 initial_stress = flag;
5366rb_gc_critical_disable(
void)
5368 rb_gc_impl_gc_rest(rb_gc_get_objspace());
5373rb_gc_critical_enable(
void)
5379rb_gc_gc_disabled_global_p(
void)
5387gc_ractor_disable_set(
bool disable)
5389 const bool was = rb_gc_impl_user_gc_disabled_set(rb_gc_get_objspace(), disable);
5390 if (was != disable) {
5404 return RBOOL(gc_ractor_disable_set(
false));
5408rb_gc_disable_no_rest(
void)
5410 return RBOOL(gc_ractor_disable_set(
true));
5416 const bool was_disabled = gc_ractor_disable_set(
true);
5417 if (!was_disabled) {
5418 rb_gc_impl_gc_rest(rb_gc_get_objspace());
5420 return RBOOL(was_disabled);
5424rb_objspace_gc_enable(
void *
objspace)
5426 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
5428 return RBOOL(disabled);
5432rb_objspace_gc_disable(
void *
objspace)
5434 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
5435 rb_gc_impl_gc_disable(
objspace,
true);
5436 return RBOOL(disabled);
5440rb_gc_objspace_enable(
void *
objspace)
5442 return rb_objspace_gc_enable(
objspace);
5446rb_gc_local_enable(
void)
5448 return rb_gc_objspace_enable(rb_gc_get_objspace());
5453rb_gc_objspace_disable_no_rest(
void *
objspace)
5455 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
5456 rb_gc_impl_gc_disable(
objspace,
false);
5457 return RBOOL(disabled);
5461rb_gc_local_disable_no_rest(
void)
5463 return rb_gc_objspace_disable_no_rest(rb_gc_get_objspace());
5469 return rb_gc_enable();
5475 return rb_gc_disable();
5480ruby_gc_set_params(
void)
5482 rb_gc_impl_set_params(rb_gc_get_objspace());
5486rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
5489 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
5500 rb_gc_mark_children(rb_gc_get_objspace(), obj);
5507 const char *category;
5508 void (*func)(
const char *category,
VALUE,
void *);
5513root_objects_from(
VALUE obj,
void *ptr)
5516 (*data->func)(data->category, obj, data->data);
5520rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
5522 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");
5526 .data = passing_data,
5532 .mark_func = root_objects_from,
5537 rb_gc_save_machine_context();
5538 rb_gc_mark_roots(rb_gc_get_objspace(), &data.category);
5550#define TYPE_NAME(t) case (t): return #t;
5577 if (obj && rb_objspace_data_type_name(obj)) {
5578 return rb_objspace_data_type_name(obj);
5587obj_type_name(
VALUE obj)
5589 return type_name(
TYPE(obj), obj);
5593rb_method_type_name(rb_method_type_t
type)
5596 case VM_METHOD_TYPE_ISEQ:
return "iseq";
5597 case VM_METHOD_TYPE_ATTRSET:
return "attrset";
5598 case VM_METHOD_TYPE_IVAR:
return "ivar";
5599 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
5600 case VM_METHOD_TYPE_ALIAS:
return "alias";
5601 case VM_METHOD_TYPE_REFINED:
return "refined";
5602 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
5603 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
5604 case VM_METHOD_TYPE_MISSING:
return "missing";
5605 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
5606 case VM_METHOD_TYPE_UNDEF:
return "undef";
5607 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
5609 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
5613rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
5615 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
5616 VALUE path = rb_iseq_path(iseq);
5617 int n = ISEQ_BODY(iseq)->location.first_lineno;
5618 VALUE label = ISEQ_BODY(iseq)->location.label;
5619 snprintf(buff, buff_size,
" %.*s@%.*s:%d",
5626str_len_no_raise(
VALUE str)
5628 long len = RSTRING_LEN(str);
5629 if (
len < 0)
return 0;
5630 if (
len > INT_MAX)
return INT_MAX;
5634#define BUFF_ARGS buff + pos, buff_size - pos
5635#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
5636#define APPEND_S(s) do { \
5637 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
5641 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
5644#define C(c, s) ((c) != 0 ? (s) : " ")
5647rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
5652 APPEND_F(
"%s", obj_type_name(obj));
5658 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
5664 if (rb_gc_impl_live_object_p(rb_gc_get_objspace(), (
void *)obj)) {
5665 APPEND_F(
"%p %s/", (
void *)obj, obj_type_name(obj));
5684 if (internal_object_p(obj)) {
5687 else if (
RBASIC(obj)->klass == 0) {
5688 APPEND_S(
"(temporary internal)");
5692 if (!
NIL_P(class_path)) {
5693 APPEND_F(
"%.*s ", str_len_no_raise(class_path), RSTRING_PTR(class_path));
5702const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
5705rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
5712 UNEXPECTED_NODE(rb_raw_obj_info);
5715 if (ARY_SHARED_P(obj)) {
5716 APPEND_S(
"shared -> ");
5717 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
5720 APPEND_F(
"[%s%s%s] ",
5721 C(ARY_EMBED_P(obj),
"E"),
5722 C(ARY_SHARED_P(obj),
"S"),
5723 C(ARY_SHARED_ROOT_P(obj),
"R"));
5725 if (ARY_EMBED_P(obj)) {
5726 APPEND_F(
"len: %ld (embed)",
5730 APPEND_F(
"len: %ld, capa:%ld ptr:%p",
5732 RARRAY(obj)->as.heap.aux.capa,
5739 C(
FL_TEST(obj, RSTRING_FSTR),
"F"),
5742 if (STR_SHARED_P(obj)) {
5743 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
5746 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
5748 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
5750 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
5754 VALUE fstr = RSYMBOL(obj)->fstr;
5755 ID id = RSYMBOL(obj)->id;
5757 APPEND_F(
":%.*s id:%d", str_len_no_raise(fstr), RSTRING_PTR(fstr), (
unsigned int)
id);
5760 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
5765 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
5769 APPEND_F(
"[%c] %"PRIdSIZE,
5770 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
5778 if (!
NIL_P(class_path)) {
5779 APPEND_F(
"%.*s", str_len_no_raise(class_path), RSTRING_PTR(class_path));
5789 if (!
NIL_P(class_path)) {
5790 APPEND_F(
"src:%.*s", str_len_no_raise(class_path), RSTRING_PTR(class_path));
5796 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
5797 if (rb_shape_embedded_p(shape_id)) {
5798 APPEND_F(
"(embed) len:%d capa:%d", RSHAPE_LEN(shape_id), RSHAPE_CAPACITY(shape_id));
5801 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
5802 if (rb_shape_complex_p(shape_id)) {
5803 size_t hash_len = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
5804 APPEND_F(
"(complex) len:%zu extended:%p", hash_len, (
void *)fields_obj);
5807 APPEND_F(
"(extended) len:%d capa:%d extended:%p", RSHAPE_LEN(shape_id), RSHAPE_CAPACITY(shape_id), (
void *)fields_obj);
5816 (block = vm_proc_block(obj)) != NULL &&
5817 (vm_block_type(block) == block_type_iseq) &&
5818 (iseq = vm_block_iseq(block)) != NULL) {
5819 rb_raw_iseq_info(BUFF_ARGS, iseq);
5821 else if (rb_ractor_p(obj)) {
5824 APPEND_F(
"r:%"PRI_SERIALT_PREFIX
"u", r->pub.id);
5830 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
5832 switch (imemo_type(obj)) {
5835 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
5836 if (rb_shape_complex_p(shape_id)) {
5837 size_t hash_len = rb_st_table_size(rb_imemo_fields_complex_tbl(obj));
5838 APPEND_F(
"(complex) len:%zu", hash_len);
5841 APPEND_F(
"(embed) len:%d capa:%d", RSHAPE_LEN(shape_id), RSHAPE_CAPACITY(shape_id));
5844 APPEND_S(
"owner -> ");
5845 rb_raw_obj_info(BUFF_ARGS,
CLASS_OF(obj));
5853 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
5854 rb_id2name(me->called_id),
5855 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
5856 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
5857 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
5858 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
5859 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
5860 me->def ? rb_method_type_name(me->def->
type) :
"NULL",
5861 me->def ? me->def->aliased : -1,
5863 (void *)me->defined_class);
5866 switch (me->def->type) {
5867 case VM_METHOD_TYPE_ISEQ:
5868 APPEND_S(
" (iseq:");
5869 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
5881 rb_raw_iseq_info(BUFF_ARGS, iseq);
5884 case imemo_callinfo:
5887 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
5888 rb_id2name(vm_ci_mid(ci)),
5891 vm_ci_kwarg(ci) ?
"available" :
"NULL");
5894 case imemo_callcache:
5899 const char *class_name;
5902 if (
NIL_P(class_path)) {
5903 class_name = vm_cc_valid(cc) ?
"??" :
"<NULL>";
5904 class_name_len = vm_cc_valid(cc) ? 2 : 6;
5907 class_name = RSTRING_PTR(class_path);
5908 class_name_len = str_len_no_raise(class_path);
5911 APPEND_F(
"(klass:%.*s cme:%s%s (%p) call:%p",
5912 class_name_len, class_name,
5913 cme ? rb_id2name(cme->called_id) :
"<NULL>",
5914 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
5916 (void *)(uintptr_t)vm_cc_call(cc));
5934#ifdef RUBY_ASAN_ENABLED
5936rb_asan_poison_object(
VALUE obj)
5938 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5939 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
5943rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
5945 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5946 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
5950rb_asan_poisoned_object_p(
VALUE obj)
5952 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5953 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
5958raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
5960 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
5961 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
5962 if (pos >= buff_size) {}
5966rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
5968 void *
objspace = rb_gc_get_objspace();
5971 raw_obj_info(buff, buff_size, obj);
5973 else if (!rb_gc_impl_live_object_p(
objspace, (
const void *)obj)) {
5974 snprintf(buff, buff_size,
"out-of-heap:%p", (
void *)obj);
5977 else if (0 && rb_gc_impl_garbage_object_p(
objspace, obj)) {
5978 snprintf(buff, buff_size,
"garbage:%p", (
void *)obj);
5982 asan_unpoisoning_object(obj) {
5983 raw_obj_info(buff, buff_size, obj);
5999 if (RB_UNLIKELY(oldval >= maxval - 1)) {
6010 if (RGENGC_OBJ_INFO) {
6013 char buffers[10][0x100];
6016 rb_atomic_t index = atomic_inc_wraparound(&info.index, numberof(info.buffers));
6017 char *
const buff = info.buffers[index];
6018 return rb_raw_obj_info(buff,
sizeof(info.buffers[0]), obj);
6020 return obj_type_name(obj);
6037 rb_vraise(argv->exc, argv->fmt, *argv->ap);
6042gc_raise(
VALUE exc,
const char *fmt, ...)
6056 fprintf(stderr,
"%s",
"[FATAL] ");
6057 vfprintf(stderr, fmt, ap);
6064NORETURN(
static void negative_size_allocation_error(
const char *));
6066negative_size_allocation_error(
const char *msg)
6072ruby_memerror_body(
void *dummy)
6078NORETURN(
static void ruby_memerror(
void));
6083 if (ruby_thread_has_gvl_p()) {
6092 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
6110 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
6113 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
6114 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
6115 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
6118 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
6119 rb_ec_raised_clear(ec);
6122 rb_ec_raised_set(ec, RAISED_NOMEMORY);
6123 exc = ruby_vm_special_exception_copy(exc);
6126 EC_JUMP_TAG(ec, TAG_RAISE);
6130rb_memerror_reentered(
void)
6133 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
6137handle_malloc_failure(
void *ptr)
6148static void *ruby_xmalloc_body(
size_t size);
6151ruby_xmalloc(
size_t size)
6153 if (RUBY_DTRACE_GC_XMALLOC_ENABLED()) {
6154 RUBY_DTRACE_GC_XMALLOC(1, size);
6157 return handle_malloc_failure(ruby_xmalloc_body(size));
6161malloc_gc_allowed(
void)
6165 return r == NULL || !r->malloc_gc_disabled;
6169ruby_xmalloc_body(
size_t size)
6171 if ((ssize_t)size < 0) {
6172 negative_size_allocation_error(
"too large allocation size");
6175 return rb_gc_impl_malloc(rb_gc_get_objspace(), size, malloc_gc_allowed());
6179ruby_malloc_size_overflow(
size_t count,
size_t elsize)
6181 rb_raise(rb_eArgError,
6182 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
6187ruby_malloc_add_size_overflow(
size_t x,
size_t y)
6189 rb_raise(rb_eArgError,
6190 "malloc: possible integer overflow (%"PRIuSIZE
"+%"PRIuSIZE
")",
6194static void *ruby_xmalloc2_body(
size_t n,
size_t size);
6197ruby_xmalloc2(
size_t n,
size_t size)
6199 if (RUBY_DTRACE_GC_XMALLOC_ENABLED()) {
6200 RUBY_DTRACE_GC_XMALLOC(n, size);
6203 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
6207ruby_xmalloc2_body(
size_t n,
size_t size)
6209 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
6212static void *ruby_xcalloc_body(
size_t n,
size_t size);
6215ruby_xcalloc(
size_t n,
size_t size)
6217 if (RUBY_DTRACE_GC_XCALLOC_ENABLED()) {
6218 RUBY_DTRACE_GC_XCALLOC(n, size);
6221 return handle_malloc_failure(ruby_xcalloc_body(n, size));
6225ruby_xcalloc_body(
size_t n,
size_t size)
6227 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
6230static void *ruby_xrealloc_sized_body(
void *ptr,
size_t new_size,
size_t old_size);
6232#ifdef ruby_xrealloc_sized
6233#undef ruby_xrealloc_sized
6236ruby_xrealloc_sized(
void *ptr,
size_t new_size,
size_t old_size)
6238 return handle_malloc_failure(ruby_xrealloc_sized_body(ptr, new_size, old_size));
6242ruby_xrealloc_sized_body(
void *ptr,
size_t new_size,
size_t old_size)
6244 if ((ssize_t)new_size < 0) {
6245 negative_size_allocation_error(
"too large allocation size");
6248 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size, malloc_gc_allowed());
6252ruby_xrealloc(
void *ptr,
size_t new_size)
6254 return ruby_xrealloc_sized(ptr, new_size, 0);
6257static void *ruby_xrealloc2_sized_body(
void *ptr,
size_t n,
size_t size,
size_t old_n);
6259#ifdef ruby_xrealloc2_sized
6260#undef ruby_xrealloc2_sized
6263ruby_xrealloc2_sized(
void *ptr,
size_t n,
size_t size,
size_t old_n)
6265 return handle_malloc_failure(ruby_xrealloc2_sized_body(ptr, n, size, old_n));
6269ruby_xrealloc2_sized_body(
void *ptr,
size_t n,
size_t size,
size_t old_n)
6271 size_t len = xmalloc2_size(n, size);
6272 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr,
len, old_n * size, malloc_gc_allowed());
6276ruby_xrealloc2(
void *ptr,
size_t n,
size_t size)
6278 return ruby_xrealloc2_sized(ptr, n, size, 0);
6281#ifdef ruby_xfree_sized
6282#undef ruby_xfree_sized
6292static bool g_nofree =
false;
6296ruby_xfree_sized(
void *x,
size_t size)
6304 if (RUBY_DTRACE_GC_XFREE_ENABLED()) {
6305 RUBY_DTRACE_GC_XFREE(x, size);
6312 if (LIKELY(GET_VM())) {
6313 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
6324 ruby_xfree_sized(x, 0);
6328rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
6330 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
6331 return ruby_xmalloc(w);
6335rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
6337 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
6338 return ruby_xcalloc(w, 1);
6342rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
6344 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
6345 return ruby_xrealloc((
void *)p, w);
6349rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
6351 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
6352 return ruby_xmalloc(u);
6356rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
6358 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
6359 return ruby_xcalloc(u, 1);
6366ruby_mimmalloc(
size_t size)
6369#if CALC_EXACT_MALLOC_SIZE
6373#if CALC_EXACT_MALLOC_SIZE
6389ruby_mimcalloc(
size_t num,
size_t size)
6392#if CALC_EXACT_MALLOC_SIZE
6393 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(num, size);
6394 if (UNLIKELY(t.overflowed)) {
6398 mem = calloc1(size);
6410 mem = calloc(num, size);
6416ruby_mimfree(
void *ptr)
6418#if CALC_EXACT_MALLOC_SIZE
6426rb_gc_adjust_memory_usage(ssize_t diff)
6428 unless_objspace(
objspace) {
return; }
6430 rb_gc_impl_adjust_memory_usage(
objspace, diff);
6434rb_obj_info(
VALUE obj)
6436 return obj_info(obj);
6440rb_obj_info_dump(
VALUE obj)
6443 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
6447rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
6450 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
6454rb_gc_before_fork(
void)
6456 rb_gc_impl_before_fork(rb_gc_get_objspace());
6460rb_gc_after_fork(rb_pid_t pid)
6462 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
6466rb_gc_obj_shareable_p(
VALUE obj)
6483check_shareable_i(
const VALUE child,
void *ptr)
6487 if (!rb_gc_obj_shareable_p(child)) {
6492 if (rb_gc_impl_shref_marked_p(rb_gc_get_objspace(), child)) {
6496 fprintf(stderr,
"(a) ");
6497 rb_gc_rp(data->parent);
6498 fprintf(stderr,
"(b) ");
6500 fprintf(stderr,
"check_shareable_i: shareable (a) -> unshareable (b) without a shref record\n");
6503 rb_bug(
"!! violate shareable constraint !!");
6516rb_gc_verify_shareable(
VALUE obj)
6527 .mark_func = check_shareable_i,
6529 .checking_shareable =
true,
6533 rb_gc_mark_children(rb_gc_get_objspace(), obj);
6537 if (data.err_count > 0) {
6538 rb_bug(
"rb_gc_verify_shareable");
6543rb_gc_checking_shareable(
void)
6546 return mfd && mfd->checking_shareable;
6582 const char* nofree_str = getenv(
"RUBY_NO_FREE");
6583 if (nofree_str && strcmp(nofree_str,
"1") == 0) {
6584 fprintf(stderr,
"WARNING: Enabling no-free mode! xfree() will never free anything!\n");
6590 malloc_offset = gc_compute_malloc_offset();
6592 rb_mGC = rb_define_module(
"GC");
6594 VALUE rb_mObjSpace = rb_define_module(
"ObjectSpace");
6601 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
6624ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
6626#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
6629 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_INC(var)
Atomically increments the value pointed by var.
#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 RUBY_ATOMIC_DEC(var)
Atomically decrements the value pointed by var.
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
#define RUBY_ATOMIC_SET(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type.
#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.
unsigned int rb_postponed_job_handle_t
The type of a handle returned from rb_postponed_job_preregister and passed to rb_postponed_job_trigge...
void rb_postponed_job_trigger(rb_postponed_job_handle_t h)
Triggers a pre-registered job registered with rb_postponed_job_preregister, scheduling it for executi...
rb_postponed_job_handle_t rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
Pre-registers a func in Ruby's postponed job preregistration table, returning an opaque handle which ...
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.
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 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 OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#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 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 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.
VALUE rb_eNoMemError
NoMemoryError exception.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
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.
Defines RBIMPL_HAS_BUILTIN.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#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_mod_name(VALUE mod)
Queries the name of a module.
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 MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
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_AREF(a, i)
#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.
#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.
#define RREGEXP(obj)
Convenient casting macro.
static struct re_pattern_buffer * RREGEXP_PTR(VALUE rexp)
Convenient getter function.
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
#define RSTRING(obj)
Convenient casting macro.
static long RSTRUCT_LEN(VALUE st)
Returns the number of struct members.
#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.
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
static const rb_data_type_t * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
#define RDATA(obj)
Convenient casting macro for backward compatibility.
#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.
Regular expression execution context.
union RMatch::@58 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::@64 function
Function pointers.
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.
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_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
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.