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);
250rb_gc_stash_cleanup_objspace(
void)
252 GET_VM()->gc.cleanup_objspace = rb_gc_get_objspace();
259NOINLINE(
static void *gc_current_objspace_slow(
rb_ractor_t *
const cr));
266 if (RB_UNLIKELY(ruby_vm_during_cleanup) && GET_VM()->gc.cleanup_objspace) {
267 return GET_VM()->gc.cleanup_objspace;
272 return GET_VM()->ractor.main_ractor->objspace;
282 if (RB_LIKELY(cr != NULL && gc_objspace_override_count == 0)) {
286 return gc_current_objspace_slow(cr);
290rb_gc_get_objspace(
void)
292 return gc_current_objspace_of(rb_current_ractor_raw(
false));
296rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
307#define RESTORE_FINALIZER() (\
308 ec->cfp = saved.cfp, \
309 ec->cfp->sp = saved.sp, \
310 ec->errinfo = saved.errinfo)
312 saved.errinfo = ec->errinfo;
314 saved.sp = ec->cfp->sp;
318 ASSERT_vm_unlocking();
319 rb_ractor_ignore_belonging(
true);
321 enum ruby_tag_type state = EC_EXEC_TAG();
322 if (state != TAG_NONE) {
325 VALUE failed_final = saved.final;
328 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
329 rb_ec_error_print(ec, ec->errinfo);
333 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
334 saved.final = callback(i, data);
338 rb_ractor_ignore_belonging(
false);
339#undef RESTORE_FINALIZER
343rb_gc_set_pending_interrupt(
void)
346 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
356 rb_ractor_t *
const cr = rb_current_ractor_raw(
false);
357 if (cr == NULL || cr->objspace !=
objspace) {
363 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
365 rb_postponed_job_trigger_for_ractor(pjob, r->pub.self);
376 rb_postponed_job_trigger_for_ractor(pjob, GET_VM()->ractor.main_ractor->pub.self);
380rb_gc_unset_pending_interrupt(
void)
383 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
387rb_gc_multi_ractor_p(
void)
389 return rb_multi_ractor_p();
393rb_gc_shutdown_call_finalizer_p(
VALUE obj)
397 if (!ruby_free_at_exit_p()) {
399 if (!rbimpl_typeddata_embedded_p(obj) && !
RTYPEDDATA(obj)->data)
return false;
401 if (rb_obj_is_thread(obj))
return false;
402 if (rb_obj_is_mutex(obj))
return false;
403 if (rb_obj_is_fiber(obj))
return false;
404 if (rb_ractor_p(obj))
return false;
405 if (rb_obj_is_fstring_table(obj))
return false;
406 if (rb_obj_is_symbol_table(obj))
return false;
420 return ruby_free_at_exit_p();
425rb_gc_obj_changed_slot_size(
VALUE obj,
size_t slot_size)
427 shape_id_t shape_id = rb_obj_shape_transition_capacity(obj, rb_shape_capacity_for_slot_size(slot_size));
428 RBASIC_SET_FULL_SHAPE_ID(obj, shape_id);
431void rb_vm_update_references(
void *ptr);
433#define rb_setjmp(env) RUBY_SETJMP(env)
434#define rb_jmp_buf rb_jmpbuf_t
436#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
437#define MAP_ANONYMOUS MAP_ANON
440#define unless_objspace(objspace) \
442 rb_vm_t *unless_objspace_vm = GET_VM(); \
443 if (unless_objspace_vm) objspace = rb_gc_get_objspace(); \
446#define RMOVED(obj) ((struct RMoved *)(obj))
448#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
449 if (gc_object_moved_p_internal((_objspace), (VALUE)(_thing))) { \
450 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
454#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
456#if RUBY_MARK_FREE_DEBUG
457int ruby_gc_debug_indent = 0;
460#ifndef RGENGC_OBJ_INFO
461# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
464#ifndef CALC_EXACT_MALLOC_SIZE
465# define CALC_EXACT_MALLOC_SIZE 0
470static size_t malloc_offset = 0;
471#if defined(HAVE_MALLOC_USABLE_SIZE)
473gc_compute_malloc_offset(
void)
484 for (offset = 0; offset <= 16; offset += 8) {
485 size_t allocated = (64 - offset);
486 void *test_ptr = malloc(allocated);
487 size_t wasted = malloc_usable_size(test_ptr) - allocated;
498gc_compute_malloc_offset(
void)
506rb_malloc_grow_capa(
size_t current,
size_t type_size)
508 size_t current_capacity = current;
509 if (current_capacity < 4) {
510 current_capacity = 4;
512 current_capacity *= type_size;
515 size_t new_capacity = (current_capacity * 2);
518 if (rb_popcount64(new_capacity) != 1) {
519 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
522 new_capacity -= malloc_offset;
523 new_capacity /= type_size;
524 if (current > new_capacity) {
525 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
531static inline struct rbimpl_size_overflow_tag
532size_mul_add_overflow(size_t x, size_t y, size_t z)
534 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
535 struct rbimpl_size_overflow_tag u = rbimpl_size_add_overflow(t.result, z);
536 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed, u.result };
539static inline struct rbimpl_size_overflow_tag
540size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
542 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
543 struct rbimpl_size_overflow_tag u = rbimpl_size_mul_overflow(z, w);
544 struct rbimpl_size_overflow_tag v = rbimpl_size_add_overflow(t.result, u.result);
545 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed || v.overflowed, v.result };
548PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
551size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
553 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
554 if (LIKELY(!t.overflowed)) {
557 else if (rb_during_gc()) {
563 "integer overflow: %"PRIuSIZE
566 x, y, (
size_t)SIZE_MAX);
571rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
573 return size_mul_or_raise(x, y, exc);
577size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
579 struct rbimpl_size_overflow_tag t = size_mul_add_overflow(x, y, z);
580 if (LIKELY(!t.overflowed)) {
583 else if (rb_during_gc()) {
589 "integer overflow: %"PRIuSIZE
593 x, y, z, (
size_t)SIZE_MAX);
598rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
600 return size_mul_add_or_raise(x, y, z, exc);
604size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
606 struct rbimpl_size_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
607 if (LIKELY(!t.overflowed)) {
610 else if (rb_during_gc()) {
616 "integer overflow: %"PRIdSIZE
621 x, y, z, w, (
size_t)SIZE_MAX);
625#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
627volatile VALUE rb_gc_guarded_val;
629rb_gc_guarded_ptr_val(
volatile VALUE *ptr,
VALUE val)
631 rb_gc_guarded_val = val;
637static const char *obj_type_name(
VALUE obj);
643rb_gc_atfork_global_locks(
void)
649#include "gc/default/default.c"
651#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
652# error "Modular GC requires dlopen"
656typedef struct gc_function_map {
658 void *(*objspace_alloc)(void);
659 void (*objspace_init)(
void *objspace_ptr);
660 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
661 void (*objspace_retire_gc)(
void *objspace_ptr);
662 void (*set_params)(
void *objspace_ptr);
665 void (*shutdown_free_objects)(
void *objspace_ptr);
666 void (*objspace_free)(
void *objspace_ptr);
667 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
669 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact,
bool global);
670 bool (*during_gc_p)(
void *objspace_ptr);
671 void (*prepare_heap)(
void *objspace_ptr);
672 void (*gc_enable)(
void *objspace_ptr);
673 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
674 bool (*gc_enabled_p)(
void *objspace_ptr);
675 bool (*user_gc_disabled_set)(
void *objspace_ptr,
bool disable);
676 bool (*user_gc_disabled_p)(
void *objspace_ptr);
677 bool (*multi_objspace_p)(void);
678 bool (*during_global_gc_p)(
void *objspace_ptr);
679 bool (*during_postmortem_p)(
void *objspace_ptr);
680 bool (*obj_foreign_p)(
void *objspace_ptr,
VALUE obj);
681 bool (*shref_marked_p)(
void *objspace_ptr,
VALUE obj);
682 size_t (*heap_page_count)(
void *objspace_ptr);
683 void (*objspace_absorb)(
void *dst_ptr,
void *src_ptr);
684 void (*gc_rest)(
void *objspace_ptr);
685 VALUE (*config_get)(
void *objpace_ptr);
686 void (*config_set)(
void *objspace_ptr,
VALUE hash);
687 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
688 VALUE (*stress_get)(
void *objspace_ptr);
691 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);
693 size_t (*obj_slot_size)(
VALUE obj);
694 size_t (*size_slot_size)(
void *objspace_ptr,
size_t size);
695 bool (*size_allocatable_p)(
size_t size);
696 size_t (*max_allocation_size)(void);
698 void *(*malloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
699 void *(*calloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
700 void *(*realloc)(
void *objspace_ptr,
void *ptr,
size_t new_size,
size_t old_size,
bool gc_allowed);
701 void (*free)(
void *objspace_ptr,
void *ptr,
size_t old_size);
702 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
704 void (*mark)(
void *objspace_ptr,
VALUE obj);
705 void (*mark_and_move)(
void *objspace_ptr,
VALUE *ptr);
706 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
707 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
709 void (*declare_weak_references)(
void *objspace_ptr,
VALUE obj);
710 bool (*handle_weak_references_alive_p)(
void *objspace_ptr,
VALUE obj);
712 void (*register_pinning_obj)(
void *objspace_ptr,
VALUE obj);
713 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
714 bool (*pinned_p)(
void *objspace_ptr,
VALUE obj);
715 VALUE (*location)(
void *objspace_ptr,
VALUE value);
717 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
718 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
719 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
720 void (*obj_became_shareable)(
void *objspace_ptr,
VALUE obj);
722 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
723 void (*each_objects_shareable)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
724 void (*each_objects_foreign)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
725 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
727 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
729 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
730 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
731 void (*shutdown_call_finalizer)(
void *objspace_ptr);
733 void (*before_fork)(
void *objspace_ptr);
734 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
736 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
737 bool (*get_measure_total_time)(
void *objspace_ptr);
738 unsigned long long (*get_total_time)(
void *objspace_ptr);
739 size_t (*gc_count)(
void *objspace_ptr);
740 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
741 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
742 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
743 const char *(*active_gc_name)(void);
746 bool (*live_object_p)(
void *objspace_ptr,
const void *ptr);
747 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
748 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
749 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
751 bool modular_gc_loaded_p;
752} rb_gc_function_map_t;
754static rb_gc_function_map_t rb_gc_functions;
756# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
757# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
760ruby_modular_gc_init(
void)
765 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
767 rb_gc_function_map_t gc_functions = { 0 };
769 char *gc_so_path = NULL;
774 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
775 char c = gc_so_file[i];
776 if (isalnum(c))
continue;
782 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
787 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
790 size_t prefix_len = 0;
791 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
792 const char *base = strrchr(dli.dli_fname,
'/');
795# define end_with_p(lit) \
796 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
797 memcmp(base - tail, lit, tail) == 0)
799 prefix_len = base - dli.dli_fname;
800 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
803 prefix_len += MODULAR_GC_DIR[0] !=
'/';
804 gc_so_path_size += prefix_len;
808 gc_so_path = alloca(gc_so_path_size);
810 size_t gc_so_path_idx = 0;
811#define GC_SO_PATH_APPEND(str) do { \
812 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
815 if (prefix_len > 0) {
816 memcpy(gc_so_path, dli.dli_fname, prefix_len);
817 gc_so_path_idx = prefix_len;
820 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
821 GC_SO_PATH_APPEND(gc_so_file);
822 GC_SO_PATH_APPEND(DLEXT);
823 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
824#undef GC_SO_PATH_APPEND
827 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
829 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
833 gc_functions.modular_gc_loaded_p =
true;
836 unsigned int err_count = 0;
838# define load_modular_gc_func(name) do { \
840 const char *func_name = "rb_gc_impl_" #name; \
841 gc_functions.name = dlsym(handle, func_name); \
842 if (!gc_functions.name) { \
843 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
848 gc_functions.name = rb_gc_impl_##name; \
853 load_modular_gc_func(objspace_alloc);
854 load_modular_gc_func(objspace_init);
855 load_modular_gc_func(ractor_cache_alloc);
856 load_modular_gc_func(objspace_retire_gc);
857 load_modular_gc_func(set_params);
858 load_modular_gc_func(init);
860 load_modular_gc_func(shutdown_free_objects);
861 load_modular_gc_func(objspace_free);
862 load_modular_gc_func(ractor_cache_free);
864 load_modular_gc_func(start);
865 load_modular_gc_func(during_gc_p);
866 load_modular_gc_func(prepare_heap);
867 load_modular_gc_func(gc_enable);
868 load_modular_gc_func(gc_disable);
869 load_modular_gc_func(gc_enabled_p);
870 load_modular_gc_func(user_gc_disabled_set);
871 load_modular_gc_func(user_gc_disabled_p);
872 load_modular_gc_func(multi_objspace_p);
873 load_modular_gc_func(during_global_gc_p);
874 load_modular_gc_func(during_postmortem_p);
875 load_modular_gc_func(obj_foreign_p);
876 load_modular_gc_func(shref_marked_p);
877 load_modular_gc_func(heap_page_count);
878 load_modular_gc_func(objspace_absorb);
879 load_modular_gc_func(gc_rest);
880 load_modular_gc_func(config_set);
881 load_modular_gc_func(config_get);
882 load_modular_gc_func(stress_set);
883 load_modular_gc_func(stress_get);
884 load_modular_gc_func(get_vm_context);
886 load_modular_gc_func(new_obj);
887 load_modular_gc_func(zjit_new_obj_fastpath);
888 load_modular_gc_func(obj_slot_size);
889 load_modular_gc_func(size_slot_size);
890 load_modular_gc_func(size_allocatable_p);
891 load_modular_gc_func(max_allocation_size);
893 load_modular_gc_func(malloc);
894 load_modular_gc_func(calloc);
895 load_modular_gc_func(realloc);
896 load_modular_gc_func(free);
897 load_modular_gc_func(adjust_memory_usage);
899 load_modular_gc_func(mark);
900 load_modular_gc_func(mark_and_move);
901 load_modular_gc_func(mark_and_pin);
902 load_modular_gc_func(mark_maybe);
904 load_modular_gc_func(declare_weak_references);
905 load_modular_gc_func(handle_weak_references_alive_p);
907 load_modular_gc_func(register_pinning_obj);
908 load_modular_gc_func(object_moved_p);
909 load_modular_gc_func(pinned_p);
910 load_modular_gc_func(location);
912 load_modular_gc_func(writebarrier);
913 load_modular_gc_func(writebarrier_unprotect);
914 load_modular_gc_func(writebarrier_remember);
915 load_modular_gc_func(obj_became_shareable);
917 load_modular_gc_func(each_objects);
918 load_modular_gc_func(each_objects_shareable);
919 load_modular_gc_func(each_objects_foreign);
920 load_modular_gc_func(each_object);
922 load_modular_gc_func(make_zombie);
923 load_modular_gc_func(define_finalizer);
924 load_modular_gc_func(undefine_finalizer);
925 load_modular_gc_func(copy_finalizer);
926 load_modular_gc_func(shutdown_call_finalizer);
928 load_modular_gc_func(before_fork);
929 load_modular_gc_func(after_fork);
931 load_modular_gc_func(set_measure_total_time);
932 load_modular_gc_func(get_measure_total_time);
933 load_modular_gc_func(get_total_time);
934 load_modular_gc_func(gc_count);
935 load_modular_gc_func(latest_gc_info);
936 load_modular_gc_func(stat);
937 load_modular_gc_func(stat_heap);
938 load_modular_gc_func(active_gc_name);
940 load_modular_gc_func(object_metadata);
941 load_modular_gc_func(live_object_p);
942 load_modular_gc_func(garbage_object_p);
943 load_modular_gc_func(set_event_hook);
944 load_modular_gc_func(copy_attributes);
947 fprintf(stderr,
"ruby_modular_gc_init: found %u missing exports in library %s\n", err_count, gc_so_path);
951# undef load_modular_gc_func
953 rb_gc_functions = gc_functions;
957# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
958# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
959# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
960# define rb_gc_impl_objspace_retire_gc rb_gc_functions.objspace_retire_gc
961# define rb_gc_impl_set_params rb_gc_functions.set_params
962# define rb_gc_impl_init rb_gc_functions.init
964# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
965# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
966# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
968# define rb_gc_impl_start rb_gc_functions.start
969# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
970# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
971# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
972# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
973# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
974# define rb_gc_impl_user_gc_disabled_set rb_gc_functions.user_gc_disabled_set
975# define rb_gc_impl_user_gc_disabled_p rb_gc_functions.user_gc_disabled_p
976# define rb_gc_impl_multi_objspace_p rb_gc_functions.multi_objspace_p
977# define rb_gc_impl_during_global_gc_p rb_gc_functions.during_global_gc_p
978# define rb_gc_impl_during_postmortem_p rb_gc_functions.during_postmortem_p
979# define rb_gc_impl_obj_foreign_p rb_gc_functions.obj_foreign_p
980# define rb_gc_impl_shref_marked_p rb_gc_functions.shref_marked_p
981# define rb_gc_impl_heap_page_count rb_gc_functions.heap_page_count
982# define rb_gc_impl_objspace_absorb rb_gc_functions.objspace_absorb
983# define rb_gc_impl_gc_rest rb_gc_functions.gc_rest
984# define rb_gc_impl_config_get rb_gc_functions.config_get
985# define rb_gc_impl_config_set rb_gc_functions.config_set
986# define rb_gc_impl_stress_set rb_gc_functions.stress_set
987# define rb_gc_impl_stress_get rb_gc_functions.stress_get
988# define rb_gc_impl_get_vm_context rb_gc_functions.get_vm_context
990# define rb_gc_impl_new_obj rb_gc_functions.new_obj
991# define rb_gc_impl_zjit_new_obj_fastpath rb_gc_functions.zjit_new_obj_fastpath
992# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
993# define rb_gc_impl_size_slot_size rb_gc_functions.size_slot_size
994# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
995# define rb_gc_impl_max_allocation_size rb_gc_functions.max_allocation_size
997# define rb_gc_impl_malloc rb_gc_functions.malloc
998# define rb_gc_impl_calloc rb_gc_functions.calloc
999# define rb_gc_impl_realloc rb_gc_functions.realloc
1000# define rb_gc_impl_free rb_gc_functions.free
1001# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
1003# define rb_gc_impl_mark rb_gc_functions.mark
1004# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
1005# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
1006# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
1008# define rb_gc_impl_declare_weak_references rb_gc_functions.declare_weak_references
1009# define rb_gc_impl_handle_weak_references_alive_p rb_gc_functions.handle_weak_references_alive_p
1011# define rb_gc_impl_register_pinning_obj rb_gc_functions.register_pinning_obj
1012# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
1013# define rb_gc_impl_pinned_p rb_gc_functions.pinned_p
1014# define rb_gc_impl_location rb_gc_functions.location
1016# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
1017# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
1018# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
1019# define rb_gc_impl_obj_became_shareable rb_gc_functions.obj_became_shareable
1021# define rb_gc_impl_each_objects rb_gc_functions.each_objects
1022# define rb_gc_impl_each_objects_shareable rb_gc_functions.each_objects_shareable
1023# define rb_gc_impl_each_objects_foreign rb_gc_functions.each_objects_foreign
1024# define rb_gc_impl_each_object rb_gc_functions.each_object
1026# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
1027# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
1028# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
1029# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
1030# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
1032# define rb_gc_impl_before_fork rb_gc_functions.before_fork
1033# define rb_gc_impl_after_fork rb_gc_functions.after_fork
1035# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
1036# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
1037# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
1038# define rb_gc_impl_gc_count rb_gc_functions.gc_count
1039# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
1040# define rb_gc_impl_stat rb_gc_functions.stat
1041# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
1042# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
1044# define rb_gc_impl_object_metadata rb_gc_functions.object_metadata
1045# define rb_gc_impl_live_object_p rb_gc_functions.live_object_p
1046# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
1047# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
1048# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
1051#ifdef RUBY_ASAN_ENABLED
1053asan_death_callback(
void)
1056 rb_bug_without_die(
"ASAN error");
1064rb_gc_init_objspaces(
void)
1067 ruby_modular_gc_init();
1070 rb_vm_t *vm = ruby_current_vm_ptr;
1072 void *
objspace = rb_gc_impl_objspace_alloc();
1074 vm->ractor.main_ractor->objspace =
objspace;
1075 rb_gc_impl_objspace_init(
objspace);
1076 rb_gc_impl_stress_set(
objspace, initial_stress);
1078#ifdef RUBY_ASAN_ENABLED
1079 __sanitizer_set_death_callback(asan_death_callback);
1086static bool gc_ever_multi_ractor =
false;
1089rb_gc_ever_multi_ractor_p(
void)
1091 if (!gc_ever_multi_ractor && rb_multi_ractor_p()) gc_ever_multi_ractor =
true;
1092 return gc_ever_multi_ractor;
1098rb_gc_objspace_alloc(
void)
1100 gc_ever_multi_ractor =
true;
1101 if (!rb_gc_impl_multi_objspace_p()) {
1103 return rb_gc_get_objspace();
1105 void *
objspace = rb_gc_impl_objspace_alloc();
1106 rb_gc_impl_objspace_init(
objspace);
1114 rb_gc_impl_objspace_free(
objspace);
1118rb_gc_obj_slot_size(
VALUE obj)
1120 return rb_gc_impl_obj_slot_size(obj);
1124gc_validate_pc(
VALUE obj)
1132 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && CFP_PC(cfp)) {
1133 const VALUE *iseq_encoded = ISEQ_BODY(CFP_ISEQ(cfp))->iseq_encoded;
1134 const VALUE *iseq_encoded_end = iseq_encoded + ISEQ_BODY(CFP_ISEQ(cfp))->iseq_size;
1135 RUBY_ASSERT(CFP_PC(cfp) >= iseq_encoded,
"PC not set when allocating, breaking tracing");
1136 RUBY_ASSERT(CFP_PC(cfp) <= iseq_encoded_end,
"PC not set when allocating, breaking tracing");
1141NOINLINE(
static void gc_newobj_hook(
VALUE obj));
1143gc_newobj_hook(
VALUE obj)
1145 int lev = RB_GC_VM_LOCK_NO_BARRIER();
1147 size_t slot_size = rb_gc_obj_slot_size(obj);
1148 memset((
char *)obj +
sizeof(
struct RBasic), 0, slot_size -
sizeof(
struct RBasic));
1155 bool gc_disabled =
RTEST(rb_gc_local_disable_no_rest());
1159 if (!gc_disabled) rb_gc_local_enable();
1161 RB_GC_VM_UNLOCK_NO_BARRIER(lev);
1173 size_t actual_alloc_size;
1174 VALUE obj = rb_gc_impl_new_obj(
objspace, cr->newobj_cache, klass, flags, wb_protected, size, &actual_alloc_size);
1176 GC_ASSERT(actual_alloc_size >= size);
1177 shape_id = rb_shape_transition_slot_size(shape_id, actual_alloc_size);
1179 RBASIC_SET_FULL_SHAPE_ID_NO_CHECKS(obj, shape_id);
1181 gc_validate_pc(obj);
1184 gc_newobj_hook(obj);
1187 if (RUBY_DTRACE_GC_OBJ_NEW_ENABLED()) {
1188 RUBY_DTRACE_GC_OBJ_NEW((
void*)obj, flags);
1191#if RGENGC_CHECK_MODE
1192# ifndef GC_DEBUG_SLOT_FILL_SPECIAL_VALUE
1193# define GC_DEBUG_SLOT_FILL_SPECIAL_VALUE 255
1197 (
void *)(obj +
sizeof(
struct RBasic)),
1198 GC_DEBUG_SLOT_FILL_SPECIAL_VALUE,
1199 rb_gc_obj_slot_size(obj) -
sizeof(
struct RBasic)
1212 return newobj_body(cr, cr->objspace, klass, flags, shape_id, wb_protected, size);
1232 return newobj_body(cr,
objspace, klass, flags, shape_id, wb_protected, size);
1247 return newobj_body(cr, cr->objspace, klass, flags, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER,
true, size);
1251rb_newobj_of_with_shape(
VALUE klass,
VALUE flags, shape_id_t shape_id,
size_t size)
1253 return rb_newobj(GET_EC(), klass, flags, shape_id,
true, size);
1257rb_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
1259 return rb_newobj(GET_EC(), klass, flags, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER,
true, size);
1263VALUE class_allocate_complex_instance(
VALUE klass, uint32_t capacity)
1265 VALUE obj = rb_newobj_of_with_shape(klass,
T_OBJECT, rb_shape_transition_extended(ROOT_COMPLEX_SHAPE_ID),
sizeof(
struct RObject));
1269 VALUE fields_obj = rb_imemo_fields_new_complex(obj, ROOT_COMPLEX_SHAPE_ID, capacity,
false);
1270 ROBJECT_SET_EXTENDED(obj, fields_obj);
1275robject_embedded_size(uint32_t fields_count)
1277 size_t size = rb_obj_embedded_size(fields_count);
1278 if (!rb_gc_size_allocatable_p(size)) {
1279 size =
sizeof(
struct RObject);
1285rb_class_allocate_instance_capa(
VALUE klass, attr_index_t max_iv_count)
1291 if (RB_UNLIKELY(max_iv_count > rb_shape_max_capacity())) {
1292 obj = class_allocate_complex_instance(klass, max_iv_count);
1295 size_t size = robject_embedded_size(max_iv_count);
1299 obj = rb_newobj_of_with_shape(klass,
T_OBJECT, rb_shape_transition_robject(0), size);
1302 VALUE *ptr = ROBJECT_FIELDS(obj);
1303 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1304 attr_index_t fields_count = RSHAPE_LEN(shape_id);
1305 attr_index_t capacity = RSHAPE_CAPACITY(shape_id);
1307 for (attr_index_t i = fields_count; i < capacity; i++) {
1315 rb_bug(
"Expected rb_class_allocate_instance to set the class correctly");
1323rb_class_allocate_instance(
VALUE klass)
1325 return rb_class_allocate_instance_capa(klass, RCLASS_MAX_IV_COUNT(klass));
1330rb_zjit_class_allocate_instance_fastpath(
VALUE klass,
size_t *size_out,
VALUE *flags_out)
1332 uint32_t index_tbl_num_entries = RCLASS_MAX_IV_COUNT(klass);
1335 if (RB_UNLIKELY(index_tbl_num_entries > rb_shape_max_capacity())) {
1339 *size_out = robject_embedded_size(index_tbl_num_entries);
1340 *flags_out =
T_OBJECT | rb_shape_transition_robject(0);
1346rb_zjit_newobj_hook_enabled_p(
void)
1353rb_gc_register_pinning_obj(
VALUE obj)
1355 rb_gc_impl_register_pinning_obj(rb_gc_get_objspace(), obj);
1358#define UNEXPECTED_NODE(func) \
1359 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1360 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1363rb_data_object_check(
VALUE klass)
1368 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1372#define RTYPEDDATA_EMBEDDED_P rbimpl_typeddata_embedded_p
1373#define RB_DATA_TYPE_EMBEDDABLE_P(type) ((type)->flags & RUBY_TYPED_EMBEDDABLE)
1374#define RTYPEDDATA_EMBEDDABLE_P(obj) RB_DATA_TYPE_EMBEDDABLE_P(RTYPEDDATA_TYPE(obj))
1379 RBIMPL_NONNULL_ARG(
type);
1380 if (klass) rb_data_object_check(klass);
1382 VALUE obj = rb_newobj_in_objspace(GET_EC(),
objspace, klass,
T_DATA, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_RDATA, wb_protected, size);
1384 rb_gc_register_pinning_obj(obj);
1397 if (UNLIKELY(RB_DATA_TYPE_EMBEDDABLE_P(
type))) {
1398 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1407 return typed_data_wrap_in(rb_ec_ractor_ptr(GET_EC())->
objspace, klass, datap,
type);
1413 return typed_data_wrap_in(
objspace, klass, datap,
type);
1419 if (RB_DATA_TYPE_EMBEDDABLE_P(
type)) {
1421 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1429 if (rb_gc_size_allocatable_p(embed_size) && !rb_gc_data_type_deferred_free_p(
type)) {
1430 VALUE obj = typed_data_alloc_in(
objspace, klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1431 memset((
char *)obj + offsetof(
struct RTypedData,
data), 0, size);
1444 return typed_data_zalloc_in(rb_ec_ractor_ptr(GET_EC())->
objspace, klass, size,
type);
1450 return typed_data_zalloc_in(
objspace, klass, size,
type);
1454ruby_xmalloc_usable_size(
void *ptr)
1456#ifdef HAVE_MALLOC_USABLE_SIZE
1457#if CALC_EXACT_MALLOC_SIZE
1461 return malloc_usable_size(ptr);
1469rb_objspace_data_type_memsize(
VALUE obj)
1472 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1475 if (RTYPEDDATA_EMBEDDABLE_P(obj) && !RTYPEDDATA_EMBEDDED_P(obj)) {
1476 size += ruby_xmalloc_usable_size((
void *)ptr);
1480 if (
type->function.dsize) {
1481 size +=
type->function.dsize(ptr);
1489rb_objspace_data_type_name(
VALUE obj)
1495rb_gc_declare_weak_references(
VALUE obj)
1497 rb_gc_impl_declare_weak_references(rb_gc_get_objspace(), obj);
1501rb_gc_handle_weak_references_alive_p(
VALUE obj)
1505 return rb_gc_impl_handle_weak_references_alive_p(rb_gc_get_objspace(), obj);
1509rb_gc_handle_weak_references(
VALUE obj)
1516 if (
type->function.handle_weak_references) {
1517 (
type->function.handle_weak_references)(RTYPEDDATA_GET_DATA(obj));
1521 "rb_gc_handle_weak_references: TypedData %s does not implement handle_weak_references",
1529 switch (imemo_type(obj)) {
1530 case imemo_callcache: {
1532 if (cc->klass !=
Qundef &&
1533 (!rb_gc_handle_weak_references_alive_p(cc->klass) ||
1534 !rb_gc_handle_weak_references_alive_p((
VALUE)cc->cme_))) {
1535 vm_cc_invalidate(cc);
1539 case imemo_subclasses: {
1541 VALUE *entries = rb_imemo_subclasses_entries(obj);
1542 for (uint32_t i = 0; i < subs->count; i++) {
1543 if (entries[i] && !rb_gc_handle_weak_references_alive_p(entries[i])) {
1550 rb_bug(
"rb_gc_handle_weak_references: unexpected imemo type");
1556 rb_bug(
"rb_gc_handle_weak_references: type not supported\n");
1561rb_gc_imemo_needs_cleanup_p(
VALUE obj)
1563 switch (imemo_type(obj)) {
1564 case imemo_constcache:
1569 case imemo_callcache:
1570 case imemo_throw_data:
1571 case imemo_cvar_entry:
1577 case imemo_callinfo:
1581 case imemo_subclasses:
1588 return rb_obj_shape_complex_p(obj);
1606rb_gc_obj_needs_cleanup_p(
VALUE obj)
1613 return rb_gc_imemo_needs_cleanup_p(obj);
1619 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1620 if (rb_shape_has_fields(shape_id) && rb_shape_layout(shape_id) == SHAPE_ID_LAYOUT_OTHER) {
1645 if (
type & TYPED_DATA_EMBEDDED) {
1655 return (flags & (RSTRING_NOEMBED | RSTRING_FSTR));
1658 return !(flags & RARRAY_EMBED_FLAG);
1661 return (flags & RHASH_ST_TABLE_FLAG);
1664 return (flags & (RMATCH_ONIG | RMATCH_OFFSETS_EXTERNAL)) || USE_DEBUG_COUNTER;
1667 return !(flags & BIGNUM_EMBED_FLAG);
1670 return !(flags & RSTRUCT_EMBED_LEN_MASK);
1677io_fptr_finalize(
void *fptr)
1686 rb_gc_impl_make_zombie(
objspace, obj, io_fptr_finalize, fptr);
1692 void *data = RTYPEDDATA_GET_DATA(obj);
1695 void (*dfree)(
void *) =
type->function.dfree;
1698 bool embedded = RTYPEDDATA_EMBEDDED_P(obj);
1700 bool free_embeddable_data = RB_DATA_TYPE_EMBEDDABLE_P(
type) && !embedded;
1705 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1708 else if (free_immediately) {
1710 if (free_embeddable_data) {
1714 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1717 rb_gc_impl_make_zombie(
objspace, obj, dfree, data);
1718 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1723 RB_DEBUG_COUNTER_INC(obj_data_empty);
1740 rb_class_classext_free(args->klass, ext, is_prime);
1748 rb_iclass_classext_free(args->klass, ext, is_prime);
1756 RB_DEBUG_COUNTER_INC(obj_free);
1760 if (RUBY_DTRACE_GC_OBJ_FREE_ENABLED()) {
1761 RUBY_DTRACE_GC_OBJ_FREE((
void*)obj,
RBASIC(obj)->flags);
1764 switch (builtin_type) {
1769 rb_bug(
"obj_free() called for broken object");
1775 switch (builtin_type) {
1781 rb_zjit_klass_free(obj);
1784 rb_class_classext_foreach(obj, classext_free, (
void *)&args);
1785 if (RCLASS_CLASSEXT_TBL(obj)) {
1786 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1798#if USE_DEBUG_COUNTER
1801 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1804 RB_DEBUG_COUNTER_INC(obj_hash_1);
1807 RB_DEBUG_COUNTER_INC(obj_hash_2);
1810 RB_DEBUG_COUNTER_INC(obj_hash_3);
1813 RB_DEBUG_COUNTER_INC(obj_hash_4);
1819 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1823 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1826 if (RHASH_AR_TABLE_P(obj)) {
1827 if (RHASH_AR_TABLE(obj) == NULL) {
1828 RB_DEBUG_COUNTER_INC(obj_hash_null);
1831 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1835 RB_DEBUG_COUNTER_INC(obj_hash_st);
1844 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1848 if (!rb_data_free(
objspace, obj))
return false;
1853#if USE_DEBUG_COUNTER
1855 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1858 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1861 RB_DEBUG_COUNTER_INC(obj_match_under4);
1865 onig_region_free(&rm->
as.onig, 0);
1869 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1873 if (
RFILE(obj)->fptr) {
1874 bool closed = rb_io_fptr_finalize_closed(
RFILE(obj)->fptr);
1875 if (!closed) make_io_zombie(
objspace, obj);
1876 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1881 RB_DEBUG_COUNTER_INC(obj_rational);
1884 RB_DEBUG_COUNTER_INC(obj_complex);
1891 rb_class_classext_foreach(obj, classext_iclass_free, (
void *)&args);
1892 if (RCLASS_CLASSEXT_TBL(obj)) {
1893 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1896 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1900 RB_DEBUG_COUNTER_INC(obj_float);
1904 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1905 SIZED_FREE_N(BIGNUM_DIGITS(obj), BIGNUM_LEN(obj));
1906 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1909 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1914 UNEXPECTED_NODE(obj_free);
1918 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1919 RSTRUCT(obj)->
as.heap.ptr == NULL) {
1920 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1923 SIZED_FREE_N(RSTRUCT(obj)->
as.heap.ptr, RSTRUCT(obj)->
as.heap.len);
1924 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1929 RB_DEBUG_COUNTER_INC(obj_symbol);
1933 rb_imemo_free((
VALUE)obj);
1937 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1942 rb_gc_impl_make_zombie(
objspace, obj, 0, 0);
1957 const rb_ractor_t *
const cr = rb_current_ractor_raw(
false);
1958 if (cr != NULL && cr != GET_VM()->ractor.main_ractor) {
1959 e &= ~RUBY_INTERNAL_EVENT_FREEOBJ;
1961 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), e);
1965internal_object_p(
VALUE obj)
1967 void *ptr = asan_unpoison_object_temporary(obj);
1969 if (
RBASIC(obj)->flags) {
1972 UNEXPECTED_NODE(internal_object_p);
1981 if (obj == rb_mRubyVMFrozenCore)
1985 if (RCLASS_SINGLETON_P(obj)) {
1986 return rb_singleton_class_internal_p(obj);
1990 if (!
RBASIC(obj)->klass)
break;
1994 if (ptr || !
RBASIC(obj)->flags) {
1995 rb_asan_poison_object(obj);
2001rb_objspace_internal_object_p(
VALUE obj)
2003 return internal_object_p(obj);
2012os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
2017 for (; v != (
VALUE)vend; v += stride) {
2018 if (!internal_object_p(v)) {
2038os_shareable_collect_i(
void *vstart,
void *vend,
size_t stride,
void *data)
2043 for (; v != (
VALUE)vend; v += stride) {
2057static void rb_gc_critical_disable(
void);
2058static void rb_gc_critical_enable(
void);
2071 rb_gc_impl_each_objects(rb_gc_get_objspace(), os_obj_of_i, &oes);
2076 if (rb_multi_ractor_p()) {
2081 rb_gc_critical_disable();
2085 void *self = rb_gc_get_objspace();
2088 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2089 if (r->objspace && r->objspace != self) {
2090 rb_gc_impl_each_objects_shareable(r->objspace, os_shareable_collect_i, &ocs);
2094 rb_gc_critical_enable();
2097 for (
long i = 0; i <
len; i++) {
2157 return os_obj_of(of);
2173 return rb_undefine_finalizer(obj);
2179 rb_check_frozen(obj);
2181 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
2187should_be_callable(
VALUE block)
2190 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
2196should_be_finalizable(
VALUE obj)
2199 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
2202 rb_check_frozen(obj);
2208 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
2288 if (rb_callable_receiver(block) == obj) {
2289 rb_warn(
"finalizer references object to be finalized");
2292 return rb_define_finalizer(obj, block);
2298 should_be_finalizable(obj);
2299 should_be_callable(block);
2301 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
2309rb_objspace_call_finalizer(
void)
2311 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
2315rb_objspace_free_objects(
void *
objspace)
2317 rb_gc_impl_shutdown_free_objects(
objspace);
2321rb_objspace_garbage_object_p(
VALUE obj)
2323 return !
SPECIAL_CONST_P(obj) && rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
2327rb_objspace_foreign_object_p(
VALUE obj)
2332#define OBJ_ID_INCREMENT (RUBY_IMMEDIATE_MASK + 1)
2333#define LAST_OBJECT_ID() (object_id_counter * OBJ_ID_INCREMENT)
2335#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
2336static size_t object_id_counter = 1;
2338static unsigned long long object_id_counter = 1;
2342generate_next_object_id(
void)
2344#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
2348 unsigned int lock_lev = RB_GC_VM_LOCK();
2349 VALUE id =
ULL2NUM(++object_id_counter * OBJ_ID_INCREMENT);
2350 RB_GC_VM_UNLOCK(lock_lev);
2355static void gc_mark_tbl_no_pin(
st_table *table);
2358class_object_id(
VALUE klass)
2360 VALUE id = RUBY_ATOMIC_VALUE_LOAD(
RCLASS(klass)->object_id);
2362 unsigned int lock_lev = RB_GC_VM_LOCK();
2363 id = generate_next_object_id();
2368 RB_GC_VM_UNLOCK(lock_lev);
2374object_id_get(
VALUE obj, shape_id_t shape_id)
2377 if (rb_shape_complex_p(shape_id)) {
2378 id = rb_obj_field_get(obj, ROOT_COMPLEX_WITH_OBJ_ID);
2381 id = rb_obj_field_get(obj, rb_shape_object_id(shape_id));
2387 rb_bug(
"Object's shape includes object_id, but it's missing %s", rb_obj_info(obj));
2395object_id0(
VALUE obj)
2398 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2400 if (rb_shape_has_object_id(shape_id)) {
2401 return object_id_get(obj, shape_id);
2404 shape_id_t object_id_shape_id = rb_obj_shape_transition_object_id(obj);
2406 id = generate_next_object_id();
2407 rb_obj_field_set(obj, object_id_shape_id, 0,
id);
2423 return class_object_id(obj);
2432 unsigned int lock_lev = RB_GC_VM_LOCK();
2433 VALUE id = object_id0(obj);
2434 RB_GC_VM_UNLOCK(lock_lev);
2438 return object_id0(obj);
2442rb_gc_obj_free_vm_weak_references(
VALUE obj)
2449 if (rb_obj_gen_fields_p(obj)) {
2456 rb_gc_free_fstring(obj);
2460 rb_gc_free_dsymbol(obj);
2463 switch (imemo_type(obj)) {
2464 case imemo_callinfo:
2483#if SIZEOF_LONG == SIZEOF_VOIDP
2490 return get_heap_object_id(obj);
2494nonspecial_obj_id(
VALUE obj)
2496#if SIZEOF_LONG == SIZEOF_VOIDP
2498#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2501# error not supported
2508 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
2545 return rb_find_object_id(rb_gc_get_objspace(), obj, object_id);
2549rb_obj_id_p(
VALUE obj)
2561rb_gc_before_updating_jit_code(
void)
2564 rb_yjit_mark_all_writeable();
2567 rb_zjit_mark_all_writable();
2577rb_gc_after_updating_jit_code(
void)
2580 rb_yjit_mark_all_executable();
2583 rb_zjit_mark_all_executable();
2590 size_t *size = (
size_t *)arg;
2593 if (RCLASSEXT_M_TBL(ext)) {
2594 s += rb_id_table_memsize(RCLASSEXT_M_TBL(ext));
2596 if (RCLASSEXT_CONST_TBL(ext)) {
2597 s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
2599 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2600 s += (RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1) *
sizeof(
VALUE);
2609classext_superclasses_memsize(
rb_classext_t *ext,
bool prime,
VALUE box_value,
void *arg)
2611 size_t *size = (
size_t *)arg;
2613 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2615 array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
2616 *size += array_size *
sizeof(
VALUE);
2621rb_obj_memsize_of(
VALUE obj)
2634 rb_class_classext_foreach(obj, classext_memsize, (
void *)&size);
2635 rb_class_classext_foreach(obj, classext_superclasses_memsize, (
void *)&size);
2638 if (RICLASS_OWNS_M_TBL_P(obj)) {
2639 if (RCLASS_M_TBL(obj)) {
2640 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
2645 size += rb_str_memsize(obj);
2648 size += rb_ary_memsize(obj);
2651 if (RHASH_ST_TABLE_P(obj)) {
2652 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
2654 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(
st_table);
2663 size += rb_objspace_data_type_memsize(obj);
2669 size += onig_region_memsize(&rm->
as.onig);
2675 if (
RFILE(obj)->fptr) {
2676 size += rb_io_memsize(
RFILE(obj)->fptr);
2683 size += rb_imemo_memsize(obj);
2691 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2692 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
2697 UNEXPECTED_NODE(obj_memsize_of);
2701 if (RSTRUCT_EMBED_LEN(obj) == 0) {
2702 size +=
sizeof(
VALUE) * RSTRUCT_LEN_RAW(obj);
2711 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
2715 return size + rb_gc_obj_slot_size(obj);
2719set_zero(st_data_t key, st_data_t val, st_data_t arg)
2723 rb_hash_aset(hash, k,
INT2FIX(0));
2734count_objects_i(
VALUE obj,
void *d)
2738 if (
RBASIC(obj)->flags) {
2789count_objects(
int argc,
VALUE *argv,
VALUE os)
2801 for (
size_t i = 0; i <=
T_MASK; i++) {
2805 types[i] = type_sym(i);
2812 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2815 hash = rb_hash_new_capa(2 +
T_MASK);
2818 rb_hash_stlike_foreach(hash, set_zero, hash);
2820 rb_hash_aset(hash, total,
SIZET2NUM(data.total));
2821 rb_hash_aset(hash, free,
SIZET2NUM(data.freed));
2823 for (
size_t i = 0; i <=
T_MASK; i++) {
2824 if (data.counts[i]) {
2825 rb_hash_aset(hash, types[i],
SIZET2NUM(data.counts[i]));
2832#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2834#define STACK_START (ec->machine.stack_start)
2835#define STACK_END (ec->machine.stack_end)
2836#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2838#if STACK_GROW_DIRECTION < 0
2839# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2840#elif STACK_GROW_DIRECTION > 0
2841# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2843# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2844 : (size_t)(STACK_END - STACK_START + 1))
2846#if !STACK_GROW_DIRECTION
2847int ruby_stack_grow_direction;
2849ruby_get_stack_grow_direction(
volatile VALUE *addr)
2852 SET_MACHINE_STACK_END(&end);
2854 if (end > addr)
return ruby_stack_grow_direction = 1;
2855 return ruby_stack_grow_direction = -1;
2864 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2865 return STACK_LENGTH;
2868#define PREVENT_STACK_OVERFLOW 1
2869#ifndef PREVENT_STACK_OVERFLOW
2870#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2871# define PREVENT_STACK_OVERFLOW 1
2873# define PREVENT_STACK_OVERFLOW 0
2876#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2882 size_t length = STACK_LENGTH;
2883 if (STACK_LEVEL_MAX == 0)
return FALSE;
2884 if (STACK_LEVEL_MAX <= (
size_t)water_mark)
return TRUE;
2885 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2887 return length > maximum_length;
2890#define stack_check(ec, water_mark) FALSE
2893#ifdef RUBY_ASAN_ENABLED
2895# define STACKFRAME_FOR_CALL_CFUNC (128 * 1024 / sizeof(VALUE))
2897# define STACKFRAME_FOR_CALL_CFUNC 2048
2903 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2909 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2923 return cr != NULL ? &cr->mark_func_data : &GET_VM()->gc.mark_func_data;
2926 return &cr->mark_func_data;
2929#define GC_MARK_FUNC_DATA_SLOTP() gc_mark_func_data_slotp_of(rb_current_ractor_raw(false))
2933#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2934 if (!RB_SPECIAL_CONST_P(obj)) { \
2935 rb_ractor_t *const mark_cr = rb_current_ractor_raw(false); \
2936 struct gc_mark_func_data_struct **mfdp = gc_mark_func_data_slotp_of(mark_cr); \
2937 struct gc_mark_func_data_struct *mark_func_data = *mfdp; \
2938 void *objspace = gc_current_objspace_of(mark_cr); \
2939 if (LIKELY(mark_func_data == NULL)) { \
2940 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2941 (func)(objspace, (obj_or_ptr)); \
2943 else if (check_obj ? \
2944 rb_gc_impl_live_object_p(objspace, (const void *)obj) && \
2945 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2947 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2949 mark_func_data->mark_func((obj), mark_func_data->data); \
2950 *mfdp = mark_func_data; \
2956gc_mark_internal(
VALUE obj)
2958 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2964 gc_mark_internal(obj);
2968rb_gc_mark_and_move(
VALUE *ptr)
2970 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr,
false);
2974gc_mark_and_pin_internal(
VALUE obj)
2976 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2982 gc_mark_and_pin_internal(obj);
2986gc_mark_maybe_internal(
VALUE obj)
2988 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2994 gc_mark_maybe_internal(obj);
2997ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2999each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
3010each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
3012 if (end <= start)
return;
3013 each_location(start, end - start, cb, data);
3017gc_mark_maybe_each_location(
VALUE obj,
void *data)
3019 gc_mark_maybe_internal(obj);
3025 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
3029rb_gc_mark_values(
long n,
const VALUE *values)
3031 for (
long i = 0; i < n; i++) {
3032 gc_mark_internal(values[i]);
3037rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
3039 for (
long i = 0; i < n; i++) {
3040 gc_mark_and_pin_internal(values[i]);
3045mark_key(st_data_t key, st_data_t value, st_data_t data)
3047 gc_mark_and_pin_internal((
VALUE)key);
3057 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
3061mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
3063 gc_mark_internal((
VALUE)key);
3064 gc_mark_internal((
VALUE)value);
3070pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
3072 gc_mark_and_pin_internal((
VALUE)key);
3073 gc_mark_and_pin_internal((
VALUE)value);
3079pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
3081 gc_mark_and_pin_internal((
VALUE)key);
3082 gc_mark_internal((
VALUE)value);
3088mark_hash(
VALUE hash)
3090 if (rb_hash_compare_by_id_p(hash)) {
3091 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
3094 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
3097 gc_mark_internal(RHASH(hash)->ifnone);
3105 st_foreach(tbl, pin_key_pin_value, 0);
3108static enum rb_id_table_iterator_result
3111 gc_mark_internal(me);
3113 return ID_TABLE_CONTINUE;
3120 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
3124static enum rb_id_table_iterator_result
3129 gc_mark_internal(ce->value);
3130 gc_mark_internal(ce->file);
3132 return ID_TABLE_CONTINUE;
3139 rb_id_table_foreach_values(tbl, mark_const_entry_i,
objspace);
3142#if STACK_GROW_DIRECTION < 0
3143#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
3144#elif STACK_GROW_DIRECTION > 0
3145#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
3147#define GET_STACK_BOUNDS(start, end, appendix) \
3148 ((STACK_END < STACK_START) ? \
3149 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
3153gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
3155 gc_mark_maybe_internal(obj);
3157#ifdef RUBY_ASAN_ENABLED
3159 void *fake_frame_start;
3160 void *fake_frame_end;
3161 bool is_fake_frame = asan_get_fake_stack_extents(
3162 ec->machine.asan_fake_stack_handle, obj,
3163 ec->machine.stack_start, ec->machine.stack_end,
3164 &fake_frame_start, &fake_frame_end
3166 if (is_fake_frame) {
3167 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
3179 return rb_gc_impl_object_moved_p(
objspace, obj);
3189 return rb_gc_impl_location(
objspace, value);
3195 return gc_location_internal(rb_gc_get_objspace(), value);
3201 VALUE destination = rb_gc_location(*ptr);
3202 if (destination != *ptr) {
3207#if defined(__wasm__)
3210static VALUE *rb_stack_range_tmp[2];
3213rb_mark_locations(
void *begin,
void *end)
3215 rb_stack_range_tmp[0] = begin;
3216 rb_stack_range_tmp[1] = end;
3220rb_gc_save_machine_context(
void)
3225# if defined(__EMSCRIPTEN__)
3230 emscripten_scan_stack(rb_mark_locations);
3231 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3233 emscripten_scan_registers(rb_mark_locations);
3234 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3241 VALUE *stack_start, *stack_end;
3243 GET_STACK_BOUNDS(stack_start, stack_end, 1);
3244 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
3246 rb_wasm_scan_locals(rb_mark_locations);
3247 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
3255rb_gc_save_machine_context(
void)
3259 RB_VM_SAVE_MACHINE_CONTEXT(thread);
3266 rb_gc_mark_machine_context(ec);
3273 VALUE *stack_start, *stack_end;
3275 GET_STACK_BOUNDS(stack_start, stack_end, 0);
3276 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
3279#ifdef RUBY_ASAN_ENABLED
3286 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
3287 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
3288 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
3292rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
3294 gc_mark_and_pin_internal((
VALUE)value);
3302 if (!tbl || tbl->num_entries == 0)
return;
3304 st_foreach(tbl, rb_mark_tbl_i, 0);
3310 if (!tbl || tbl->num_entries == 0)
return;
3312 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
3318 gc_mark_tbl_no_pin(tbl);
3322rb_gc_mark_set_no_pin(
st_table *tbl)
3324 if (!tbl || tbl->num_entries == 0)
return;
3326 st_foreach(tbl, gc_mark_set_no_pin_i, 0);
3332 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
3338 void *
objspace = rb_gc_get_objspace();
3340 if (RB_LIKELY(rb_gc_impl_during_gc_p(
objspace))) {
3341 return rb_gc_impl_get_vm_context(
objspace)->ec;
3349rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
3352 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3354#define MARK_CHECKPOINT(category) do { \
3355 if (categoryp) *categoryp = category; \
3360 const bool global_gc = rb_gc_impl_during_global_gc_p(
objspace) ||
3361 !rb_gc_impl_multi_objspace_p();
3365 MARK_CHECKPOINT(
"ractor");
3368 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
3369 rb_ractor_mark_local_roots(r);
3374 if (vm->ractor.cnt == 0 && vm->ractor.main_ractor) {
3375 rb_ractor_mark_local_roots(vm->ractor.main_ractor);
3381 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
3382 rb_ractor_t *owner = vm->gc.zombie_objspaces[i].owner;
3384 rb_gc_mark_vm_stack_values((
long)owner->registered_marks_cnt,
3385 owner->registered_marks);
3392 if (!rb_gc_impl_multi_objspace_p()) {
3395 ccan_list_for_each(&vm->ractor.terminated_set,
tr, vmlr_node) {
3396 rb_gc_mark_vm_stack_values((
long)
tr->registered_marks_cnt,
3397 tr->registered_marks);
3403 rb_ractor_mark_local_roots(rb_ec_ractor_ptr(ec));
3409 MARK_CHECKPOINT(
"registered_globals");
3411 for (
size_t i = 0; i < vm->gc.registered_globals.addrs_cnt; i++) {
3412 rb_gc_mark_maybe(*vm->gc.registered_globals.addrs[i]);
3419 MARK_CHECKPOINT(
"trap_list");
3420 rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
3424 if (global_gc ||
objspace == vm->ractor.main_ractor->objspace) {
3427 MARK_CHECKPOINT(
"end_proc");
3430 MARK_CHECKPOINT(
"vm");
3434 const bool vm_mark_needs_lock = rb_multi_ractor_p() && !global_gc;
3435 unsigned int vm_mark_lock_lev = 0;
3436 if (vm_mark_needs_lock) vm_mark_lock_lev = RB_GC_VM_LOCK_NO_BARRIER();
3439 MARK_CHECKPOINT(
"global_tbl");
3440 rb_gc_mark_global_tbl();
3443 void rb_yjit_root_mark(
void);
3445 if (rb_yjit_enabled_p) {
3446 MARK_CHECKPOINT(
"YJIT");
3447 rb_yjit_root_mark();
3452 void rb_zjit_root_mark(
void);
3453 if (rb_zjit_enabled_p) {
3454 MARK_CHECKPOINT(
"ZJIT");
3455 rb_zjit_root_mark();
3458 if (vm_mark_needs_lock) RB_GC_VM_UNLOCK_NO_BARRIER(vm_mark_lock_lev);
3460 if (global_gc || rb_gc_single_objspace_p()) {
3461 MARK_CHECKPOINT(
"global_symbols");
3462 rb_sym_global_symbols_mark_and_move();
3470 if (!rb_gc_impl_during_postmortem_p(
objspace)) {
3471 MARK_CHECKPOINT(
"machine_context");
3472 mark_current_machine_context(ec);
3475 MARK_CHECKPOINT(
"finish");
3477#undef MARK_CHECKPOINT
3491 if (RCLASSEXT_SUPER(ext)) {
3492 gc_mark_internal(RCLASSEXT_SUPER(ext));
3494 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3496 gc_mark_internal(RCLASSEXT_FIELDS_OBJ(ext));
3497 gc_mark_internal(RCLASSEXT_CVC_TBL(ext));
3499 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
3500 mark_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
3502 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3503 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3504 if (RCLASSEXT_SUBCLASSES(ext)) {
3505 gc_mark_internal(RCLASSEXT_SUBCLASSES(ext));
3507 gc_mark_internal(RCLASSEXT_CLASSPATH(ext));
3516 if (RCLASSEXT_SUPER(ext)) {
3517 gc_mark_internal(RCLASSEXT_SUPER(ext));
3519 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
3520 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3522 if (RCLASSEXT_INCLUDER(ext)) {
3523 gc_mark_internal(RCLASSEXT_INCLUDER(ext));
3525 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3526 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3527 if (RCLASSEXT_SUBCLASSES(ext)) {
3528 gc_mark_internal(RCLASSEXT_SUBCLASSES(ext));
3532#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA_TYPE(d)->function.dmark
3535rb_obj_using_gen_fields_table_p(
VALUE obj)
3546 return rb_obj_gen_fields_p(obj);
3550rb_gc_move_obj_during_marking(
VALUE from,
VALUE to)
3552 if (rb_obj_using_gen_fields_table_p(to)) {
3553 rb_mark_generic_ivar(from);
3562 if (rb_obj_using_gen_fields_table_p(obj)) {
3563 rb_mark_generic_ivar(obj);
3573 rb_bug(
"rb_gc_mark() called for broken object");
3577 UNEXPECTED_NODE(rb_gc_mark);
3581 rb_imemo_mark_and_move(obj,
false);
3588 gc_mark_internal(
RBASIC(obj)->klass);
3593 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
3598 foreach_args.obj = obj;
3599 rb_class_classext_foreach(obj, gc_mark_classext_module, (
void *)&foreach_args);
3600 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3601 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3607 foreach_args.obj = obj;
3608 rb_class_classext_foreach(obj, gc_mark_classext_iclass, (
void *)&foreach_args);
3609 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3610 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3615 if (ARY_SHARED_P(obj)) {
3616 VALUE root = ARY_SHARED_ROOT(obj);
3618 gc_mark_internal(root);
3624 gc_mark_and_pin_internal(root);
3630 for (
long i = 0; i <
len; i++) {
3631 gc_mark_internal(ptr[i]);
3641 gc_mark_internal(RSYMBOL(obj)->fstr);
3645 if (STR_SHARED_P(obj)) {
3646 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
3651 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
3654 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
3660 void *
const ptr = RTYPEDDATA_GET_DATA(obj);
3662 gc_mark_internal(
RTYPEDDATA(obj)->fields_obj);
3666 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3668 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3669 gc_mark_internal(*(
VALUE *)((
char *)ptr + offset));
3674 if (mark_func) (*mark_func)(ptr);
3682 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
3683 if (rb_shape_embedded_p(shape_id)) {
3684 uint32_t
len = RSHAPE_LEN(shape_id);
3687 for (uint32_t i = 0; i <
len; i++) {
3688 gc_mark_internal(ptr[i]);
3692 if (!rb_gc_checking_shareable()) {
3693 gc_mark_internal(
ROBJECT(obj)->as.extended);
3700 if (
RFILE(obj)->fptr) {
3701 gc_mark_internal(
RFILE(obj)->fptr->self);
3702 gc_mark_internal(
RFILE(obj)->fptr->pathv);
3703 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
3704 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
3705 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
3706 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
3707 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
3708 gc_mark_internal(
RFILE(obj)->fptr->timeout);
3709 gc_mark_internal(
RFILE(obj)->fptr->wakeup_mutex);
3714 gc_mark_internal(
RREGEXP(obj)->src);
3718 gc_mark_internal(
RMATCH(obj)->regexp);
3720 gc_mark_internal(
RMATCH(obj)->str);
3725 gc_mark_internal(RRATIONAL(obj)->num);
3726 gc_mark_internal(RRATIONAL(obj)->den);
3730 gc_mark_internal(RCOMPLEX(obj)->real);
3731 gc_mark_internal(RCOMPLEX(obj)->imag);
3736 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
3738 for (
long i = 0; i <
len; i++) {
3739 gc_mark_internal(ptr[i]);
3742 gc_mark_internal(RSTRUCT_FIELDS_OBJ(obj));
3751 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
3753 rb_gc_impl_live_object_p(
objspace, (
void *)obj) ?
"corrupted object" :
"non object");
3758rb_gc_obj_optimal_size(
VALUE obj)
3763 size_t size = rb_ary_size_as_embedded(obj);
3764 if (rb_gc_size_allocatable_p(size)) {
3768 return sizeof(
struct RArray);
3773 if (rb_obj_shape_complex_p(obj)) {
3774 return sizeof(
struct RObject);
3777 size_t size = rb_obj_embedded_size(RSHAPE_CAPACITY(RBASIC_SHAPE_ID(obj)));
3778 if (rb_gc_size_allocatable_p(size)) {
3782 return sizeof(
struct RObject);
3788 size_t size = rb_str_size_as_embedded(obj);
3789 if (rb_gc_size_allocatable_p(size)) {
3793 return sizeof(
struct RString);
3799 if (RHASH_AR_TABLE_P(obj)) {
3800 const unsigned bound = RHASH_AR_TABLE_BOUND(obj);
3801 const size_t ar_size = RHASH_AR_SLOT_SIZE(bound);
3802 if (ar_size > RHASH_ST_SLOT_SIZE ||
OBJ_FROZEN(obj)) {
3807 return RHASH_ST_SLOT_SIZE;
3818 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
3824 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
3831rb_gc_writebarrier_remember(
VALUE obj)
3833 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
3839rb_gc_obj_became_shareable(
VALUE obj)
3841 rb_gc_impl_obj_became_shareable(rb_gc_get_objspace(), obj);
3849 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
3854rb_gc_modular_gc_loaded_p(
void)
3856 return rb_gc_functions.modular_gc_loaded_p;
3860rb_gc_active_gc_name(
void)
3862 const char *gc_name = rb_gc_impl_active_gc_name();
3864 const size_t len = strlen(gc_name);
3865 if (
len > RB_GC_MAX_NAME_LEN) {
3866 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
3867 RB_GC_MAX_NAME_LEN,
len, gc_name);
3875rb_gc_object_metadata(
VALUE obj)
3877 return rb_gc_impl_object_metadata(rb_gc_get_objspace(), obj);
3885 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
3889rb_gc_ractor_cache_free(
void *cache)
3891 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3897#if defined(RUBY_ASAN_ENABLED)
3898 (void)rb_gc_impl_zjit_new_obj_fastpath;
3901 return rb_gc_impl_zjit_new_obj_fastpath(rb_gc_get_objspace(), alloc_size, flags, klass, fastpath);
3912 if (rb_gc_impl_live_object_p(rb_gc_get_objspace(), (
void *)obj)) {
3913 rb_vm_register_global_object(obj);
3924 if (vm->gc.registered_globals.addrs_cnt == vm->gc.registered_globals.addrs_capa) {
3925 size_t nc = vm->gc.registered_globals.addrs_capa ? vm->gc.registered_globals.addrs_capa * 2 : 64;
3926 VALUE **p = realloc(vm->gc.registered_globals.addrs, nc *
sizeof(
VALUE *));
3927 if (!p) rb_bug(
"rb_gc_register_address: out of memory");
3928 vm->gc.registered_globals.addrs = p;
3929 vm->gc.registered_globals.addrs_capa = nc;
3931 vm->gc.registered_globals.addrs[vm->gc.registered_globals.addrs_cnt++] = addr;
3947 for (
size_t i = 0; i < vm->gc.registered_globals.addrs_cnt; i++) {
3948 if (vm->gc.registered_globals.addrs[i] == addr) {
3949 MEMMOVE(&vm->gc.registered_globals.addrs[i], &vm->gc.registered_globals.addrs[i + 1],
3950 VALUE *, vm->gc.registered_globals.addrs_cnt - i - 1);
3951 vm->gc.registered_globals.addrs_cnt--;
3961 rb_gc_register_address(var);
3967 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact),
RTEST(global));
3974 int (*callback)(
void *,
void *, size_t,
void *);
3979each_objects_foreign_i(
void *
objspace,
void *arg)
3983 rb_gc_impl_each_objects_foreign(
objspace, a->callback, a->data);
4031rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
4036 void *self = rb_gc_get_objspace();
4037 rb_gc_impl_each_objects(self, callback, data);
4044 rb_gc_vm_each_objspace(each_objects_foreign_i, &arg);
4052rb_gc_vm_each_objspace(
void (*func)(
void *
objspace,
void *data),
void *data)
4054 ASSERT_vm_locking();
4058 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4060 func(r->objspace, data);
4065 if (r->creating_child_objspace) {
4066 func(r->creating_child_objspace, data);
4069 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4070 func(vm->gc.zombie_objspaces[i].objspace, data);
4078static void gc_orphan_merge_job(
void *unused);
4086 ASSERT_vm_locking();
4087 if (vm->gc.zombie_objspaces_count == vm->gc.zombie_objspaces_capa) {
4088 size_t new_capa = vm->gc.zombie_objspaces_capa ? vm->gc.zombie_objspaces_capa * 2 : 16;
4089 struct rb_objspace_zombie *grown =
4090 realloc(vm->gc.zombie_objspaces, new_capa *
sizeof(
struct rb_objspace_zombie));
4091 if (grown == NULL) rb_bug(
"zombie_objspaces_push: out of memory");
4092 vm->gc.zombie_objspaces = grown;
4093 vm->gc.zombie_objspaces_capa = new_capa;
4095 size_t pages = rb_gc_impl_heap_page_count(
objspace);
4096 vm->gc.zombie_objspaces[vm->gc.zombie_objspaces_count++] = (
struct rb_objspace_zombie){
4098 .owner_slot = owner_slot,
4102 vm->gc.zombie_total_pages += pages;
4113gc_orphan_merge_pjob_ensure(
void)
4115 if (GET_VM()->gc.orphan_merge_pjob == POSTPONED_JOB_HANDLE_INVALID) {
4117 if (GET_VM()->gc.orphan_merge_pjob == POSTPONED_JOB_HANDLE_INVALID) {
4118 rb_bug(
"Could not preregister postponed job for GC");
4125rb_gc_objspace_retire_gc(
void)
4127 rb_gc_impl_objspace_retire_gc(rb_gc_get_objspace());
4131rb_gc_objspace_retire(
void **objspace_slot)
4135 if (!rb_gc_impl_multi_objspace_p()) {
4137 *objspace_slot = NULL;
4143 if (rb_gc_impl_user_gc_disabled_set(*objspace_slot,
false)) {
4148 gc_orphan_merge_pjob_ensure();
4154 zombie_objspaces_push(vm, *objspace_slot, objspace_slot, owner);
4162rb_gc_objspace_disown(
void *
objspace)
4164 if (!rb_gc_impl_multi_objspace_p())
return;
4165 ASSERT_vm_locking();
4169 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4170 if (vm->gc.zombie_objspaces[i].objspace ==
objspace) {
4171 vm->gc.zombie_objspaces[i].owner_slot = NULL;
4174 vm->gc.zombie_objspaces[i].owner = NULL;
4180 zombie_objspaces_push(vm,
objspace, NULL, NULL);
4185 gc_orphan_merge_pjob_ensure();
4186 rb_postponed_job_trigger_for_ractor(GET_VM()->gc.orphan_merge_pjob, vm->ractor.main_ractor->pub.self);
4192rb_gc_during_global_gc_p(
void)
4194 return rb_gc_impl_during_global_gc_p(rb_gc_get_objspace());
4198rb_gc_vm_forget_zombie(
void *
objspace)
4200 ASSERT_vm_locking();
4202 size_t n = vm->gc.zombie_objspaces_count;
4203 for (
size_t i = 0; i < n; i++) {
4204 if (vm->gc.zombie_objspaces[i].objspace ==
objspace) {
4205 vm->gc.zombie_total_pages -= vm->gc.zombie_objspaces[i].pages;
4206 vm->gc.zombie_objspaces[i] = vm->gc.zombie_objspaces[n - 1];
4207 vm->gc.zombie_objspaces_count = n - 1;
4217rb_gc_vm_zombie_total_pages(
void)
4219 return GET_VM()->gc.zombie_total_pages;
4224rb_gc_vm_ractor_count(
void)
4226 return GET_VM()->ractor.cnt;
4231rb_gc_vm_refresh_zombie_pages(
void)
4235 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4236 size_t pages = rb_gc_impl_heap_page_count(vm->gc.zombie_objspaces[i].objspace);
4237 vm->gc.zombie_objspaces[i].pages = pages;
4240 vm->gc.zombie_total_pages = total;
4249 rb_gc_impl_gc_rest(rb_gc_get_objspace());
4256static int gc_absorbing_zombie = 0;
4261static bool gc_absorbed_since_global_gc =
false;
4264rb_gc_reset_absorbed_since_global_gc(
void)
4266 gc_absorbed_since_global_gc =
false;
4279rb_gc_multi_objspace_p(
void)
4281 return rb_gc_impl_multi_objspace_p();
4287rb_gc_obj_foreign_p(
VALUE obj)
4289 return rb_gc_impl_obj_foreign_p(rb_gc_get_objspace(), obj);
4293rb_gc_single_objspace_p(
void)
4295 if (!rb_gc_impl_multi_objspace_p())
return true;
4299 return (ruby_single_main_ractor != NULL || vm->ractor.cnt == 1) &&
4300 vm->gc.zombie_objspaces_count == 0 && gc_absorbing_zombie == 0 &&
4301 !gc_absorbed_since_global_gc &&
4302 (vm->ractor.main_ractor == NULL ||
4303 vm->ractor.main_ractor->creating_child_objspace == NULL);
4310objspace_absorb_merge(
void *dst,
void *src)
4312 ASSERT_vm_locking();
4313 rb_gc_impl_objspace_absorb(dst, src);
4314 gc_absorbed_since_global_gc =
true;
4321rb_gc_objspace_postmortem_self(
void)
4323 if (!rb_gc_impl_multi_objspace_p())
return;
4325 rb_gc_impl_objspace_retire_gc(rb_gc_get_objspace());
4329rb_gc_objspace_absorb_into_current(
void **objspace_slot)
4331 if (!rb_gc_impl_multi_objspace_p()) {
4332 *objspace_slot = NULL;
4338 *objspace_slot = NULL;
4339 gc_absorbing_zombie++;
4341 objspace_absorb_merge(rb_gc_get_objspace(),
objspace);
4342 gc_absorbing_zombie--;
4351objspace_absorb_disowned_zombies(
void)
4357 while (i < vm->gc.zombie_objspaces_count) {
4358 if (vm->gc.zombie_objspaces[i].owner_slot == NULL) {
4359 void *zombie = vm->gc.zombie_objspaces[i].objspace;
4363 gc_absorbing_zombie++;
4364 rb_gc_vm_forget_zombie(zombie);
4365 objspace_absorb_merge(rb_gc_get_objspace(), zombie);
4366 gc_absorbing_zombie--;
4376gc_orphan_merge_job(
void *unused)
4379 objspace_absorb_disowned_zombies();
4387rb_gc_disable_holders_atfork(
void)
4390 rb_gc_impl_user_gc_disabled_p(rb_gc_get_objspace()) ? 1 : 0);
4394rb_gc_zombie_objspaces_atfork(
void)
4398 for (
size_t i = 0; i < vm->gc.zombie_objspaces_count; i++) {
4399 if (vm->gc.zombie_objspaces[i].owner_slot == NULL) {
4400 rb_postponed_job_trigger_for_ractor(GET_VM()->gc.orphan_merge_pjob, vm->ractor.main_ractor->pub.self);
4410rb_gc_objspace_absorb_all_zombies(
void)
4416 objspace_absorb_disowned_zombies();
4418 while (vm->gc.zombie_objspaces_count > 0) {
4419 size_t before = vm->gc.zombie_objspaces_count;
4420 GC_ASSERT(vm->gc.zombie_objspaces[0].owner_slot != NULL);
4423 rb_ractor_t *owner = vm->gc.zombie_objspaces[0].owner;
4425 rb_ractor_absorb_registered_marks(GET_RACTOR(), owner);
4427 rb_gc_objspace_absorb_into_current(vm->gc.zombie_objspaces[0].owner_slot);
4428 if (vm->gc.zombie_objspaces_count >= before) {
4429 rb_bug(
"rb_gc_objspace_absorb_all_zombies: zombie list did not shrink");
4437 if (ARY_SHARED_P(v)) {
4438 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
4442 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
4446 if (
RB_TYPE_P(new_root,
T_ARRAY) && ARY_EMBED_P(new_root) && new_root != old_root) {
4447 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
4448 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
4449 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
4457 for (
long i = 0; i <
len; i++) {
4462 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
4466 if (rb_ary_embeddable_p(v) && !rb_gc_impl_pinned_p(
objspace, v)) {
4467 rb_ary_make_embedded(v);
4476 RUBY_ASSERT(rb_gc_obj_slot_size(v) == rb_obj_shape_slot_size(v));
4477 shape_id_t shape_id = RBASIC_SHAPE_ID(v);
4479 if (!rb_shape_embedded_p(shape_id)) {
4482 if (!rb_shape_complex_p(shape_id) && rb_shape_embedded_capacity(shape_id) >= RSHAPE_LEN(shape_id)) {
4483 VALUE *embedded_fields = ROBJECT_EMBEDDED_FIELDS(v);
4484 VALUE *extended_fields = ROBJECT_FIELDS(v);
4485 MEMCPY(embedded_fields, extended_fields,
VALUE, RSHAPE_LEN(shape_id));
4486 shape_id = rb_shape_transition_robject(shape_id);
4487 RBASIC_SET_FULL_SHAPE_ID(v, shape_id);
4488 rb_gc_writebarrier_remember(v);
4495 VALUE *ptr = ROBJECT_FIELDS(v);
4496 attr_index_t
len = RSHAPE_LEN(shape_id);
4497 for (attr_index_t i = 0; i <
len; i++) {
4503rb_gc_ref_update_table_values_only(
st_table *tbl)
4505 gc_ref_update_table_values_only(tbl);
4512 gc_update_table_refs(ptr);
4516rb_gc_update_set_refs_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
4518 if (rb_gc_location((
VALUE)key) != (
VALUE)key) {
4526rb_gc_update_set_refs_replace_i(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
4528 rb_gc_update_moved((
VALUE *)key);
4534rb_gc_update_set_refs(
st_table *tbl)
4536 if (!tbl || tbl->num_entries == 0)
return;
4538 if (st_foreach_with_replace(tbl, rb_gc_update_set_refs_i, rb_gc_update_set_refs_replace_i, 0)) {
4546 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
4552 for (
long i = 0; i < n; i++) {
4553 UPDATE_IF_MOVED(
objspace, values[i]);
4558rb_gc_update_values(
long n,
VALUE *values)
4560 gc_update_values(rb_gc_get_objspace(), n, values);
4563static enum rb_id_table_iterator_result
4564check_id_table_move(
VALUE value,
void *data)
4569 return ID_TABLE_REPLACE;
4572 return ID_TABLE_CONTINUE;
4576rb_gc_prepare_heap_process_object(
VALUE obj)
4582 rb_enc_str_coderange(obj);
4590rb_gc_prepare_heap(
void)
4592 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
4596rb_gc_size_slot_size(
size_t size)
4598 return rb_gc_impl_size_slot_size(rb_gc_get_objspace(), size);
4602rb_gc_size_allocatable_p(
size_t size)
4604 return rb_gc_impl_size_allocatable_p(size);
4608rb_gc_max_allocation_size(
void)
4610 return rb_gc_impl_max_allocation_size();
4613static enum rb_id_table_iterator_result
4614update_id_table(
VALUE *value,
void *data,
int existing)
4622 return ID_TABLE_CONTINUE;
4629 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
4633static enum rb_id_table_iterator_result
4638 if (gc_object_moved_p_internal(
objspace, ce->value)) {
4639 ce->value = gc_location_internal(
objspace, ce->value);
4642 if (gc_object_moved_p_internal(
objspace, ce->file)) {
4643 ce->file = gc_location_internal(
objspace, ce->file);
4646 return ID_TABLE_CONTINUE;
4653 rb_id_table_foreach_values(tbl, update_const_tbl_i,
objspace);
4659 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
4660 size_t array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
4661 for (
size_t i = 0; i < array_size; i++) {
4662 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPERCLASSES(ext)[i]);
4670 UPDATE_IF_MOVED(
objspace, RCLASSEXT_ORIGIN(ext));
4671 UPDATE_IF_MOVED(
objspace, RCLASSEXT_REFINED_CLASS(ext));
4672 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CLASSPATH(ext));
4674 UPDATE_IF_MOVED(
objspace, RCLASSEXT_INCLUDER(ext));
4684 if (RCLASSEXT_SUPER(ext)) {
4685 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
4688 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
4690 UPDATE_IF_MOVED(
objspace, ext->fields_obj);
4691 if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
4692 update_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
4694 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
4695 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
4696 update_superclasses(
objspace, ext);
4697 if (RCLASSEXT_SUBCLASSES(ext)) {
4698 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUBCLASSES(ext));
4701 update_classext_values(
objspace, ext,
false);
4710 if (RCLASSEXT_SUPER(ext)) {
4711 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
4713 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
4714 update_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
4715 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
4716 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
4717 if (RCLASSEXT_SUBCLASSES(ext)) {
4718 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUBCLASSES(ext));
4721 update_classext_values(
objspace, ext,
true);
4725 vm_table_foreach_callback_func callback;
4726 vm_table_update_callback_func update_callback;
4731 struct st_table *gen_fields_current_tbl;
4735 size_t gf_deferred_cnt, gf_deferred_capa;
4739vm_weak_table_foreach_weak_key(st_data_t key, st_data_t value, st_data_t data,
int error)
4743 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4745 if (!iter_data->weak_only) {
4746 if (ret != ST_CONTINUE)
return ret;
4748 ret = iter_data->callback((
VALUE)value, iter_data->data);
4755vm_weak_table_foreach_update_weak_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
4759 int ret = iter_data->update_callback((
VALUE *)key, iter_data->data);
4761 if (!iter_data->weak_only) {
4762 if (ret != ST_CONTINUE)
return ret;
4764 ret = iter_data->update_callback((
VALUE *)value, iter_data->data);
4771vm_weak_table_sym_set_foreach(
VALUE *sym_ptr,
void *data)
4773 VALUE sym = *sym_ptr;
4778 int ret = iter_data->callback(sym, iter_data->data);
4780 if (ret == ST_REPLACE) {
4781 ret = iter_data->update_callback(sym_ptr, iter_data->data);
4787struct st_table *rb_generic_fields_tbl_get(
void);
4790vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
4794 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4809 RBASIC_SET_SHAPE_ID((
VALUE)key, ROOT_SHAPE_ID);
4814 ret = iter_data->update_callback(&new_key, iter_data->data);
4815 if (key != new_key) {
4822 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ret);
4825 if (!iter_data->weak_only) {
4826 int ivar_ret = iter_data->callback(new_value, iter_data->data);
4832 iter_data->update_callback(&new_value, iter_data->data);
4839 RBASIC_SET_SHAPE_ID((
VALUE)key, ROOT_SHAPE_ID);
4843 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ivar_ret);
4847 if (key != new_key) {
4849 if (iter_data->gf_deferred_cnt == iter_data->gf_deferred_capa) {
4850 size_t nc = iter_data->gf_deferred_capa ? iter_data->gf_deferred_capa * 2 : 64;
4851 struct gen_fields_deferred_insert *p =
4852 realloc(iter_data->gf_deferred, nc *
sizeof(*p));
4853 if (!p) rb_bug(
"vm_weak_table_gen_fields_foreach: out of memory");
4854 iter_data->gf_deferred = p;
4855 iter_data->gf_deferred_capa = nc;
4857 iter_data->gf_deferred[iter_data->gf_deferred_cnt++] =
4858 (
struct gen_fields_deferred_insert){ .k = (st_data_t)new_key, .v = (st_data_t)new_value };
4860 else if (value != new_value) {
4861 DURING_GC_COULD_MALLOC_REGION_START();
4864 st_insert(iter_data->gen_fields_current_tbl, (st_data_t)new_key, new_value);
4866 DURING_GC_COULD_MALLOC_REGION_END();
4873vm_weak_table_frozen_strings_foreach(
VALUE *str,
void *data)
4877 int retval = iter_data->callback(*str, iter_data->data);
4879 if (retval == ST_REPLACE) {
4880 retval = iter_data->update_callback(str, iter_data->data);
4883 if (retval == ST_DELETE) {
4890void rb_fstring_foreach_with_replace(
int (*callback)(
VALUE *str,
void *data),
void *data);
4896vm_weak_table_gen_fields_tbl_cb(
struct st_table *tbl,
void *arg)
4899 foreach_data->gen_fields_current_tbl = tbl;
4900 st_foreach(tbl, vm_weak_table_gen_fields_foreach, (st_data_t)foreach_data);
4904rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
4905 vm_table_update_callback_func update_callback,
4908 enum rb_gc_vm_weak_tables table)
4913 .callback = callback,
4914 .update_callback = update_callback,
4916 .weak_only = weak_only,
4920 case RB_GC_VM_CI_TABLE: {
4921 st_foreach_with_replace(
4923 vm_weak_table_foreach_weak_key,
4924 vm_weak_table_foreach_update_weak_key,
4925 (st_data_t)&foreach_data
4929 case RB_GC_VM_OVERLOADED_CME_TABLE: {
4930 st_foreach_with_replace(
4931 &vm->overloaded_cme_table,
4932 vm_weak_table_foreach_weak_key,
4933 vm_weak_table_foreach_update_weak_key,
4934 (st_data_t)&foreach_data
4938 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
4939 rb_sym_global_symbol_table_foreach_weak_reference(
4940 vm_weak_table_sym_set_foreach,
4945 case RB_GC_VM_GENERIC_FIELDS_TABLE: {
4950 if (rb_gc_during_global_gc_p()) {
4951 rb_generic_fields_tables_foreach(vm_weak_table_gen_fields_tbl_cb, (
void *)&foreach_data);
4953 else if (!weak_only) {
4954 rb_generic_fields_shared_table_foreach(vm_weak_table_gen_fields_tbl_cb, (
void *)&foreach_data);
4956 if (foreach_data.gf_deferred != NULL) {
4957 DURING_GC_COULD_MALLOC_REGION_START();
4959 for (
size_t i = 0; i < foreach_data.gf_deferred_cnt; i++) {
4960 struct gen_fields_deferred_insert *
const d = &foreach_data.gf_deferred[i];
4961 st_insert(foreach_data.gen_fields_current_tbl, d->k, d->v);
4964 DURING_GC_COULD_MALLOC_REGION_END();
4965 free(foreach_data.gf_deferred);
4969 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
4970 rb_fstring_foreach_with_replace(
4971 vm_weak_table_frozen_strings_foreach,
4976 case RB_GC_VM_WEAK_TABLE_COUNT:
4977 rb_bug(
"Unreachable");
4979 rb_bug(
"rb_gc_vm_weak_table_foreach: unknown table %d", table);
4991gf_mark_foreach_i(st_data_t key, st_data_t val, st_data_t data)
4994 return ctx->cb((
VALUE)key, (
VALUE)val, ctx->arg);
4998gf_mark_foreach_table_cb(
struct st_table *tbl,
void *arg)
5000 st_foreach(tbl, gf_mark_foreach_i, (st_data_t)arg);
5004rb_gc_vm_generic_fields_mark_foreach(
int (*cb)(
VALUE key,
VALUE val,
void *arg),
void *arg)
5007 rb_generic_fields_tables_foreach(gf_mark_foreach_table_cb, &ctx);
5011 bool (*is_dead)(
VALUE key);
5015gf_drain_i(st_data_t key, st_data_t val, st_data_t data)
5018 if (ctx->is_dead((
VALUE)key)) {
5028gf_drain_table_cb(
struct st_table *tbl,
void *arg)
5030 st_foreach(tbl, gf_drain_i, (st_data_t)arg);
5034rb_gc_vm_generic_fields_drain_dead(
bool (*is_dead)(
VALUE key))
5037 rb_generic_fields_tables_foreach(gf_drain_table_cb, &ctx);
5041rb_gc_vm_top_self(
void)
5043 return rb_vm_top_self();
5047rb_gc_update_vm_references(
void *
objspace)
5050 rb_vm_t *vm = rb_ec_vm_ptr(ec);
5052 rb_vm_update_references(vm);
5053 rb_gc_update_global_tbl();
5054 rb_sym_global_symbols_mark_and_move();
5057 void rb_yjit_root_update_references(
void);
5059 if (rb_yjit_enabled_p) {
5060 rb_yjit_root_update_references();
5065 void rb_zjit_root_update_references(
void);
5067 if (rb_zjit_enabled_p) {
5068 rb_zjit_root_update_references();
5081 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
5087 rb_class_classext_foreach(obj, update_classext, (
void *)&args);
5092 rb_class_classext_foreach(obj, update_iclass_classext, (
void *)&args);
5096 rb_imemo_mark_and_move(obj,
true);
5108 gc_ref_update_array(
objspace, obj);
5113 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
5118 if (STR_SHARED_P(obj)) {
5126 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
5127 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)
5128 && !rb_gc_impl_pinned_p(
objspace, obj)) {
5129 rb_str_make_embedded(obj);
5138 void *
const ptr = RTYPEDDATA_GET_DATA(obj);
5144 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
5146 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
5147 VALUE *ref = (
VALUE *)((
char *)ptr + offset);
5148 *ref = gc_location_internal(
objspace, *ref);
5153 if (compact_func) (*compact_func)(ptr);
5160 gc_ref_update_object(
objspace, obj);
5164 if (
RFILE(obj)->fptr) {
5167 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
5168 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
5169 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
5181 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
5197 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
5198 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
5202 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
5203 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
5210 VALUE *ptr = (
VALUE *)RSTRUCT_CONST_PTR(obj);
5212 for (i = 0; i <
len; i++) {
5216 UPDATE_IF_MOVED(
objspace, RSTRUCT(obj)->fields_obj);
5220 rb_bug(
"unreachable");
5237 unless_objspace(
objspace) {
return; }
5239 rb_gc_impl_start(
objspace,
true,
true,
true,
false,
true);
5245 unless_objspace(
objspace) {
return FALSE; }
5247 return rb_gc_impl_during_gc_p(
objspace);
5253 return rb_gc_impl_gc_count(rb_gc_get_objspace());
5269 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
5272 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
5282 arg = rb_hash_new();
5288 VALUE ret = rb_gc_impl_stat(
RTEST(global_scope) ? NULL : rb_gc_get_objspace(), arg);
5293 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
5306 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
5311 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
5326 arg = rb_hash_new();
5329 if (
NIL_P(heap_name)) {
5340 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
5343 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
5348 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
5357 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
5358 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
5366 void *
objspace = rb_gc_get_objspace();
5368 rb_gc_impl_config_set(
objspace, hash);
5376 return rb_gc_impl_stress_get(rb_gc_get_objspace());
5382 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
5388rb_gc_initial_stress_set(
VALUE flag)
5390 initial_stress = flag;
5398rb_gc_critical_disable(
void)
5400 rb_gc_impl_gc_rest(rb_gc_get_objspace());
5405rb_gc_critical_enable(
void)
5411rb_gc_gc_disabled_global_p(
void)
5419gc_ractor_disable_set(
bool disable)
5421 const bool was = rb_gc_impl_user_gc_disabled_set(rb_gc_get_objspace(), disable);
5422 if (was != disable) {
5436 return RBOOL(gc_ractor_disable_set(
false));
5440rb_gc_disable_no_rest(
void)
5442 return RBOOL(gc_ractor_disable_set(
true));
5448 const bool was_disabled = gc_ractor_disable_set(
true);
5449 if (!was_disabled) {
5450 rb_gc_impl_gc_rest(rb_gc_get_objspace());
5452 return RBOOL(was_disabled);
5456rb_objspace_gc_enable(
void *
objspace)
5458 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
5460 return RBOOL(disabled);
5464rb_objspace_gc_disable(
void *
objspace)
5466 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
5467 rb_gc_impl_gc_disable(
objspace,
true);
5468 return RBOOL(disabled);
5472rb_gc_objspace_enable(
void *
objspace)
5474 return rb_objspace_gc_enable(
objspace);
5478rb_gc_local_enable(
void)
5480 return rb_gc_objspace_enable(rb_gc_get_objspace());
5485rb_gc_objspace_disable_no_rest(
void *
objspace)
5487 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
5488 rb_gc_impl_gc_disable(
objspace,
false);
5489 return RBOOL(disabled);
5493rb_gc_local_disable_no_rest(
void)
5495 return rb_gc_objspace_disable_no_rest(rb_gc_get_objspace());
5501 return rb_gc_enable();
5507 return rb_gc_disable();
5512ruby_gc_set_params(
void)
5514 rb_gc_impl_set_params(rb_gc_get_objspace());
5518rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
5521 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
5532 rb_gc_mark_children(rb_gc_get_objspace(), obj);
5539 const char *category;
5540 void (*func)(
const char *category,
VALUE,
void *);
5545root_objects_from(
VALUE obj,
void *ptr)
5548 (*data->func)(data->category, obj, data->data);
5552rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
5554 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");
5558 .data = passing_data,
5564 .mark_func = root_objects_from,
5569 rb_gc_save_machine_context();
5570 rb_gc_mark_roots(rb_gc_get_objspace(), &data.category);
5582#define TYPE_NAME(t) case (t): return #t;
5609 if (obj && rb_objspace_data_type_name(obj)) {
5610 return rb_objspace_data_type_name(obj);
5619obj_type_name(
VALUE obj)
5621 return type_name(
TYPE(obj), obj);
5625rb_method_type_name(rb_method_type_t
type)
5628 case VM_METHOD_TYPE_ISEQ:
return "iseq";
5629 case VM_METHOD_TYPE_ATTRSET:
return "attrset";
5630 case VM_METHOD_TYPE_IVAR:
return "ivar";
5631 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
5632 case VM_METHOD_TYPE_ALIAS:
return "alias";
5633 case VM_METHOD_TYPE_REFINED:
return "refined";
5634 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
5635 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
5636 case VM_METHOD_TYPE_MISSING:
return "missing";
5637 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
5638 case VM_METHOD_TYPE_UNDEF:
return "undef";
5639 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
5641 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
5645rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
5647 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
5648 VALUE path = rb_iseq_path(iseq);
5649 int n = ISEQ_BODY(iseq)->location.first_lineno;
5650 VALUE label = ISEQ_BODY(iseq)->location.label;
5651 snprintf(buff, buff_size,
" %.*s@%.*s:%d",
5658str_len_no_raise(
VALUE str)
5660 long len = RSTRING_LEN(str);
5661 if (
len < 0)
return 0;
5662 if (
len > INT_MAX)
return INT_MAX;
5666#define BUFF_ARGS buff + pos, buff_size - pos
5667#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
5668#define APPEND_S(s) do { \
5669 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
5673 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
5676#define C(c, s) ((c) != 0 ? (s) : " ")
5679rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
5684 APPEND_F(
"%s", obj_type_name(obj));
5690 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
5696 if (rb_gc_impl_live_object_p(rb_gc_get_objspace(), (
void *)obj)) {
5697 APPEND_F(
"%p %s/", (
void *)obj, obj_type_name(obj));
5716 if (internal_object_p(obj)) {
5719 else if (
RBASIC(obj)->klass == 0) {
5720 APPEND_S(
"(temporary internal)");
5724 if (!
NIL_P(class_path)) {
5725 APPEND_F(
"%.*s ", str_len_no_raise(class_path), RSTRING_PTR(class_path));
5734const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
5737rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
5744 UNEXPECTED_NODE(rb_raw_obj_info);
5747 if (ARY_SHARED_P(obj)) {
5748 APPEND_S(
"shared -> ");
5749 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
5752 APPEND_F(
"[%s%s%s] ",
5753 C(ARY_EMBED_P(obj),
"E"),
5754 C(ARY_SHARED_P(obj),
"S"),
5755 C(ARY_SHARED_ROOT_P(obj),
"R"));
5757 if (ARY_EMBED_P(obj)) {
5758 APPEND_F(
"len: %ld (embed)",
5762 APPEND_F(
"len: %ld, capa:%ld ptr:%p",
5764 RARRAY(obj)->as.heap.aux.capa,
5771 C(
FL_TEST(obj, RSTRING_FSTR),
"F"),
5774 if (STR_SHARED_P(obj)) {
5775 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
5778 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
5780 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
5782 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
5786 VALUE fstr = RSYMBOL(obj)->fstr;
5787 ID id = RSYMBOL(obj)->id;
5789 APPEND_F(
":%.*s id:%d", str_len_no_raise(fstr), RSTRING_PTR(fstr), (
unsigned int)
id);
5792 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
5797 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
5801 APPEND_F(
"[%c] %"PRIdSIZE,
5802 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
5810 if (!
NIL_P(class_path)) {
5811 APPEND_F(
"%.*s", str_len_no_raise(class_path), RSTRING_PTR(class_path));
5821 if (!
NIL_P(class_path)) {
5822 APPEND_F(
"src:%.*s", str_len_no_raise(class_path), RSTRING_PTR(class_path));
5828 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
5829 if (rb_shape_embedded_p(shape_id)) {
5830 APPEND_F(
"(embed) len:%d capa:%d", RSHAPE_LEN(shape_id), RSHAPE_CAPACITY(shape_id));
5833 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
5834 if (rb_shape_complex_p(shape_id)) {
5835 size_t hash_len = rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
5836 APPEND_F(
"(complex) len:%zu extended:%p", hash_len, (
void *)fields_obj);
5839 APPEND_F(
"(extended) len:%d capa:%d extended:%p", RSHAPE_LEN(shape_id), RSHAPE_CAPACITY(shape_id), (
void *)fields_obj);
5848 (block = vm_proc_block(obj)) != NULL &&
5849 (vm_block_type(block) == block_type_iseq) &&
5850 (iseq = vm_block_iseq(block)) != NULL) {
5851 rb_raw_iseq_info(BUFF_ARGS, iseq);
5853 else if (rb_ractor_p(obj)) {
5856 APPEND_F(
"r:%"PRI_SERIALT_PREFIX
"u", r->pub.id);
5862 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
5864 switch (imemo_type(obj)) {
5867 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
5868 if (rb_shape_complex_p(shape_id)) {
5869 size_t hash_len = rb_st_table_size(rb_imemo_fields_complex_tbl(obj));
5870 APPEND_F(
"(complex) len:%zu", hash_len);
5873 APPEND_F(
"(embed) len:%d capa:%d", RSHAPE_LEN(shape_id), RSHAPE_CAPACITY(shape_id));
5876 APPEND_S(
"owner -> ");
5877 rb_raw_obj_info(BUFF_ARGS,
CLASS_OF(obj));
5885 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
5886 rb_id2name(me->called_id),
5887 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
5888 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
5889 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
5890 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
5891 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
5892 me->def ? rb_method_type_name(me->def->
type) :
"NULL",
5893 me->def ? me->def->aliased : -1,
5895 (void *)me->defined_class);
5898 switch (me->def->type) {
5899 case VM_METHOD_TYPE_ISEQ:
5900 APPEND_S(
" (iseq:");
5901 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
5913 rb_raw_iseq_info(BUFF_ARGS, iseq);
5916 case imemo_callinfo:
5919 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
5920 rb_id2name(vm_ci_mid(ci)),
5923 vm_ci_kwarg(ci) ?
"available" :
"NULL");
5926 case imemo_callcache:
5931 const char *class_name;
5934 if (
NIL_P(class_path)) {
5935 class_name = vm_cc_valid(cc) ?
"??" :
"<NULL>";
5936 class_name_len = vm_cc_valid(cc) ? 2 : 6;
5939 class_name = RSTRING_PTR(class_path);
5940 class_name_len = str_len_no_raise(class_path);
5943 APPEND_F(
"(klass:%.*s cme:%s%s (%p) call:%p",
5944 class_name_len, class_name,
5945 cme ? rb_id2name(cme->called_id) :
"<NULL>",
5946 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
5948 (void *)(uintptr_t)vm_cc_call(cc));
5966#ifdef RUBY_ASAN_ENABLED
5968rb_asan_poison_object(
VALUE obj)
5970 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5971 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
5975rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
5977 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5978 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
5982rb_asan_poisoned_object_p(
VALUE obj)
5984 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
5985 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
5990raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
5992 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
5993 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
5994 if (pos >= buff_size) {}
5998rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
6000 void *
objspace = rb_gc_get_objspace();
6003 raw_obj_info(buff, buff_size, obj);
6005 else if (!rb_gc_impl_live_object_p(
objspace, (
const void *)obj)) {
6006 snprintf(buff, buff_size,
"out-of-heap:%p", (
void *)obj);
6009 else if (0 && rb_gc_impl_garbage_object_p(
objspace, obj)) {
6010 snprintf(buff, buff_size,
"garbage:%p", (
void *)obj);
6014 asan_unpoisoning_object(obj) {
6015 raw_obj_info(buff, buff_size, obj);
6031 if (RB_UNLIKELY(oldval >= maxval - 1)) {
6042 if (RGENGC_OBJ_INFO) {
6045 char buffers[10][0x100];
6048 rb_atomic_t index = atomic_inc_wraparound(&info.index, numberof(info.buffers));
6049 char *
const buff = info.buffers[index];
6050 return rb_raw_obj_info(buff,
sizeof(info.buffers[0]), obj);
6052 return obj_type_name(obj);
6069 rb_vraise(argv->exc, argv->fmt, *argv->ap);
6074gc_raise(
VALUE exc,
const char *fmt, ...)
6088 fprintf(stderr,
"%s",
"[FATAL] ");
6089 vfprintf(stderr, fmt, ap);
6096NORETURN(
static void negative_size_allocation_error(
const char *));
6098negative_size_allocation_error(
const char *msg)
6104ruby_memerror_body(
void *dummy)
6110NORETURN(
static void ruby_memerror(
void));
6115 if (ruby_thread_has_gvl_p()) {
6124 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
6142 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
6145 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
6146 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
6147 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
6150 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
6151 rb_ec_raised_clear(ec);
6154 rb_ec_raised_set(ec, RAISED_NOMEMORY);
6155 exc = ruby_vm_special_exception_copy(exc);
6158 EC_JUMP_TAG(ec, TAG_RAISE);
6162rb_memerror_reentered(
void)
6165 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
6169handle_malloc_failure(
void *ptr)
6180static void *ruby_xmalloc_body(
size_t size);
6183ruby_xmalloc(
size_t size)
6185 if (RUBY_DTRACE_GC_XMALLOC_ENABLED()) {
6186 RUBY_DTRACE_GC_XMALLOC(1, size);
6189 return handle_malloc_failure(ruby_xmalloc_body(size));
6193malloc_gc_allowed(
void)
6197 return r == NULL || !r->malloc_gc_disabled;
6201ruby_xmalloc_body(
size_t size)
6203 if ((ssize_t)size < 0) {
6204 negative_size_allocation_error(
"too large allocation size");
6207 return rb_gc_impl_malloc(rb_gc_get_objspace(), size, malloc_gc_allowed());
6211ruby_malloc_size_overflow(
size_t count,
size_t elsize)
6213 rb_raise(rb_eArgError,
6214 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
6219ruby_malloc_add_size_overflow(
size_t x,
size_t y)
6221 rb_raise(rb_eArgError,
6222 "malloc: possible integer overflow (%"PRIuSIZE
"+%"PRIuSIZE
")",
6226static void *ruby_xmalloc2_body(
size_t n,
size_t size);
6229ruby_xmalloc2(
size_t n,
size_t size)
6231 if (RUBY_DTRACE_GC_XMALLOC_ENABLED()) {
6232 RUBY_DTRACE_GC_XMALLOC(n, size);
6235 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
6239ruby_xmalloc2_body(
size_t n,
size_t size)
6241 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
6244static void *ruby_xcalloc_body(
size_t n,
size_t size);
6247ruby_xcalloc(
size_t n,
size_t size)
6249 if (RUBY_DTRACE_GC_XCALLOC_ENABLED()) {
6250 RUBY_DTRACE_GC_XCALLOC(n, size);
6253 return handle_malloc_failure(ruby_xcalloc_body(n, size));
6257ruby_xcalloc_body(
size_t n,
size_t size)
6259 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
6262static void *ruby_xrealloc_sized_body(
void *ptr,
size_t new_size,
size_t old_size);
6264#ifdef ruby_xrealloc_sized
6265#undef ruby_xrealloc_sized
6268ruby_xrealloc_sized(
void *ptr,
size_t new_size,
size_t old_size)
6270 return handle_malloc_failure(ruby_xrealloc_sized_body(ptr, new_size, old_size));
6274ruby_xrealloc_sized_body(
void *ptr,
size_t new_size,
size_t old_size)
6276 if ((ssize_t)new_size < 0) {
6277 negative_size_allocation_error(
"too large allocation size");
6280 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size, malloc_gc_allowed());
6284ruby_xrealloc(
void *ptr,
size_t new_size)
6286 return ruby_xrealloc_sized(ptr, new_size, 0);
6289static void *ruby_xrealloc2_sized_body(
void *ptr,
size_t n,
size_t size,
size_t old_n);
6291#ifdef ruby_xrealloc2_sized
6292#undef ruby_xrealloc2_sized
6295ruby_xrealloc2_sized(
void *ptr,
size_t n,
size_t size,
size_t old_n)
6297 return handle_malloc_failure(ruby_xrealloc2_sized_body(ptr, n, size, old_n));
6301ruby_xrealloc2_sized_body(
void *ptr,
size_t n,
size_t size,
size_t old_n)
6303 size_t len = xmalloc2_size(n, size);
6304 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr,
len, old_n * size, malloc_gc_allowed());
6308ruby_xrealloc2(
void *ptr,
size_t n,
size_t size)
6310 return ruby_xrealloc2_sized(ptr, n, size, 0);
6313#ifdef ruby_xfree_sized
6314#undef ruby_xfree_sized
6324static bool g_nofree =
false;
6328ruby_xfree_sized(
void *x,
size_t size)
6336 if (RUBY_DTRACE_GC_XFREE_ENABLED()) {
6337 RUBY_DTRACE_GC_XFREE(x, size);
6344 if (LIKELY(GET_VM())) {
6345 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
6356 ruby_xfree_sized(x, 0);
6360rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
6362 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
6363 return ruby_xmalloc(w);
6367rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
6369 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
6370 return ruby_xcalloc(w, 1);
6374rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
6376 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
6377 return ruby_xrealloc((
void *)p, w);
6381rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
6383 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
6384 return ruby_xmalloc(u);
6388rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
6390 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
6391 return ruby_xcalloc(u, 1);
6398ruby_mimmalloc(
size_t size)
6401#if CALC_EXACT_MALLOC_SIZE
6405#if CALC_EXACT_MALLOC_SIZE
6421ruby_mimcalloc(
size_t num,
size_t size)
6424#if CALC_EXACT_MALLOC_SIZE
6425 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(num, size);
6426 if (UNLIKELY(t.overflowed)) {
6430 mem = calloc1(size);
6442 mem = calloc(num, size);
6448ruby_mimfree(
void *ptr)
6450#if CALC_EXACT_MALLOC_SIZE
6458rb_gc_adjust_memory_usage(ssize_t diff)
6460 unless_objspace(
objspace) {
return; }
6462 rb_gc_impl_adjust_memory_usage(
objspace, diff);
6466rb_obj_info(
VALUE obj)
6468 return obj_info(obj);
6472rb_obj_info_dump(
VALUE obj)
6475 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
6479rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
6482 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
6486rb_gc_before_fork(
void)
6488 rb_gc_impl_before_fork(rb_gc_get_objspace());
6492rb_gc_after_fork(rb_pid_t pid)
6494 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
6498rb_gc_obj_shareable_p(
VALUE obj)
6515check_shareable_i(
const VALUE child,
void *ptr)
6519 if (!rb_gc_obj_shareable_p(child)) {
6524 if (rb_gc_impl_shref_marked_p(rb_gc_get_objspace(), child)) {
6528 fprintf(stderr,
"(a) ");
6529 rb_gc_rp(data->parent);
6530 fprintf(stderr,
"(b) ");
6532 fprintf(stderr,
"check_shareable_i: shareable (a) -> unshareable (b) without a shref record\n");
6535 rb_bug(
"!! violate shareable constraint !!");
6548rb_gc_verify_shareable(
VALUE obj)
6559 .mark_func = check_shareable_i,
6561 .checking_shareable =
true,
6565 rb_gc_mark_children(rb_gc_get_objspace(), obj);
6569 if (data.err_count > 0) {
6570 rb_bug(
"rb_gc_verify_shareable");
6575rb_gc_checking_shareable(
void)
6578 return mfd && mfd->checking_shareable;
6614 const char* nofree_str = getenv(
"RUBY_NO_FREE");
6615 if (nofree_str && strcmp(nofree_str,
"1") == 0) {
6616 fprintf(stderr,
"WARNING: Enabling no-free mode! xfree() will never free anything!\n");
6622 malloc_offset = gc_compute_malloc_offset();
6624 rb_mGC = rb_define_module(
"GC");
6626 VALUE rb_mObjSpace = rb_define_module(
"ObjectSpace");
6633 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
6656ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
6658#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
6661 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.