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_overflow_tag
495size_mul_add_overflow(size_t x, size_t y, size_t z)
497 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
498 struct rbimpl_size_overflow_tag u = rbimpl_size_add_overflow(t.result, z);
499 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed, u.result };
502static inline struct rbimpl_size_overflow_tag
503size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
505 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
506 struct rbimpl_size_overflow_tag u = rbimpl_size_mul_overflow(z, w);
507 struct rbimpl_size_overflow_tag v = rbimpl_size_add_overflow(t.result, u.result);
508 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed || v.overflowed, v.result };
511PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
514size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
516 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
517 if (LIKELY(!t.overflowed)) {
526 "integer overflow: %"PRIuSIZE
529 x, y, (
size_t)SIZE_MAX);
534rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
536 return size_mul_or_raise(x, y, exc);
540size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
542 struct rbimpl_size_overflow_tag t = size_mul_add_overflow(x, y, z);
543 if (LIKELY(!t.overflowed)) {
552 "integer overflow: %"PRIuSIZE
556 x, y, z, (
size_t)SIZE_MAX);
561rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
563 return size_mul_add_or_raise(x, y, z, exc);
567size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
569 struct rbimpl_size_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
570 if (LIKELY(!t.overflowed)) {
579 "integer overflow: %"PRIdSIZE
584 x, y, z, w, (
size_t)SIZE_MAX);
588#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
590volatile VALUE rb_gc_guarded_val;
592rb_gc_guarded_ptr_val(
volatile VALUE *ptr,
VALUE val)
594 rb_gc_guarded_val = val;
600static const char *obj_type_name(
VALUE obj);
601#include "gc/default/default.c"
603#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
604# error "Modular GC requires dlopen"
608typedef struct gc_function_map {
610 void *(*objspace_alloc)(void);
611 void (*objspace_init)(
void *objspace_ptr);
612 void (*objspace_free)(
void *objspace_ptr);
613 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
614 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
615 void (*set_params)(
void *objspace_ptr);
617 size_t *(*heap_sizes)(
void *objspace_ptr);
619 void (*shutdown_free_objects)(
void *objspace_ptr);
621 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
622 bool (*during_gc_p)(
void *objspace_ptr);
623 void (*prepare_heap)(
void *objspace_ptr);
624 void (*gc_enable)(
void *objspace_ptr);
625 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
626 bool (*gc_enabled_p)(
void *objspace_ptr);
627 VALUE (*config_get)(
void *objpace_ptr);
628 void (*config_set)(
void *objspace_ptr,
VALUE hash);
629 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
630 VALUE (*stress_get)(
void *objspace_ptr);
632 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);
633 size_t (*obj_slot_size)(
VALUE obj);
634 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
635 bool (*size_allocatable_p)(
size_t size);
637 void *(*malloc)(
void *objspace_ptr,
size_t size);
638 void *(*calloc)(
void *objspace_ptr,
size_t size);
639 void *(*realloc)(
void *objspace_ptr,
void *
ptr,
size_t new_size,
size_t old_size);
640 void (*free)(
void *objspace_ptr,
void *
ptr,
size_t old_size);
641 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
643 void (*mark)(
void *objspace_ptr,
VALUE obj);
644 void (*mark_and_move)(
void *objspace_ptr,
VALUE *
ptr);
645 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
646 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
647 void (*mark_weak)(
void *objspace_ptr,
VALUE *
ptr);
648 void (*remove_weak)(
void *objspace_ptr,
VALUE parent_obj,
VALUE *
ptr);
650 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
651 VALUE (*location)(
void *objspace_ptr,
VALUE value);
653 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
654 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
655 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
657 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
658 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
660 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
662 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
663 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
664 void (*shutdown_call_finalizer)(
void *objspace_ptr);
666 VALUE (*object_id)(
void *objspace_ptr,
VALUE obj);
667 VALUE (*object_id_to_ref)(
void *objspace_ptr,
VALUE object_id);
669 void (*before_fork)(
void *objspace_ptr);
670 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
672 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
673 bool (*get_measure_total_time)(
void *objspace_ptr);
674 unsigned long long (*get_total_time)(
void *objspace_ptr);
675 size_t (*gc_count)(
void *objspace_ptr);
676 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
677 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
678 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
679 const char *(*active_gc_name)(void);
681 size_t (*obj_flags)(
void *objspace_ptr,
VALUE obj,
ID* flags,
size_t max);
682 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *
ptr);
683 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
684 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
685 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
687 bool modular_gc_loaded_p;
688} rb_gc_function_map_t;
690static rb_gc_function_map_t rb_gc_functions;
692# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
693# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
696ruby_modular_gc_init(
void)
701 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
703 rb_gc_function_map_t gc_functions = { 0 };
705 char *gc_so_path = NULL;
710 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
711 char c = gc_so_file[i];
712 if (isalnum(c))
continue;
718 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
723 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
726 size_t prefix_len = 0;
727 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
728 const char *base = strrchr(dli.dli_fname,
'/');
731# define end_with_p(lit) \
732 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
733 memcmp(base - tail, lit, tail) == 0)
735 prefix_len = base - dli.dli_fname;
736 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
739 prefix_len += MODULAR_GC_DIR[0] !=
'/';
740 gc_so_path_size += prefix_len;
744 gc_so_path = alloca(gc_so_path_size);
746 size_t gc_so_path_idx = 0;
747#define GC_SO_PATH_APPEND(str) do { \
748 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
751 if (prefix_len > 0) {
752 memcpy(gc_so_path, dli.dli_fname, prefix_len);
753 gc_so_path_idx = prefix_len;
756 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
757 GC_SO_PATH_APPEND(gc_so_file);
758 GC_SO_PATH_APPEND(DLEXT);
759 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
760#undef GC_SO_PATH_APPEND
763 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
765 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
769 gc_functions.modular_gc_loaded_p =
true;
772# define load_modular_gc_func(name) do { \
774 const char *func_name = "rb_gc_impl_" #name; \
775 gc_functions.name = dlsym(handle, func_name); \
776 if (!gc_functions.name) { \
777 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
782 gc_functions.name = rb_gc_impl_##name; \
787 load_modular_gc_func(objspace_alloc);
788 load_modular_gc_func(objspace_init);
789 load_modular_gc_func(objspace_free);
790 load_modular_gc_func(ractor_cache_alloc);
791 load_modular_gc_func(ractor_cache_free);
792 load_modular_gc_func(set_params);
793 load_modular_gc_func(init);
794 load_modular_gc_func(heap_sizes);
796 load_modular_gc_func(shutdown_free_objects);
798 load_modular_gc_func(start);
799 load_modular_gc_func(during_gc_p);
800 load_modular_gc_func(prepare_heap);
801 load_modular_gc_func(gc_enable);
802 load_modular_gc_func(gc_disable);
803 load_modular_gc_func(gc_enabled_p);
804 load_modular_gc_func(config_set);
805 load_modular_gc_func(config_get);
806 load_modular_gc_func(stress_set);
807 load_modular_gc_func(stress_get);
809 load_modular_gc_func(new_obj);
810 load_modular_gc_func(obj_slot_size);
811 load_modular_gc_func(heap_id_for_size);
812 load_modular_gc_func(size_allocatable_p);
814 load_modular_gc_func(malloc);
815 load_modular_gc_func(calloc);
816 load_modular_gc_func(realloc);
817 load_modular_gc_func(free);
818 load_modular_gc_func(adjust_memory_usage);
820 load_modular_gc_func(mark);
821 load_modular_gc_func(mark_and_move);
822 load_modular_gc_func(mark_and_pin);
823 load_modular_gc_func(mark_maybe);
824 load_modular_gc_func(mark_weak);
825 load_modular_gc_func(remove_weak);
827 load_modular_gc_func(object_moved_p);
828 load_modular_gc_func(location);
830 load_modular_gc_func(writebarrier);
831 load_modular_gc_func(writebarrier_unprotect);
832 load_modular_gc_func(writebarrier_remember);
834 load_modular_gc_func(each_objects);
835 load_modular_gc_func(each_object);
837 load_modular_gc_func(make_zombie);
838 load_modular_gc_func(define_finalizer);
839 load_modular_gc_func(undefine_finalizer);
840 load_modular_gc_func(copy_finalizer);
841 load_modular_gc_func(shutdown_call_finalizer);
843 load_modular_gc_func(object_id);
844 load_modular_gc_func(object_id_to_ref);
846 load_modular_gc_func(before_fork);
847 load_modular_gc_func(after_fork);
849 load_modular_gc_func(set_measure_total_time);
850 load_modular_gc_func(get_measure_total_time);
851 load_modular_gc_func(get_total_time);
852 load_modular_gc_func(gc_count);
853 load_modular_gc_func(latest_gc_info);
854 load_modular_gc_func(stat);
855 load_modular_gc_func(stat_heap);
856 load_modular_gc_func(active_gc_name);
858 load_modular_gc_func(obj_flags);
859 load_modular_gc_func(pointer_to_heap_p);
860 load_modular_gc_func(garbage_object_p);
861 load_modular_gc_func(set_event_hook);
862 load_modular_gc_func(copy_attributes);
864# undef load_modular_gc_func
866 rb_gc_functions = gc_functions;
870# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
871# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
872# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
873# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
874# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
875# define rb_gc_impl_set_params rb_gc_functions.set_params
876# define rb_gc_impl_init rb_gc_functions.init
877# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
879# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
881# define rb_gc_impl_start rb_gc_functions.start
882# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
883# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
884# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
885# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
886# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
887# define rb_gc_impl_config_get rb_gc_functions.config_get
888# define rb_gc_impl_config_set rb_gc_functions.config_set
889# define rb_gc_impl_stress_set rb_gc_functions.stress_set
890# define rb_gc_impl_stress_get rb_gc_functions.stress_get
892# define rb_gc_impl_new_obj rb_gc_functions.new_obj
893# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
894# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
895# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
897# define rb_gc_impl_malloc rb_gc_functions.malloc
898# define rb_gc_impl_calloc rb_gc_functions.calloc
899# define rb_gc_impl_realloc rb_gc_functions.realloc
900# define rb_gc_impl_free rb_gc_functions.free
901# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
903# define rb_gc_impl_mark rb_gc_functions.mark
904# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
905# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
906# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
907# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
908# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
910# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
911# define rb_gc_impl_location rb_gc_functions.location
913# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
914# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
915# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
917# define rb_gc_impl_each_objects rb_gc_functions.each_objects
918# define rb_gc_impl_each_object rb_gc_functions.each_object
920# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
921# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
922# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
923# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
924# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
926# define rb_gc_impl_object_id rb_gc_functions.object_id
927# define rb_gc_impl_object_id_to_ref rb_gc_functions.object_id_to_ref
929# define rb_gc_impl_before_fork rb_gc_functions.before_fork
930# define rb_gc_impl_after_fork rb_gc_functions.after_fork
932# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
933# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
934# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
935# define rb_gc_impl_gc_count rb_gc_functions.gc_count
936# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
937# define rb_gc_impl_stat rb_gc_functions.stat
938# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
939# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
941# define rb_gc_impl_obj_flags rb_gc_functions.obj_flags
942# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
943# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
944# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
945# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
948#ifdef RUBY_ASAN_ENABLED
950asan_death_callback(
void)
953 rb_bug_without_die(
"ASAN error");
961rb_objspace_alloc(
void)
964 ruby_modular_gc_init();
967 void *
objspace = rb_gc_impl_objspace_alloc();
968 ruby_current_vm_ptr->gc.objspace =
objspace;
971 rb_gc_impl_stress_set(
objspace, initial_stress);
973#ifdef RUBY_ASAN_ENABLED
974 __sanitizer_set_death_callback(asan_death_callback);
987rb_gc_obj_slot_size(
VALUE obj)
989 return rb_gc_impl_obj_slot_size(obj);
993gc_validate_pc(
void) {
995 rb_execution_context_t *ec = GET_EC();
996 const rb_control_frame_t *cfp = ec->cfp;
997 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc) {
998 RUBY_ASSERT(cfp->pc >= ISEQ_BODY(cfp->iseq)->iseq_encoded);
999 RUBY_ASSERT(cfp->pc <= ISEQ_BODY(cfp->iseq)->iseq_encoded + ISEQ_BODY(cfp->iseq)->iseq_size);
1007 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
1013 RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
1015 memset((
char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
1022 bool gc_disabled =
RTEST(rb_gc_disable_no_rest());
1028 RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
1035rb_wb_unprotected_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
1038 return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
1042rb_wb_protected_newobj_of(rb_execution_context_t *ec,
VALUE klass,
VALUE flags,
size_t size)
1045 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
1048#define UNEXPECTED_NODE(func) \
1049 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1050 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1053rb_data_object_check(
VALUE klass)
1055 if (klass != rb_cObject && (
rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
1057 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1065 if (klass) rb_data_object_check(klass);
1072 VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
1080 RBIMPL_NONNULL_ARG(
type);
1081 if (klass) rb_data_object_check(klass);
1083 return newobj_of(GET_RACTOR(), klass,
T_DATA, (
VALUE)
type, 1 | typed_flag, (
VALUE)datap, wb_protected, size);
1089 if (UNLIKELY(
type->flags & RUBY_TYPED_EMBEDDABLE)) {
1090 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1093 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
1099 if (
type->flags & RUBY_TYPED_EMBEDDABLE) {
1100 if (!(
type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1101 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1104 size_t embed_size = offsetof(
struct RTypedData, data) + size;
1105 if (rb_gc_size_allocatable_p(embed_size)) {
1106 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1107 memset((
char *)obj + offsetof(
struct RTypedData, data), 0, size);
1118rb_objspace_data_type_memsize(
VALUE obj)
1123 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1125 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1126#ifdef HAVE_MALLOC_USABLE_SIZE
1127 size += malloc_usable_size((
void *)ptr);
1131 if (ptr &&
type->function.dsize) {
1132 size +=
type->function.dsize(ptr);
1140rb_objspace_data_type_name(
VALUE obj)
1150static enum rb_id_table_iterator_result
1151cvar_table_free_i(
VALUE value,
void *ctx)
1153 xfree((
void *)value);
1154 return ID_TABLE_CONTINUE;
1160 rb_io_t *fptr =
RFILE(obj)->fptr;
1161 rb_gc_impl_make_zombie(
objspace, obj, rb_io_fptr_finalize_internal, fptr);
1169 int free_immediately =
false;
1170 void (*dfree)(
void *);
1173 free_immediately = (
RTYPEDDATA(obj)->type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1174 dfree =
RTYPEDDATA(obj)->type->function.dfree;
1177 dfree =
RDATA(obj)->dfree;
1182 if (!
RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1184 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1187 else if (free_immediately) {
1189 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1193 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1196 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, dfree, data);
1197 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1202 RB_DEBUG_COUNTER_INC(obj_data_empty);
1210rb_gc_obj_free_vm_weak_references(
VALUE obj)
1219 if (
FL_TEST(obj, RSTRING_FSTR)) {
1222 st_data_t fstr = (st_data_t)obj;
1223 st_delete(rb_vm_fstring_table(), &fstr, NULL);
1224 RB_DEBUG_COUNTER_INC(obj_str_fstr);
1230 rb_gc_free_dsymbol(obj);
1233 switch (imemo_type(obj)) {
1234 case imemo_callinfo:
1238 rb_free_method_entry_vm_weak_references((
const rb_method_entry_t *)obj);
1252 RB_DEBUG_COUNTER_INC(obj_free);
1259 rb_bug(
"obj_free() called for broken object");
1267 if (rb_shape_obj_too_complex(obj)) {
1268 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1269 st_free_table(ROBJECT_IV_HASH(obj));
1271 else if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1272 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1276 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1281 rb_id_table_free(RCLASS_M_TBL(obj));
1282 rb_cc_table_free(obj);
1283 if (rb_shape_obj_too_complex(obj)) {
1284 st_free_table((st_table *)RCLASS_IVPTR(obj));
1287 xfree(RCLASS_IVPTR(obj));
1290 if (RCLASS_CONST_TBL(obj)) {
1291 rb_free_const_table(RCLASS_CONST_TBL(obj));
1293 if (RCLASS_CVC_TBL(obj)) {
1294 rb_id_table_foreach_values(RCLASS_CVC_TBL(obj), cvar_table_free_i, NULL);
1295 rb_id_table_free(RCLASS_CVC_TBL(obj));
1297 rb_class_remove_subclass_head(obj);
1298 rb_class_remove_from_module_subclasses(obj);
1299 rb_class_remove_from_super_subclasses(obj);
1300 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1301 xfree(RCLASS_SUPERCLASSES(obj));
1314#if USE_DEBUG_COUNTER
1317 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1320 RB_DEBUG_COUNTER_INC(obj_hash_1);
1323 RB_DEBUG_COUNTER_INC(obj_hash_2);
1326 RB_DEBUG_COUNTER_INC(obj_hash_3);
1329 RB_DEBUG_COUNTER_INC(obj_hash_4);
1335 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1339 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1342 if (RHASH_AR_TABLE_P(obj)) {
1343 if (RHASH_AR_TABLE(obj) == NULL) {
1344 RB_DEBUG_COUNTER_INC(obj_hash_null);
1347 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1351 RB_DEBUG_COUNTER_INC(obj_hash_st);
1360 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1364 if (!rb_data_free(
objspace, obj))
return false;
1368 rb_matchext_t *rm = RMATCH_EXT(obj);
1369#if USE_DEBUG_COUNTER
1370 if (rm->
regs.num_regs >= 8) {
1371 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1373 else if (rm->
regs.num_regs >= 4) {
1374 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1376 else if (rm->
regs.num_regs >= 1) {
1377 RB_DEBUG_COUNTER_INC(obj_match_under4);
1380 onig_region_free(&rm->
regs, 0);
1383 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1387 if (
RFILE(obj)->fptr) {
1389 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1394 RB_DEBUG_COUNTER_INC(obj_rational);
1397 RB_DEBUG_COUNTER_INC(obj_complex);
1403 if (RICLASS_OWNS_M_TBL_P(obj)) {
1405 rb_id_table_free(RCLASS_M_TBL(obj));
1407 if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
1408 rb_id_table_free(RCLASS_CALLABLE_M_TBL(obj));
1410 rb_class_remove_subclass_head(obj);
1411 rb_cc_table_free(obj);
1412 rb_class_remove_from_module_subclasses(obj);
1413 rb_class_remove_from_super_subclasses(obj);
1415 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1419 RB_DEBUG_COUNTER_INC(obj_float);
1423 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1424 xfree(BIGNUM_DIGITS(obj));
1425 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1428 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1433 UNEXPECTED_NODE(obj_free);
1437 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1438 RSTRUCT(obj)->as.heap.ptr == NULL) {
1439 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1442 xfree((
void *)RSTRUCT(obj)->as.heap.ptr);
1443 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1448 RB_DEBUG_COUNTER_INC(obj_symbol);
1452 rb_imemo_free((
VALUE)obj);
1456 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1461 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, 0, 0);
1472 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1476internal_object_p(
VALUE obj)
1478 void *ptr = asan_unpoison_object_temporary(obj);
1480 if (
RBASIC(obj)->flags) {
1483 UNEXPECTED_NODE(internal_object_p);
1492 if (!
RBASIC(obj)->klass)
break;
1493 if (RCLASS_SINGLETON_P(obj)) {
1494 return rb_singleton_class_internal_p(obj);
1498 if (!
RBASIC(obj)->klass)
break;
1502 if (ptr || !
RBASIC(obj)->flags) {
1503 rb_asan_poison_object(obj);
1509rb_objspace_internal_object_p(
VALUE obj)
1511 return internal_object_p(obj);
1520os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1525 for (; v != (
VALUE)vend; v += stride) {
1526 if (!internal_object_p(v)) {
1546 rb_objspace_each_objects(os_obj_of_i, &oes);
1593 return os_obj_of(of);
1607 return rb_undefine_finalizer(obj);
1613 rb_check_frozen(obj);
1615 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1621should_be_callable(
VALUE block)
1624 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
1630should_be_finalizable(
VALUE obj)
1633 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
1636 rb_check_frozen(obj);
1642 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1717 if (rb_callable_receiver(block) == obj) {
1718 rb_warn(
"finalizer references object to be finalized");
1721 return rb_define_finalizer(obj, block);
1727 should_be_finalizable(obj);
1728 should_be_callable(block);
1730 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1738rb_objspace_call_finalizer(
void)
1740 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1744rb_objspace_free_objects(
void *
objspace)
1746 rb_gc_impl_shutdown_free_objects(
objspace);
1750rb_objspace_garbage_object_p(
VALUE obj)
1752 return rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1756rb_gc_pointer_to_heap_p(
VALUE obj)
1758 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj);
1779#if SIZEOF_LONG == SIZEOF_VOIDP
1780#define NUM2PTR(x) NUM2ULONG(x)
1781#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1782#define NUM2PTR(x) NUM2ULL(x)
1785 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
1786 VALUE ptr = NUM2PTR(objid);
1796 if (rb_static_id_valid_p(
SYM2ID(ptr))) {
1800 rb_raise(
rb_eRangeError,
"%p is not symbol id value", (
void *)ptr);
1804 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is not id value", rb_int2str(objid, 10));
1808 VALUE obj = rb_gc_impl_object_id_to_ref(rb_gc_get_objspace(), objid);
1813 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is id of the unshareable object on multi-ractor", rb_int2str(objid, 10));
1821 return id2ref(objid);
1828#if SIZEOF_LONG == SIZEOF_VOIDP
1835 return get_heap_object_id(
objspace, obj);
1839nonspecial_obj_id(
void *_objspace,
VALUE obj)
1841#if SIZEOF_LONG == SIZEOF_VOIDP
1843#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1846# error not supported
1853 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
1890 return rb_find_object_id(rb_gc_get_objspace(), obj, rb_gc_impl_object_id);
1893static enum rb_id_table_iterator_result
1894cc_table_memsize_i(
VALUE ccs_ptr,
void *data_ptr)
1896 size_t *total_size = data_ptr;
1898 *total_size +=
sizeof(*ccs);
1899 *total_size +=
sizeof(ccs->entries[0]) * ccs->capa;
1900 return ID_TABLE_CONTINUE;
1906 size_t total = rb_id_table_memsize(cc_table);
1907 rb_id_table_foreach_values(cc_table, cc_table_memsize_i, &total);
1912rb_obj_memsize_of(
VALUE obj)
1921 size += rb_generic_ivar_memsize(obj);
1926 if (rb_shape_obj_too_complex(obj)) {
1927 size += rb_st_memsize(ROBJECT_IV_HASH(obj));
1929 else if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1930 size += ROBJECT_IV_CAPACITY(obj) *
sizeof(
VALUE);
1935 if (RCLASS_M_TBL(obj)) {
1936 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1939 size +=
SIZEOF_VALUE << bit_length(RCLASS_IV_COUNT(obj));
1940 if (RCLASS_CVC_TBL(obj)) {
1941 size += rb_id_table_memsize(RCLASS_CVC_TBL(obj));
1943 if (RCLASS_EXT(obj)->const_tbl) {
1944 size += rb_id_table_memsize(RCLASS_EXT(obj)->const_tbl);
1946 if (RCLASS_CC_TBL(obj)) {
1947 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1949 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1950 size += (RCLASS_SUPERCLASS_DEPTH(obj) + 1) *
sizeof(
VALUE);
1954 if (RICLASS_OWNS_M_TBL_P(obj)) {
1955 if (RCLASS_M_TBL(obj)) {
1956 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1959 if (RCLASS_CC_TBL(obj)) {
1960 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1964 size += rb_str_memsize(obj);
1967 size += rb_ary_memsize(obj);
1970 if (RHASH_ST_TABLE_P(obj)) {
1971 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
1973 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(st_table);
1982 size += rb_objspace_data_type_memsize(obj);
1986 rb_matchext_t *rm = RMATCH_EXT(obj);
1987 size += onig_region_memsize(&rm->
regs);
1992 if (
RFILE(obj)->fptr) {
1993 size += rb_io_memsize(
RFILE(obj)->fptr);
2000 size += rb_imemo_memsize(obj);
2008 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2009 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
2014 UNEXPECTED_NODE(obj_memsize_of);
2018 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
2019 RSTRUCT(obj)->as.heap.ptr) {
2020 size +=
sizeof(
VALUE) * RSTRUCT_LEN(obj);
2029 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
2033 return size + rb_gc_obj_slot_size(obj);
2037set_zero(st_data_t key, st_data_t val, st_data_t arg)
2041 rb_hash_aset(hash, k,
INT2FIX(0));
2052count_objects_i(
VALUE obj,
void *d)
2056 if (
RBASIC(obj)->flags) {
2102count_objects(
int argc,
VALUE *argv,
VALUE os)
2113 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2119 rb_hash_stlike_foreach(hash, set_zero, hash);
2121 rb_hash_aset(hash,
ID2SYM(rb_intern(
"TOTAL")),
SIZET2NUM(data.total));
2124 for (
size_t i = 0; i <=
T_MASK; i++) {
2133#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2135#define STACK_START (ec->machine.stack_start)
2136#define STACK_END (ec->machine.stack_end)
2137#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2139#if STACK_GROW_DIRECTION < 0
2140# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2141#elif STACK_GROW_DIRECTION > 0
2142# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2144# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2145 : (size_t)(STACK_END - STACK_START + 1))
2147#if !STACK_GROW_DIRECTION
2148int ruby_stack_grow_direction;
2150ruby_get_stack_grow_direction(
volatile VALUE *addr)
2153 SET_MACHINE_STACK_END(&end);
2155 if (end > addr)
return ruby_stack_grow_direction = 1;
2156 return ruby_stack_grow_direction = -1;
2163 rb_execution_context_t *ec = GET_EC();
2165 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2166 return STACK_LENGTH;
2169#define PREVENT_STACK_OVERFLOW 1
2170#ifndef PREVENT_STACK_OVERFLOW
2171#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2172# define PREVENT_STACK_OVERFLOW 1
2174# define PREVENT_STACK_OVERFLOW 0
2177#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2179stack_check(rb_execution_context_t *ec,
int water_mark)
2183 size_t length = STACK_LENGTH;
2184 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2186 return length > maximum_length;
2189#define stack_check(ec, water_mark) FALSE
2192#define STACKFRAME_FOR_CALL_CFUNC 2048
2195rb_ec_stack_check(rb_execution_context_t *ec)
2197 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2203 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2208#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2209 if (!RB_SPECIAL_CONST_P(obj)) { \
2210 rb_vm_t *vm = GET_VM(); \
2211 void *objspace = vm->gc.objspace; \
2212 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2213 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2214 (func)(objspace, (obj_or_ptr)); \
2216 else if (check_obj ? \
2217 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2218 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2220 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2221 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2222 vm->gc.mark_func_data = NULL; \
2223 mark_func_data->mark_func((obj), mark_func_data->data); \
2224 vm->gc.mark_func_data = mark_func_data; \
2230gc_mark_internal(
VALUE obj)
2232 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2238 gc_mark_internal(obj);
2242rb_gc_mark_and_move(
VALUE *ptr)
2244 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr,
false);
2248gc_mark_and_pin_internal(
VALUE obj)
2250 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2256 gc_mark_and_pin_internal(obj);
2260gc_mark_maybe_internal(
VALUE obj)
2262 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2268 gc_mark_maybe_internal(obj);
2272rb_gc_mark_weak(
VALUE *ptr)
2276 rb_vm_t *vm = GET_VM();
2278 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2279 GC_ASSERT(rb_gc_impl_during_gc_p(
objspace));
2281 rb_gc_impl_mark_weak(
objspace, ptr);
2284 GC_ASSERT(!rb_gc_impl_during_gc_p(
objspace));
2289rb_gc_remove_weak(
VALUE parent_obj,
VALUE *ptr)
2291 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj, ptr);
2294ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2296each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2307each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2309 if (end <= start)
return;
2310 each_location(start, end - start, cb, data);
2314gc_mark_maybe_each_location(
VALUE obj,
void *data)
2316 gc_mark_maybe_internal(obj);
2322 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2326rb_gc_mark_values(
long n,
const VALUE *values)
2328 for (
long i = 0; i < n; i++) {
2329 gc_mark_internal(values[i]);
2334rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
2336 for (
long i = 0; i < n; i++) {
2337 gc_mark_and_pin_internal(values[i]);
2342mark_key(st_data_t key, st_data_t value, st_data_t data)
2344 gc_mark_and_pin_internal((
VALUE)key);
2350rb_mark_set(st_table *tbl)
2354 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2358mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2360 gc_mark_internal((
VALUE)key);
2361 gc_mark_internal((
VALUE)value);
2367pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2369 gc_mark_and_pin_internal((
VALUE)key);
2370 gc_mark_and_pin_internal((
VALUE)value);
2376pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2378 gc_mark_and_pin_internal((
VALUE)key);
2379 gc_mark_internal((
VALUE)value);
2385mark_hash(
VALUE hash)
2387 if (rb_hash_compare_by_id_p(hash)) {
2388 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2391 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2394 gc_mark_internal(RHASH(hash)->ifnone);
2398rb_mark_hash(st_table *tbl)
2405static enum rb_id_table_iterator_result
2408 gc_mark_internal(me);
2410 return ID_TABLE_CONTINUE;
2417 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
2421#if STACK_GROW_DIRECTION < 0
2422#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2423#elif STACK_GROW_DIRECTION > 0
2424#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2426#define GET_STACK_BOUNDS(start, end, appendix) \
2427 ((STACK_END < STACK_START) ? \
2428 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2432gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
2434 gc_mark_maybe_internal(obj);
2436#ifdef RUBY_ASAN_ENABLED
2437 const rb_execution_context_t *ec = (
const rb_execution_context_t *)data;
2438 void *fake_frame_start;
2439 void *fake_frame_end;
2440 bool is_fake_frame = asan_get_fake_stack_extents(
2441 ec->machine.asan_fake_stack_handle, obj,
2442 ec->machine.stack_start, ec->machine.stack_end,
2443 &fake_frame_start, &fake_frame_end
2445 if (is_fake_frame) {
2446 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2458 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)value));
2460 return rb_gc_impl_location(
objspace, value);
2466 return gc_location_internal(rb_gc_get_objspace(), value);
2469#if defined(__wasm__)
2472static VALUE *rb_stack_range_tmp[2];
2475rb_mark_locations(
void *begin,
void *end)
2477 rb_stack_range_tmp[0] = begin;
2478 rb_stack_range_tmp[1] = end;
2482rb_gc_save_machine_context(
void)
2487# if defined(__EMSCRIPTEN__)
2490mark_current_machine_context(
const rb_execution_context_t *ec)
2492 emscripten_scan_stack(rb_mark_locations);
2493 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2495 emscripten_scan_registers(rb_mark_locations);
2496 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2501mark_current_machine_context(
const rb_execution_context_t *ec)
2503 VALUE *stack_start, *stack_end;
2505 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2506 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2508 rb_wasm_scan_locals(rb_mark_locations);
2509 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2517rb_gc_save_machine_context(
void)
2519 rb_thread_t *thread = GET_THREAD();
2521 RB_VM_SAVE_MACHINE_CONTEXT(thread);
2526mark_current_machine_context(
const rb_execution_context_t *ec)
2528 rb_gc_mark_machine_context(ec);
2533rb_gc_mark_machine_context(
const rb_execution_context_t *ec)
2535 VALUE *stack_start, *stack_end;
2537 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2538 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2541#ifdef RUBY_ASAN_ENABLED
2543 (rb_execution_context_t *)ec;
2548 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2549 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
2550 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2554rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2556 gc_mark_and_pin_internal((
VALUE)value);
2562rb_mark_tbl(st_table *tbl)
2564 if (!tbl || tbl->num_entries == 0)
return;
2570gc_mark_tbl_no_pin(st_table *tbl)
2572 if (!tbl || tbl->num_entries == 0)
return;
2578rb_mark_tbl_no_pin(st_table *tbl)
2580 gc_mark_tbl_no_pin(tbl);
2583static enum rb_id_table_iterator_result
2591 gc_mark_internal((
VALUE)entry->cref);
2593 return ID_TABLE_CONTINUE;
2601 rb_id_table_foreach_values(tbl, mark_cvc_tbl_i,
objspace);
2608 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
2611static enum rb_id_table_iterator_result
2614 const rb_const_entry_t *ce = (
const rb_const_entry_t *)value;
2616 gc_mark_internal(ce->value);
2617 gc_mark_internal(ce->file);
2619 return ID_TABLE_CONTINUE;
2623rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
2625 rb_execution_context_t *ec = GET_EC();
2626 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2628#define MARK_CHECKPOINT(category) do { \
2629 if (categoryp) *categoryp = category; \
2632 MARK_CHECKPOINT(
"vm");
2634 if (vm->self) gc_mark_internal(vm->self);
2636 MARK_CHECKPOINT(
"end_proc");
2639 MARK_CHECKPOINT(
"global_tbl");
2640 rb_gc_mark_global_tbl();
2643 void rb_yjit_root_mark(
void);
2645 if (rb_yjit_enabled_p) {
2646 MARK_CHECKPOINT(
"YJIT");
2647 rb_yjit_root_mark();
2651 MARK_CHECKPOINT(
"machine_context");
2652 mark_current_machine_context(ec);
2654 MARK_CHECKPOINT(
"finish");
2656#undef MARK_CHECKPOINT
2659#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA(d)->type->function.dmark
2665 rb_mark_generic_ivar(obj);
2680 rb_bug(
"rb_gc_mark() called for broken object");
2684 UNEXPECTED_NODE(rb_gc_mark);
2688 rb_imemo_mark_and_move(obj,
false);
2695 gc_mark_internal(
RBASIC(obj)->klass);
2700 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
2708 mark_m_tbl(
objspace, RCLASS_M_TBL(obj));
2710 rb_cc_table_mark(obj);
2711 if (rb_shape_obj_too_complex(obj)) {
2712 gc_mark_tbl_no_pin((st_table *)RCLASS_IVPTR(obj));
2715 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
2716 gc_mark_internal(RCLASS_IVPTR(obj)[i]);
2720 if (RCLASS_CONST_TBL(obj)) {
2721 rb_id_table_foreach_values(RCLASS_CONST_TBL(obj), mark_const_table_i,
objspace);
2724 gc_mark_internal(RCLASS_EXT(obj)->classpath);
2728 if (RICLASS_OWNS_M_TBL_P(obj)) {
2729 mark_m_tbl(
objspace, RCLASS_M_TBL(obj));
2735 if (RCLASS_INCLUDER(obj)) {
2736 gc_mark_internal(RCLASS_INCLUDER(obj));
2738 mark_m_tbl(
objspace, RCLASS_CALLABLE_M_TBL(obj));
2739 rb_cc_table_mark(obj);
2743 if (ARY_SHARED_P(obj)) {
2744 VALUE root = ARY_SHARED_ROOT(obj);
2745 gc_mark_internal(root);
2750 for (
long i = 0; i <
len; i++) {
2751 gc_mark_internal(ptr[i]);
2761 if (STR_SHARED_P(obj)) {
2762 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
2767 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
2770 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
2780 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
2782 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
2783 gc_mark_internal(*(
VALUE *)((
char *)ptr + offset));
2790 if (mark_func) (*mark_func)(ptr);
2798 rb_shape_t *shape = rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj));
2800 if (rb_shape_obj_too_complex(obj)) {
2801 gc_mark_tbl_no_pin(ROBJECT_IV_HASH(obj));
2806 uint32_t
len = ROBJECT_IV_COUNT(obj);
2807 for (uint32_t i = 0; i <
len; i++) {
2808 gc_mark_internal(ptr[i]);
2816 attr_index_t num_of_ivs = shape->next_iv_index;
2817 if (RCLASS_EXT(klass)->max_iv_count < num_of_ivs) {
2818 RCLASS_EXT(klass)->max_iv_count = num_of_ivs;
2826 if (
RFILE(obj)->fptr) {
2827 gc_mark_internal(
RFILE(obj)->fptr->self);
2828 gc_mark_internal(
RFILE(obj)->fptr->pathv);
2829 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
2830 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
2831 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
2832 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
2833 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
2834 gc_mark_internal(
RFILE(obj)->fptr->timeout);
2839 gc_mark_internal(
RREGEXP(obj)->src);
2843 gc_mark_internal(
RMATCH(obj)->regexp);
2845 gc_mark_internal(
RMATCH(obj)->str);
2850 gc_mark_internal(RRATIONAL(obj)->num);
2851 gc_mark_internal(RRATIONAL(obj)->den);
2855 gc_mark_internal(RCOMPLEX(obj)->real);
2856 gc_mark_internal(RCOMPLEX(obj)->imag);
2860 const long len = RSTRUCT_LEN(obj);
2861 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
2863 for (
long i = 0; i <
len; i++) {
2864 gc_mark_internal(ptr[i]);
2874 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
2876 rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj) ?
"corrupted object" :
"non object");
2881rb_gc_obj_optimal_size(
VALUE obj)
2885 return rb_ary_size_as_embedded(obj);
2888 if (rb_shape_obj_too_complex(obj)) {
2889 return sizeof(
struct RObject);
2896 return rb_str_size_as_embedded(obj);
2899 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));
2907rb_gc_writebarrier(VALUE a, VALUE b)
2909 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
2915 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
2922rb_gc_writebarrier_remember(
VALUE obj)
2924 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
2930 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
2934rb_gc_modular_gc_loaded_p(
void)
2937 return rb_gc_functions.modular_gc_loaded_p;
2944rb_gc_active_gc_name(
void)
2946 const char *gc_name = rb_gc_impl_active_gc_name();
2948 const size_t len = strlen(gc_name);
2949 if (
len > RB_GC_MAX_NAME_LEN) {
2950 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
2951 RB_GC_MAX_NAME_LEN,
len, gc_name);
2959rb_obj_gc_flags(
VALUE obj,
ID* flags,
size_t max)
2961 return rb_gc_impl_obj_flags(rb_gc_get_objspace(), obj, flags, max);
2967rb_gc_ractor_cache_alloc(rb_ractor_t *ractor)
2969 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
2973rb_gc_ractor_cache_free(
void *cache)
2975 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
2981 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
2984 rb_vm_register_global_object(obj);
2990 rb_vm_t *vm = GET_VM();
2995 tmp->next = vm->global_object_list;
2997 vm->global_object_list = tmp;
3005 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
3007 rb_print_backtrace(stderr);
3014 rb_vm_t *vm = GET_VM();
3017 if (tmp->varptr == addr) {
3018 vm->global_object_list = tmp->next;
3023 if (tmp->next->varptr == addr) {
3026 tmp->next = tmp->next->next;
3037 rb_gc_register_address(var);
3041gc_start_internal(rb_execution_context_t *ec,
VALUE self,
VALUE full_mark,
VALUE immediate_mark,
VALUE immediate_sweep,
VALUE compact)
3043 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
3087rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
3089 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3095 if (ARY_SHARED_P(v)) {
3096 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
3100 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
3102 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3103 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
3104 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
3105 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
3113 for (
long i = 0; i <
len; i++) {
3118 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3119 if (rb_ary_embeddable_p(v)) {
3120 rb_ary_make_embedded(v);
3131 if (rb_shape_obj_too_complex(v)) {
3132 gc_ref_update_table_values_only(ROBJECT_IV_HASH(v));
3136 size_t slot_size = rb_gc_obj_slot_size(v);
3138 if (slot_size >= embed_size && !
RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
3140 memcpy(
ROBJECT(v)->as.ary, ptr,
sizeof(
VALUE) * ROBJECT_IV_COUNT(v));
3146 for (uint32_t i = 0; i < ROBJECT_IV_COUNT(v); i++) {
3152rb_gc_ref_update_table_values_only(st_table *tbl)
3154 gc_ref_update_table_values_only(tbl);
3159rb_gc_update_tbl_refs(st_table *ptr)
3161 gc_update_table_refs(ptr);
3167 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
3173 for (
long i = 0; i < n; i++) {
3174 UPDATE_IF_MOVED(
objspace, values[i]);
3179rb_gc_update_values(
long n,
VALUE *values)
3181 gc_update_values(rb_gc_get_objspace(), n, values);
3184static enum rb_id_table_iterator_result
3185check_id_table_move(
VALUE value,
void *data)
3190 return ID_TABLE_REPLACE;
3193 return ID_TABLE_CONTINUE;
3197rb_gc_prepare_heap_process_object(
VALUE obj)
3211rb_gc_prepare_heap(
void)
3213 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3217rb_gc_heap_id_for_size(
size_t size)
3219 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3223rb_gc_size_allocatable_p(
size_t size)
3225 return rb_gc_impl_size_allocatable_p(size);
3228static enum rb_id_table_iterator_result
3229update_id_table(
VALUE *value,
void *data,
int existing)
3237 return ID_TABLE_CONTINUE;
3244 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
3248static enum rb_id_table_iterator_result
3252 VM_ASSERT(vm_ccs_p(ccs));
3255 ccs->cme = (
const rb_callable_method_entry_t *)gc_location_internal(
objspace, (
VALUE)ccs->cme);
3258 for (
int i=0; i<ccs->len; i++) {
3259 if (rb_gc_impl_object_moved_p(
objspace, (
VALUE)ccs->entries[i].cc)) {
3265 return ID_TABLE_CONTINUE;
3273 rb_id_table_foreach_values(tbl, update_cc_tbl_i,
objspace);
3277static enum rb_id_table_iterator_result
3285 TYPED_UPDATE_IF_MOVED(
objspace, rb_cref_t *, entry->cref);
3288 entry->class_value = gc_location_internal(
objspace, entry->class_value);
3290 return ID_TABLE_CONTINUE;
3298 rb_id_table_foreach_values(tbl, update_cvc_tbl_i,
objspace);
3302static enum rb_id_table_iterator_result
3305 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3307 if (rb_gc_impl_object_moved_p(
objspace, ce->value)) {
3308 ce->value = gc_location_internal(
objspace, ce->value);
3311 if (rb_gc_impl_object_moved_p(
objspace, ce->file)) {
3312 ce->file = gc_location_internal(
objspace, ce->file);
3315 return ID_TABLE_CONTINUE;
3322 rb_id_table_foreach_values(tbl, update_const_table,
objspace);
3326update_subclass_entries(
void *
objspace, rb_subclass_entry_t *entry)
3329 UPDATE_IF_MOVED(
objspace, entry->klass);
3330 entry = entry->next;
3335update_class_ext(
void *
objspace, rb_classext_t *ext)
3337 UPDATE_IF_MOVED(
objspace, ext->origin_);
3338 UPDATE_IF_MOVED(
objspace, ext->includer);
3339 UPDATE_IF_MOVED(
objspace, ext->refined_class);
3340 update_subclass_entries(
objspace, ext->subclasses);
3346 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
3347 for (
size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj) + 1; i++) {
3348 UPDATE_IF_MOVED(
objspace, RCLASS_SUPERCLASSES(obj)[i]);
3354#define global_symbols ruby_global_symbols
3357struct global_vm_table_foreach_data {
3358 vm_table_foreach_callback_func callback;
3359 vm_table_update_callback_func update_callback;
3364vm_weak_table_foreach_key(st_data_t key, st_data_t value, st_data_t data,
int error)
3366 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3368 return iter_data->callback((
VALUE)key, iter_data->data);
3372vm_weak_table_foreach_update_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3374 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3376 return iter_data->update_callback((
VALUE *)key, iter_data->data);
3380vm_weak_table_str_sym_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3382 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3388 return iter_data->callback((
VALUE)value, iter_data->data);
3393vm_weak_table_foreach_update_value(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3395 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3397 return iter_data->update_callback((
VALUE *)value, iter_data->data);
3401vm_weak_table_gen_ivar_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3403 int retval = vm_weak_table_foreach_key(key, value, data, error);
3404 if (retval == ST_DELETE) {
3411vm_weak_table_frozen_strings_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3415 int retval = vm_weak_table_foreach_key(key, value, data, error);
3416 if (retval == ST_DELETE) {
3422struct st_table *rb_generic_ivtbl_get(
void);
3425rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
3426 vm_table_update_callback_func update_callback,
3428 enum rb_gc_vm_weak_tables table)
3430 rb_vm_t *vm = GET_VM();
3432 struct global_vm_table_foreach_data foreach_data = {
3433 .callback = callback,
3434 .update_callback = update_callback,
3439 case RB_GC_VM_CI_TABLE: {
3440 st_foreach_with_replace(
3442 vm_weak_table_foreach_key,
3443 vm_weak_table_foreach_update_key,
3444 (st_data_t)&foreach_data
3448 case RB_GC_VM_OVERLOADED_CME_TABLE: {
3449 st_foreach_with_replace(
3450 vm->overloaded_cme_table,
3451 vm_weak_table_foreach_key,
3452 vm_weak_table_foreach_update_key,
3453 (st_data_t)&foreach_data
3457 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
3458 st_foreach_with_replace(
3459 global_symbols.str_sym,
3460 vm_weak_table_str_sym_foreach,
3461 vm_weak_table_foreach_update_value,
3462 (st_data_t)&foreach_data
3466 case RB_GC_VM_GENERIC_IV_TABLE: {
3467 st_table *generic_iv_tbl = rb_generic_ivtbl_get();
3468 st_foreach_with_replace(
3470 vm_weak_table_gen_ivar_foreach,
3471 vm_weak_table_foreach_update_key,
3472 (st_data_t)&foreach_data
3476 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
3477 st_table *frozen_strings = GET_VM()->frozen_strings;
3478 st_foreach_with_replace(
3480 vm_weak_table_frozen_strings_foreach,
3481 vm_weak_table_foreach_update_key,
3482 (st_data_t)&foreach_data
3487 rb_bug(
"rb_gc_vm_weak_table_foreach: unknown table %d", table);
3493rb_gc_update_vm_references(
void *
objspace)
3495 rb_execution_context_t *ec = GET_EC();
3496 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3498 rb_vm_update_references(vm);
3499 rb_gc_update_global_tbl();
3500 global_symbols.ids = gc_location_internal(
objspace, global_symbols.ids);
3501 global_symbols.dsymbol_fstr_hash = gc_location_internal(
objspace, global_symbols.dsymbol_fstr_hash);
3502 gc_update_table_refs(global_symbols.str_sym);
3505 void rb_yjit_root_update_references(
void);
3507 if (rb_yjit_enabled_p) {
3508 rb_yjit_root_update_references();
3517 rb_ref_update_generic_ivar(obj);
3523 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
3530 update_m_tbl(
objspace, RCLASS_M_TBL(obj));
3533 update_superclasses(
objspace, obj);
3535 if (rb_shape_obj_too_complex(obj)) {
3536 gc_ref_update_table_values_only(RCLASS_IV_HASH(obj));
3539 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
3540 UPDATE_IF_MOVED(
objspace, RCLASS_IVPTR(obj)[i]);
3544 update_class_ext(
objspace, RCLASS_EXT(obj));
3545 update_const_tbl(
objspace, RCLASS_CONST_TBL(obj));
3547 UPDATE_IF_MOVED(
objspace, RCLASS_EXT(obj)->classpath);
3551 if (RICLASS_OWNS_M_TBL_P(obj)) {
3552 update_m_tbl(
objspace, RCLASS_M_TBL(obj));
3557 update_class_ext(
objspace, RCLASS_EXT(obj));
3558 update_m_tbl(
objspace, RCLASS_CALLABLE_M_TBL(obj));
3563 rb_imemo_mark_and_move(obj,
true);
3575 gc_ref_update_array(
objspace, obj);
3580 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
3585 if (STR_SHARED_P(obj)) {
3591 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
3592 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
3593 rb_str_make_embedded(obj);
3605 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3607 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3608 VALUE *ref = (
VALUE *)((
char *)ptr + offset);
3609 *ref = gc_location_internal(
objspace, *ref);
3614 if (compact_func) (*compact_func)(ptr);
3621 gc_ref_update_object(
objspace, obj);
3625 if (
RFILE(obj)->fptr) {
3628 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
3629 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
3630 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
3640 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
3656 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
3657 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
3661 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
3662 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
3668 long i,
len = RSTRUCT_LEN(obj);
3669 VALUE *ptr = (
VALUE *)RSTRUCT_CONST_PTR(obj);
3671 for (i = 0; i <
len; i++) {
3677 rb_bug(
"unreachable");
3694 unless_objspace(
objspace) {
return; }
3696 rb_gc_impl_start(
objspace,
true,
true,
true,
false);
3702 unless_objspace(
objspace) {
return FALSE; }
3704 return rb_gc_impl_during_gc_p(
objspace);
3710 return rb_gc_impl_gc_count(rb_gc_get_objspace());
3714gc_count(rb_execution_context_t *ec,
VALUE self)
3726 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
3729 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
3736gc_stat(rb_execution_context_t *ec,
VALUE self,
VALUE arg)
3745 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3750 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3763 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3768 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3780gc_stat_heap(rb_execution_context_t *ec,
VALUE self,
VALUE heap_name,
VALUE arg)
3786 if (
NIL_P(heap_name)) {
3797 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
3800 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
3805 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3812gc_config_get(rb_execution_context_t *ec,
VALUE self)
3814 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
3815 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
3821gc_config_set(rb_execution_context_t *ec,
VALUE self,
VALUE hash)
3823 void *
objspace = rb_gc_get_objspace();
3825 rb_gc_impl_config_set(
objspace, hash);
3827 return rb_gc_impl_config_get(
objspace);
3831gc_stress_get(rb_execution_context_t *ec,
VALUE self)
3833 return rb_gc_impl_stress_get(rb_gc_get_objspace());
3837gc_stress_set_m(rb_execution_context_t *ec,
VALUE self,
VALUE flag)
3839 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
3845rb_gc_initial_stress_set(
VALUE flag)
3847 initial_stress = flag;
3851rb_gc_heap_sizes(
void)
3853 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
3859 return rb_objspace_gc_enable(rb_gc_get_objspace());
3863rb_objspace_gc_enable(
void *
objspace)
3865 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3867 return RBOOL(disabled);
3871gc_enable(rb_execution_context_t *ec,
VALUE _)
3879 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3880 rb_gc_impl_gc_disable(
objspace,
false);
3881 return RBOOL(disabled);
3885rb_gc_disable_no_rest(
void)
3887 return gc_disable_no_rest(rb_gc_get_objspace());
3893 return rb_objspace_gc_disable(rb_gc_get_objspace());
3897rb_objspace_gc_disable(
void *
objspace)
3899 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3900 rb_gc_impl_gc_disable(
objspace,
true);
3901 return RBOOL(disabled);
3905gc_disable(rb_execution_context_t *ec,
VALUE _)
3912ruby_gc_set_params(
void)
3914 rb_gc_impl_set_params(rb_gc_get_objspace());
3918rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
3922 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
3925 rb_vm_t *vm = GET_VM();
3926 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3927 struct gc_mark_func_data_struct mfd = {
3932 vm->gc.mark_func_data = &mfd;
3933 rb_gc_mark_children(rb_gc_get_objspace(), obj);
3934 vm->gc.mark_func_data = prev_mfd;
3941 const char *category;
3942 void (*func)(
const char *category,
VALUE,
void *);
3947root_objects_from(
VALUE obj,
void *ptr)
3950 (*data->func)(data->category, obj, data->data);
3954rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
3956 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");
3958 rb_vm_t *vm = GET_VM();
3962 .data = passing_data,
3965 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3966 struct gc_mark_func_data_struct mfd = {
3967 .mark_func = root_objects_from,
3971 vm->gc.mark_func_data = &mfd;
3972 rb_gc_save_machine_context();
3973 rb_gc_mark_roots(vm->gc.objspace, &data.category);
3974 vm->gc.mark_func_data = prev_mfd;
3985#define TYPE_NAME(t) case (t): return #t;
4012 if (obj && rb_objspace_data_type_name(obj)) {
4013 return rb_objspace_data_type_name(obj);
4022obj_type_name(
VALUE obj)
4024 return type_name(
TYPE(obj), obj);
4028rb_method_type_name(rb_method_type_t
type)
4031 case VM_METHOD_TYPE_ISEQ:
return "iseq";
4032 case VM_METHOD_TYPE_ATTRSET:
return "attrest";
4033 case VM_METHOD_TYPE_IVAR:
return "ivar";
4034 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
4035 case VM_METHOD_TYPE_ALIAS:
return "alias";
4036 case VM_METHOD_TYPE_REFINED:
return "refined";
4037 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
4038 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
4039 case VM_METHOD_TYPE_MISSING:
return "missing";
4040 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
4041 case VM_METHOD_TYPE_UNDEF:
return "undef";
4042 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
4044 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
4048rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
4050 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
4051 VALUE path = rb_iseq_path(iseq);
4052 int n = ISEQ_BODY(iseq)->location.first_lineno;
4053 snprintf(buff, buff_size,
" %s@%s:%d",
4054 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4055 RSTRING_PTR(path), n);
4060str_len_no_raise(
VALUE str)
4062 long len = RSTRING_LEN(str);
4063 if (
len < 0)
return 0;
4064 if (
len > INT_MAX)
return INT_MAX;
4068#define BUFF_ARGS buff + pos, buff_size - pos
4069#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4070#define APPEND_S(s) do { \
4071 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4075 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4078#define C(c, s) ((c) != 0 ? (s) : " ")
4081rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
4086 APPEND_F(
"%s", obj_type_name(obj));
4092 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
4098 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
4117 if (internal_object_p(obj)) {
4120 else if (
RBASIC(obj)->klass == 0) {
4121 APPEND_S(
"(temporary internal)");
4125 if (!
NIL_P(class_path)) {
4126 APPEND_F(
"(%s)", RSTRING_PTR(class_path));
4135const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
4138rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
4145 UNEXPECTED_NODE(rb_raw_obj_info);
4148 if (ARY_SHARED_P(obj)) {
4149 APPEND_S(
"shared -> ");
4150 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4152 else if (ARY_EMBED_P(obj)) {
4153 APPEND_F(
"[%s%s] len: %ld (embed)",
4154 C(ARY_EMBED_P(obj),
"E"),
4155 C(ARY_SHARED_P(obj),
"S"),
4159 APPEND_F(
"[%s%s] len: %ld, capa:%ld ptr:%p",
4160 C(ARY_EMBED_P(obj),
"E"),
4161 C(ARY_SHARED_P(obj),
"S"),
4163 ARY_EMBED_P(obj) ? -1L :
RARRAY(obj)->as.heap.aux.capa,
4168 if (STR_SHARED_P(obj)) {
4169 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
4172 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
4174 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
4176 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
4180 VALUE fstr = RSYMBOL(obj)->fstr;
4181 ID id = RSYMBOL(obj)->id;
4183 APPEND_F(
":%s id:%d", RSTRING_PTR(fstr), (
unsigned int)
id);
4186 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
4191 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
4195 APPEND_F(
"[%c] %"PRIdSIZE,
4196 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
4204 if (!
NIL_P(class_path)) {
4205 APPEND_F(
"%s", RSTRING_PTR(class_path));
4215 if (!
NIL_P(class_path)) {
4216 APPEND_F(
"src:%s", RSTRING_PTR(class_path));
4222 if (rb_shape_obj_too_complex(obj)) {
4223 size_t hash_len = rb_st_table_size(ROBJECT_IV_HASH(obj));
4224 APPEND_F(
"(too_complex) len:%zu", hash_len);
4227 uint32_t
len = ROBJECT_IV_CAPACITY(obj);
4229 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
4230 APPEND_F(
"(embed) len:%d",
len);
4234 APPEND_F(
"len:%d ptr:%p",
len, (
void *)ptr);
4241 const rb_iseq_t *iseq;
4243 (block = vm_proc_block(obj)) != NULL &&
4244 (vm_block_type(block) == block_type_iseq) &&
4245 (iseq = vm_block_iseq(block)) != NULL) {
4246 rb_raw_iseq_info(BUFF_ARGS, iseq);
4248 else if (rb_ractor_p(obj)) {
4249 rb_ractor_t *r = (
void *)
DATA_PTR(obj);
4251 APPEND_F(
"r:%d", r->pub.id);
4255 const char *
const type_name = rb_objspace_data_type_name(obj);
4257 APPEND_F(
"%s", type_name);
4263 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
4265 switch (imemo_type(obj)) {
4268 const rb_method_entry_t *me = (
const rb_method_entry_t *)obj;
4270 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
4271 rb_id2name(me->called_id),
4272 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
4273 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
4274 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
4275 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
4276 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
4277 me->def ? rb_method_type_name(me->def->type) :
"NULL",
4278 me->def ? me->def->aliased : -1,
4280 (
void *)me->defined_class);
4283 switch (me->def->type) {
4284 case VM_METHOD_TYPE_ISEQ:
4285 APPEND_S(
" (iseq:");
4286 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
4297 const rb_iseq_t *iseq = (
const rb_iseq_t *)obj;
4298 rb_raw_iseq_info(BUFF_ARGS, iseq);
4301 case imemo_callinfo:
4304 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
4305 rb_id2name(vm_ci_mid(ci)),
4308 vm_ci_kwarg(ci) ?
"available" :
"NULL");
4311 case imemo_callcache:
4315 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4317 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
4318 NIL_P(class_path) ? (cc->klass ?
"??" :
"<NULL>") : RSTRING_PTR(class_path),
4319 cme ? rb_id2name(cme->called_id) :
"<NULL>",
4320 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
4322 (
void *)(uintptr_t)vm_cc_call(cc));
4341rb_asan_poison_object(
VALUE obj)
4343 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4344 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
4348rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
4350 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4351 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
4355rb_asan_poisoned_object_p(
VALUE obj)
4357 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4358 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
4361#define asan_unpoisoning_object(obj) \
4362 for (void *poisoned = asan_unpoison_object_temporary(obj), \
4363 *unpoisoning = &poisoned; \
4365 unpoisoning = asan_poison_object_restore(obj, poisoned))
4368rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
4370 asan_unpoisoning_object(obj) {
4371 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4372 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4373 if (pos >= buff_size) {}
4384#define OBJ_INFO_BUFFERS_NUM 10
4385#define OBJ_INFO_BUFFERS_SIZE 0x100
4387static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
4395 if (RB_UNLIKELY(oldval >= maxval - 1)) {
4406 rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
4407 char *
const buff = obj_info_buffers[index];
4408 return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
4414 return obj_type_name(obj);
4432 rb_vraise(argv->exc, argv->fmt, *argv->ap);
4437gc_raise(
VALUE exc,
const char *fmt, ...)
4445 if (ruby_thread_has_gvl_p()) {
4455 fprintf(stderr,
"%s",
"[FATAL] ");
4456 vfprintf(stderr, fmt, ap);
4463NORETURN(
static void negative_size_allocation_error(
const char *));
4465negative_size_allocation_error(
const char *msg)
4471ruby_memerror_body(
void *dummy)
4477NORETURN(
static void ruby_memerror(
void));
4482 if (ruby_thread_has_gvl_p()) {
4491 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4508 rb_execution_context_t *ec = GET_EC();
4509 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
4512 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
4513 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
4514 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4517 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4518 rb_ec_raised_clear(ec);
4521 rb_ec_raised_set(ec, RAISED_NOMEMORY);
4522 exc = ruby_vm_special_exception_copy(exc);
4525 EC_JUMP_TAG(ec, TAG_RAISE);
4529rb_memerror_reentered(
void)
4531 rb_execution_context_t *ec = GET_EC();
4532 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
4536rb_malloc_info_show_results(
void)
4541handle_malloc_failure(
void *ptr)
4552static void *ruby_xmalloc_body(
size_t size);
4555ruby_xmalloc(
size_t size)
4557 return handle_malloc_failure(ruby_xmalloc_body(size));
4561ruby_xmalloc_body(
size_t size)
4563 if ((ssize_t)size < 0) {
4564 negative_size_allocation_error(
"too large allocation size");
4567 return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
4571ruby_malloc_size_overflow(
size_t count,
size_t elsize)
4573 rb_raise(rb_eArgError,
4574 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
4579ruby_malloc_add_size_overflow(
size_t x,
size_t y)
4581 rb_raise(rb_eArgError,
4582 "malloc: possible integer overflow (%"PRIuSIZE
"+%"PRIuSIZE
")",
4586static void *ruby_xmalloc2_body(
size_t n,
size_t size);
4589ruby_xmalloc2(
size_t n,
size_t size)
4591 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
4595ruby_xmalloc2_body(
size_t n,
size_t size)
4597 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4600static void *ruby_xcalloc_body(
size_t n,
size_t size);
4603ruby_xcalloc(
size_t n,
size_t size)
4605 return handle_malloc_failure(ruby_xcalloc_body(n, size));
4609ruby_xcalloc_body(
size_t n,
size_t size)
4611 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4614static void *ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size);
4616#ifdef ruby_sized_xrealloc
4617#undef ruby_sized_xrealloc
4620ruby_sized_xrealloc(
void *ptr,
size_t new_size,
size_t old_size)
4622 return handle_malloc_failure(ruby_sized_xrealloc_body(ptr, new_size, old_size));
4626ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size)
4628 if ((ssize_t)new_size < 0) {
4629 negative_size_allocation_error(
"too large allocation size");
4632 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size);
4636ruby_xrealloc(
void *ptr,
size_t new_size)
4638 return ruby_sized_xrealloc(ptr, new_size, 0);
4641static void *ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n);
4643#ifdef ruby_sized_xrealloc2
4644#undef ruby_sized_xrealloc2
4647ruby_sized_xrealloc2(
void *ptr,
size_t n,
size_t size,
size_t old_n)
4649 return handle_malloc_failure(ruby_sized_xrealloc2_body(ptr, n, size, old_n));
4653ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n)
4655 size_t len = xmalloc2_size(n, size);
4656 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr,
len, old_n * size);
4660ruby_xrealloc2(
void *ptr,
size_t n,
size_t size)
4662 return ruby_sized_xrealloc2(ptr, n, size, 0);
4665#ifdef ruby_sized_xfree
4666#undef ruby_sized_xfree
4669ruby_sized_xfree(
void *x,
size_t size)
4675 if (LIKELY(GET_VM())) {
4676 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
4687 ruby_sized_xfree(x, 0);
4691rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
4693 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4694 return ruby_xmalloc(w);
4698rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
4700 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4701 return ruby_xcalloc(w, 1);
4705rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
4707 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4708 return ruby_xrealloc((
void *)p, w);
4712rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4714 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4715 return ruby_xmalloc(u);
4719rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4721 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4722 return ruby_xcalloc(u, 1);
4729ruby_mimmalloc(
size_t size)
4732#if CALC_EXACT_MALLOC_SIZE
4736#if CALC_EXACT_MALLOC_SIZE
4752ruby_mimcalloc(
size_t num,
size_t size)
4755#if CALC_EXACT_MALLOC_SIZE
4756 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(num, size);
4757 if (UNLIKELY(t.overflowed)) {
4761 mem = calloc1(size);
4773 mem = calloc(num, size);
4779ruby_mimfree(
void *ptr)
4781#if CALC_EXACT_MALLOC_SIZE
4789rb_gc_adjust_memory_usage(ssize_t diff)
4791 unless_objspace(
objspace) {
return; }
4793 rb_gc_impl_adjust_memory_usage(
objspace, diff);
4797rb_obj_info(
VALUE obj)
4799 return obj_info(obj);
4803rb_obj_info_dump(
VALUE obj)
4806 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
4810rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
4813 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
4817rb_gc_before_fork(
void)
4819 rb_gc_impl_before_fork(rb_gc_get_objspace());
4823rb_gc_after_fork(rb_pid_t pid)
4825 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
4879 malloc_offset = gc_compute_malloc_offset();
4892 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
4915ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
4917#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
4920 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.
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.