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/concurrent_set.h"
95#include "internal/cont.h"
96#include "internal/error.h"
97#include "internal/eval.h"
98#include "internal/gc.h"
99#include "internal/hash.h"
100#include "internal/imemo.h"
101#include "internal/io.h"
102#include "internal/numeric.h"
103#include "internal/object.h"
104#include "internal/proc.h"
105#include "internal/rational.h"
106#include "internal/sanitizers.h"
107#include "internal/struct.h"
108#include "internal/symbol.h"
109#include "internal/thread.h"
110#include "internal/variable.h"
111#include "internal/warnings.h"
121#include "ruby_assert.h"
122#include "ruby_atomic.h"
127#include "vm_callinfo.h"
128#include "ractor_core.h"
136rb_gc_vm_lock(
const char *file,
int line)
138 unsigned int lev = 0;
139 rb_vm_lock_enter(&lev, file, line);
144rb_gc_vm_unlock(
unsigned int lev,
const char *file,
int line)
146 rb_vm_lock_leave(&lev, file, line);
150rb_gc_cr_lock(
const char *file,
int line)
153 rb_vm_lock_enter_cr(GET_RACTOR(), &lev, file, line);
158rb_gc_cr_unlock(
unsigned int lev,
const char *file,
int line)
160 rb_vm_lock_leave_cr(GET_RACTOR(), &lev, file, line);
164rb_gc_vm_lock_no_barrier(
const char *file,
int line)
166 unsigned int lev = 0;
167 rb_vm_lock_enter_nb(&lev, file, line);
172rb_gc_vm_unlock_no_barrier(
unsigned int lev,
const char *file,
int line)
174 rb_vm_lock_leave_nb(&lev, file, line);
178rb_gc_vm_barrier(
void)
184rb_gc_get_ractor_newobj_cache(
void)
186 return GET_RACTOR()->newobj_cache;
191rb_gc_initialize_vm_context(
struct rb_gc_vm_context *context)
194 context->ec = GET_EC();
198rb_gc_worker_thread_set_vm_context(
struct rb_gc_vm_context *context)
202 GC_ASSERT(rb_current_execution_context(
false) == NULL);
204#ifdef RB_THREAD_LOCAL_SPECIFIER
205 rb_current_ec_set(context->ec);
207 native_tls_set(ruby_current_ec_key, context->ec);
212rb_gc_worker_thread_unset_vm_context(
struct rb_gc_vm_context *context)
216 GC_ASSERT(rb_current_execution_context(
true) == context->ec);
218#ifdef RB_THREAD_LOCAL_SPECIFIER
219 rb_current_ec_set(NULL);
221 native_tls_set(ruby_current_ec_key, NULL);
229 return ruby_vm_event_flags & event;
235 if (LIKELY(!rb_gc_event_hook_required_p(event)))
return;
237 rb_execution_context_t *ec = GET_EC();
238 if (!ec->cfp)
return;
240 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
244rb_gc_get_objspace(
void)
246 return GET_VM()->gc.objspace;
251rb_gc_ractor_newobj_cache_foreach(
void (*func)(
void *cache,
void *data),
void *data)
253 rb_ractor_t *r = NULL;
254 if (RB_LIKELY(ruby_single_main_ractor)) {
256 ccan_list_empty(&GET_VM()->ractor.set) ||
257 (ccan_list_top(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor &&
258 ccan_list_tail(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor)
261 func(ruby_single_main_ractor->newobj_cache, data);
264 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
265 func(r->newobj_cache, data);
271rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
276 rb_control_frame_t *cfp;
281 rb_execution_context_t *
volatile ec = GET_EC();
282#define RESTORE_FINALIZER() (\
283 ec->cfp = saved.cfp, \
284 ec->cfp->sp = saved.sp, \
285 ec->errinfo = saved.errinfo)
287 saved.errinfo = ec->errinfo;
289 saved.sp = ec->cfp->sp;
293 ASSERT_vm_unlocking();
294 rb_ractor_ignore_belonging(
true);
296 enum ruby_tag_type state = EC_EXEC_TAG();
297 if (state != TAG_NONE) {
300 VALUE failed_final = saved.final;
303 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
304 rb_ec_error_print(ec, ec->errinfo);
308 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
309 saved.final = callback(i, data);
313 rb_ractor_ignore_belonging(
false);
314#undef RESTORE_FINALIZER
318rb_gc_set_pending_interrupt(
void)
320 rb_execution_context_t *ec = GET_EC();
321 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
325rb_gc_unset_pending_interrupt(
void)
327 rb_execution_context_t *ec = GET_EC();
328 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
332rb_gc_multi_ractor_p(
void)
334 return rb_multi_ractor_p();
338rb_gc_shutdown_call_finalizer_p(
VALUE obj)
342 if (!ruby_free_at_exit_p() && (!
DATA_PTR(obj) || !
RDATA(obj)->dfree))
return false;
343 if (rb_obj_is_thread(obj))
return false;
344 if (rb_obj_is_mutex(obj))
return false;
345 if (rb_obj_is_fiber(obj))
return false;
346 if (rb_ractor_p(obj))
return false;
347 if (rb_obj_is_fstring_table(obj))
return false;
348 if (rb_obj_is_symbol_table(obj))
return false;
362 return ruby_free_at_exit_p();
367rb_gc_get_shape(
VALUE obj)
369 return (uint32_t)rb_obj_shape_id(obj);
373rb_gc_set_shape(
VALUE obj, uint32_t shape_id)
375 RBASIC_SET_SHAPE_ID(obj, (uint32_t)shape_id);
379rb_gc_rebuild_shape(
VALUE obj,
size_t heap_id)
383 return (uint32_t)rb_shape_transition_heap(obj, heap_id);
386void rb_vm_update_references(
void *
ptr);
388#define rb_setjmp(env) RUBY_SETJMP(env)
389#define rb_jmp_buf rb_jmpbuf_t
390#undef rb_data_object_wrap
392#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
393#define MAP_ANONYMOUS MAP_ANON
396#define unless_objspace(objspace) \
398 rb_vm_t *unless_objspace_vm = GET_VM(); \
399 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
402#define RMOVED(obj) ((struct RMoved *)(obj))
404#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
405 if (gc_object_moved_p_internal((_objspace), (VALUE)(_thing))) { \
406 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
410#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
412#if RUBY_MARK_FREE_DEBUG
413int ruby_gc_debug_indent = 0;
416#ifndef RGENGC_OBJ_INFO
417# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
420#ifndef CALC_EXACT_MALLOC_SIZE
421# define CALC_EXACT_MALLOC_SIZE 0
426static size_t malloc_offset = 0;
427#if defined(HAVE_MALLOC_USABLE_SIZE)
429gc_compute_malloc_offset(
void)
440 for (offset = 0; offset <= 16; offset += 8) {
441 size_t allocated = (64 - offset);
442 void *test_ptr = malloc(allocated);
443 size_t wasted = malloc_usable_size(test_ptr) - allocated;
454gc_compute_malloc_offset(
void)
462rb_malloc_grow_capa(
size_t current,
size_t type_size)
464 size_t current_capacity = current;
465 if (current_capacity < 4) {
466 current_capacity = 4;
468 current_capacity *= type_size;
471 size_t new_capacity = (current_capacity * 2);
474 if (rb_popcount64(new_capacity) != 1) {
475 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
478 new_capacity -= malloc_offset;
479 new_capacity /= type_size;
480 if (current > new_capacity) {
481 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
487static inline struct rbimpl_size_overflow_tag
488size_mul_add_overflow(size_t x, size_t y, size_t z)
490 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
491 struct rbimpl_size_overflow_tag u = rbimpl_size_add_overflow(t.result, z);
492 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed, u.result };
495static inline struct rbimpl_size_overflow_tag
496size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
498 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
499 struct rbimpl_size_overflow_tag u = rbimpl_size_mul_overflow(z, w);
500 struct rbimpl_size_overflow_tag v = rbimpl_size_add_overflow(t.result, u.result);
501 return (
struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed || v.overflowed, v.result };
504PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
507size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
509 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
510 if (LIKELY(!t.overflowed)) {
513 else if (rb_during_gc()) {
519 "integer overflow: %"PRIuSIZE
522 x, y, (
size_t)SIZE_MAX);
527rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
529 return size_mul_or_raise(x, y, exc);
533size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
535 struct rbimpl_size_overflow_tag t = size_mul_add_overflow(x, y, z);
536 if (LIKELY(!t.overflowed)) {
539 else if (rb_during_gc()) {
545 "integer overflow: %"PRIuSIZE
549 x, y, z, (
size_t)SIZE_MAX);
554rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
556 return size_mul_add_or_raise(x, y, z, exc);
560size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
562 struct rbimpl_size_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
563 if (LIKELY(!t.overflowed)) {
566 else if (rb_during_gc()) {
572 "integer overflow: %"PRIdSIZE
577 x, y, z, w, (
size_t)SIZE_MAX);
581#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
583volatile VALUE rb_gc_guarded_val;
587 rb_gc_guarded_val = val;
593static const char *obj_type_name(
VALUE obj);
594#include "gc/default/default.c"
596#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
597# error "Modular GC requires dlopen"
601typedef struct gc_function_map {
603 void *(*objspace_alloc)(void);
604 void (*objspace_init)(
void *objspace_ptr);
605 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
606 void (*set_params)(
void *objspace_ptr);
608 size_t *(*heap_sizes)(
void *objspace_ptr);
610 void (*shutdown_free_objects)(
void *objspace_ptr);
611 void (*objspace_free)(
void *objspace_ptr);
612 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
614 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
615 bool (*during_gc_p)(
void *objspace_ptr);
616 void (*prepare_heap)(
void *objspace_ptr);
617 void (*gc_enable)(
void *objspace_ptr);
618 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
619 bool (*gc_enabled_p)(
void *objspace_ptr);
620 VALUE (*config_get)(
void *objpace_ptr);
621 void (*config_set)(
void *objspace_ptr,
VALUE hash);
622 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
623 VALUE (*stress_get)(
void *objspace_ptr);
625 VALUE (*new_obj)(
void *objspace_ptr,
void *cache_ptr,
VALUE klass,
VALUE flags,
bool wb_protected,
size_t alloc_size);
626 size_t (*obj_slot_size)(
VALUE obj);
627 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
628 bool (*size_allocatable_p)(
size_t size);
630 void *(*malloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
631 void *(*calloc)(
void *objspace_ptr,
size_t size,
bool gc_allowed);
632 void *(*realloc)(
void *objspace_ptr,
void *
ptr,
size_t new_size,
size_t old_size,
bool gc_allowed);
633 void (*free)(
void *objspace_ptr,
void *
ptr,
size_t old_size);
634 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
636 void (*mark)(
void *objspace_ptr,
VALUE obj);
637 void (*mark_and_move)(
void *objspace_ptr,
VALUE *
ptr);
638 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
639 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
640 void (*mark_weak)(
void *objspace_ptr,
VALUE *
ptr);
641 void (*remove_weak)(
void *objspace_ptr,
VALUE parent_obj,
VALUE *
ptr);
643 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
644 bool (*pinned_p)(
void *objspace_ptr,
VALUE obj);
645 VALUE (*location)(
void *objspace_ptr,
VALUE value);
647 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
648 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
649 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
651 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
652 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
654 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
656 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
657 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
658 void (*shutdown_call_finalizer)(
void *objspace_ptr);
660 void (*before_fork)(
void *objspace_ptr);
661 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
663 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
664 bool (*get_measure_total_time)(
void *objspace_ptr);
665 unsigned long long (*get_total_time)(
void *objspace_ptr);
666 size_t (*gc_count)(
void *objspace_ptr);
667 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
668 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
669 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
670 const char *(*active_gc_name)(void);
672 struct rb_gc_object_metadata_entry *(*object_metadata)(
void *objspace_ptr,
VALUE obj);
673 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *
ptr);
674 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
675 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
676 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
678 bool modular_gc_loaded_p;
679} rb_gc_function_map_t;
681static rb_gc_function_map_t rb_gc_functions;
683# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
684# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
687ruby_modular_gc_init(
void)
692 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
694 rb_gc_function_map_t gc_functions = { 0 };
696 char *gc_so_path = NULL;
701 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
702 char c = gc_so_file[i];
703 if (isalnum(c))
continue;
709 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
714 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
717 size_t prefix_len = 0;
718 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
719 const char *base = strrchr(dli.dli_fname,
'/');
722# define end_with_p(lit) \
723 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
724 memcmp(base - tail, lit, tail) == 0)
726 prefix_len = base - dli.dli_fname;
727 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
730 prefix_len += MODULAR_GC_DIR[0] !=
'/';
731 gc_so_path_size += prefix_len;
735 gc_so_path = alloca(gc_so_path_size);
737 size_t gc_so_path_idx = 0;
738#define GC_SO_PATH_APPEND(str) do { \
739 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
742 if (prefix_len > 0) {
743 memcpy(gc_so_path, dli.dli_fname, prefix_len);
744 gc_so_path_idx = prefix_len;
747 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
748 GC_SO_PATH_APPEND(gc_so_file);
749 GC_SO_PATH_APPEND(DLEXT);
750 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
751#undef GC_SO_PATH_APPEND
754 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
756 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
760 gc_functions.modular_gc_loaded_p =
true;
763# define load_modular_gc_func(name) do { \
765 const char *func_name = "rb_gc_impl_" #name; \
766 gc_functions.name = dlsym(handle, func_name); \
767 if (!gc_functions.name) { \
768 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
773 gc_functions.name = rb_gc_impl_##name; \
778 load_modular_gc_func(objspace_alloc);
779 load_modular_gc_func(objspace_init);
780 load_modular_gc_func(ractor_cache_alloc);
781 load_modular_gc_func(set_params);
782 load_modular_gc_func(init);
783 load_modular_gc_func(heap_sizes);
785 load_modular_gc_func(shutdown_free_objects);
786 load_modular_gc_func(objspace_free);
787 load_modular_gc_func(ractor_cache_free);
789 load_modular_gc_func(start);
790 load_modular_gc_func(during_gc_p);
791 load_modular_gc_func(prepare_heap);
792 load_modular_gc_func(gc_enable);
793 load_modular_gc_func(gc_disable);
794 load_modular_gc_func(gc_enabled_p);
795 load_modular_gc_func(config_set);
796 load_modular_gc_func(config_get);
797 load_modular_gc_func(stress_set);
798 load_modular_gc_func(stress_get);
800 load_modular_gc_func(new_obj);
801 load_modular_gc_func(obj_slot_size);
802 load_modular_gc_func(heap_id_for_size);
803 load_modular_gc_func(size_allocatable_p);
805 load_modular_gc_func(malloc);
806 load_modular_gc_func(calloc);
807 load_modular_gc_func(realloc);
808 load_modular_gc_func(free);
809 load_modular_gc_func(adjust_memory_usage);
811 load_modular_gc_func(mark);
812 load_modular_gc_func(mark_and_move);
813 load_modular_gc_func(mark_and_pin);
814 load_modular_gc_func(mark_maybe);
815 load_modular_gc_func(mark_weak);
816 load_modular_gc_func(remove_weak);
818 load_modular_gc_func(object_moved_p);
819 load_modular_gc_func(pinned_p);
820 load_modular_gc_func(location);
822 load_modular_gc_func(writebarrier);
823 load_modular_gc_func(writebarrier_unprotect);
824 load_modular_gc_func(writebarrier_remember);
826 load_modular_gc_func(each_objects);
827 load_modular_gc_func(each_object);
829 load_modular_gc_func(make_zombie);
830 load_modular_gc_func(define_finalizer);
831 load_modular_gc_func(undefine_finalizer);
832 load_modular_gc_func(copy_finalizer);
833 load_modular_gc_func(shutdown_call_finalizer);
835 load_modular_gc_func(before_fork);
836 load_modular_gc_func(after_fork);
838 load_modular_gc_func(set_measure_total_time);
839 load_modular_gc_func(get_measure_total_time);
840 load_modular_gc_func(get_total_time);
841 load_modular_gc_func(gc_count);
842 load_modular_gc_func(latest_gc_info);
843 load_modular_gc_func(stat);
844 load_modular_gc_func(stat_heap);
845 load_modular_gc_func(active_gc_name);
847 load_modular_gc_func(object_metadata);
848 load_modular_gc_func(pointer_to_heap_p);
849 load_modular_gc_func(garbage_object_p);
850 load_modular_gc_func(set_event_hook);
851 load_modular_gc_func(copy_attributes);
853# undef load_modular_gc_func
855 rb_gc_functions = gc_functions;
859# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
860# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
861# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
862# define rb_gc_impl_set_params rb_gc_functions.set_params
863# define rb_gc_impl_init rb_gc_functions.init
864# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
866# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
867# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
868# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
870# define rb_gc_impl_start rb_gc_functions.start
871# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
872# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
873# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
874# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
875# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
876# define rb_gc_impl_config_get rb_gc_functions.config_get
877# define rb_gc_impl_config_set rb_gc_functions.config_set
878# define rb_gc_impl_stress_set rb_gc_functions.stress_set
879# define rb_gc_impl_stress_get rb_gc_functions.stress_get
881# define rb_gc_impl_new_obj rb_gc_functions.new_obj
882# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
883# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
884# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
886# define rb_gc_impl_malloc rb_gc_functions.malloc
887# define rb_gc_impl_calloc rb_gc_functions.calloc
888# define rb_gc_impl_realloc rb_gc_functions.realloc
889# define rb_gc_impl_free rb_gc_functions.free
890# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
892# define rb_gc_impl_mark rb_gc_functions.mark
893# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
894# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
895# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
896# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
897# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
899# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
900# define rb_gc_impl_pinned_p rb_gc_functions.pinned_p
901# define rb_gc_impl_location rb_gc_functions.location
903# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
904# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
905# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
907# define rb_gc_impl_each_objects rb_gc_functions.each_objects
908# define rb_gc_impl_each_object rb_gc_functions.each_object
910# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
911# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
912# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
913# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
914# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
916# define rb_gc_impl_before_fork rb_gc_functions.before_fork
917# define rb_gc_impl_after_fork rb_gc_functions.after_fork
919# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
920# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
921# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
922# define rb_gc_impl_gc_count rb_gc_functions.gc_count
923# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
924# define rb_gc_impl_stat rb_gc_functions.stat
925# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
926# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
928# define rb_gc_impl_object_metadata rb_gc_functions.object_metadata
929# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
930# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
931# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
932# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
935#ifdef RUBY_ASAN_ENABLED
937asan_death_callback(
void)
940 rb_bug_without_die(
"ASAN error");
948rb_objspace_alloc(
void)
951 ruby_modular_gc_init();
954 void *
objspace = rb_gc_impl_objspace_alloc();
955 ruby_current_vm_ptr->gc.objspace =
objspace;
957 rb_gc_impl_stress_set(
objspace, initial_stress);
959#ifdef RUBY_ASAN_ENABLED
960 __sanitizer_set_death_callback(asan_death_callback);
973rb_gc_obj_slot_size(
VALUE obj)
975 return rb_gc_impl_obj_slot_size(obj);
979gc_validate_pc(
VALUE obj)
985 rb_execution_context_t *ec = GET_EC();
986 const rb_control_frame_t *cfp = ec->cfp;
987 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc) {
988 const VALUE *iseq_encoded = ISEQ_BODY(cfp->iseq)->iseq_encoded;
989 const VALUE *iseq_encoded_end = iseq_encoded + ISEQ_BODY(cfp->iseq)->iseq_size;
990 RUBY_ASSERT(cfp->pc >= iseq_encoded,
"PC not set when allocating, breaking tracing");
991 RUBY_ASSERT(cfp->pc <= iseq_encoded_end,
"PC not set when allocating, breaking tracing");
997newobj_of(rb_ractor_t *cr,
VALUE klass,
VALUE flags, shape_id_t shape_id,
bool wb_protected,
size_t size)
999 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, wb_protected, size);
1000 RBASIC_SET_SHAPE_ID_NO_CHECKS(obj, shape_id);
1002 gc_validate_pc(obj);
1005 int lev = RB_GC_VM_LOCK_NO_BARRIER();
1007 size_t slot_size = rb_gc_obj_slot_size(obj);
1008 if (slot_size > RVALUE_SIZE) {
1009 memset((
char *)obj + RVALUE_SIZE, 0, slot_size - RVALUE_SIZE);
1017 bool gc_disabled =
RTEST(rb_gc_disable_no_rest());
1021 if (!gc_disabled) rb_gc_enable();
1023 RB_GC_VM_UNLOCK_NO_BARRIER(lev);
1026#if RGENGC_CHECK_MODE
1027# ifndef GC_DEBUG_SLOT_FILL_SPECIAL_VALUE
1028# define GC_DEBUG_SLOT_FILL_SPECIAL_VALUE 255
1032 (
void *)(obj +
sizeof(
struct RBasic)),
1033 GC_DEBUG_SLOT_FILL_SPECIAL_VALUE,
1034 rb_gc_obj_slot_size(obj) -
sizeof(
struct RBasic)
1042rb_wb_unprotected_newobj_of(
VALUE klass,
VALUE flags, shape_id_t shape_id,
size_t size)
1045 return newobj_of(GET_RACTOR(), klass, flags, shape_id, FALSE, size);
1049rb_wb_protected_newobj_of(rb_execution_context_t *ec,
VALUE klass,
VALUE flags, shape_id_t shape_id,
size_t size)
1052 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, shape_id, TRUE, size);
1055#define UNEXPECTED_NODE(func) \
1056 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1057 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1060rb_data_object_check(
VALUE klass)
1062 if (klass != rb_cObject && (
rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
1064 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1072 if (klass) rb_data_object_check(klass);
1073 VALUE obj = newobj_of(GET_RACTOR(), klass,
T_DATA, ROOT_SHAPE_ID, !dmark,
sizeof(
struct RTypedData));
1094 RBIMPL_NONNULL_ARG(
type);
1095 if (klass) rb_data_object_check(klass);
1097 VALUE obj = newobj_of(GET_RACTOR(), klass,
T_DATA | RUBY_TYPED_FL_IS_TYPED_DATA, ROOT_SHAPE_ID, wb_protected, size);
1110 if (UNLIKELY(
type->flags & RUBY_TYPED_EMBEDDABLE)) {
1111 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1114 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
1120 if (
type->flags & RUBY_TYPED_EMBEDDABLE) {
1121 if (!(
type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1122 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1126 if (rb_gc_size_allocatable_p(embed_size)) {
1127 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1128 memset((
char *)obj + offsetof(
struct RTypedData,
data), 0, size);
1139rb_objspace_data_type_memsize(
VALUE obj)
1142 if (RTYPEDDATA_P(obj)) {
1144 const void *
ptr = RTYPEDDATA_GET_DATA(obj);
1146 if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1147#ifdef HAVE_MALLOC_USABLE_SIZE
1148 size += malloc_usable_size((
void *)
ptr);
1152 if (
ptr &&
type->function.dsize) {
1153 size +=
type->function.dsize(
ptr);
1161rb_objspace_data_type_name(
VALUE obj)
1163 if (RTYPEDDATA_P(obj)) {
1164 return RTYPEDDATA_TYPE(obj)->wrap_struct_name;
1172io_fptr_finalize(
void *fptr)
1180 rb_io_t *fptr =
RFILE(obj)->fptr;
1181 rb_gc_impl_make_zombie(
objspace, obj, io_fptr_finalize, fptr);
1187 bool typed = RTYPEDDATA_P(obj);
1188 void *
data = typed ? RTYPEDDATA_GET_DATA(obj) :
DATA_PTR(obj);
1190 int free_immediately =
false;
1191 bool embedded =
false;
1192 bool free_embeddable_data =
false;
1193 void (*dfree)(
void *);
1197 dfree =
type->function.dfree;
1199 embedded = RTYPEDDATA_EMBEDDED_P(obj);
1200 free_immediately = (
type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1201 free_embeddable_data = (
type->flags & RUBY_TYPED_EMBEDDABLE) && !embedded;
1205 dfree =
RDATA(obj)->dfree;
1210 if (!typed || !embedded) {
1212 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1215 else if (free_immediately) {
1217 if (free_embeddable_data) {
1221 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1225 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1230 RB_DEBUG_COUNTER_INC(obj_data_empty);
1239 rb_objspace_t *objspace;
1243classext_free(rb_classext_t *ext,
bool is_prime,
VALUE box_value,
void *arg)
1247 rb_class_classext_free(args->klass, ext, is_prime);
1251classext_iclass_free(rb_classext_t *ext,
bool is_prime,
VALUE box_value,
void *arg)
1255 rb_iclass_classext_free(args->klass, ext, is_prime);
1263 RB_DEBUG_COUNTER_INC(obj_free);
1270 rb_bug(
"obj_free() called for broken object");
1279 if (rb_shape_obj_too_complex_p(obj)) {
1280 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1281 st_free_table(ROBJECT_FIELDS_HASH(obj));
1285 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1289 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1295 rb_zjit_klass_free(obj);
1298 rb_class_classext_foreach(obj, classext_free, (
void *)&args);
1299 if (RCLASS_CLASSEXT_TBL(obj)) {
1300 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1312#if USE_DEBUG_COUNTER
1315 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1318 RB_DEBUG_COUNTER_INC(obj_hash_1);
1321 RB_DEBUG_COUNTER_INC(obj_hash_2);
1324 RB_DEBUG_COUNTER_INC(obj_hash_3);
1327 RB_DEBUG_COUNTER_INC(obj_hash_4);
1333 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1337 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1340 if (RHASH_AR_TABLE_P(obj)) {
1341 if (RHASH_AR_TABLE(obj) == NULL) {
1342 RB_DEBUG_COUNTER_INC(obj_hash_null);
1345 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1349 RB_DEBUG_COUNTER_INC(obj_hash_st);
1358 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1362 if (!rb_data_free(
objspace, obj))
return false;
1366 rb_matchext_t *rm = RMATCH_EXT(obj);
1367#if USE_DEBUG_COUNTER
1368 if (rm->
regs.num_regs >= 8) {
1369 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1371 else if (rm->
regs.num_regs >= 4) {
1372 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1374 else if (rm->
regs.num_regs >= 1) {
1375 RB_DEBUG_COUNTER_INC(obj_match_under4);
1378 onig_region_free(&rm->
regs, 0);
1381 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1385 if (
RFILE(obj)->fptr) {
1387 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1392 RB_DEBUG_COUNTER_INC(obj_rational);
1395 RB_DEBUG_COUNTER_INC(obj_complex);
1402 rb_class_classext_foreach(obj, classext_iclass_free, (
void *)&args);
1403 if (RCLASS_CLASSEXT_TBL(obj)) {
1404 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1407 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1411 RB_DEBUG_COUNTER_INC(obj_float);
1415 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1416 xfree(BIGNUM_DIGITS(obj));
1417 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1420 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1425 UNEXPECTED_NODE(obj_free);
1429 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1430 RSTRUCT(obj)->as.heap.ptr == NULL) {
1431 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1434 xfree((
void *)RSTRUCT(obj)->as.heap.ptr);
1435 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1440 RB_DEBUG_COUNTER_INC(obj_symbol);
1444 rb_imemo_free((
VALUE)obj);
1448 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1453 rb_gc_impl_make_zombie(
objspace, obj, 0, 0);
1464 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1468internal_object_p(
VALUE obj)
1470 void *
ptr = asan_unpoison_object_temporary(obj);
1472 if (
RBASIC(obj)->flags) {
1475 UNEXPECTED_NODE(internal_object_p);
1484 if (obj == rb_mRubyVMFrozenCore)
1487 if (!RBASIC_CLASS(obj))
break;
1488 if (RCLASS_SINGLETON_P(obj)) {
1489 return rb_singleton_class_internal_p(obj);
1493 if (!
RBASIC(obj)->klass)
break;
1498 rb_asan_poison_object(obj);
1504rb_objspace_internal_object_p(
VALUE obj)
1506 return internal_object_p(obj);
1515os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1520 for (; v != (
VALUE)vend; v += stride) {
1521 if (!internal_object_p(v)) {
1523 if (!rb_multi_ractor_p() || rb_ractor_shareable_p(v)) {
1541 rb_objspace_each_objects(os_obj_of_i, &oes);
1590 of = (!rb_check_arity(argc, 0, 1) ? 0 : argv[0]);
1592 return os_obj_of(of);
1606 return rb_undefine_finalizer(obj);
1612 rb_check_frozen(obj);
1614 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1620should_be_callable(
VALUE block)
1623 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
1629should_be_finalizable(
VALUE obj)
1632 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
1635 rb_check_frozen(obj);
1641 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1716 if (rb_callable_receiver(block) == obj) {
1717 rb_warn(
"finalizer references object to be finalized");
1720 return rb_define_finalizer(obj, block);
1726 should_be_finalizable(obj);
1727 should_be_callable(block);
1729 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1737rb_objspace_call_finalizer(
void)
1739 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1743rb_objspace_free_objects(
void *
objspace)
1745 rb_gc_impl_shutdown_free_objects(
objspace);
1749rb_objspace_garbage_object_p(
VALUE obj)
1751 return !
SPECIAL_CONST_P(obj) && rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1755rb_gc_pointer_to_heap_p(
VALUE obj)
1757 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj);
1760#define OBJ_ID_INCREMENT (RUBY_IMMEDIATE_MASK + 1)
1761#define LAST_OBJECT_ID() (object_id_counter * OBJ_ID_INCREMENT)
1762static VALUE id2ref_value = 0;
1763static st_table *id2ref_tbl = NULL;
1765#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1766static size_t object_id_counter = 1;
1768static unsigned long long object_id_counter = 1;
1772generate_next_object_id(
void)
1774#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1778 unsigned int lock_lev = RB_GC_VM_LOCK();
1779 VALUE id =
ULL2NUM(++object_id_counter * OBJ_ID_INCREMENT);
1780 RB_GC_VM_UNLOCK(lock_lev);
1786rb_gc_obj_id_moved(
VALUE obj)
1788 if (UNLIKELY(id2ref_tbl)) {
1789 st_insert(id2ref_tbl, (st_data_t)rb_obj_id(obj), (st_data_t)obj);
1794object_id_cmp(st_data_t x, st_data_t y)
1797 return !rb_big_eql(x, y);
1805object_id_hash(st_data_t n)
1810static const struct st_hash_type object_id_hash_type = {
1815static void gc_mark_tbl_no_pin(st_table *table);
1818id2ref_tbl_mark(
void *data)
1820 st_table *table = (st_table *)data;
1830id2ref_tbl_memsize(
const void *data)
1832 return rb_st_memsize(data);
1836id2ref_tbl_free(
void *data)
1839 st_table *table = (st_table *)data;
1840 st_free_table(table);
1844 .wrap_struct_name =
"VM/_id2ref_table",
1846 .dmark = id2ref_tbl_mark,
1847 .dfree = id2ref_tbl_free,
1848 .dsize = id2ref_tbl_memsize,
1852 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
1856class_object_id(
VALUE klass)
1858 VALUE id = RUBY_ATOMIC_VALUE_LOAD(
RCLASS(klass)->object_id);
1860 unsigned int lock_lev = RB_GC_VM_LOCK();
1861 id = generate_next_object_id();
1866 else if (RB_UNLIKELY(id2ref_tbl)) {
1867 st_insert(id2ref_tbl,
id, klass);
1869 RB_GC_VM_UNLOCK(lock_lev);
1875object_id_get(
VALUE obj, shape_id_t shape_id)
1878 if (rb_shape_too_complex_p(shape_id)) {
1879 id = rb_obj_field_get(obj, ROOT_TOO_COMPLEX_WITH_OBJ_ID);
1882 id = rb_obj_field_get(obj, rb_shape_object_id(shape_id));
1888 rb_bug(
"Object's shape includes object_id, but it's missing %s", rb_obj_info(obj));
1896object_id0(
VALUE obj)
1899 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1901 if (rb_shape_has_object_id(shape_id)) {
1902 return object_id_get(obj, shape_id);
1905 shape_id_t object_id_shape_id = rb_shape_transition_object_id(obj);
1907 id = generate_next_object_id();
1908 rb_obj_field_set(obj, object_id_shape_id, 0,
id);
1910 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == object_id_shape_id);
1913 if (RB_UNLIKELY(id2ref_tbl)) {
1915 st_insert(id2ref_tbl, (st_data_t)
id, (st_data_t)obj);
1930 return class_object_id(obj);
1938 if (UNLIKELY(rb_gc_multi_ractor_p() && rb_ractor_shareable_p(obj))) {
1939 unsigned int lock_lev = RB_GC_VM_LOCK();
1940 VALUE id = object_id0(obj);
1941 RB_GC_VM_UNLOCK(lock_lev);
1945 return object_id0(obj);
1949build_id2ref_i(
VALUE obj,
void *data)
1951 st_table *id2ref_tbl = (st_table *)data;
1957 if (
RCLASS(obj)->object_id) {
1958 st_insert(id2ref_tbl,
RCLASS(obj)->object_id, obj);
1963 if (IMEMO_TYPE_P(obj, imemo_fields) && rb_shape_obj_has_id(obj)) {
1964 st_insert(id2ref_tbl, rb_obj_id(obj), rb_imemo_fields_owner(obj));
1969 if (rb_shape_obj_has_id(obj)) {
1970 st_insert(id2ref_tbl, rb_obj_id(obj), obj);
1980object_id_to_ref(
void *objspace_ptr,
VALUE object_id)
1982 rb_objspace_t *
objspace = objspace_ptr;
1984 unsigned int lev = RB_GC_VM_LOCK();
1992 st_table *tmp_id2ref_tbl = st_init_table(&object_id_hash_type);
1998 bool gc_disabled =
RTEST(rb_gc_disable());
2000 id2ref_tbl = tmp_id2ref_tbl;
2001 id2ref_value = tmp_id2ref_value;
2003 rb_gc_impl_each_object(
objspace, build_id2ref_i, (
void *)id2ref_tbl);
2005 if (!gc_disabled) rb_gc_enable();
2009 bool found = st_lookup(id2ref_tbl, object_id, &obj) && !rb_gc_impl_garbage_object_p(
objspace, obj);
2011 RB_GC_VM_UNLOCK(lev);
2018 rb_raise(
rb_eRangeError,
"%"PRIsVALUE
" is not an id value", object_id);
2021 rb_raise(
rb_eRangeError,
"%"PRIsVALUE
" is a recycled object", object_id);
2026obj_free_object_id(
VALUE obj)
2029 if (RB_UNLIKELY(id2ref_tbl)) {
2033 obj_id =
RCLASS(obj)->object_id;
2036 if (!IMEMO_TYPE_P(obj, imemo_fields)) {
2042 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2043 if (rb_shape_has_object_id(shape_id)) {
2044 obj_id = object_id_get(obj, shape_id);
2053 if (RB_UNLIKELY(obj_id)) {
2056 if (!st_delete(id2ref_tbl, (st_data_t *)&obj_id, NULL)) {
2059 if (!RB_TYPE_P(obj,
T_IMEMO)) {
2060 rb_bug(
"Object ID seen, but not in _id2ref table: object_id=%llu object=%s",
NUM2ULL(obj_id), rb_obj_info(obj));
2068rb_gc_obj_free_vm_weak_references(
VALUE obj)
2070 ASSUME(!RB_SPECIAL_CONST_P(obj));
2071 obj_free_object_id(obj);
2073 if (rb_obj_gen_fields_p(obj)) {
2080 rb_gc_free_fstring(obj);
2084 rb_gc_free_dsymbol(obj);
2087 switch (imemo_type(obj)) {
2088 case imemo_callcache: {
2091 if (vm_cc_refinement_p(cc)) {
2092 rb_vm_delete_cc_refinement(cc);
2097 case imemo_callinfo:
2101 rb_free_method_entry_vm_weak_references((
const rb_method_entry_t *)obj);
2133 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
2144 if (rb_static_id_valid_p(
SYM2ID(
ptr))) {
2152 rb_raise(
rb_eRangeError,
"%"PRIsVALUE
" is not an id value", objid);
2156 VALUE obj = object_id_to_ref(rb_gc_get_objspace(), objid);
2157 if (!rb_multi_ractor_p() || rb_ractor_shareable_p(obj)) {
2161 rb_raise(
rb_eRangeError,
"%"PRIsVALUE
" is the id of an unshareable object on multi-ractor", objid);
2170 return id2ref(objid);
2177#if SIZEOF_LONG == SIZEOF_VOIDP
2184 return get_heap_object_id(obj);
2188nonspecial_obj_id(
VALUE obj)
2190#if SIZEOF_LONG == SIZEOF_VOIDP
2192#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2195# error not supported
2202 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
2239 return rb_find_object_id(rb_gc_get_objspace(), obj, object_id);
2243rb_obj_id_p(
VALUE obj)
2245 return !RB_TYPE_P(obj,
T_IMEMO) && rb_shape_obj_has_id(obj);
2255rb_gc_before_updating_jit_code(
void)
2258 rb_yjit_mark_all_writeable();
2268rb_gc_after_updating_jit_code(
void)
2271 rb_yjit_mark_all_executable();
2276classext_memsize(rb_classext_t *ext,
bool prime,
VALUE box_value,
void *arg)
2278 size_t *size = (
size_t *)arg;
2281 if (RCLASSEXT_M_TBL(ext)) {
2282 s += rb_id_table_memsize(RCLASSEXT_M_TBL(ext));
2284 if (RCLASSEXT_CONST_TBL(ext)) {
2285 s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
2287 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2288 s += (RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1) *
sizeof(
VALUE);
2291 s +=
sizeof(rb_classext_t);
2297classext_superclasses_memsize(rb_classext_t *ext,
bool prime,
VALUE box_value,
void *arg)
2299 size_t *size = (
size_t *)arg;
2301 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2303 array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
2304 *size += array_size *
sizeof(
VALUE);
2309rb_obj_memsize_of(
VALUE obj)
2320 if (rb_shape_obj_too_complex_p(obj)) {
2321 size += rb_st_memsize(ROBJECT_FIELDS_HASH(obj));
2324 size += ROBJECT_FIELDS_CAPACITY(obj) *
sizeof(
VALUE);
2330 rb_class_classext_foreach(obj, classext_memsize, (
void *)&size);
2331 rb_class_classext_foreach(obj, classext_superclasses_memsize, (
void *)&size);
2334 if (RICLASS_OWNS_M_TBL_P(obj)) {
2335 if (RCLASS_M_TBL(obj)) {
2336 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
2341 size += rb_str_memsize(obj);
2344 size += rb_ary_memsize(obj);
2347 if (RHASH_ST_TABLE_P(obj)) {
2348 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
2350 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(st_table);
2359 size += rb_objspace_data_type_memsize(obj);
2363 rb_matchext_t *rm = RMATCH_EXT(obj);
2364 size += onig_region_memsize(&rm->
regs);
2369 if (
RFILE(obj)->fptr) {
2370 size += rb_io_memsize(
RFILE(obj)->fptr);
2377 size += rb_imemo_memsize(obj);
2385 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2386 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
2391 UNEXPECTED_NODE(obj_memsize_of);
2395 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
2396 RSTRUCT(obj)->as.heap.ptr) {
2397 size +=
sizeof(
VALUE) * RSTRUCT_LEN(obj);
2406 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
2410 return size + rb_gc_obj_slot_size(obj);
2414set_zero(st_data_t key, st_data_t val, st_data_t arg)
2418 rb_hash_aset(hash, k,
INT2FIX(0));
2429count_objects_i(
VALUE obj,
void *d)
2433 if (
RBASIC(obj)->flags) {
2479count_objects(
int argc,
VALUE *argv,
VALUE os)
2485 if (rb_check_arity(argc, 0, 1) == 1) {
2487 if (!RB_TYPE_P(hash,
T_HASH))
2491 for (
size_t i = 0; i <=
T_MASK; i++) {
2495 types[i] = type_sym(i);
2502 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2505 hash = rb_hash_new();
2508 rb_hash_stlike_foreach(hash, set_zero, hash);
2510 rb_hash_aset(hash, total,
SIZET2NUM(data.total));
2511 rb_hash_aset(hash, free,
SIZET2NUM(data.freed));
2513 for (
size_t i = 0; i <=
T_MASK; i++) {
2514 if (data.counts[i]) {
2515 rb_hash_aset(hash, types[i],
SIZET2NUM(data.counts[i]));
2522#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2524#define STACK_START (ec->machine.stack_start)
2525#define STACK_END (ec->machine.stack_end)
2526#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2528#if STACK_GROW_DIRECTION < 0
2529# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2530#elif STACK_GROW_DIRECTION > 0
2531# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2533# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2534 : (size_t)(STACK_END - STACK_START + 1))
2536#if !STACK_GROW_DIRECTION
2537int ruby_stack_grow_direction;
2539ruby_get_stack_grow_direction(
volatile VALUE *addr)
2542 SET_MACHINE_STACK_END(&end);
2544 if (end > addr)
return ruby_stack_grow_direction = 1;
2545 return ruby_stack_grow_direction = -1;
2552 rb_execution_context_t *ec = GET_EC();
2554 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2555 return STACK_LENGTH;
2558#define PREVENT_STACK_OVERFLOW 1
2559#ifndef PREVENT_STACK_OVERFLOW
2560#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2561# define PREVENT_STACK_OVERFLOW 1
2563# define PREVENT_STACK_OVERFLOW 0
2566#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2568stack_check(rb_execution_context_t *ec,
int water_mark)
2572 size_t length = STACK_LENGTH;
2573 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2575 return length > maximum_length;
2578#define stack_check(ec, water_mark) FALSE
2581#define STACKFRAME_FOR_CALL_CFUNC 2048
2584rb_ec_stack_check(rb_execution_context_t *ec)
2586 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2592 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2597#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2598 if (!RB_SPECIAL_CONST_P(obj)) { \
2599 rb_vm_t *vm = GET_VM(); \
2600 void *objspace = vm->gc.objspace; \
2601 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2602 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2603 (func)(objspace, (obj_or_ptr)); \
2605 else if (check_obj ? \
2606 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2607 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2609 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2610 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2611 vm->gc.mark_func_data = NULL; \
2612 mark_func_data->mark_func((obj), mark_func_data->data); \
2613 vm->gc.mark_func_data = mark_func_data; \
2619gc_mark_internal(
VALUE obj)
2621 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2627 gc_mark_internal(obj);
2633 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move,
ptr, *
ptr,
false);
2637gc_mark_and_pin_internal(
VALUE obj)
2639 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2645 gc_mark_and_pin_internal(obj);
2649gc_mark_maybe_internal(
VALUE obj)
2651 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2657 gc_mark_maybe_internal(obj);
2663 if (RB_SPECIAL_CONST_P(*
ptr))
return;
2665 rb_vm_t *vm = GET_VM();
2667 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2668 GC_ASSERT(rb_gc_impl_during_gc_p(
objspace));
2673 GC_ASSERT(!rb_gc_impl_during_gc_p(
objspace));
2680 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj,
ptr);
2683ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2685each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2696each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2698 if (end <= start)
return;
2699 each_location(start, end - start, cb, data);
2703gc_mark_maybe_each_location(
VALUE obj,
void *data)
2705 gc_mark_maybe_internal(obj);
2711 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2715rb_gc_mark_values(
long n,
const VALUE *values)
2717 for (
long i = 0; i < n; i++) {
2718 gc_mark_internal(values[i]);
2723rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
2725 for (
long i = 0; i < n; i++) {
2726 gc_mark_and_pin_internal(values[i]);
2731mark_key(st_data_t key, st_data_t value, st_data_t data)
2733 gc_mark_and_pin_internal((
VALUE)key);
2739rb_mark_set(st_table *tbl)
2743 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2747mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2749 gc_mark_internal((
VALUE)key);
2750 gc_mark_internal((
VALUE)value);
2756pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2758 gc_mark_and_pin_internal((
VALUE)key);
2759 gc_mark_and_pin_internal((
VALUE)value);
2765pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2767 gc_mark_and_pin_internal((
VALUE)key);
2768 gc_mark_internal((
VALUE)value);
2774mark_hash(
VALUE hash)
2776 if (rb_hash_compare_by_id_p(hash)) {
2777 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2780 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2783 gc_mark_internal(RHASH(hash)->ifnone);
2787rb_mark_hash(st_table *tbl)
2791 st_foreach(tbl, pin_key_pin_value, 0);
2794static enum rb_id_table_iterator_result
2797 gc_mark_internal(me);
2799 return ID_TABLE_CONTINUE;
2806 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
2810static enum rb_id_table_iterator_result
2813 const rb_const_entry_t *ce = (
const rb_const_entry_t *)value;
2815 if (!rb_gc_checking_shareable()) {
2816 gc_mark_internal(ce->value);
2817 gc_mark_internal(ce->file);
2819 return ID_TABLE_CONTINUE;
2826 rb_id_table_foreach_values(tbl, mark_const_entry_i,
objspace);
2829#if STACK_GROW_DIRECTION < 0
2830#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2831#elif STACK_GROW_DIRECTION > 0
2832#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2834#define GET_STACK_BOUNDS(start, end, appendix) \
2835 ((STACK_END < STACK_START) ? \
2836 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2840gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
2842 gc_mark_maybe_internal(obj);
2844#ifdef RUBY_ASAN_ENABLED
2845 const rb_execution_context_t *ec = (
const rb_execution_context_t *)data;
2846 void *fake_frame_start;
2847 void *fake_frame_end;
2848 bool is_fake_frame = asan_get_fake_stack_extents(
2849 ec->machine.asan_fake_stack_handle, obj,
2850 ec->machine.stack_start, ec->machine.stack_end,
2851 &fake_frame_start, &fake_frame_end
2853 if (is_fake_frame) {
2854 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2866 return rb_gc_impl_object_moved_p(
objspace, obj);
2876 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)value));
2878 return rb_gc_impl_location(
objspace, value);
2884 return gc_location_internal(rb_gc_get_objspace(), value);
2887#if defined(__wasm__)
2890static VALUE *rb_stack_range_tmp[2];
2893rb_mark_locations(
void *begin,
void *end)
2895 rb_stack_range_tmp[0] = begin;
2896 rb_stack_range_tmp[1] = end;
2900rb_gc_save_machine_context(
void)
2905# if defined(__EMSCRIPTEN__)
2908mark_current_machine_context(
const rb_execution_context_t *ec)
2910 emscripten_scan_stack(rb_mark_locations);
2911 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2913 emscripten_scan_registers(rb_mark_locations);
2914 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2919mark_current_machine_context(rb_execution_context_t *ec)
2921 VALUE *stack_start, *stack_end;
2923 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2924 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2926 rb_wasm_scan_locals(rb_mark_locations);
2927 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2935rb_gc_save_machine_context(
void)
2937 rb_thread_t *thread = GET_THREAD();
2939 RB_VM_SAVE_MACHINE_CONTEXT(thread);
2944mark_current_machine_context(
const rb_execution_context_t *ec)
2946 rb_gc_mark_machine_context(ec);
2951rb_gc_mark_machine_context(
const rb_execution_context_t *ec)
2953 VALUE *stack_start, *stack_end;
2955 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2956 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2959#ifdef RUBY_ASAN_ENABLED
2961 (rb_execution_context_t *)ec;
2966 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2967 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
2968 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2972rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2974 gc_mark_and_pin_internal((
VALUE)value);
2980rb_mark_tbl(st_table *tbl)
2982 if (!tbl || tbl->num_entries == 0)
return;
2984 st_foreach(tbl, rb_mark_tbl_i, 0);
2988gc_mark_tbl_no_pin(st_table *tbl)
2990 if (!tbl || tbl->num_entries == 0)
return;
2992 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
2996rb_mark_tbl_no_pin(st_table *tbl)
2998 gc_mark_tbl_no_pin(tbl);
3004 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
3008rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
3010 rb_execution_context_t *ec = GET_EC();
3011 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3013#define MARK_CHECKPOINT(category) do { \
3014 if (categoryp) *categoryp = category; \
3017 MARK_CHECKPOINT(
"vm");
3020 MARK_CHECKPOINT(
"end_proc");
3023 MARK_CHECKPOINT(
"global_tbl");
3024 rb_gc_mark_global_tbl();
3027 void rb_yjit_root_mark(
void);
3029 if (rb_yjit_enabled_p) {
3030 MARK_CHECKPOINT(
"YJIT");
3031 rb_yjit_root_mark();
3036 void rb_zjit_root_mark(
void);
3037 if (rb_zjit_enabled_p) {
3038 MARK_CHECKPOINT(
"ZJIT");
3039 rb_zjit_root_mark();
3043 MARK_CHECKPOINT(
"machine_context");
3044 mark_current_machine_context(ec);
3046 MARK_CHECKPOINT(
"global_symbols");
3047 rb_sym_global_symbols_mark_and_move();
3049 MARK_CHECKPOINT(
"finish");
3051#undef MARK_CHECKPOINT
3055 rb_objspace_t *objspace;
3060gc_mark_classext_module(rb_classext_t *ext,
bool prime,
VALUE box_value,
void *arg)
3065 if (RCLASSEXT_SUPER(ext)) {
3066 gc_mark_internal(RCLASSEXT_SUPER(ext));
3068 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3070 if (!rb_gc_checking_shareable()) {
3072 gc_mark_internal(RCLASSEXT_FIELDS_OBJ(ext));
3073 gc_mark_internal(RCLASSEXT_CVC_TBL(ext));
3076 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
3077 mark_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
3079 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3080 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3081 gc_mark_internal(RCLASSEXT_CLASSPATH(ext));
3085gc_mark_classext_iclass(rb_classext_t *ext,
bool prime,
VALUE box_value,
void *arg)
3090 if (RCLASSEXT_SUPER(ext)) {
3091 gc_mark_internal(RCLASSEXT_SUPER(ext));
3093 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
3094 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3096 if (RCLASSEXT_INCLUDER(ext)) {
3097 gc_mark_internal(RCLASSEXT_INCLUDER(ext));
3099 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3100 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3103#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA_TYPE(d)->function.dmark
3110 if (rb_obj_gen_fields_p(obj)) {
3111 rb_mark_generic_ivar(obj);
3121 rb_bug(
"rb_gc_mark() called for broken object");
3125 UNEXPECTED_NODE(rb_gc_mark);
3129 rb_imemo_mark_and_move(obj,
false);
3136 gc_mark_internal(
RBASIC(obj)->klass);
3141 !rb_gc_checking_shareable()) {
3142 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
3147 foreach_args.obj = obj;
3148 rb_class_classext_foreach(obj, gc_mark_classext_module, (
void *)&foreach_args);
3149 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3150 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3156 foreach_args.obj = obj;
3157 rb_class_classext_foreach(obj, gc_mark_classext_iclass, (
void *)&foreach_args);
3158 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3159 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3164 if (ARY_SHARED_P(obj)) {
3165 VALUE root = ARY_SHARED_ROOT(obj);
3166 gc_mark_internal(root);
3171 for (
long i = 0; i <
len; i++) {
3172 gc_mark_internal(
ptr[i]);
3182 gc_mark_internal(RSYMBOL(obj)->fstr);
3186 if (STR_SHARED_P(obj)) {
3187 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
3192 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
3195 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
3201 bool typed_data = RTYPEDDATA_P(obj);
3202 void *
const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) :
DATA_PTR(obj);
3205 gc_mark_internal(
RTYPEDDATA(obj)->fields_obj);
3209 if (typed_data && gc_declarative_marking_p(RTYPEDDATA_TYPE(obj))) {
3210 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3212 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3213 gc_mark_internal(*(
VALUE *)((
char *)
ptr + offset));
3218 RTYPEDDATA_TYPE(obj)->function.dmark :
3220 if (mark_func) (*mark_func)(
ptr);
3229 if (rb_shape_obj_too_complex_p(obj)) {
3230 gc_mark_tbl_no_pin(ROBJECT_FIELDS_HASH(obj));
3231 len = ROBJECT_FIELDS_COUNT_COMPLEX(obj);
3234 const VALUE *
const ptr = ROBJECT_FIELDS(obj);
3236 len = ROBJECT_FIELDS_COUNT_NOT_COMPLEX(obj);
3237 for (uint32_t i = 0; i <
len; i++) {
3238 gc_mark_internal(
ptr[i]);
3242 attr_index_t fields_count = (attr_index_t)
len;
3244 VALUE klass = RBASIC_CLASS(obj);
3247 if (RCLASS_MAX_IV_COUNT(klass) < fields_count) {
3248 RCLASS_SET_MAX_IV_COUNT(klass, fields_count);
3256 if (
RFILE(obj)->fptr) {
3257 gc_mark_internal(
RFILE(obj)->fptr->self);
3258 gc_mark_internal(
RFILE(obj)->fptr->pathv);
3259 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
3260 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
3261 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
3262 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
3263 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
3264 gc_mark_internal(
RFILE(obj)->fptr->timeout);
3265 gc_mark_internal(
RFILE(obj)->fptr->wakeup_mutex);
3270 gc_mark_internal(
RREGEXP(obj)->src);
3274 gc_mark_internal(
RMATCH(obj)->regexp);
3276 gc_mark_internal(
RMATCH(obj)->str);
3281 gc_mark_internal(RRATIONAL(obj)->num);
3282 gc_mark_internal(RRATIONAL(obj)->den);
3286 gc_mark_internal(RCOMPLEX(obj)->real);
3287 gc_mark_internal(RCOMPLEX(obj)->imag);
3291 const long len = RSTRUCT_LEN(obj);
3292 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
3294 for (
long i = 0; i <
len; i++) {
3295 gc_mark_internal(
ptr[i]);
3298 if (rb_shape_obj_has_fields(obj) && !
FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
3299 gc_mark_internal(RSTRUCT_FIELDS_OBJ(obj));
3309 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
3311 rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)obj) ?
"corrupted object" :
"non object");
3316rb_gc_obj_optimal_size(
VALUE obj)
3321 size_t size = rb_ary_size_as_embedded(obj);
3322 if (rb_gc_size_allocatable_p(size)) {
3326 return sizeof(
struct RArray);
3331 if (rb_shape_obj_too_complex_p(obj)) {
3332 return sizeof(
struct RObject);
3336 if (rb_gc_size_allocatable_p(size)) {
3340 return sizeof(
struct RObject);
3346 size_t size = rb_str_size_as_embedded(obj);
3347 if (rb_gc_size_allocatable_p(size)) {
3351 return sizeof(
struct RString);
3356 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));
3364rb_gc_writebarrier(VALUE a, VALUE b)
3366 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
3372 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
3379rb_gc_writebarrier_remember(
VALUE obj)
3381 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
3387 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
3391rb_gc_modular_gc_loaded_p(
void)
3394 return rb_gc_functions.modular_gc_loaded_p;
3401rb_gc_active_gc_name(
void)
3403 const char *gc_name = rb_gc_impl_active_gc_name();
3405 const size_t len = strlen(gc_name);
3406 if (
len > RB_GC_MAX_NAME_LEN) {
3407 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
3408 RB_GC_MAX_NAME_LEN,
len, gc_name);
3415rb_gc_object_metadata(
VALUE obj)
3417 return rb_gc_impl_object_metadata(rb_gc_get_objspace(), obj);
3423rb_gc_ractor_cache_alloc(rb_ractor_t *ractor)
3425 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
3429rb_gc_ractor_cache_free(
void *cache)
3431 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3437 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
3440 rb_vm_register_global_object(obj);
3446 rb_vm_t *vm = GET_VM();
3451 tmp->next = vm->global_object_list;
3453 vm->global_object_list = tmp;
3461 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
3463 rb_print_backtrace(stderr);
3470 rb_vm_t *vm = GET_VM();
3473 if (tmp->varptr == addr) {
3474 vm->global_object_list = tmp->next;
3479 if (tmp->next->varptr == addr) {
3482 tmp->next = tmp->next->next;
3493 rb_gc_register_address(var);
3497gc_start_internal(rb_execution_context_t *ec,
VALUE self,
VALUE full_mark,
VALUE immediate_mark,
VALUE immediate_sweep,
VALUE compact)
3499 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
3543rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
3545 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3551 if (ARY_SHARED_P(v)) {
3552 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
3556 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
3558 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3559 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
3560 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
3561 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
3569 for (
long i = 0; i <
len; i++) {
3574 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3578 if (rb_ary_embeddable_p(v) && !rb_gc_impl_pinned_p(
objspace, v)) {
3579 rb_ary_make_embedded(v);
3591 if (rb_shape_obj_too_complex_p(v)) {
3592 gc_ref_update_table_values_only(ROBJECT_FIELDS_HASH(v));
3596 size_t slot_size = rb_gc_obj_slot_size(v);
3598 if (slot_size >= embed_size) {
3600 memcpy(
ROBJECT(v)->as.ary,
ptr,
sizeof(
VALUE) * ROBJECT_FIELDS_COUNT(v));
3607 for (uint32_t i = 0; i < ROBJECT_FIELDS_COUNT(v); i++) {
3613rb_gc_ref_update_table_values_only(st_table *tbl)
3615 gc_ref_update_table_values_only(tbl);
3620rb_gc_update_tbl_refs(st_table *
ptr)
3622 gc_update_table_refs(
ptr);
3628 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
3634 for (
long i = 0; i < n; i++) {
3635 UPDATE_IF_MOVED(
objspace, values[i]);
3640rb_gc_update_values(
long n,
VALUE *values)
3642 gc_update_values(rb_gc_get_objspace(), n, values);
3645static enum rb_id_table_iterator_result
3646check_id_table_move(
VALUE value,
void *data)
3651 return ID_TABLE_REPLACE;
3654 return ID_TABLE_CONTINUE;
3658rb_gc_prepare_heap_process_object(
VALUE obj)
3672rb_gc_prepare_heap(
void)
3674 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3678rb_gc_heap_id_for_size(
size_t size)
3680 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3684rb_gc_size_allocatable_p(
size_t size)
3686 return rb_gc_impl_size_allocatable_p(size);
3689static enum rb_id_table_iterator_result
3690update_id_table(
VALUE *value,
void *data,
int existing)
3698 return ID_TABLE_CONTINUE;
3705 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
3709static enum rb_id_table_iterator_result
3712 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3714 if (gc_object_moved_p_internal(
objspace, ce->value)) {
3715 ce->value = gc_location_internal(
objspace, ce->value);
3718 if (gc_object_moved_p_internal(
objspace, ce->file)) {
3719 ce->file = gc_location_internal(
objspace, ce->file);
3722 return ID_TABLE_CONTINUE;
3729 rb_id_table_foreach_values(tbl, update_const_tbl_i,
objspace);
3733update_subclasses(
void *
objspace, rb_classext_t *ext)
3735 rb_subclass_entry_t *entry = RCLASSEXT_SUBCLASSES(ext);
3739 UPDATE_IF_MOVED(
objspace, entry->klass);
3740 entry = entry->next;
3745update_superclasses(rb_objspace_t *
objspace, rb_classext_t *ext)
3747 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
3748 size_t array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
3749 for (
size_t i = 0; i < array_size; i++) {
3750 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPERCLASSES(ext)[i]);
3756update_classext_values(rb_objspace_t *
objspace, rb_classext_t *ext,
bool is_iclass)
3758 UPDATE_IF_MOVED(
objspace, RCLASSEXT_ORIGIN(ext));
3759 UPDATE_IF_MOVED(
objspace, RCLASSEXT_REFINED_CLASS(ext));
3760 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CLASSPATH(ext));
3762 UPDATE_IF_MOVED(
objspace, RCLASSEXT_INCLUDER(ext));
3767update_classext(rb_classext_t *ext,
bool is_prime,
VALUE box_value,
void *arg)
3770 rb_objspace_t *
objspace = args->objspace;
3772 if (RCLASSEXT_SUPER(ext)) {
3773 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
3776 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3778 UPDATE_IF_MOVED(
objspace, ext->fields_obj);
3779 if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
3780 update_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
3782 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
3783 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
3784 update_superclasses(
objspace, ext);
3787 update_classext_values(
objspace, ext,
false);
3791update_iclass_classext(rb_classext_t *ext,
bool is_prime,
VALUE box_value,
void *arg)
3794 rb_objspace_t *
objspace = args->objspace;
3796 if (RCLASSEXT_SUPER(ext)) {
3797 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
3799 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3800 update_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3801 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CC_TBL(ext));
3802 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CVC_TBL(ext));
3805 update_classext_values(
objspace, ext,
true);
3809 vm_table_foreach_callback_func callback;
3810 vm_table_update_callback_func update_callback;
3816vm_weak_table_foreach_weak_key(st_data_t key, st_data_t value, st_data_t data,
int error)
3820 int ret = iter_data->callback((
VALUE)key, iter_data->data);
3822 if (!iter_data->weak_only) {
3823 if (ret != ST_CONTINUE)
return ret;
3825 ret = iter_data->callback((
VALUE)value, iter_data->data);
3832vm_weak_table_foreach_update_weak_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3836 int ret = iter_data->update_callback((
VALUE *)key, iter_data->data);
3838 if (!iter_data->weak_only) {
3839 if (ret != ST_CONTINUE)
return ret;
3841 ret = iter_data->update_callback((
VALUE *)value, iter_data->data);
3848vm_weak_table_cc_refinement_foreach(st_data_t key, st_data_t data,
int error)
3852 return iter_data->callback((
VALUE)key, iter_data->data);
3856vm_weak_table_cc_refinement_foreach_update_update(st_data_t *key, st_data_t data,
int existing)
3860 return iter_data->update_callback((
VALUE *)key, iter_data->data);
3865vm_weak_table_sym_set_foreach(
VALUE *sym_ptr,
void *data)
3867 VALUE sym = *sym_ptr;
3870 if (RB_SPECIAL_CONST_P(sym))
return ST_CONTINUE;
3872 int ret = iter_data->callback(sym, iter_data->data);
3874 if (ret == ST_REPLACE) {
3875 ret = iter_data->update_callback(sym_ptr, iter_data->data);
3881struct st_table *rb_generic_fields_tbl_get(
void);
3884vm_weak_table_id2ref_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3889 int ret = iter_data->callback((
VALUE)key, iter_data->data);
3890 if (ret != ST_CONTINUE)
return ret;
3893 return iter_data->callback((
VALUE)value, iter_data->data);
3897vm_weak_table_id2ref_foreach_update(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3901 iter_data->update_callback((
VALUE *)value, iter_data->data);
3904 iter_data->update_callback((
VALUE *)key, iter_data->data);
3911vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
3915 int ret = iter_data->callback((
VALUE)key, iter_data->data);
3925 RBASIC_SET_SHAPE_ID((
VALUE)key, ROOT_SHAPE_ID);
3929 ret = iter_data->update_callback(&new_key, iter_data->data);
3930 if (key != new_key) {
3937 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ret);
3940 if (!iter_data->weak_only) {
3941 int ivar_ret = iter_data->callback(new_value, iter_data->data);
3947 iter_data->update_callback(&new_value, iter_data->data);
3951 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ivar_ret);
3955 if (key != new_key || value != new_value) {
3956 DURING_GC_COULD_MALLOC_REGION_START();
3958 st_insert(rb_generic_fields_tbl_get(), (st_data_t)new_key, new_value);
3960 DURING_GC_COULD_MALLOC_REGION_END();
3967vm_weak_table_frozen_strings_foreach(
VALUE *str,
void *data)
3971 int retval = iter_data->callback(*str, iter_data->data);
3973 if (retval == ST_REPLACE) {
3974 retval = iter_data->update_callback(str, iter_data->data);
3977 if (retval == ST_DELETE) {
3984void rb_fstring_foreach_with_replace(
int (*callback)(
VALUE *str,
void *data),
void *data);
3986rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
3987 vm_table_update_callback_func update_callback,
3990 enum rb_gc_vm_weak_tables table)
3992 rb_vm_t *vm = GET_VM();
3995 .callback = callback,
3996 .update_callback = update_callback,
3998 .weak_only = weak_only,
4002 case RB_GC_VM_CI_TABLE: {
4004 st_foreach_with_replace(
4006 vm_weak_table_foreach_weak_key,
4007 vm_weak_table_foreach_update_weak_key,
4008 (st_data_t)&foreach_data
4013 case RB_GC_VM_OVERLOADED_CME_TABLE: {
4014 if (vm->overloaded_cme_table) {
4015 st_foreach_with_replace(
4016 vm->overloaded_cme_table,
4017 vm_weak_table_foreach_weak_key,
4018 vm_weak_table_foreach_update_weak_key,
4019 (st_data_t)&foreach_data
4024 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
4025 rb_sym_global_symbol_table_foreach_weak_reference(
4026 vm_weak_table_sym_set_foreach,
4031 case RB_GC_VM_ID2REF_TABLE: {
4033 st_foreach_with_replace(
4035 vm_weak_table_id2ref_foreach,
4036 vm_weak_table_id2ref_foreach_update,
4037 (st_data_t)&foreach_data
4042 case RB_GC_VM_GENERIC_FIELDS_TABLE: {
4043 st_table *generic_fields_tbl = rb_generic_fields_tbl_get();
4044 if (generic_fields_tbl) {
4047 vm_weak_table_gen_fields_foreach,
4048 (st_data_t)&foreach_data
4053 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
4054 rb_fstring_foreach_with_replace(
4055 vm_weak_table_frozen_strings_foreach,
4060 case RB_GC_VM_CC_REFINEMENT_TABLE: {
4061 if (vm->cc_refinement_table) {
4062 set_foreach_with_replace(
4063 vm->cc_refinement_table,
4064 vm_weak_table_cc_refinement_foreach,
4065 vm_weak_table_cc_refinement_foreach_update_update,
4066 (st_data_t)&foreach_data
4071 case RB_GC_VM_WEAK_TABLE_COUNT:
4072 rb_bug(
"Unreachable");
4074 rb_bug(
"rb_gc_vm_weak_table_foreach: unknown table %d", table);
4079rb_gc_update_vm_references(
void *
objspace)
4081 rb_execution_context_t *ec = GET_EC();
4082 rb_vm_t *vm = rb_ec_vm_ptr(ec);
4084 rb_vm_update_references(vm);
4085 rb_gc_update_global_tbl();
4086 rb_sym_global_symbols_mark_and_move();
4089 void rb_yjit_root_update_references(
void);
4091 if (rb_yjit_enabled_p) {
4092 rb_yjit_root_update_references();
4097 void rb_zjit_root_update_references(
void);
4099 if (rb_zjit_enabled_p) {
4100 rb_zjit_root_update_references();
4113 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
4119 rb_class_classext_foreach(obj, update_classext, (
void *)&args);
4124 rb_class_classext_foreach(obj, update_iclass_classext, (
void *)&args);
4128 rb_imemo_mark_and_move(obj,
true);
4140 gc_ref_update_array(
objspace, obj);
4145 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
4150 if (STR_SHARED_P(obj)) {
4158 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
4159 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)
4160 && !rb_gc_impl_pinned_p(
objspace, obj)) {
4161 rb_str_make_embedded(obj);
4170 bool typed_data = RTYPEDDATA_P(obj);
4171 void *
const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) :
DATA_PTR(obj);
4178 if (typed_data && gc_declarative_marking_p(RTYPEDDATA_TYPE(obj))) {
4179 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
4181 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
4183 *ref = gc_location_internal(
objspace, *ref);
4186 else if (typed_data) {
4187 RUBY_DATA_FUNC compact_func = RTYPEDDATA_TYPE(obj)->function.dcompact;
4188 if (compact_func) (*compact_func)(
ptr);
4195 gc_ref_update_object(
objspace, obj);
4199 if (
RFILE(obj)->fptr) {
4202 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
4203 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
4204 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
4216 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
4232 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
4233 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
4237 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
4238 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
4244 long i,
len = RSTRUCT_LEN(obj);
4247 for (i = 0; i <
len; i++) {
4251 if (RSTRUCT_EMBED_LEN(obj)) {
4257 UPDATE_IF_MOVED(
objspace, RSTRUCT(obj)->as.heap.fields_obj);
4262 rb_bug(
"unreachable");
4279 unless_objspace(
objspace) {
return; }
4281 rb_gc_impl_start(
objspace,
true,
true,
true,
false);
4287 unless_objspace(
objspace) {
return FALSE; }
4289 return rb_gc_impl_during_gc_p(
objspace);
4295 return rb_gc_impl_gc_count(rb_gc_get_objspace());
4299gc_count(rb_execution_context_t *ec,
VALUE self)
4311 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
4314 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
4321gc_stat(rb_execution_context_t *ec,
VALUE self,
VALUE arg)
4324 arg = rb_hash_new();
4330 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4335 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4348 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4353 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4365gc_stat_heap(rb_execution_context_t *ec,
VALUE self,
VALUE heap_name,
VALUE arg)
4368 arg = rb_hash_new();
4371 if (
NIL_P(heap_name)) {
4372 if (!RB_TYPE_P(arg,
T_HASH)) {
4382 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
4385 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
4390 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4397gc_config_get(rb_execution_context_t *ec,
VALUE self)
4399 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
4400 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
4406gc_config_set(rb_execution_context_t *ec,
VALUE self,
VALUE hash)
4408 void *
objspace = rb_gc_get_objspace();
4410 rb_gc_impl_config_set(
objspace, hash);
4416gc_stress_get(rb_execution_context_t *ec,
VALUE self)
4418 return rb_gc_impl_stress_get(rb_gc_get_objspace());
4422gc_stress_set_m(rb_execution_context_t *ec,
VALUE self,
VALUE flag)
4424 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
4430rb_gc_initial_stress_set(
VALUE flag)
4432 initial_stress = flag;
4436rb_gc_heap_sizes(
void)
4438 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
4444 return rb_objspace_gc_enable(rb_gc_get_objspace());
4448rb_objspace_gc_enable(
void *
objspace)
4450 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4452 return RBOOL(disabled);
4456gc_enable(rb_execution_context_t *ec,
VALUE _)
4458 return rb_gc_enable();
4464 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4465 rb_gc_impl_gc_disable(
objspace,
false);
4466 return RBOOL(disabled);
4470rb_gc_disable_no_rest(
void)
4472 return gc_disable_no_rest(rb_gc_get_objspace());
4478 return rb_objspace_gc_disable(rb_gc_get_objspace());
4482rb_objspace_gc_disable(
void *
objspace)
4484 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4485 rb_gc_impl_gc_disable(
objspace,
true);
4486 return RBOOL(disabled);
4490gc_disable(rb_execution_context_t *ec,
VALUE _)
4492 return rb_gc_disable();
4497ruby_gc_set_params(
void)
4499 rb_gc_impl_set_params(rb_gc_get_objspace());
4503rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
4506 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
4508 if (!RB_SPECIAL_CONST_P(obj)) {
4509 rb_vm_t *vm = GET_VM();
4510 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4511 struct gc_mark_func_data_struct mfd = {
4516 vm->gc.mark_func_data = &mfd;
4517 rb_gc_mark_children(rb_gc_get_objspace(), obj);
4518 vm->gc.mark_func_data = prev_mfd;
4524 const char *category;
4525 void (*func)(
const char *category,
VALUE,
void *);
4530root_objects_from(
VALUE obj,
void *
ptr)
4533 (*data->func)(data->category, obj, data->data);
4537rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
4539 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");
4541 rb_vm_t *vm = GET_VM();
4545 .data = passing_data,
4548 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4549 struct gc_mark_func_data_struct mfd = {
4550 .mark_func = root_objects_from,
4554 vm->gc.mark_func_data = &mfd;
4555 rb_gc_save_machine_context();
4556 rb_gc_mark_roots(vm->gc.objspace, &data.category);
4557 vm->gc.mark_func_data = prev_mfd;
4568#define TYPE_NAME(t) case (t): return #t;
4595 if (obj && rb_objspace_data_type_name(obj)) {
4596 return rb_objspace_data_type_name(obj);
4605obj_type_name(
VALUE obj)
4607 return type_name(
TYPE(obj), obj);
4611rb_method_type_name(rb_method_type_t
type)
4614 case VM_METHOD_TYPE_ISEQ:
return "iseq";
4615 case VM_METHOD_TYPE_ATTRSET:
return "attrest";
4616 case VM_METHOD_TYPE_IVAR:
return "ivar";
4617 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
4618 case VM_METHOD_TYPE_ALIAS:
return "alias";
4619 case VM_METHOD_TYPE_REFINED:
return "refined";
4620 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
4621 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
4622 case VM_METHOD_TYPE_MISSING:
return "missing";
4623 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
4624 case VM_METHOD_TYPE_UNDEF:
return "undef";
4625 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
4627 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
4631rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
4633 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
4634 VALUE path = rb_iseq_path(iseq);
4635 int n = ISEQ_BODY(iseq)->location.first_lineno;
4636 snprintf(buff, buff_size,
" %s@%s:%d",
4637 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4638 RSTRING_PTR(path), n);
4643str_len_no_raise(
VALUE str)
4645 long len = RSTRING_LEN(str);
4646 if (
len < 0)
return 0;
4647 if (
len > INT_MAX)
return INT_MAX;
4651#define BUFF_ARGS buff + pos, buff_size - pos
4652#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4653#define APPEND_S(s) do { \
4654 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4658 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4661#define C(c, s) ((c) != 0 ? (s) : " ")
4664rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
4669 APPEND_F(
"%s", obj_type_name(obj));
4675 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
4681 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
4682 APPEND_F(
"%p %s/", (
void *)obj, obj_type_name(obj));
4701 if (internal_object_p(obj)) {
4704 else if (
RBASIC(obj)->klass == 0) {
4705 APPEND_S(
"(temporary internal)");
4709 if (!
NIL_P(class_path)) {
4710 APPEND_F(
"%s ", RSTRING_PTR(class_path));
4719const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
4722rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
4729 UNEXPECTED_NODE(rb_raw_obj_info);
4732 if (ARY_SHARED_P(obj)) {
4733 APPEND_S(
"shared -> ");
4734 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4737 APPEND_F(
"[%s%s%s] ",
4738 C(ARY_EMBED_P(obj),
"E"),
4739 C(ARY_SHARED_P(obj),
"S"),
4740 C(ARY_SHARED_ROOT_P(obj),
"R"));
4742 if (ARY_EMBED_P(obj)) {
4743 APPEND_F(
"len: %ld (embed)",
4747 APPEND_F(
"len: %ld, capa:%ld ptr:%p",
4749 RARRAY(obj)->as.heap.aux.capa,
4755 if (STR_SHARED_P(obj)) {
4756 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
4759 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
4761 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
4763 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
4767 VALUE fstr = RSYMBOL(obj)->fstr;
4768 ID id = RSYMBOL(obj)->id;
4770 APPEND_F(
":%s id:%d", RSTRING_PTR(fstr), (
unsigned int)
id);
4773 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
4778 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
4782 APPEND_F(
"[%c] %"PRIdSIZE,
4783 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
4791 if (!
NIL_P(class_path)) {
4792 APPEND_F(
"%s", RSTRING_PTR(class_path));
4802 if (!
NIL_P(class_path)) {
4803 APPEND_F(
"src:%s", RSTRING_PTR(class_path));
4810 if (rb_shape_obj_too_complex_p(obj)) {
4811 size_t hash_len = rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
4812 APPEND_F(
"(too_complex) len:%zu", hash_len);
4815 APPEND_F(
"(embed) len:%d capa:%d", RSHAPE_LEN(RBASIC_SHAPE_ID(obj)), ROBJECT_FIELDS_CAPACITY(obj));
4819 APPEND_F(
"len:%d capa:%d ptr:%p", RSHAPE_LEN(RBASIC_SHAPE_ID(obj)), ROBJECT_FIELDS_CAPACITY(obj), (
void *)ROBJECT_FIELDS(obj));
4825 const rb_iseq_t *iseq;
4827 (block = vm_proc_block(obj)) != NULL &&
4828 (vm_block_type(block) == block_type_iseq) &&
4829 (iseq = vm_block_iseq(block)) != NULL) {
4830 rb_raw_iseq_info(BUFF_ARGS, iseq);
4832 else if (rb_ractor_p(obj)) {
4833 rb_ractor_t *r = (
void *)
DATA_PTR(obj);
4835 APPEND_F(
"r:%d", r->pub.id);
4839 const char *
const type_name = rb_objspace_data_type_name(obj);
4841 APPEND_F(
"%s", type_name);
4847 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
4849 switch (imemo_type(obj)) {
4852 const rb_method_entry_t *me = (
const rb_method_entry_t *)obj;
4854 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
4855 rb_id2name(me->called_id),
4856 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
4857 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
4858 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
4859 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
4860 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
4861 me->def ? rb_method_type_name(me->def->type) :
"NULL",
4862 me->def ? me->def->aliased : -1,
4864 (
void *)me->defined_class);
4867 switch (me->def->type) {
4868 case VM_METHOD_TYPE_ISEQ:
4869 APPEND_S(
" (iseq:");
4870 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.iseqptr);
4881 const rb_iseq_t *iseq = (
const rb_iseq_t *)obj;
4882 rb_raw_iseq_info(BUFF_ARGS, iseq);
4885 case imemo_callinfo:
4888 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
4889 rb_id2name(vm_ci_mid(ci)),
4892 vm_ci_kwarg(ci) ?
"available" :
"NULL");
4895 case imemo_callcache:
4899 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4901 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
4902 NIL_P(class_path) ? (vm_cc_valid(cc) ?
"??" :
"<NULL>") : RSTRING_PTR(class_path),
4903 cme ? rb_id2name(cme->called_id) :
"<NULL>",
4904 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
4906 (
void *)(uintptr_t)vm_cc_call(cc));
4924#ifdef RUBY_ASAN_ENABLED
4926rb_asan_poison_object(
VALUE obj)
4928 MAYBE_UNUSED(
struct RVALUE *)
ptr = (
void *)obj;
4929 asan_poison_memory_region(
ptr, rb_gc_obj_slot_size(obj));
4933rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
4935 MAYBE_UNUSED(
struct RVALUE *)
ptr = (
void *)obj;
4936 asan_unpoison_memory_region(
ptr, rb_gc_obj_slot_size(obj), newobj_p);
4940rb_asan_poisoned_object_p(
VALUE obj)
4942 MAYBE_UNUSED(
struct RVALUE *)
ptr = (
void *)obj;
4943 return __asan_region_is_poisoned(
ptr, rb_gc_obj_slot_size(obj));
4948raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
4950 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4951 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4952 if (pos >= buff_size) {}
4956rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
4958 void *
objspace = rb_gc_get_objspace();
4961 raw_obj_info(buff, buff_size, obj);
4963 else if (!rb_gc_impl_pointer_to_heap_p(
objspace, (
const void *)obj)) {
4964 snprintf(buff, buff_size,
"out-of-heap:%p", (
void *)obj);
4967 else if (0 && rb_gc_impl_garbage_object_p(
objspace, obj)) {
4968 snprintf(buff, buff_size,
"garbage:%p", (
void *)obj);
4972 asan_unpoisoning_object(obj) {
4973 raw_obj_info(buff, buff_size, obj);
4989 if (RB_UNLIKELY(oldval >= maxval - 1)) {
5000 if (RGENGC_OBJ_INFO) {
5003 char buffers[10][0x100];
5006 rb_atomic_t index = atomic_inc_wraparound(&info.index, numberof(info.buffers));
5007 char *
const buff = info.buffers[index];
5008 return rb_raw_obj_info(buff,
sizeof(info.buffers[0]), obj);
5010 return obj_type_name(obj);
5027 rb_vraise(argv->exc, argv->fmt, *argv->ap);
5032gc_raise(
VALUE exc,
const char *fmt, ...)
5046 fprintf(stderr,
"%s",
"[FATAL] ");
5047 vfprintf(stderr, fmt, ap);
5054NORETURN(
static void negative_size_allocation_error(
const char *));
5056negative_size_allocation_error(
const char *msg)
5062ruby_memerror_body(
void *dummy)
5068NORETURN(
static void ruby_memerror(
void));
5073 if (ruby_thread_has_gvl_p()) {
5082 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
5099 rb_execution_context_t *ec = GET_EC();
5100 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
5103 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
5104 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
5105 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
5108 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
5109 rb_ec_raised_clear(ec);
5112 rb_ec_raised_set(ec, RAISED_NOMEMORY);
5113 exc = ruby_vm_special_exception_copy(exc);
5116 EC_JUMP_TAG(ec, TAG_RAISE);
5120rb_memerror_reentered(
void)
5122 rb_execution_context_t *ec = GET_EC();
5123 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
5127handle_malloc_failure(
void *
ptr)
5138static void *ruby_xmalloc_body(
size_t size);
5141ruby_xmalloc(
size_t size)
5143 return handle_malloc_failure(ruby_xmalloc_body(size));
5147malloc_gc_allowed(
void)
5149 rb_ractor_t *r = rb_current_ractor_raw(
false);
5151 return r == NULL || !r->malloc_gc_disabled;
5155ruby_xmalloc_body(
size_t size)
5157 if ((ssize_t)size < 0) {
5158 negative_size_allocation_error(
"too large allocation size");
5161 return rb_gc_impl_malloc(rb_gc_get_objspace(), size, malloc_gc_allowed());
5165ruby_malloc_size_overflow(
size_t count,
size_t elsize)
5167 rb_raise(rb_eArgError,
5168 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
5173ruby_malloc_add_size_overflow(
size_t x,
size_t y)
5175 rb_raise(rb_eArgError,
5176 "malloc: possible integer overflow (%"PRIuSIZE
"+%"PRIuSIZE
")",
5180static void *ruby_xmalloc2_body(
size_t n,
size_t size);
5183ruby_xmalloc2(
size_t n,
size_t size)
5185 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
5189ruby_xmalloc2_body(
size_t n,
size_t size)
5191 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
5194static void *ruby_xcalloc_body(
size_t n,
size_t size);
5197ruby_xcalloc(
size_t n,
size_t size)
5199 return handle_malloc_failure(ruby_xcalloc_body(n, size));
5203ruby_xcalloc_body(
size_t n,
size_t size)
5205 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
5208static void *ruby_sized_xrealloc_body(
void *
ptr,
size_t new_size,
size_t old_size);
5210#ifdef ruby_sized_xrealloc
5211#undef ruby_sized_xrealloc
5214ruby_sized_xrealloc(
void *
ptr,
size_t new_size,
size_t old_size)
5216 return handle_malloc_failure(ruby_sized_xrealloc_body(
ptr, new_size, old_size));
5220ruby_sized_xrealloc_body(
void *
ptr,
size_t new_size,
size_t old_size)
5222 if ((ssize_t)new_size < 0) {
5223 negative_size_allocation_error(
"too large allocation size");
5226 return rb_gc_impl_realloc(rb_gc_get_objspace(),
ptr, new_size, old_size, malloc_gc_allowed());
5230ruby_xrealloc(
void *
ptr,
size_t new_size)
5232 return ruby_sized_xrealloc(
ptr, new_size, 0);
5235static void *ruby_sized_xrealloc2_body(
void *
ptr,
size_t n,
size_t size,
size_t old_n);
5237#ifdef ruby_sized_xrealloc2
5238#undef ruby_sized_xrealloc2
5241ruby_sized_xrealloc2(
void *
ptr,
size_t n,
size_t size,
size_t old_n)
5243 return handle_malloc_failure(ruby_sized_xrealloc2_body(
ptr, n, size, old_n));
5247ruby_sized_xrealloc2_body(
void *
ptr,
size_t n,
size_t size,
size_t old_n)
5249 size_t len = xmalloc2_size(n, size);
5250 return rb_gc_impl_realloc(rb_gc_get_objspace(),
ptr,
len, old_n * size, malloc_gc_allowed());
5254ruby_xrealloc2(
void *
ptr,
size_t n,
size_t size)
5256 return ruby_sized_xrealloc2(
ptr, n, size, 0);
5259#ifdef ruby_sized_xfree
5260#undef ruby_sized_xfree
5263ruby_sized_xfree(
void *x,
size_t size)
5269 if (LIKELY(GET_VM())) {
5270 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
5281 ruby_sized_xfree(x, 0);
5285rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
5287 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5288 return ruby_xmalloc(w);
5292rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
5294 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5295 return ruby_xcalloc(w, 1);
5299rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
5301 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5302 return ruby_xrealloc((
void *)p, w);
5306rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
5308 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5309 return ruby_xmalloc(u);
5313rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
5315 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5316 return ruby_xcalloc(u, 1);
5323ruby_mimmalloc(
size_t size)
5326#if CALC_EXACT_MALLOC_SIZE
5330#if CALC_EXACT_MALLOC_SIZE
5346ruby_mimcalloc(
size_t num,
size_t size)
5349#if CALC_EXACT_MALLOC_SIZE
5350 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(num, size);
5351 if (UNLIKELY(t.overflowed)) {
5355 mem = calloc1(size);
5367 mem = calloc(num, size);
5373ruby_mimfree(
void *
ptr)
5375#if CALC_EXACT_MALLOC_SIZE
5383rb_gc_adjust_memory_usage(ssize_t diff)
5385 unless_objspace(
objspace) {
return; }
5387 rb_gc_impl_adjust_memory_usage(
objspace, diff);
5391rb_obj_info(
VALUE obj)
5393 return obj_info(obj);
5397rb_obj_info_dump(
VALUE obj)
5400 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
5404rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
5407 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
5411rb_gc_before_fork(
void)
5413 rb_gc_impl_before_fork(rb_gc_get_objspace());
5417rb_gc_after_fork(rb_pid_t pid)
5419 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
5423rb_gc_obj_shareable_p(
VALUE obj)
5440check_shareable_i(
const VALUE child,
void *
ptr)
5444 if (!rb_gc_obj_shareable_p(child)) {
5445 fprintf(stderr,
"(a) ");
5446 rb_gc_rp(data->parent);
5447 fprintf(stderr,
"(b) ");
5449 fprintf(stderr,
"check_shareable_i: shareable (a) -> unshareable (b)\n");
5452 rb_bug(
"!! violate shareable constraint !!");
5456static bool gc_checking_shareable =
false;
5465 unsigned int lev = RB_GC_VM_LOCK();
5467 gc_checking_shareable =
true;
5468 rb_objspace_reachable_objects_from(obj, check_shareable_i, (
void *)data);
5469 gc_checking_shareable =
false;
5471 RB_GC_VM_UNLOCK(lev);
5476rb_gc_verify_shareable(
VALUE obj)
5478 rb_objspace_t *
objspace = rb_gc_get_objspace();
5483 gc_verify_shareable(
objspace, obj, &data);
5485 if (data.err_count > 0) {
5486 rb_bug(
"rb_gc_verify_shareable");
5491rb_gc_checking_shareable(
void)
5493 return gc_checking_shareable;
5547 rb_gc_register_address(&id2ref_value);
5549 malloc_offset = gc_compute_malloc_offset();
5562 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
5585ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
5587#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
5590 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (
unsigned long)addr, size, name);
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ATOMIC_VALUE_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
#define RUBY_ATOMIC_SIZE_FETCH_ADD(var, val)
Identical to RUBY_ATOMIC_FETCH_ADD, except it expects its arguments to be size_t.
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
VALUE rb_define_module(const char *name)
Defines a top-level module.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define T_FILE
Old name of RUBY_T_FILE.
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define T_MASK
Old name of RUBY_T_MASK.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define T_DATA
Old name of RUBY_T_DATA.
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define LL2NUM
Old name of RB_LL2NUM.
#define CLASS_OF
Old name of rb_class_of.
#define T_NONE
Old name of RUBY_T_NONE.
#define T_NODE
Old name of RUBY_T_NODE.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_ABLE
Old name of RB_FL_ABLE.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define T_UNDEF
Old name of RUBY_T_UNDEF.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_MATCH
Old name of RUBY_T_MATCH.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define T_MOVED
Old name of RUBY_T_MOVED.
#define xcalloc
Old name of ruby_xcalloc.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
int ruby_stack_check(void)
Checks for stack overflow.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMemError
NoMemoryError exception.
VALUE rb_eRangeError
RangeError exception.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_mKernel
Kernel module.
VALUE rb_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.
size_t rb_obj_embedded_size(uint32_t fields_count)
Internal header for Object.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
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 rb_io_fptr_finalize(rb_io_t *fptr)
Destroys the given IO.
int len
Length of the buffer.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
VALUE rb_yield(VALUE val)
Yields the block.
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]].
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY(obj)
Convenient casting macro.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS(obj)
Convenient casting macro.
#define DATA_PTR(obj)
Convenient getter macro.
#define RDATA(obj)
Convenient casting macro.
void(*) RUBY_DATA_FUNC(void *)
This is the type of callbacks registered to RData.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RFILE(obj)
Convenient casting macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RMATCH(obj)
Convenient casting macro.
#define ROBJECT(obj)
Convenient casting macro.
#define RREGEXP(obj)
Convenient casting macro.
#define RREGEXP_PTR(obj)
Convenient accessor macro.
#define RSTRING(obj)
Convenient casting macro.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
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.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
void rb_p(VALUE obj)
Inspects an object.
#define errno
Ractor-aware version of errno.
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Ruby object's base components.
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
void * data
Pointer to the actual C level struct that you want to wrap.
void * data
Pointer to the actual C level struct that you want to wrap.
VALUE fields_obj
Direct reference to the slots that holds instance variables, if any.
Ruby's IO, metadata and buffers.
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.
Represents the region of a capture group.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
ruby_value_type
C-level type of an object.