14#define rb_data_object_alloc rb_data_object_alloc
15#define rb_data_typed_object_alloc rb_data_typed_object_alloc
17#include "ruby/internal/config.h"
22#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
23# include "wasm/setjmp.h"
24# include "wasm/machine.h"
32#ifndef HAVE_MALLOC_USABLE_SIZE
34# define HAVE_MALLOC_USABLE_SIZE
35# define malloc_usable_size(a) _msize(a)
36# elif defined HAVE_MALLOC_SIZE
37# define HAVE_MALLOC_USABLE_SIZE
38# define malloc_usable_size(a) malloc_size(a)
42#ifdef HAVE_MALLOC_USABLE_SIZE
43# ifdef RUBY_ALTERNATIVE_MALLOC_HEADER
45# elif defined(HAVE_MALLOC_H)
47# elif defined(HAVE_MALLOC_NP_H)
48# include <malloc_np.h>
49# elif defined(HAVE_MALLOC_MALLOC_H)
50# include <malloc/malloc.h>
60#ifdef HAVE_SYS_RESOURCE_H
61# include <sys/resource.h>
64#if defined _WIN32 || defined __CYGWIN__
66#elif defined(HAVE_POSIX_MEMALIGN)
67#elif defined(HAVE_MEMALIGN)
74#include <emscripten.h>
78#ifdef HAVE_SYS_PRCTL_H
86#include "debug_counter.h"
87#include "eval_intern.h"
91#include "internal/class.h"
92#include "internal/compile.h"
93#include "internal/complex.h"
94#include "internal/cont.h"
95#include "internal/error.h"
96#include "internal/eval.h"
97#include "internal/gc.h"
98#include "internal/hash.h"
99#include "internal/imemo.h"
100#include "internal/io.h"
101#include "internal/numeric.h"
102#include "internal/object.h"
103#include "internal/proc.h"
104#include "internal/rational.h"
105#include "internal/sanitizers.h"
106#include "internal/struct.h"
107#include "internal/symbol.h"
108#include "internal/thread.h"
109#include "internal/variable.h"
110#include "internal/warnings.h"
121#include "ruby_assert.h"
122#include "ruby_atomic.h"
126#include "vm_callinfo.h"
127#include "ractor_core.h"
137 RB_VM_LOCK_ENTER_LEV(&lev);
142rb_gc_vm_unlock(
unsigned int lev)
144 RB_VM_LOCK_LEAVE_LEV(&lev);
151 RB_VM_LOCK_ENTER_CR_LEV(GET_RACTOR(), &lev);
156rb_gc_cr_unlock(
unsigned int lev)
158 RB_VM_LOCK_LEAVE_CR_LEV(GET_RACTOR(), &lev);
162rb_gc_vm_lock_no_barrier(
void)
164 unsigned int lev = 0;
165 RB_VM_LOCK_ENTER_LEV_NB(&lev);
170rb_gc_vm_unlock_no_barrier(
unsigned int lev)
172 RB_VM_LOCK_LEAVE_LEV(&lev);
176rb_gc_vm_barrier(
void)
183rb_gc_get_ractor_newobj_cache(
void)
185 return GET_RACTOR()->newobj_cache;
189rb_gc_initialize_vm_context(
struct rb_gc_vm_context *context)
192 context->ec = GET_EC();
196rb_gc_worker_thread_set_vm_context(
struct rb_gc_vm_context *context)
200 GC_ASSERT(rb_current_execution_context(
false) == NULL);
202#ifdef RB_THREAD_LOCAL_SPECIFIER
203 rb_current_ec_set(context->ec);
205 native_tls_set(ruby_current_ec_key, context->ec);
210rb_gc_worker_thread_unset_vm_context(
struct rb_gc_vm_context *context)
214 GC_ASSERT(rb_current_execution_context(
true) == context->ec);
216#ifdef RB_THREAD_LOCAL_SPECIFIER
217 rb_current_ec_set(NULL);
219 native_tls_set(ruby_current_ec_key, NULL);
227 return ruby_vm_event_flags & event;
233 if (LIKELY(!rb_gc_event_hook_required_p(event)))
return;
235 rb_execution_context_t *ec = GET_EC();
236 if (!ec->cfp)
return;
238 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
242rb_gc_get_objspace(
void)
244 return GET_VM()->gc.objspace;
249rb_gc_ractor_newobj_cache_foreach(
void (*func)(
void *cache,
void *data),
void *data)
251 rb_ractor_t *r = NULL;
252 if (RB_LIKELY(ruby_single_main_ractor)) {
254 ccan_list_empty(&GET_VM()->ractor.set) ||
255 (ccan_list_top(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor &&
256 ccan_list_tail(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor)
259 func(ruby_single_main_ractor->newobj_cache, data);
262 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
263 func(r->newobj_cache, data);
269rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
274 rb_control_frame_t *cfp;
279 rb_execution_context_t *
volatile ec = GET_EC();
280#define RESTORE_FINALIZER() (\
281 ec->cfp = saved.cfp, \
282 ec->cfp->sp = saved.sp, \
283 ec->errinfo = saved.errinfo)
285 saved.errinfo = ec->errinfo;
287 saved.sp = ec->cfp->sp;
292 enum ruby_tag_type state = EC_EXEC_TAG();
293 if (state != TAG_NONE) {
296 VALUE failed_final = saved.final;
299 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
300 rb_ec_error_print(ec, ec->errinfo);
304 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
305 saved.final = callback(i, data);
309#undef RESTORE_FINALIZER
313rb_gc_set_pending_interrupt(
void)
315 rb_execution_context_t *ec = GET_EC();
316 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
320rb_gc_unset_pending_interrupt(
void)
322 rb_execution_context_t *ec = GET_EC();
323 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
327rb_gc_multi_ractor_p(
void)
329 return rb_multi_ractor_p();
332bool rb_obj_is_main_ractor(
VALUE gv);
335rb_gc_shutdown_call_finalizer_p(
VALUE obj)
340 if (rb_obj_is_thread(obj))
return false;
341 if (rb_obj_is_mutex(obj))
return false;
342 if (rb_obj_is_fiber(obj))
return false;
343 if (rb_obj_is_main_ractor(obj))
return false;
351 if (RSYMBOL(obj)->fstr &&
354 RSYMBOL(obj)->fstr = 0;
367rb_gc_get_shape(
VALUE obj)
369 return (uint32_t)rb_shape_get_shape_id(obj);
373rb_gc_set_shape(
VALUE obj, uint32_t shape_id)
375 rb_shape_set_shape_id(obj, (uint32_t)shape_id);
379rb_gc_rebuild_shape(
VALUE obj,
size_t heap_id)
381 rb_shape_t *orig_shape = rb_shape_get_shape(obj);
383 if (rb_shape_obj_too_complex(obj))
return (uint32_t)OBJ_TOO_COMPLEX_SHAPE_ID;
385 rb_shape_t *initial_shape = rb_shape_get_shape_by_id((shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID));
386 rb_shape_t *new_shape = rb_shape_traverse_from_new_root(initial_shape, orig_shape);
388 if (!new_shape)
return 0;
390 return (uint32_t)rb_shape_id(new_shape);
393void rb_vm_update_references(
void *ptr);
395#define rb_setjmp(env) RUBY_SETJMP(env)
396#define rb_jmp_buf rb_jmpbuf_t
397#undef rb_data_object_wrap
399#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
400#define MAP_ANONYMOUS MAP_ANON
403#define unless_objspace(objspace) \
405 rb_vm_t *unless_objspace_vm = GET_VM(); \
406 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
409#define RMOVED(obj) ((struct RMoved *)(obj))
411#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
412 if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
413 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
417#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
419#if RUBY_MARK_FREE_DEBUG
420int ruby_gc_debug_indent = 0;
423#ifndef RGENGC_OBJ_INFO
424# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
427#ifndef CALC_EXACT_MALLOC_SIZE
428# define CALC_EXACT_MALLOC_SIZE 0
433static size_t malloc_offset = 0;
434#if defined(HAVE_MALLOC_USABLE_SIZE)
436gc_compute_malloc_offset(
void)
447 for (offset = 0; offset <= 16; offset += 8) {
448 size_t allocated = (64 - offset);
449 void *test_ptr = malloc(allocated);
450 size_t wasted = malloc_usable_size(test_ptr) - allocated;
461gc_compute_malloc_offset(
void)
469rb_malloc_grow_capa(
size_t current,
size_t type_size)
471 size_t current_capacity = current;
472 if (current_capacity < 4) {
473 current_capacity = 4;
475 current_capacity *= type_size;
478 size_t new_capacity = (current_capacity * 2);
481 if (rb_popcount64(new_capacity) != 1) {
482 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
485 new_capacity -= malloc_offset;
486 new_capacity /= type_size;
487 if (current > new_capacity) {
488 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
494static inline struct rbimpl_size_mul_overflow_tag
495size_add_overflow(size_t x, size_t y)
501#elif defined(ckd_add)
502 p = ckd_add(&z, x, y);
504#elif __has_builtin(__builtin_add_overflow)
505 p = __builtin_add_overflow(x, y, &z);
507#elif defined(DSIZE_T)
519 return (
struct rbimpl_size_mul_overflow_tag) { p, z, };
522static inline struct rbimpl_size_mul_overflow_tag
523size_mul_add_overflow(size_t x, size_t y, size_t z)
525 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
526 struct rbimpl_size_mul_overflow_tag u = size_add_overflow(t.right, z);
527 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
530static inline struct rbimpl_size_mul_overflow_tag
531size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
533 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
534 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
535 struct rbimpl_size_mul_overflow_tag v = size_add_overflow(t.right, u.right);
536 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
539PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
542size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
544 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
545 if (LIKELY(!t.left)) {
554 "integer overflow: %"PRIuSIZE
557 x, y, (
size_t)SIZE_MAX);
562rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
564 return size_mul_or_raise(x, y, exc);
568size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
570 struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
571 if (LIKELY(!t.left)) {
580 "integer overflow: %"PRIuSIZE
584 x, y, z, (
size_t)SIZE_MAX);
589rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
591 return size_mul_add_or_raise(x, y, z, exc);
595size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
597 struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
598 if (LIKELY(!t.left)) {
607 "integer overflow: %"PRIdSIZE
612 x, y, z, w, (
size_t)SIZE_MAX);
616#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
618volatile VALUE rb_gc_guarded_val;
620rb_gc_guarded_ptr_val(
volatile VALUE *ptr,
VALUE val)
622 rb_gc_guarded_val = val;
628static const char *obj_type_name(
VALUE obj);
629#include "gc/default/default.c"
631#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
632# error "Modular GC requires dlopen"
636typedef struct gc_function_map {
638 void *(*objspace_alloc)(void);
639 void (*objspace_init)(
void *objspace_ptr);
640 void (*objspace_free)(
void *objspace_ptr);
641 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
642 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
643 void (*set_params)(
void *objspace_ptr);
645 size_t *(*heap_sizes)(
void *objspace_ptr);
647 void (*shutdown_free_objects)(
void *objspace_ptr);
649 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
650 bool (*during_gc_p)(
void *objspace_ptr);
651 void (*prepare_heap)(
void *objspace_ptr);
652 void (*gc_enable)(
void *objspace_ptr);
653 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
654 bool (*gc_enabled_p)(
void *objspace_ptr);
655 VALUE (*config_get)(
void *objpace_ptr);
656 void (*config_set)(
void *objspace_ptr,
VALUE hash);
657 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
658 VALUE (*stress_get)(
void *objspace_ptr);
660 VALUE (*new_obj)(
void *objspace_ptr,
void *cache_ptr,
VALUE klass,
VALUE flags,
VALUE v1,
VALUE v2,
VALUE v3,
bool wb_protected,
size_t alloc_size);
661 size_t (*obj_slot_size)(
VALUE obj);
662 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
663 bool (*size_allocatable_p)(
size_t size);
665 void *(*malloc)(
void *objspace_ptr,
size_t size);
666 void *(*calloc)(
void *objspace_ptr,
size_t size);
667 void *(*realloc)(
void *objspace_ptr,
void *
ptr,
size_t new_size,
size_t old_size);
668 void (*free)(
void *objspace_ptr,
void *
ptr,
size_t old_size);
669 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
671 void (*mark)(
void *objspace_ptr,
VALUE obj);
672 void (*mark_and_move)(
void *objspace_ptr,
VALUE *
ptr);
673 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
674 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
675 void (*mark_weak)(
void *objspace_ptr,
VALUE *
ptr);
676 void (*remove_weak)(
void *objspace_ptr,
VALUE parent_obj,
VALUE *
ptr);
678 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
679 VALUE (*location)(
void *objspace_ptr,
VALUE value);
681 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
682 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
683 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
685 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
686 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
688 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
690 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
691 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
692 void (*shutdown_call_finalizer)(
void *objspace_ptr);
694 VALUE (*object_id)(
void *objspace_ptr,
VALUE obj);
695 VALUE (*object_id_to_ref)(
void *objspace_ptr,
VALUE object_id);
697 void (*before_fork)(
void *objspace_ptr);
698 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
700 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
701 bool (*get_measure_total_time)(
void *objspace_ptr);
702 unsigned long long (*get_total_time)(
void *objspace_ptr);
703 size_t (*gc_count)(
void *objspace_ptr);
704 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
705 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
706 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
707 const char *(*active_gc_name)(void);
709 size_t (*obj_flags)(
void *objspace_ptr,
VALUE obj,
ID* flags,
size_t max);
710 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *
ptr);
711 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
712 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
713 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
715 bool modular_gc_loaded_p;
716} rb_gc_function_map_t;
718static rb_gc_function_map_t rb_gc_functions;
720# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
721# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
724ruby_modular_gc_init(
void)
729 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
731 rb_gc_function_map_t gc_functions = { 0 };
733 char *gc_so_path = NULL;
738 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
739 char c = gc_so_file[i];
740 if (isalnum(c))
continue;
746 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
751 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
754 size_t prefix_len = 0;
755 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
756 const char *base = strrchr(dli.dli_fname,
'/');
759# define end_with_p(lit) \
760 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
761 memcmp(base - tail, lit, tail) == 0)
763 prefix_len = base - dli.dli_fname;
764 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
767 prefix_len += MODULAR_GC_DIR[0] !=
'/';
768 gc_so_path_size += prefix_len;
772 gc_so_path = alloca(gc_so_path_size);
774 size_t gc_so_path_idx = 0;
775#define GC_SO_PATH_APPEND(str) do { \
776 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
779 if (prefix_len > 0) {
780 memcpy(gc_so_path, dli.dli_fname, prefix_len);
781 gc_so_path_idx = prefix_len;
784 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
785 GC_SO_PATH_APPEND(gc_so_file);
786 GC_SO_PATH_APPEND(DLEXT);
787 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
788#undef GC_SO_PATH_APPEND
791 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
793 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
797 gc_functions.modular_gc_loaded_p =
true;
800# define load_modular_gc_func(name) do { \
802 const char *func_name = "rb_gc_impl_" #name; \
803 gc_functions.name = dlsym(handle, func_name); \
804 if (!gc_functions.name) { \
805 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
810 gc_functions.name = rb_gc_impl_##name; \
815 load_modular_gc_func(objspace_alloc);
816 load_modular_gc_func(objspace_init);
817 load_modular_gc_func(objspace_free);
818 load_modular_gc_func(ractor_cache_alloc);
819 load_modular_gc_func(ractor_cache_free);
820 load_modular_gc_func(set_params);
821 load_modular_gc_func(init);
822 load_modular_gc_func(heap_sizes);
824 load_modular_gc_func(shutdown_free_objects);
826 load_modular_gc_func(start);
827 load_modular_gc_func(during_gc_p);
828 load_modular_gc_func(prepare_heap);
829 load_modular_gc_func(gc_enable);
830 load_modular_gc_func(gc_disable);
831 load_modular_gc_func(gc_enabled_p);
832 load_modular_gc_func(config_set);
833 load_modular_gc_func(config_get);
834 load_modular_gc_func(stress_set);
835 load_modular_gc_func(stress_get);
837 load_modular_gc_func(new_obj);
838 load_modular_gc_func(obj_slot_size);
839 load_modular_gc_func(heap_id_for_size);
840 load_modular_gc_func(size_allocatable_p);
842 load_modular_gc_func(malloc);
843 load_modular_gc_func(calloc);
844 load_modular_gc_func(realloc);
845 load_modular_gc_func(free);
846 load_modular_gc_func(adjust_memory_usage);
848 load_modular_gc_func(mark);
849 load_modular_gc_func(mark_and_move);
850 load_modular_gc_func(mark_and_pin);
851 load_modular_gc_func(mark_maybe);
852 load_modular_gc_func(mark_weak);
853 load_modular_gc_func(remove_weak);
855 load_modular_gc_func(object_moved_p);
856 load_modular_gc_func(location);
858 load_modular_gc_func(writebarrier);
859 load_modular_gc_func(writebarrier_unprotect);
860 load_modular_gc_func(writebarrier_remember);
862 load_modular_gc_func(each_objects);
863 load_modular_gc_func(each_object);
865 load_modular_gc_func(make_zombie);
866 load_modular_gc_func(define_finalizer);
867 load_modular_gc_func(undefine_finalizer);
868 load_modular_gc_func(copy_finalizer);
869 load_modular_gc_func(shutdown_call_finalizer);
871 load_modular_gc_func(object_id);
872 load_modular_gc_func(object_id_to_ref);
874 load_modular_gc_func(before_fork);
875 load_modular_gc_func(after_fork);
877 load_modular_gc_func(set_measure_total_time);
878 load_modular_gc_func(get_measure_total_time);
879 load_modular_gc_func(get_total_time);
880 load_modular_gc_func(gc_count);
881 load_modular_gc_func(latest_gc_info);
882 load_modular_gc_func(stat);
883 load_modular_gc_func(stat_heap);
884 load_modular_gc_func(active_gc_name);
886 load_modular_gc_func(obj_flags);
887 load_modular_gc_func(pointer_to_heap_p);
888 load_modular_gc_func(garbage_object_p);
889 load_modular_gc_func(set_event_hook);
890 load_modular_gc_func(copy_attributes);
892# undef load_modular_gc_func
894 rb_gc_functions = gc_functions;
898# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
899# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
900# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
901# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
902# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
903# define rb_gc_impl_set_params rb_gc_functions.set_params
904# define rb_gc_impl_init rb_gc_functions.init
905# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
907# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
909# define rb_gc_impl_start rb_gc_functions.start
910# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
911# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
912# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
913# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
914# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
915# define rb_gc_impl_config_get rb_gc_functions.config_get
916# define rb_gc_impl_config_set rb_gc_functions.config_set
917# define rb_gc_impl_stress_set rb_gc_functions.stress_set
918# define rb_gc_impl_stress_get rb_gc_functions.stress_get
920# define rb_gc_impl_new_obj rb_gc_functions.new_obj
921# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
922# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
923# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
925# define rb_gc_impl_malloc rb_gc_functions.malloc
926# define rb_gc_impl_calloc rb_gc_functions.calloc
927# define rb_gc_impl_realloc rb_gc_functions.realloc
928# define rb_gc_impl_free rb_gc_functions.free
929# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
931# define rb_gc_impl_mark rb_gc_functions.mark
932# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
933# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
934# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
935# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
936# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
938# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
939# define rb_gc_impl_location rb_gc_functions.location
941# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
942# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
943# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
945# define rb_gc_impl_each_objects rb_gc_functions.each_objects
946# define rb_gc_impl_each_object rb_gc_functions.each_object
948# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
949# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
950# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
951# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
952# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
954# define rb_gc_impl_object_id rb_gc_functions.object_id
955# define rb_gc_impl_object_id_to_ref rb_gc_functions.object_id_to_ref
957# define rb_gc_impl_before_fork rb_gc_functions.before_fork
958# define rb_gc_impl_after_fork rb_gc_functions.after_fork
960# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
961# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
962# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
963# define rb_gc_impl_gc_count rb_gc_functions.gc_count
964# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
965# define rb_gc_impl_stat rb_gc_functions.stat
966# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
967# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
969# define rb_gc_impl_obj_flags rb_gc_functions.obj_flags
970# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
971# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
972# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
973# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
976#ifdef RUBY_ASAN_ENABLED
978asan_death_callback(
void)
981 rb_bug_without_die(
"ASAN error");
989rb_objspace_alloc(
void)
992 ruby_modular_gc_init();
995 void *
objspace = rb_gc_impl_objspace_alloc();
996 ruby_current_vm_ptr->gc.objspace =
objspace;
999 rb_gc_impl_stress_set(
objspace, initial_stress);
1001#ifdef RUBY_ASAN_ENABLED
1002 __sanitizer_set_death_callback(asan_death_callback);
1011 rb_gc_impl_objspace_free(
objspace);
1015rb_gc_obj_slot_size(
VALUE obj)
1017 return rb_gc_impl_obj_slot_size(obj);
1021gc_validate_pc(
void) {
1023 rb_execution_context_t *ec = GET_EC();
1024 const rb_control_frame_t *cfp = ec->cfp;
1025 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc) {
1026 RUBY_ASSERT(cfp->pc >= ISEQ_BODY(cfp->iseq)->iseq_encoded);
1027 RUBY_ASSERT(cfp->pc <= ISEQ_BODY(cfp->iseq)->iseq_encoded + ISEQ_BODY(cfp->iseq)->iseq_size);
1035 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
1041 RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
1043 memset((
char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
1050 bool gc_disabled =
RTEST(rb_gc_disable_no_rest());
1056 RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
1063rb_wb_unprotected_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
1066 return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
1070rb_wb_protected_newobj_of(rb_execution_context_t *ec,
VALUE klass,
VALUE flags,
size_t size)
1073 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
1076#define UNEXPECTED_NODE(func) \
1077 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1078 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1081rb_data_object_check(
VALUE klass)
1083 if (klass != rb_cObject && (
rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
1085 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1093 if (klass) rb_data_object_check(klass);
1100 VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
1108 RBIMPL_NONNULL_ARG(
type);
1109 if (klass) rb_data_object_check(klass);
1111 return newobj_of(GET_RACTOR(), klass,
T_DATA, (
VALUE)
type, 1 | typed_flag, (
VALUE)datap, wb_protected, size);
1117 if (UNLIKELY(
type->flags & RUBY_TYPED_EMBEDDABLE)) {
1118 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1121 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
1127 if (
type->flags & RUBY_TYPED_EMBEDDABLE) {
1128 if (!(
type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1129 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1132 size_t embed_size = offsetof(
struct RTypedData, data) + size;
1133 if (rb_gc_size_allocatable_p(embed_size)) {
1134 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1135 memset((
char *)obj + offsetof(
struct RTypedData, data), 0, size);
1146rb_objspace_data_type_memsize(
VALUE obj)
1151 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1153 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1154#ifdef HAVE_MALLOC_USABLE_SIZE
1155 size += malloc_usable_size((
void *)ptr);
1159 if (ptr &&
type->function.dsize) {
1160 size +=
type->function.dsize(ptr);
1168rb_objspace_data_type_name(
VALUE obj)
1178static enum rb_id_table_iterator_result
1179cvar_table_free_i(
VALUE value,
void *ctx)
1181 xfree((
void *)value);
1182 return ID_TABLE_CONTINUE;
1188 rb_io_t *fptr =
RFILE(obj)->fptr;
1189 rb_gc_impl_make_zombie(
objspace, obj, rb_io_fptr_finalize_internal, fptr);
1197 int free_immediately =
false;
1198 void (*dfree)(
void *);
1201 free_immediately = (
RTYPEDDATA(obj)->type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1202 dfree =
RTYPEDDATA(obj)->type->function.dfree;
1205 dfree =
RDATA(obj)->dfree;
1210 if (!
RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1212 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1215 else if (free_immediately) {
1217 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1221 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1224 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, dfree, data);
1225 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1230 RB_DEBUG_COUNTER_INC(obj_data_empty);
1238rb_gc_obj_free_vm_weak_references(
VALUE obj)
1247 if (
FL_TEST(obj, RSTRING_FSTR)) {
1248 st_data_t fstr = (st_data_t)obj;
1249 st_delete(rb_vm_fstring_table(), &fstr, NULL);
1250 RB_DEBUG_COUNTER_INC(obj_str_fstr);
1256 rb_gc_free_dsymbol(obj);
1259 switch (imemo_type(obj)) {
1260 case imemo_callinfo:
1264 rb_free_method_entry_vm_weak_references((
const rb_method_entry_t *)obj);
1278 RB_DEBUG_COUNTER_INC(obj_free);
1285 rb_bug(
"obj_free() called for broken object");
1293 if (rb_shape_obj_too_complex(obj)) {
1294 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1295 st_free_table(ROBJECT_IV_HASH(obj));
1297 else if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1298 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1302 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1307 rb_id_table_free(RCLASS_M_TBL(obj));
1308 rb_cc_table_free(obj);
1309 if (rb_shape_obj_too_complex(obj)) {
1310 st_free_table((st_table *)RCLASS_IVPTR(obj));
1313 xfree(RCLASS_IVPTR(obj));
1316 if (RCLASS_CONST_TBL(obj)) {
1317 rb_free_const_table(RCLASS_CONST_TBL(obj));
1319 if (RCLASS_CVC_TBL(obj)) {
1320 rb_id_table_foreach_values(RCLASS_CVC_TBL(obj), cvar_table_free_i, NULL);
1321 rb_id_table_free(RCLASS_CVC_TBL(obj));
1323 rb_class_remove_subclass_head(obj);
1324 rb_class_remove_from_module_subclasses(obj);
1325 rb_class_remove_from_super_subclasses(obj);
1326 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1327 xfree(RCLASS_SUPERCLASSES(obj));
1340#if USE_DEBUG_COUNTER
1343 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1346 RB_DEBUG_COUNTER_INC(obj_hash_1);
1349 RB_DEBUG_COUNTER_INC(obj_hash_2);
1352 RB_DEBUG_COUNTER_INC(obj_hash_3);
1355 RB_DEBUG_COUNTER_INC(obj_hash_4);
1361 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1365 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1368 if (RHASH_AR_TABLE_P(obj)) {
1369 if (RHASH_AR_TABLE(obj) == NULL) {
1370 RB_DEBUG_COUNTER_INC(obj_hash_null);
1373 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1377 RB_DEBUG_COUNTER_INC(obj_hash_st);
1386 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1390 if (!rb_data_free(
objspace, obj))
return false;
1394 rb_matchext_t *rm = RMATCH_EXT(obj);
1395#if USE_DEBUG_COUNTER
1396 if (rm->
regs.num_regs >= 8) {
1397 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1399 else if (rm->
regs.num_regs >= 4) {
1400 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1402 else if (rm->
regs.num_regs >= 1) {
1403 RB_DEBUG_COUNTER_INC(obj_match_under4);
1406 onig_region_free(&rm->
regs, 0);
1409 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1413 if (
RFILE(obj)->fptr) {
1415 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1420 RB_DEBUG_COUNTER_INC(obj_rational);
1423 RB_DEBUG_COUNTER_INC(obj_complex);
1429 if (RICLASS_OWNS_M_TBL_P(obj)) {
1431 rb_id_table_free(RCLASS_M_TBL(obj));
1433 if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
1434 rb_id_table_free(RCLASS_CALLABLE_M_TBL(obj));
1436 rb_class_remove_subclass_head(obj);
1437 rb_cc_table_free(obj);
1438 rb_class_remove_from_module_subclasses(obj);
1439 rb_class_remove_from_super_subclasses(obj);
1441 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1445 RB_DEBUG_COUNTER_INC(obj_float);
1449 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1450 xfree(BIGNUM_DIGITS(obj));
1451 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1454 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1459 UNEXPECTED_NODE(obj_free);
1463 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1464 RSTRUCT(obj)->as.heap.ptr == NULL) {
1465 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1468 xfree((
void *)RSTRUCT(obj)->as.heap.ptr);
1469 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1474 RB_DEBUG_COUNTER_INC(obj_symbol);
1478 rb_imemo_free((
VALUE)obj);
1482 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1487 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, 0, 0);
1498 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1502internal_object_p(
VALUE obj)
1504 void *ptr = asan_unpoison_object_temporary(obj);
1506 if (
RBASIC(obj)->flags) {
1509 UNEXPECTED_NODE(internal_object_p);
1518 if (!
RBASIC(obj)->klass)
break;
1519 if (RCLASS_SINGLETON_P(obj)) {
1520 return rb_singleton_class_internal_p(obj);
1524 if (!
RBASIC(obj)->klass)
break;
1528 if (ptr || !
RBASIC(obj)->flags) {
1529 rb_asan_poison_object(obj);
1535rb_objspace_internal_object_p(
VALUE obj)
1537 return internal_object_p(obj);
1546os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1551 for (; v != (
VALUE)vend; v += stride) {
1552 if (!internal_object_p(v)) {
1572 rb_objspace_each_objects(os_obj_of_i, &oes);
1619 return os_obj_of(of);
1633 return rb_undefine_finalizer(obj);
1639 rb_check_frozen(obj);
1641 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1647should_be_callable(
VALUE block)
1650 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
1656should_be_finalizable(
VALUE obj)
1659 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
1662 rb_check_frozen(obj);
1668 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1743 if (rb_callable_receiver(block) == obj) {
1744 rb_warn(
"finalizer references object to be finalized");
1747 return rb_define_finalizer(obj, block);
1753 should_be_finalizable(obj);
1754 should_be_callable(block);
1756 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1764rb_objspace_call_finalizer(
void)
1766 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1770rb_objspace_free_objects(
void *
objspace)
1772 rb_gc_impl_shutdown_free_objects(
objspace);
1776rb_objspace_garbage_object_p(
VALUE obj)
1778 return rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1782rb_gc_pointer_to_heap_p(
VALUE obj)
1784 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj);
1805#if SIZEOF_LONG == SIZEOF_VOIDP
1806#define NUM2PTR(x) NUM2ULONG(x)
1807#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1808#define NUM2PTR(x) NUM2ULL(x)
1811 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
1812 VALUE ptr = NUM2PTR(objid);
1822 if (rb_static_id_valid_p(
SYM2ID(ptr))) {
1826 rb_raise(
rb_eRangeError,
"%p is not symbol id value", (
void *)ptr);
1830 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is not id value", rb_int2str(objid, 10));
1834 VALUE obj = rb_gc_impl_object_id_to_ref(rb_gc_get_objspace(), objid);
1839 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is id of the unshareable object on multi-ractor", rb_int2str(objid, 10));
1847 return id2ref(objid);
1854#if SIZEOF_LONG == SIZEOF_VOIDP
1861 return get_heap_object_id(
objspace, obj);
1865nonspecial_obj_id(
void *_objspace,
VALUE obj)
1867#if SIZEOF_LONG == SIZEOF_VOIDP
1869#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1872# error not supported
1879 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
1916 return rb_find_object_id(rb_gc_get_objspace(), obj, rb_gc_impl_object_id);
1919static enum rb_id_table_iterator_result
1920cc_table_memsize_i(
VALUE ccs_ptr,
void *data_ptr)
1922 size_t *total_size = data_ptr;
1924 *total_size +=
sizeof(*ccs);
1925 *total_size +=
sizeof(ccs->entries[0]) * ccs->capa;
1926 return ID_TABLE_CONTINUE;
1932 size_t total = rb_id_table_memsize(cc_table);
1933 rb_id_table_foreach_values(cc_table, cc_table_memsize_i, &total);
1938rb_obj_memsize_of(
VALUE obj)
1947 size += rb_generic_ivar_memsize(obj);
1952 if (rb_shape_obj_too_complex(obj)) {
1953 size += rb_st_memsize(ROBJECT_IV_HASH(obj));
1955 else if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1956 size += ROBJECT_IV_CAPACITY(obj) *
sizeof(
VALUE);
1961 if (RCLASS_M_TBL(obj)) {
1962 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1965 size +=
SIZEOF_VALUE << bit_length(RCLASS_IV_COUNT(obj));
1966 if (RCLASS_CVC_TBL(obj)) {
1967 size += rb_id_table_memsize(RCLASS_CVC_TBL(obj));
1969 if (RCLASS_EXT(obj)->const_tbl) {
1970 size += rb_id_table_memsize(RCLASS_EXT(obj)->const_tbl);
1972 if (RCLASS_CC_TBL(obj)) {
1973 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1975 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1976 size += (RCLASS_SUPERCLASS_DEPTH(obj) + 1) *
sizeof(
VALUE);
1980 if (RICLASS_OWNS_M_TBL_P(obj)) {
1981 if (RCLASS_M_TBL(obj)) {
1982 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1985 if (RCLASS_CC_TBL(obj)) {
1986 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1990 size += rb_str_memsize(obj);
1993 size += rb_ary_memsize(obj);
1996 if (RHASH_ST_TABLE_P(obj)) {
1997 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
1999 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(st_table);
2008 size += rb_objspace_data_type_memsize(obj);
2012 rb_matchext_t *rm = RMATCH_EXT(obj);
2013 size += onig_region_memsize(&rm->
regs);
2018 if (
RFILE(obj)->fptr) {
2019 size += rb_io_memsize(
RFILE(obj)->fptr);
2026 size += rb_imemo_memsize(obj);
2034 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2035 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
2040 UNEXPECTED_NODE(obj_memsize_of);
2044 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
2045 RSTRUCT(obj)->as.heap.ptr) {
2046 size +=
sizeof(
VALUE) * RSTRUCT_LEN(obj);
2055 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
2059 return size + rb_gc_obj_slot_size(obj);
2063set_zero(st_data_t key, st_data_t val, st_data_t arg)
2067 rb_hash_aset(hash, k,
INT2FIX(0));
2078count_objects_i(
VALUE obj,
void *d)
2082 if (
RBASIC(obj)->flags) {
2128count_objects(
int argc,
VALUE *argv,
VALUE os)
2139 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2145 rb_hash_stlike_foreach(hash, set_zero, hash);
2147 rb_hash_aset(hash,
ID2SYM(rb_intern(
"TOTAL")),
SIZET2NUM(data.total));
2150 for (
size_t i = 0; i <=
T_MASK; i++) {
2159#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2161#define STACK_START (ec->machine.stack_start)
2162#define STACK_END (ec->machine.stack_end)
2163#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2165#if STACK_GROW_DIRECTION < 0
2166# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2167#elif STACK_GROW_DIRECTION > 0
2168# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2170# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2171 : (size_t)(STACK_END - STACK_START + 1))
2173#if !STACK_GROW_DIRECTION
2174int ruby_stack_grow_direction;
2176ruby_get_stack_grow_direction(
volatile VALUE *addr)
2179 SET_MACHINE_STACK_END(&end);
2181 if (end > addr)
return ruby_stack_grow_direction = 1;
2182 return ruby_stack_grow_direction = -1;
2189 rb_execution_context_t *ec = GET_EC();
2191 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2192 return STACK_LENGTH;
2195#define PREVENT_STACK_OVERFLOW 1
2196#ifndef PREVENT_STACK_OVERFLOW
2197#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2198# define PREVENT_STACK_OVERFLOW 1
2200# define PREVENT_STACK_OVERFLOW 0
2203#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2205stack_check(rb_execution_context_t *ec,
int water_mark)
2209 size_t length = STACK_LENGTH;
2210 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2212 return length > maximum_length;
2215#define stack_check(ec, water_mark) FALSE
2218#define STACKFRAME_FOR_CALL_CFUNC 2048
2221rb_ec_stack_check(rb_execution_context_t *ec)
2223 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2229 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2234#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2235 if (!RB_SPECIAL_CONST_P(obj)) { \
2236 rb_vm_t *vm = GET_VM(); \
2237 void *objspace = vm->gc.objspace; \
2238 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2239 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2240 (func)(objspace, (obj_or_ptr)); \
2242 else if (check_obj ? \
2243 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2244 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2246 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2247 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2248 vm->gc.mark_func_data = NULL; \
2249 mark_func_data->mark_func((obj), mark_func_data->data); \
2250 vm->gc.mark_func_data = mark_func_data; \
2256gc_mark_internal(
VALUE obj)
2258 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2264 gc_mark_internal(obj);
2268rb_gc_mark_and_move(
VALUE *ptr)
2270 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr,
false);
2274gc_mark_and_pin_internal(
VALUE obj)
2276 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2282 gc_mark_and_pin_internal(obj);
2286gc_mark_maybe_internal(
VALUE obj)
2288 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2294 gc_mark_maybe_internal(obj);
2298rb_gc_mark_weak(
VALUE *ptr)
2302 rb_vm_t *vm = GET_VM();
2304 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2305 GC_ASSERT(rb_gc_impl_during_gc_p(
objspace));
2307 rb_gc_impl_mark_weak(
objspace, ptr);
2310 GC_ASSERT(!rb_gc_impl_during_gc_p(
objspace));
2315rb_gc_remove_weak(
VALUE parent_obj,
VALUE *ptr)
2317 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj, ptr);
2320ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2322each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2333each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2335 if (end <= start)
return;
2336 each_location(start, end - start, cb, data);
2340gc_mark_maybe_each_location(
VALUE obj,
void *data)
2342 gc_mark_maybe_internal(obj);
2348 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2352rb_gc_mark_values(
long n,
const VALUE *values)
2354 for (
long i = 0; i < n; i++) {
2355 gc_mark_internal(values[i]);
2360rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
2362 for (
long i = 0; i < n; i++) {
2363 gc_mark_and_pin_internal(values[i]);
2368mark_key(st_data_t key, st_data_t value, st_data_t data)
2370 gc_mark_and_pin_internal((
VALUE)key);
2376rb_mark_set(st_table *tbl)
2380 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2384mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2386 gc_mark_internal((
VALUE)key);
2387 gc_mark_internal((
VALUE)value);
2393pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2395 gc_mark_and_pin_internal((
VALUE)key);
2396 gc_mark_and_pin_internal((
VALUE)value);
2402pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2404 gc_mark_and_pin_internal((
VALUE)key);
2405 gc_mark_internal((
VALUE)value);
2411mark_hash(
VALUE hash)
2413 if (rb_hash_compare_by_id_p(hash)) {
2414 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2417 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2420 gc_mark_internal(RHASH(hash)->ifnone);
2424rb_mark_hash(st_table *tbl)
2431static enum rb_id_table_iterator_result
2434 gc_mark_internal(me);
2436 return ID_TABLE_CONTINUE;
2443 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
2447#if STACK_GROW_DIRECTION < 0
2448#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2449#elif STACK_GROW_DIRECTION > 0
2450#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2452#define GET_STACK_BOUNDS(start, end, appendix) \
2453 ((STACK_END < STACK_START) ? \
2454 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2458gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
2460 gc_mark_maybe_internal(obj);
2462#ifdef RUBY_ASAN_ENABLED
2463 const rb_execution_context_t *ec = (
const rb_execution_context_t *)data;
2464 void *fake_frame_start;
2465 void *fake_frame_end;
2466 bool is_fake_frame = asan_get_fake_stack_extents(
2467 ec->machine.asan_fake_stack_handle, obj,
2468 ec->machine.stack_start, ec->machine.stack_end,
2469 &fake_frame_start, &fake_frame_end
2471 if (is_fake_frame) {
2472 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2484 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)value));
2486 return rb_gc_impl_location(
objspace, value);
2492 return gc_location_internal(rb_gc_get_objspace(), value);
2495#if defined(__wasm__)
2498static VALUE *rb_stack_range_tmp[2];
2501rb_mark_locations(
void *begin,
void *end)
2503 rb_stack_range_tmp[0] = begin;
2504 rb_stack_range_tmp[1] = end;
2508rb_gc_save_machine_context(
void)
2513# if defined(__EMSCRIPTEN__)
2516mark_current_machine_context(
const rb_execution_context_t *ec)
2518 emscripten_scan_stack(rb_mark_locations);
2519 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2521 emscripten_scan_registers(rb_mark_locations);
2522 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2527mark_current_machine_context(
const rb_execution_context_t *ec)
2529 VALUE *stack_start, *stack_end;
2531 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2532 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2534 rb_wasm_scan_locals(rb_mark_locations);
2535 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2543rb_gc_save_machine_context(
void)
2545 rb_thread_t *thread = GET_THREAD();
2547 RB_VM_SAVE_MACHINE_CONTEXT(thread);
2552mark_current_machine_context(
const rb_execution_context_t *ec)
2554 rb_gc_mark_machine_context(ec);
2559rb_gc_mark_machine_context(
const rb_execution_context_t *ec)
2561 VALUE *stack_start, *stack_end;
2563 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2564 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2567#ifdef RUBY_ASAN_ENABLED
2569 (rb_execution_context_t *)ec;
2574 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2575 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
2576 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2580rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2582 gc_mark_and_pin_internal((
VALUE)value);
2588rb_mark_tbl(st_table *tbl)
2590 if (!tbl || tbl->num_entries == 0)
return;
2596gc_mark_tbl_no_pin(st_table *tbl)
2598 if (!tbl || tbl->num_entries == 0)
return;
2604rb_mark_tbl_no_pin(st_table *tbl)
2606 gc_mark_tbl_no_pin(tbl);
2609static enum rb_id_table_iterator_result
2617 gc_mark_internal((
VALUE)entry->cref);
2619 return ID_TABLE_CONTINUE;
2627 rb_id_table_foreach_values(tbl, mark_cvc_tbl_i,
objspace);
2634 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
2637static enum rb_id_table_iterator_result
2640 const rb_const_entry_t *ce = (
const rb_const_entry_t *)value;
2642 gc_mark_internal(ce->value);
2643 gc_mark_internal(ce->file);
2645 return ID_TABLE_CONTINUE;
2649rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
2651 rb_execution_context_t *ec = GET_EC();
2652 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2654#define MARK_CHECKPOINT(category) do { \
2655 if (categoryp) *categoryp = category; \
2658 MARK_CHECKPOINT(
"vm");
2660 if (vm->self) gc_mark_internal(vm->self);
2662 MARK_CHECKPOINT(
"end_proc");
2665 MARK_CHECKPOINT(
"global_tbl");
2666 rb_gc_mark_global_tbl();
2669 void rb_yjit_root_mark(
void);
2671 if (rb_yjit_enabled_p) {
2672 MARK_CHECKPOINT(
"YJIT");
2673 rb_yjit_root_mark();
2677 MARK_CHECKPOINT(
"machine_context");
2678 mark_current_machine_context(ec);
2680 MARK_CHECKPOINT(
"finish");
2682#undef MARK_CHECKPOINT
2685#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA(d)->type->function.dmark
2691 rb_mark_generic_ivar(obj);
2706 rb_bug(
"rb_gc_mark() called for broken object");
2710 UNEXPECTED_NODE(rb_gc_mark);
2714 rb_imemo_mark_and_move(obj,
false);
2721 gc_mark_internal(
RBASIC(obj)->klass);
2726 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
2734 mark_m_tbl(
objspace, RCLASS_M_TBL(obj));
2736 rb_cc_table_mark(obj);
2737 if (rb_shape_obj_too_complex(obj)) {
2738 gc_mark_tbl_no_pin((st_table *)RCLASS_IVPTR(obj));
2741 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
2742 gc_mark_internal(RCLASS_IVPTR(obj)[i]);
2746 if (RCLASS_CONST_TBL(obj)) {
2747 rb_id_table_foreach_values(RCLASS_CONST_TBL(obj), mark_const_table_i,
objspace);
2750 gc_mark_internal(RCLASS_EXT(obj)->classpath);
2754 if (RICLASS_OWNS_M_TBL_P(obj)) {
2755 mark_m_tbl(
objspace, RCLASS_M_TBL(obj));
2761 if (RCLASS_INCLUDER(obj)) {
2762 gc_mark_internal(RCLASS_INCLUDER(obj));
2764 mark_m_tbl(
objspace, RCLASS_CALLABLE_M_TBL(obj));
2765 rb_cc_table_mark(obj);
2769 if (ARY_SHARED_P(obj)) {
2770 VALUE root = ARY_SHARED_ROOT(obj);
2771 gc_mark_internal(root);
2776 for (
long i = 0; i <
len; i++) {
2777 gc_mark_internal(ptr[i]);
2787 if (STR_SHARED_P(obj)) {
2788 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
2793 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
2796 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
2806 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
2808 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
2809 gc_mark_internal(*(
VALUE *)((
char *)ptr + offset));
2816 if (mark_func) (*mark_func)(ptr);
2824 rb_shape_t *shape = rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj));
2826 if (rb_shape_obj_too_complex(obj)) {
2827 gc_mark_tbl_no_pin(ROBJECT_IV_HASH(obj));
2832 uint32_t
len = ROBJECT_IV_COUNT(obj);
2833 for (uint32_t i = 0; i <
len; i++) {
2834 gc_mark_internal(ptr[i]);
2842 attr_index_t num_of_ivs = shape->next_iv_index;
2843 if (RCLASS_EXT(klass)->max_iv_count < num_of_ivs) {
2844 RCLASS_EXT(klass)->max_iv_count = num_of_ivs;
2852 if (
RFILE(obj)->fptr) {
2853 gc_mark_internal(
RFILE(obj)->fptr->self);
2854 gc_mark_internal(
RFILE(obj)->fptr->pathv);
2855 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
2856 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
2857 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
2858 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
2859 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
2860 gc_mark_internal(
RFILE(obj)->fptr->timeout);
2865 gc_mark_internal(
RREGEXP(obj)->src);
2869 gc_mark_internal(
RMATCH(obj)->regexp);
2871 gc_mark_internal(
RMATCH(obj)->str);
2876 gc_mark_internal(RRATIONAL(obj)->num);
2877 gc_mark_internal(RRATIONAL(obj)->den);
2881 gc_mark_internal(RCOMPLEX(obj)->real);
2882 gc_mark_internal(RCOMPLEX(obj)->imag);
2886 const long len = RSTRUCT_LEN(obj);
2887 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
2889 for (
long i = 0; i <
len; i++) {
2890 gc_mark_internal(ptr[i]);
2900 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
2902 rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj) ?
"corrupted object" :
"non object");
2907rb_gc_obj_optimal_size(
VALUE obj)
2911 return rb_ary_size_as_embedded(obj);
2914 if (rb_shape_obj_too_complex(obj)) {
2915 return sizeof(
struct RObject);
2922 return rb_str_size_as_embedded(obj);
2925 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));
2933rb_gc_writebarrier(VALUE a, VALUE b)
2935 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
2941 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
2948rb_gc_writebarrier_remember(
VALUE obj)
2950 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
2956 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
2960rb_gc_modular_gc_loaded_p(
void)
2963 return rb_gc_functions.modular_gc_loaded_p;
2970rb_gc_active_gc_name(
void)
2972 const char *gc_name = rb_gc_impl_active_gc_name();
2974 const size_t len = strlen(gc_name);
2975 if (
len > RB_GC_MAX_NAME_LEN) {
2976 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
2977 RB_GC_MAX_NAME_LEN,
len, gc_name);
2985rb_obj_gc_flags(
VALUE obj,
ID* flags,
size_t max)
2987 return rb_gc_impl_obj_flags(rb_gc_get_objspace(), obj, flags, max);
2993rb_gc_ractor_cache_alloc(rb_ractor_t *ractor)
2995 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
2999rb_gc_ractor_cache_free(
void *cache)
3001 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3007 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
3010 rb_vm_register_global_object(obj);
3016 rb_vm_t *vm = GET_VM();
3021 tmp->next = vm->global_object_list;
3023 vm->global_object_list = tmp;
3031 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
3033 rb_print_backtrace(stderr);
3040 rb_vm_t *vm = GET_VM();
3043 if (tmp->varptr == addr) {
3044 vm->global_object_list = tmp->next;
3049 if (tmp->next->varptr == addr) {
3052 tmp->next = tmp->next->next;
3063 rb_gc_register_address(var);
3067gc_start_internal(rb_execution_context_t *ec,
VALUE self,
VALUE full_mark,
VALUE immediate_mark,
VALUE immediate_sweep,
VALUE compact)
3069 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
3113rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
3115 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3121 if (ARY_SHARED_P(v)) {
3122 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
3126 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
3128 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3129 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
3130 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
3131 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
3139 for (
long i = 0; i <
len; i++) {
3144 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3145 if (rb_ary_embeddable_p(v)) {
3146 rb_ary_make_embedded(v);
3157 if (rb_shape_obj_too_complex(v)) {
3158 gc_ref_update_table_values_only(ROBJECT_IV_HASH(v));
3162 size_t slot_size = rb_gc_obj_slot_size(v);
3164 if (slot_size >= embed_size && !
RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
3166 memcpy(
ROBJECT(v)->as.ary, ptr,
sizeof(
VALUE) * ROBJECT_IV_COUNT(v));
3172 for (uint32_t i = 0; i < ROBJECT_IV_COUNT(v); i++) {
3178rb_gc_ref_update_table_values_only(st_table *tbl)
3180 gc_ref_update_table_values_only(tbl);
3185rb_gc_update_tbl_refs(st_table *ptr)
3187 gc_update_table_refs(ptr);
3193 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
3199 for (
long i = 0; i < n; i++) {
3200 UPDATE_IF_MOVED(
objspace, values[i]);
3205rb_gc_update_values(
long n,
VALUE *values)
3207 gc_update_values(rb_gc_get_objspace(), n, values);
3210static enum rb_id_table_iterator_result
3211check_id_table_move(
VALUE value,
void *data)
3216 return ID_TABLE_REPLACE;
3219 return ID_TABLE_CONTINUE;
3223rb_gc_prepare_heap_process_object(
VALUE obj)
3237rb_gc_prepare_heap(
void)
3239 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3243rb_gc_heap_id_for_size(
size_t size)
3245 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3249rb_gc_size_allocatable_p(
size_t size)
3251 return rb_gc_impl_size_allocatable_p(size);
3254static enum rb_id_table_iterator_result
3255update_id_table(
VALUE *value,
void *data,
int existing)
3263 return ID_TABLE_CONTINUE;
3270 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
3274static enum rb_id_table_iterator_result
3278 VM_ASSERT(vm_ccs_p(ccs));
3281 ccs->cme = (
const rb_callable_method_entry_t *)gc_location_internal(
objspace, (
VALUE)ccs->cme);
3284 for (
int i=0; i<ccs->len; i++) {
3285 if (rb_gc_impl_object_moved_p(
objspace, (
VALUE)ccs->entries[i].cc)) {
3291 return ID_TABLE_CONTINUE;
3299 rb_id_table_foreach_values(tbl, update_cc_tbl_i,
objspace);
3303static enum rb_id_table_iterator_result
3311 TYPED_UPDATE_IF_MOVED(
objspace, rb_cref_t *, entry->cref);
3314 entry->class_value = gc_location_internal(
objspace, entry->class_value);
3316 return ID_TABLE_CONTINUE;
3324 rb_id_table_foreach_values(tbl, update_cvc_tbl_i,
objspace);
3328static enum rb_id_table_iterator_result
3331 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3333 if (rb_gc_impl_object_moved_p(
objspace, ce->value)) {
3334 ce->value = gc_location_internal(
objspace, ce->value);
3337 if (rb_gc_impl_object_moved_p(
objspace, ce->file)) {
3338 ce->file = gc_location_internal(
objspace, ce->file);
3341 return ID_TABLE_CONTINUE;
3348 rb_id_table_foreach_values(tbl, update_const_table,
objspace);
3352update_subclass_entries(
void *
objspace, rb_subclass_entry_t *entry)
3355 UPDATE_IF_MOVED(
objspace, entry->klass);
3356 entry = entry->next;
3361update_class_ext(
void *
objspace, rb_classext_t *ext)
3363 UPDATE_IF_MOVED(
objspace, ext->origin_);
3364 UPDATE_IF_MOVED(
objspace, ext->includer);
3365 UPDATE_IF_MOVED(
objspace, ext->refined_class);
3366 update_subclass_entries(
objspace, ext->subclasses);
3372 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
3373 for (
size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj) + 1; i++) {
3374 UPDATE_IF_MOVED(
objspace, RCLASS_SUPERCLASSES(obj)[i]);
3380#define global_symbols ruby_global_symbols
3383struct global_vm_table_foreach_data {
3384 vm_table_foreach_callback_func callback;
3385 vm_table_update_callback_func update_callback;
3390vm_weak_table_foreach_key(st_data_t key, st_data_t value, st_data_t data,
int error)
3392 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3394 return iter_data->callback((
VALUE)key, iter_data->data);
3398vm_weak_table_foreach_update_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3400 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3402 return iter_data->update_callback((
VALUE *)key, iter_data->data);
3406vm_weak_table_str_sym_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3408 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3414 return iter_data->callback((
VALUE)value, iter_data->data);
3419vm_weak_table_foreach_update_value(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3421 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3423 return iter_data->update_callback((
VALUE *)value, iter_data->data);
3427vm_weak_table_gen_ivar_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3429 int retval = vm_weak_table_foreach_key(key, value, data, error);
3430 if (retval == ST_DELETE) {
3437vm_weak_table_frozen_strings_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3441 int retval = vm_weak_table_foreach_key(key, value, data, error);
3442 if (retval == ST_DELETE) {
3448struct st_table *rb_generic_ivtbl_get(
void);
3451rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
3452 vm_table_update_callback_func update_callback,
3454 enum rb_gc_vm_weak_tables table)
3456 rb_vm_t *vm = GET_VM();
3458 struct global_vm_table_foreach_data foreach_data = {
3459 .callback = callback,
3460 .update_callback = update_callback,
3465 case RB_GC_VM_CI_TABLE: {
3466 st_foreach_with_replace(
3468 vm_weak_table_foreach_key,
3469 vm_weak_table_foreach_update_key,
3470 (st_data_t)&foreach_data
3474 case RB_GC_VM_OVERLOADED_CME_TABLE: {
3475 st_foreach_with_replace(
3476 vm->overloaded_cme_table,
3477 vm_weak_table_foreach_key,
3478 vm_weak_table_foreach_update_key,
3479 (st_data_t)&foreach_data
3483 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
3484 st_foreach_with_replace(
3485 global_symbols.str_sym,
3486 vm_weak_table_str_sym_foreach,
3487 vm_weak_table_foreach_update_value,
3488 (st_data_t)&foreach_data
3492 case RB_GC_VM_GENERIC_IV_TABLE: {
3493 st_table *generic_iv_tbl = rb_generic_ivtbl_get();
3494 st_foreach_with_replace(
3496 vm_weak_table_gen_ivar_foreach,
3497 vm_weak_table_foreach_update_key,
3498 (st_data_t)&foreach_data
3502 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
3503 st_table *frozen_strings = GET_VM()->frozen_strings;
3504 st_foreach_with_replace(
3506 vm_weak_table_frozen_strings_foreach,
3507 vm_weak_table_foreach_update_key,
3508 (st_data_t)&foreach_data
3513 rb_bug(
"rb_gc_vm_weak_table_foreach: unknown table %d", table);
3519rb_gc_update_vm_references(
void *
objspace)
3521 rb_execution_context_t *ec = GET_EC();
3522 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3524 rb_vm_update_references(vm);
3525 rb_gc_update_global_tbl();
3526 global_symbols.ids = gc_location_internal(
objspace, global_symbols.ids);
3527 global_symbols.dsymbol_fstr_hash = gc_location_internal(
objspace, global_symbols.dsymbol_fstr_hash);
3528 gc_update_table_refs(global_symbols.str_sym);
3531 void rb_yjit_root_update_references(
void);
3533 if (rb_yjit_enabled_p) {
3534 rb_yjit_root_update_references();
3543 rb_ref_update_generic_ivar(obj);
3549 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
3556 update_m_tbl(
objspace, RCLASS_M_TBL(obj));
3559 update_superclasses(
objspace, obj);
3561 if (rb_shape_obj_too_complex(obj)) {
3562 gc_ref_update_table_values_only(RCLASS_IV_HASH(obj));
3565 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
3566 UPDATE_IF_MOVED(
objspace, RCLASS_IVPTR(obj)[i]);
3570 update_class_ext(
objspace, RCLASS_EXT(obj));
3571 update_const_tbl(
objspace, RCLASS_CONST_TBL(obj));
3573 UPDATE_IF_MOVED(
objspace, RCLASS_EXT(obj)->classpath);
3577 if (RICLASS_OWNS_M_TBL_P(obj)) {
3578 update_m_tbl(
objspace, RCLASS_M_TBL(obj));
3583 update_class_ext(
objspace, RCLASS_EXT(obj));
3584 update_m_tbl(
objspace, RCLASS_CALLABLE_M_TBL(obj));
3589 rb_imemo_mark_and_move(obj,
true);
3601 gc_ref_update_array(
objspace, obj);
3606 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
3611 if (STR_SHARED_P(obj)) {
3617 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
3618 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
3619 rb_str_make_embedded(obj);
3631 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3633 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3634 VALUE *ref = (
VALUE *)((
char *)ptr + offset);
3635 *ref = gc_location_internal(
objspace, *ref);
3640 if (compact_func) (*compact_func)(ptr);
3647 gc_ref_update_object(
objspace, obj);
3651 if (
RFILE(obj)->fptr) {
3654 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
3655 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
3656 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
3666 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
3682 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
3683 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
3687 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
3688 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
3694 long i,
len = RSTRUCT_LEN(obj);
3695 VALUE *ptr = (
VALUE *)RSTRUCT_CONST_PTR(obj);
3697 for (i = 0; i <
len; i++) {
3703 rb_bug(
"unreachable");
3720 unless_objspace(
objspace) {
return; }
3722 rb_gc_impl_start(
objspace,
true,
true,
true,
false);
3728 unless_objspace(
objspace) {
return FALSE; }
3730 return rb_gc_impl_during_gc_p(
objspace);
3736 return rb_gc_impl_gc_count(rb_gc_get_objspace());
3740gc_count(rb_execution_context_t *ec,
VALUE self)
3752 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
3755 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
3762gc_stat(rb_execution_context_t *ec,
VALUE self,
VALUE arg)
3771 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3776 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3789 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3794 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3806gc_stat_heap(rb_execution_context_t *ec,
VALUE self,
VALUE heap_name,
VALUE arg)
3812 if (
NIL_P(heap_name)) {
3823 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
3826 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
3831 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3838gc_config_get(rb_execution_context_t *ec,
VALUE self)
3840 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
3841 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
3847gc_config_set(rb_execution_context_t *ec,
VALUE self,
VALUE hash)
3849 void *
objspace = rb_gc_get_objspace();
3851 rb_gc_impl_config_set(
objspace, hash);
3853 return rb_gc_impl_config_get(
objspace);
3857gc_stress_get(rb_execution_context_t *ec,
VALUE self)
3859 return rb_gc_impl_stress_get(rb_gc_get_objspace());
3863gc_stress_set_m(rb_execution_context_t *ec,
VALUE self,
VALUE flag)
3865 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
3871rb_gc_initial_stress_set(
VALUE flag)
3873 initial_stress = flag;
3877rb_gc_heap_sizes(
void)
3879 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
3885 return rb_objspace_gc_enable(rb_gc_get_objspace());
3889rb_objspace_gc_enable(
void *
objspace)
3891 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3893 return RBOOL(disabled);
3897gc_enable(rb_execution_context_t *ec,
VALUE _)
3905 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3906 rb_gc_impl_gc_disable(
objspace,
false);
3907 return RBOOL(disabled);
3911rb_gc_disable_no_rest(
void)
3913 return gc_disable_no_rest(rb_gc_get_objspace());
3919 return rb_objspace_gc_disable(rb_gc_get_objspace());
3923rb_objspace_gc_disable(
void *
objspace)
3925 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3926 rb_gc_impl_gc_disable(
objspace,
true);
3927 return RBOOL(disabled);
3931gc_disable(rb_execution_context_t *ec,
VALUE _)
3938ruby_gc_set_params(
void)
3940 rb_gc_impl_set_params(rb_gc_get_objspace());
3944rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
3948 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
3951 rb_vm_t *vm = GET_VM();
3952 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3953 struct gc_mark_func_data_struct mfd = {
3958 vm->gc.mark_func_data = &mfd;
3959 rb_gc_mark_children(rb_gc_get_objspace(), obj);
3960 vm->gc.mark_func_data = prev_mfd;
3967 const char *category;
3968 void (*func)(
const char *category,
VALUE,
void *);
3973root_objects_from(
VALUE obj,
void *ptr)
3976 (*data->func)(data->category, obj, data->data);
3980rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
3982 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");
3984 rb_vm_t *vm = GET_VM();
3988 .data = passing_data,
3991 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3992 struct gc_mark_func_data_struct mfd = {
3993 .mark_func = root_objects_from,
3997 vm->gc.mark_func_data = &mfd;
3998 rb_gc_save_machine_context();
3999 rb_gc_mark_roots(vm->gc.objspace, &data.category);
4000 vm->gc.mark_func_data = prev_mfd;
4011#define TYPE_NAME(t) case (t): return #t;
4038 if (obj && rb_objspace_data_type_name(obj)) {
4039 return rb_objspace_data_type_name(obj);
4048obj_type_name(
VALUE obj)
4050 return type_name(
TYPE(obj), obj);
4054rb_method_type_name(rb_method_type_t
type)
4057 case VM_METHOD_TYPE_ISEQ:
return "iseq";
4058 case VM_METHOD_TYPE_ATTRSET:
return "attrest";
4059 case VM_METHOD_TYPE_IVAR:
return "ivar";
4060 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
4061 case VM_METHOD_TYPE_ALIAS:
return "alias";
4062 case VM_METHOD_TYPE_REFINED:
return "refined";
4063 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
4064 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
4065 case VM_METHOD_TYPE_MISSING:
return "missing";
4066 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
4067 case VM_METHOD_TYPE_UNDEF:
return "undef";
4068 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
4070 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
4074rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
4076 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
4077 VALUE path = rb_iseq_path(iseq);
4078 int n = ISEQ_BODY(iseq)->location.first_lineno;
4079 snprintf(buff, buff_size,
" %s@%s:%d",
4080 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4081 RSTRING_PTR(path), n);
4086str_len_no_raise(
VALUE str)
4088 long len = RSTRING_LEN(str);
4089 if (
len < 0)
return 0;
4090 if (
len > INT_MAX)
return INT_MAX;
4094#define BUFF_ARGS buff + pos, buff_size - pos
4095#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4096#define APPEND_S(s) do { \
4097 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4101 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4104#define C(c, s) ((c) != 0 ? (s) : " ")
4107rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
4112 APPEND_F(
"%s", obj_type_name(obj));
4118 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
4124 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
4143 if (internal_object_p(obj)) {
4146 else if (
RBASIC(obj)->klass == 0) {
4147 APPEND_S(
"(temporary internal)");
4151 if (!
NIL_P(class_path)) {
4152 APPEND_F(
"(%s)", RSTRING_PTR(class_path));
4161const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
4164rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
4171 UNEXPECTED_NODE(rb_raw_obj_info);
4174 if (ARY_SHARED_P(obj)) {
4175 APPEND_S(
"shared -> ");
4176 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4178 else if (ARY_EMBED_P(obj)) {
4179 APPEND_F(
"[%s%s] len: %ld (embed)",
4180 C(ARY_EMBED_P(obj),
"E"),
4181 C(ARY_SHARED_P(obj),
"S"),
4185 APPEND_F(
"[%s%s] len: %ld, capa:%ld ptr:%p",
4186 C(ARY_EMBED_P(obj),
"E"),
4187 C(ARY_SHARED_P(obj),
"S"),
4189 ARY_EMBED_P(obj) ? -1L :
RARRAY(obj)->as.heap.aux.capa,
4194 if (STR_SHARED_P(obj)) {
4195 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
4198 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
4200 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
4202 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
4206 VALUE fstr = RSYMBOL(obj)->fstr;
4207 ID id = RSYMBOL(obj)->id;
4209 APPEND_F(
":%s id:%d", RSTRING_PTR(fstr), (
unsigned int)
id);
4212 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
4217 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
4221 APPEND_F(
"[%c] %"PRIdSIZE,
4222 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
4230 if (!
NIL_P(class_path)) {
4231 APPEND_F(
"%s", RSTRING_PTR(class_path));
4241 if (!
NIL_P(class_path)) {
4242 APPEND_F(
"src:%s", RSTRING_PTR(class_path));
4248 if (rb_shape_obj_too_complex(obj)) {
4249 size_t hash_len = rb_st_table_size(ROBJECT_IV_HASH(obj));
4250 APPEND_F(
"(too_complex) len:%zu", hash_len);
4253 uint32_t
len = ROBJECT_IV_CAPACITY(obj);
4255 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
4256 APPEND_F(
"(embed) len:%d",
len);
4260 APPEND_F(
"len:%d ptr:%p",
len, (
void *)ptr);
4267 const rb_iseq_t *iseq;
4269 (block = vm_proc_block(obj)) != NULL &&
4270 (vm_block_type(block) == block_type_iseq) &&
4271 (iseq = vm_block_iseq(block)) != NULL) {
4272 rb_raw_iseq_info(BUFF_ARGS, iseq);
4274 else if (rb_ractor_p(obj)) {
4275 rb_ractor_t *r = (
void *)
DATA_PTR(obj);
4277 APPEND_F(
"r:%d", r->pub.id);
4281 const char *
const type_name = rb_objspace_data_type_name(obj);
4283 APPEND_F(
"%s", type_name);
4289 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
4291 switch (imemo_type(obj)) {
4294 const rb_method_entry_t *me = (
const rb_method_entry_t *)obj;
4296 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
4297 rb_id2name(me->called_id),
4298 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
4299 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
4300 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
4301 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
4302 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
4303 me->def ? rb_method_type_name(me->def->type) :
"NULL",
4304 me->def ? me->def->aliased : -1,
4306 (
void *)me->defined_class);
4309 switch (me->def->type) {
4310 case VM_METHOD_TYPE_ISEQ:
4311 APPEND_S(
" (iseq:");
4312 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
4323 const rb_iseq_t *iseq = (
const rb_iseq_t *)obj;
4324 rb_raw_iseq_info(BUFF_ARGS, iseq);
4327 case imemo_callinfo:
4330 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
4331 rb_id2name(vm_ci_mid(ci)),
4334 vm_ci_kwarg(ci) ?
"available" :
"NULL");
4337 case imemo_callcache:
4341 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4343 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
4344 NIL_P(class_path) ? (cc->klass ?
"??" :
"<NULL>") : RSTRING_PTR(class_path),
4345 cme ? rb_id2name(cme->called_id) :
"<NULL>",
4346 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
4348 (
void *)(uintptr_t)vm_cc_call(cc));
4367rb_asan_poison_object(
VALUE obj)
4369 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4370 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
4374rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
4376 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4377 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
4381rb_asan_poisoned_object_p(
VALUE obj)
4383 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4384 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
4387#define asan_unpoisoning_object(obj) \
4388 for (void *poisoned = asan_unpoison_object_temporary(obj), \
4389 *unpoisoning = &poisoned; \
4391 unpoisoning = asan_poison_object_restore(obj, poisoned))
4394rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
4396 asan_unpoisoning_object(obj) {
4397 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4398 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4399 if (pos >= buff_size) {}
4410#define OBJ_INFO_BUFFERS_NUM 10
4411#define OBJ_INFO_BUFFERS_SIZE 0x100
4413static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
4421 if (RB_UNLIKELY(oldval >= maxval - 1)) {
4432 rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
4433 char *
const buff = obj_info_buffers[index];
4434 return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
4440 return obj_type_name(obj);
4458 rb_vraise(argv->exc, argv->fmt, *argv->ap);
4463gc_raise(
VALUE exc,
const char *fmt, ...)
4471 if (ruby_thread_has_gvl_p()) {
4481 fprintf(stderr,
"%s",
"[FATAL] ");
4482 vfprintf(stderr, fmt, ap);
4489NORETURN(
static void negative_size_allocation_error(
const char *));
4491negative_size_allocation_error(
const char *msg)
4497ruby_memerror_body(
void *dummy)
4503NORETURN(
static void ruby_memerror(
void));
4508 if (ruby_thread_has_gvl_p()) {
4517 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4534 rb_execution_context_t *ec = GET_EC();
4535 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
4538 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
4539 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
4540 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4543 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4544 rb_ec_raised_clear(ec);
4547 rb_ec_raised_set(ec, RAISED_NOMEMORY);
4548 exc = ruby_vm_special_exception_copy(exc);
4551 EC_JUMP_TAG(ec, TAG_RAISE);
4555rb_memerror_reentered(
void)
4557 rb_execution_context_t *ec = GET_EC();
4558 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
4562rb_malloc_info_show_results(
void)
4567handle_malloc_failure(
void *ptr)
4578static void *ruby_xmalloc_body(
size_t size);
4581ruby_xmalloc(
size_t size)
4583 return handle_malloc_failure(ruby_xmalloc_body(size));
4587ruby_xmalloc_body(
size_t size)
4589 if ((ssize_t)size < 0) {
4590 negative_size_allocation_error(
"too large allocation size");
4593 return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
4597ruby_malloc_size_overflow(
size_t count,
size_t elsize)
4599 rb_raise(rb_eArgError,
4600 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
4604static void *ruby_xmalloc2_body(
size_t n,
size_t size);
4607ruby_xmalloc2(
size_t n,
size_t size)
4609 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
4613ruby_xmalloc2_body(
size_t n,
size_t size)
4615 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4618static void *ruby_xcalloc_body(
size_t n,
size_t size);
4621ruby_xcalloc(
size_t n,
size_t size)
4623 return handle_malloc_failure(ruby_xcalloc_body(n, size));
4627ruby_xcalloc_body(
size_t n,
size_t size)
4629 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4632static void *ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size);
4634#ifdef ruby_sized_xrealloc
4635#undef ruby_sized_xrealloc
4638ruby_sized_xrealloc(
void *ptr,
size_t new_size,
size_t old_size)
4640 return handle_malloc_failure(ruby_sized_xrealloc_body(ptr, new_size, old_size));
4644ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size)
4646 if ((ssize_t)new_size < 0) {
4647 negative_size_allocation_error(
"too large allocation size");
4650 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size);
4654ruby_xrealloc(
void *ptr,
size_t new_size)
4656 return ruby_sized_xrealloc(ptr, new_size, 0);
4659static void *ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n);
4661#ifdef ruby_sized_xrealloc2
4662#undef ruby_sized_xrealloc2
4665ruby_sized_xrealloc2(
void *ptr,
size_t n,
size_t size,
size_t old_n)
4667 return handle_malloc_failure(ruby_sized_xrealloc2_body(ptr, n, size, old_n));
4671ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n)
4673 size_t len = xmalloc2_size(n, size);
4674 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr,
len, old_n * size);
4678ruby_xrealloc2(
void *ptr,
size_t n,
size_t size)
4680 return ruby_sized_xrealloc2(ptr, n, size, 0);
4683#ifdef ruby_sized_xfree
4684#undef ruby_sized_xfree
4687ruby_sized_xfree(
void *x,
size_t size)
4693 if (LIKELY(GET_VM())) {
4694 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
4705 ruby_sized_xfree(x, 0);
4709rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
4711 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4712 return ruby_xmalloc(w);
4716rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
4718 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4719 return ruby_xcalloc(w, 1);
4723rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
4725 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4726 return ruby_xrealloc((
void *)p, w);
4730rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4732 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4733 return ruby_xmalloc(u);
4737rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4739 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4740 return ruby_xcalloc(u, 1);
4747ruby_mimmalloc(
size_t size)
4750#if CALC_EXACT_MALLOC_SIZE
4754#if CALC_EXACT_MALLOC_SIZE
4770ruby_mimcalloc(
size_t num,
size_t size)
4773#if CALC_EXACT_MALLOC_SIZE
4774 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
4775 if (UNLIKELY(t.left)) {
4779 mem = calloc1(size);
4791 mem = calloc(num, size);
4797ruby_mimfree(
void *ptr)
4799#if CALC_EXACT_MALLOC_SIZE
4807rb_gc_adjust_memory_usage(ssize_t diff)
4809 unless_objspace(
objspace) {
return; }
4811 rb_gc_impl_adjust_memory_usage(
objspace, diff);
4815rb_obj_info(
VALUE obj)
4817 return obj_info(obj);
4821rb_obj_info_dump(
VALUE obj)
4824 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
4828rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
4831 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
4835rb_gc_before_fork(
void)
4837 rb_gc_impl_before_fork(rb_gc_get_objspace());
4841rb_gc_after_fork(rb_pid_t pid)
4843 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
4897 malloc_offset = gc_compute_malloc_offset();
4910 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
4933ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
4935#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
4938 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_CAS(var, oldval, newval)
Atomic compare-and-swap.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
VALUE rb_define_module(const char *name)
Defines a top-level module.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define T_FILE
Old name of RUBY_T_FILE.
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define T_MASK
Old name of RUBY_T_MASK.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define T_DATA
Old name of RUBY_T_DATA.
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define LL2NUM
Old name of RB_LL2NUM.
#define 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 STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
#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 T_UNDEF
Old name of RUBY_T_UNDEF.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define 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.
VALUE rb_eRangeError
RangeError exception.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
size_t rb_obj_embedded_size(uint32_t numiv)
Internal header for Object.
VALUE rb_mKernel
Kernel module.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
void rb_memerror(void)
Triggers out-of-memory error.
VALUE rb_gc_disable(void)
Disables GC.
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
VALUE rb_gc_enable(void)
(Re-) enables GC.
int rb_during_gc(void)
Queries if the GC is busy.
void rb_gc(void)
Triggers a GC process.
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
#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_hash_new(void)
Creates a new, empty hash object.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
void rb_str_free(VALUE str)
Destroys the given string for no reason.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
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.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
bool ruby_free_at_exit_p(void)
Returns whether the Ruby VM will free all memory at shutdown.
VALUE rb_yield(VALUE val)
Yields the block.
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY(obj)
Convenient casting macro.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RCLASS(obj)
Convenient casting macro.
#define DATA_PTR(obj)
Convenient getter macro.
#define RDATA(obj)
Convenient casting macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
#define RFILE(obj)
Convenient casting macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RMATCH(obj)
Convenient casting macro.
#define ROBJECT(obj)
Convenient casting macro.
static VALUE * ROBJECT_IVPTR(VALUE obj)
Queries the instance variables.
#define RREGEXP(obj)
Convenient casting macro.
#define RREGEXP_PTR(obj)
Convenient accessor macro.
#define RSTRING(obj)
Convenient casting macro.
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define RTYPEDDATA(obj)
Convenient casting macro.
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed 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.
const char * wrap_struct_name
Name of structs of this kind.
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.
struct re_registers regs
"Registers" of a match.
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.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
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.