14#define rb_data_object_alloc rb_data_object_alloc
15#define rb_data_typed_object_alloc rb_data_typed_object_alloc
17#include "ruby/internal/config.h"
22#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
23# include "wasm/setjmp.h"
24# include "wasm/machine.h"
32#ifndef HAVE_MALLOC_USABLE_SIZE
34# define HAVE_MALLOC_USABLE_SIZE
35# define malloc_usable_size(a) _msize(a)
36# elif defined HAVE_MALLOC_SIZE
37# define HAVE_MALLOC_USABLE_SIZE
38# define malloc_usable_size(a) malloc_size(a)
42#ifdef HAVE_MALLOC_USABLE_SIZE
43# ifdef RUBY_ALTERNATIVE_MALLOC_HEADER
45# elif defined(HAVE_MALLOC_H)
47# elif defined(HAVE_MALLOC_NP_H)
48# include <malloc_np.h>
49# elif defined(HAVE_MALLOC_MALLOC_H)
50# include <malloc/malloc.h>
60#ifdef HAVE_SYS_RESOURCE_H
61# include <sys/resource.h>
64#if defined _WIN32 || defined __CYGWIN__
66#elif defined(HAVE_POSIX_MEMALIGN)
67#elif defined(HAVE_MEMALIGN)
74#include <emscripten.h>
78#ifdef HAVE_SYS_PRCTL_H
86#include "debug_counter.h"
87#include "eval_intern.h"
91#include "internal/class.h"
92#include "internal/compile.h"
93#include "internal/complex.h"
94#include "internal/cont.h"
95#include "internal/error.h"
96#include "internal/eval.h"
97#include "internal/gc.h"
98#include "internal/hash.h"
99#include "internal/imemo.h"
100#include "internal/io.h"
101#include "internal/numeric.h"
102#include "internal/object.h"
103#include "internal/proc.h"
104#include "internal/rational.h"
105#include "internal/sanitizers.h"
106#include "internal/struct.h"
107#include "internal/symbol.h"
108#include "internal/thread.h"
109#include "internal/variable.h"
110#include "internal/warnings.h"
121#include "ruby_assert.h"
122#include "ruby_atomic.h"
126#include "vm_callinfo.h"
127#include "ractor_core.h"
137 RB_VM_LOCK_ENTER_LEV(&lev);
142rb_gc_vm_unlock(
unsigned int lev)
144 RB_VM_LOCK_LEAVE_LEV(&lev);
151 RB_VM_LOCK_ENTER_CR_LEV(GET_RACTOR(), &lev);
156rb_gc_cr_unlock(
unsigned int lev)
158 RB_VM_LOCK_LEAVE_CR_LEV(GET_RACTOR(), &lev);
162rb_gc_vm_lock_no_barrier(
void)
164 unsigned int lev = 0;
165 RB_VM_LOCK_ENTER_LEV_NB(&lev);
170rb_gc_vm_unlock_no_barrier(
unsigned int lev)
172 RB_VM_LOCK_LEAVE_LEV(&lev);
176rb_gc_vm_barrier(
void)
183rb_gc_get_ractor_newobj_cache(
void)
185 return GET_RACTOR()->newobj_cache;
189rb_gc_initialize_vm_context(
struct rb_gc_vm_context *context)
192 context->ec = GET_EC();
196rb_gc_worker_thread_set_vm_context(
struct rb_gc_vm_context *context)
200 GC_ASSERT(rb_current_execution_context(
false) == NULL);
202#ifdef RB_THREAD_LOCAL_SPECIFIER
203 rb_current_ec_set(context->ec);
205 native_tls_set(ruby_current_ec_key, context->ec);
210rb_gc_worker_thread_unset_vm_context(
struct rb_gc_vm_context *context)
214 GC_ASSERT(rb_current_execution_context(
true) == context->ec);
216#ifdef RB_THREAD_LOCAL_SPECIFIER
217 rb_current_ec_set(NULL);
219 native_tls_set(ruby_current_ec_key, NULL);
227 return ruby_vm_event_flags & event;
233 if (LIKELY(!rb_gc_event_hook_required_p(event)))
return;
235 rb_execution_context_t *ec = GET_EC();
236 if (!ec->cfp)
return;
238 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
242rb_gc_get_objspace(
void)
244 return GET_VM()->gc.objspace;
249rb_gc_ractor_newobj_cache_foreach(
void (*func)(
void *cache,
void *data),
void *data)
251 rb_ractor_t *r = NULL;
252 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
253 func(r->newobj_cache, data);
258rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
263 rb_control_frame_t *cfp;
268 rb_execution_context_t *
volatile ec = GET_EC();
269#define RESTORE_FINALIZER() (\
270 ec->cfp = saved.cfp, \
271 ec->cfp->sp = saved.sp, \
272 ec->errinfo = saved.errinfo)
274 saved.errinfo = ec->errinfo;
276 saved.sp = ec->cfp->sp;
281 enum ruby_tag_type state = EC_EXEC_TAG();
282 if (state != TAG_NONE) {
285 VALUE failed_final = saved.final;
288 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
289 rb_ec_error_print(ec, ec->errinfo);
293 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
294 saved.final = callback(i, data);
298#undef RESTORE_FINALIZER
302rb_gc_set_pending_interrupt(
void)
304 rb_execution_context_t *ec = GET_EC();
305 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
309rb_gc_unset_pending_interrupt(
void)
311 rb_execution_context_t *ec = GET_EC();
312 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
316rb_gc_multi_ractor_p(
void)
318 return rb_multi_ractor_p();
321bool rb_obj_is_main_ractor(
VALUE gv);
324rb_gc_shutdown_call_finalizer_p(
VALUE obj)
328 if (!ruby_free_at_exit_p() && (!
DATA_PTR(obj) || !
RDATA(obj)->dfree))
return false;
329 if (rb_obj_is_thread(obj))
return false;
330 if (rb_obj_is_mutex(obj))
return false;
331 if (rb_obj_is_fiber(obj))
return false;
332 if (rb_obj_is_main_ractor(obj))
return false;
340 if (RSYMBOL(obj)->fstr &&
343 RSYMBOL(obj)->fstr = 0;
351 return ruby_free_at_exit_p();
356rb_gc_get_shape(
VALUE obj)
358 return (uint32_t)rb_shape_get_shape_id(obj);
362rb_gc_set_shape(
VALUE obj, uint32_t shape_id)
364 rb_shape_set_shape_id(obj, (uint32_t)shape_id);
368rb_gc_rebuild_shape(
VALUE obj,
size_t heap_id)
370 rb_shape_t *orig_shape = rb_shape_get_shape(obj);
372 if (rb_shape_obj_too_complex(obj))
return (uint32_t)OBJ_TOO_COMPLEX_SHAPE_ID;
374 rb_shape_t *initial_shape = rb_shape_get_shape_by_id((shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID));
375 rb_shape_t *new_shape = rb_shape_traverse_from_new_root(initial_shape, orig_shape);
377 if (!new_shape)
return 0;
379 return (uint32_t)rb_shape_id(new_shape);
382void rb_vm_update_references(
void *ptr);
384#define rb_setjmp(env) RUBY_SETJMP(env)
385#define rb_jmp_buf rb_jmpbuf_t
386#undef rb_data_object_wrap
388#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
389#define MAP_ANONYMOUS MAP_ANON
392#define unless_objspace(objspace) \
394 rb_vm_t *unless_objspace_vm = GET_VM(); \
395 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
398#define RMOVED(obj) ((struct RMoved *)(obj))
400#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
401 if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
402 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
406#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
408#if RUBY_MARK_FREE_DEBUG
409int ruby_gc_debug_indent = 0;
412#ifndef RGENGC_OBJ_INFO
413# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
416#ifndef CALC_EXACT_MALLOC_SIZE
417# define CALC_EXACT_MALLOC_SIZE 0
422static size_t malloc_offset = 0;
423#if defined(HAVE_MALLOC_USABLE_SIZE)
425gc_compute_malloc_offset(
void)
436 for (offset = 0; offset <= 16; offset += 8) {
437 size_t allocated = (64 - offset);
438 void *test_ptr = malloc(allocated);
439 size_t wasted = malloc_usable_size(test_ptr) - allocated;
450gc_compute_malloc_offset(
void)
458rb_malloc_grow_capa(
size_t current,
size_t type_size)
460 size_t current_capacity = current;
461 if (current_capacity < 4) {
462 current_capacity = 4;
464 current_capacity *= type_size;
467 size_t new_capacity = (current_capacity * 2);
470 if (rb_popcount64(new_capacity) != 1) {
471 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
474 new_capacity -= malloc_offset;
475 new_capacity /= type_size;
476 if (current > new_capacity) {
477 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
483static inline struct rbimpl_size_mul_overflow_tag
484size_add_overflow(size_t x, size_t y)
490#elif defined(ckd_add)
491 p = ckd_add(&z, x, y);
493#elif __has_builtin(__builtin_add_overflow)
494 p = __builtin_add_overflow(x, y, &z);
496#elif defined(DSIZE_T)
508 return (
struct rbimpl_size_mul_overflow_tag) { p, z, };
511static inline struct rbimpl_size_mul_overflow_tag
512size_mul_add_overflow(size_t x, size_t y, size_t z)
514 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
515 struct rbimpl_size_mul_overflow_tag u = size_add_overflow(t.right, z);
516 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
519static inline struct rbimpl_size_mul_overflow_tag
520size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
522 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
523 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
524 struct rbimpl_size_mul_overflow_tag v = size_add_overflow(t.right, u.right);
525 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
528PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
531size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
533 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
534 if (LIKELY(!t.left)) {
537 else if (rb_during_gc()) {
543 "integer overflow: %"PRIuSIZE
546 x, y, (
size_t)SIZE_MAX);
551rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
553 return size_mul_or_raise(x, y, exc);
557size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
559 struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
560 if (LIKELY(!t.left)) {
563 else if (rb_during_gc()) {
569 "integer overflow: %"PRIuSIZE
573 x, y, z, (
size_t)SIZE_MAX);
578rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
580 return size_mul_add_or_raise(x, y, z, exc);
584size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
586 struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
587 if (LIKELY(!t.left)) {
590 else if (rb_during_gc()) {
596 "integer overflow: %"PRIdSIZE
601 x, y, z, w, (
size_t)SIZE_MAX);
605#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
607volatile VALUE rb_gc_guarded_val;
609rb_gc_guarded_ptr_val(
volatile VALUE *ptr,
VALUE val)
611 rb_gc_guarded_val = val;
617static const char *obj_type_name(
VALUE obj);
618#include "gc/default/default.c"
620#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
621# error "Modular GC requires dlopen"
625typedef struct gc_function_map {
627 void *(*objspace_alloc)(void);
628 void (*objspace_init)(
void *objspace_ptr);
629 void (*objspace_free)(
void *objspace_ptr);
630 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
631 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
632 void (*set_params)(
void *objspace_ptr);
634 size_t *(*heap_sizes)(
void *objspace_ptr);
636 void (*shutdown_free_objects)(
void *objspace_ptr);
638 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
639 bool (*during_gc_p)(
void *objspace_ptr);
640 void (*prepare_heap)(
void *objspace_ptr);
641 void (*gc_enable)(
void *objspace_ptr);
642 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
643 bool (*gc_enabled_p)(
void *objspace_ptr);
644 VALUE (*config_get)(
void *objpace_ptr);
645 void (*config_set)(
void *objspace_ptr,
VALUE hash);
646 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
647 VALUE (*stress_get)(
void *objspace_ptr);
649 VALUE (*new_obj)(
void *objspace_ptr,
void *cache_ptr,
VALUE klass,
VALUE flags,
VALUE v1,
VALUE v2,
VALUE v3,
bool wb_protected,
size_t alloc_size);
650 size_t (*obj_slot_size)(
VALUE obj);
651 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
652 bool (*size_allocatable_p)(
size_t size);
654 void *(*malloc)(
void *objspace_ptr,
size_t size);
655 void *(*calloc)(
void *objspace_ptr,
size_t size);
656 void *(*realloc)(
void *objspace_ptr,
void *ptr,
size_t new_size,
size_t old_size);
657 void (*free)(
void *objspace_ptr,
void *ptr,
size_t old_size);
658 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
660 void (*mark)(
void *objspace_ptr,
VALUE obj);
661 void (*mark_and_move)(
void *objspace_ptr,
VALUE *ptr);
662 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
663 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
664 void (*mark_weak)(
void *objspace_ptr,
VALUE *ptr);
665 void (*remove_weak)(
void *objspace_ptr,
VALUE parent_obj,
VALUE *ptr);
667 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
668 VALUE (*location)(
void *objspace_ptr,
VALUE value);
670 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
671 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
672 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
674 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
675 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
677 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
679 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
680 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
681 void (*shutdown_call_finalizer)(
void *objspace_ptr);
683 VALUE (*object_id)(
void *objspace_ptr,
VALUE obj);
684 VALUE (*object_id_to_ref)(
void *objspace_ptr,
VALUE object_id);
686 void (*before_fork)(
void *objspace_ptr);
687 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
689 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
690 bool (*get_measure_total_time)(
void *objspace_ptr);
691 unsigned long long (*get_total_time)(
void *objspace_ptr);
692 size_t (*gc_count)(
void *objspace_ptr);
693 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
694 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
695 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
696 const char *(*active_gc_name)(void);
698 size_t (*obj_flags)(
void *objspace_ptr,
VALUE obj,
ID* flags,
size_t max);
699 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *ptr);
700 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
701 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
702 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
704 bool modular_gc_loaded_p;
705} rb_gc_function_map_t;
707static rb_gc_function_map_t rb_gc_functions;
709# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
710# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
713ruby_modular_gc_init(
void)
718 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
720 rb_gc_function_map_t gc_functions = { 0 };
722 char *gc_so_path = NULL;
727 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
728 char c = gc_so_file[i];
729 if (isalnum(c))
continue;
735 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
740 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
743 size_t prefix_len = 0;
744 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
745 const char *base = strrchr(dli.dli_fname,
'/');
748# define end_with_p(lit) \
749 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
750 memcmp(base - tail, lit, tail) == 0)
752 prefix_len = base - dli.dli_fname;
753 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
756 prefix_len += MODULAR_GC_DIR[0] !=
'/';
757 gc_so_path_size += prefix_len;
761 gc_so_path = alloca(gc_so_path_size);
763 size_t gc_so_path_idx = 0;
764#define GC_SO_PATH_APPEND(str) do { \
765 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
768 if (prefix_len > 0) {
769 memcpy(gc_so_path, dli.dli_fname, prefix_len);
770 gc_so_path_idx = prefix_len;
773 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
774 GC_SO_PATH_APPEND(gc_so_file);
775 GC_SO_PATH_APPEND(DLEXT);
776 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
777#undef GC_SO_PATH_APPEND
780 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
782 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
786 gc_functions.modular_gc_loaded_p =
true;
789# define load_modular_gc_func(name) do { \
791 const char *func_name = "rb_gc_impl_" #name; \
792 gc_functions.name = dlsym(handle, func_name); \
793 if (!gc_functions.name) { \
794 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
799 gc_functions.name = rb_gc_impl_##name; \
804 load_modular_gc_func(objspace_alloc);
805 load_modular_gc_func(objspace_init);
806 load_modular_gc_func(objspace_free);
807 load_modular_gc_func(ractor_cache_alloc);
808 load_modular_gc_func(ractor_cache_free);
809 load_modular_gc_func(set_params);
810 load_modular_gc_func(init);
811 load_modular_gc_func(heap_sizes);
813 load_modular_gc_func(shutdown_free_objects);
815 load_modular_gc_func(start);
816 load_modular_gc_func(during_gc_p);
817 load_modular_gc_func(prepare_heap);
818 load_modular_gc_func(gc_enable);
819 load_modular_gc_func(gc_disable);
820 load_modular_gc_func(gc_enabled_p);
821 load_modular_gc_func(config_set);
822 load_modular_gc_func(config_get);
823 load_modular_gc_func(stress_set);
824 load_modular_gc_func(stress_get);
826 load_modular_gc_func(new_obj);
827 load_modular_gc_func(obj_slot_size);
828 load_modular_gc_func(heap_id_for_size);
829 load_modular_gc_func(size_allocatable_p);
831 load_modular_gc_func(malloc);
832 load_modular_gc_func(calloc);
833 load_modular_gc_func(realloc);
834 load_modular_gc_func(free);
835 load_modular_gc_func(adjust_memory_usage);
837 load_modular_gc_func(mark);
838 load_modular_gc_func(mark_and_move);
839 load_modular_gc_func(mark_and_pin);
840 load_modular_gc_func(mark_maybe);
841 load_modular_gc_func(mark_weak);
842 load_modular_gc_func(remove_weak);
844 load_modular_gc_func(object_moved_p);
845 load_modular_gc_func(location);
847 load_modular_gc_func(writebarrier);
848 load_modular_gc_func(writebarrier_unprotect);
849 load_modular_gc_func(writebarrier_remember);
851 load_modular_gc_func(each_objects);
852 load_modular_gc_func(each_object);
854 load_modular_gc_func(make_zombie);
855 load_modular_gc_func(define_finalizer);
856 load_modular_gc_func(undefine_finalizer);
857 load_modular_gc_func(copy_finalizer);
858 load_modular_gc_func(shutdown_call_finalizer);
860 load_modular_gc_func(object_id);
861 load_modular_gc_func(object_id_to_ref);
863 load_modular_gc_func(before_fork);
864 load_modular_gc_func(after_fork);
866 load_modular_gc_func(set_measure_total_time);
867 load_modular_gc_func(get_measure_total_time);
868 load_modular_gc_func(get_total_time);
869 load_modular_gc_func(gc_count);
870 load_modular_gc_func(latest_gc_info);
871 load_modular_gc_func(stat);
872 load_modular_gc_func(stat_heap);
873 load_modular_gc_func(active_gc_name);
875 load_modular_gc_func(obj_flags);
876 load_modular_gc_func(pointer_to_heap_p);
877 load_modular_gc_func(garbage_object_p);
878 load_modular_gc_func(set_event_hook);
879 load_modular_gc_func(copy_attributes);
881# undef load_modular_gc_func
883 rb_gc_functions = gc_functions;
887# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
888# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
889# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
890# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
891# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
892# define rb_gc_impl_set_params rb_gc_functions.set_params
893# define rb_gc_impl_init rb_gc_functions.init
894# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
896# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
898# define rb_gc_impl_start rb_gc_functions.start
899# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
900# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
901# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
902# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
903# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
904# define rb_gc_impl_config_get rb_gc_functions.config_get
905# define rb_gc_impl_config_set rb_gc_functions.config_set
906# define rb_gc_impl_stress_set rb_gc_functions.stress_set
907# define rb_gc_impl_stress_get rb_gc_functions.stress_get
909# define rb_gc_impl_new_obj rb_gc_functions.new_obj
910# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
911# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
912# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
914# define rb_gc_impl_malloc rb_gc_functions.malloc
915# define rb_gc_impl_calloc rb_gc_functions.calloc
916# define rb_gc_impl_realloc rb_gc_functions.realloc
917# define rb_gc_impl_free rb_gc_functions.free
918# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
920# define rb_gc_impl_mark rb_gc_functions.mark
921# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
922# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
923# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
924# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
925# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
927# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
928# define rb_gc_impl_location rb_gc_functions.location
930# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
931# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
932# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
934# define rb_gc_impl_each_objects rb_gc_functions.each_objects
935# define rb_gc_impl_each_object rb_gc_functions.each_object
937# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
938# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
939# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
940# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
941# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
943# define rb_gc_impl_object_id rb_gc_functions.object_id
944# define rb_gc_impl_object_id_to_ref rb_gc_functions.object_id_to_ref
946# define rb_gc_impl_before_fork rb_gc_functions.before_fork
947# define rb_gc_impl_after_fork rb_gc_functions.after_fork
949# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
950# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
951# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
952# define rb_gc_impl_gc_count rb_gc_functions.gc_count
953# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
954# define rb_gc_impl_stat rb_gc_functions.stat
955# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
956# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
958# define rb_gc_impl_obj_flags rb_gc_functions.obj_flags
959# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
960# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
961# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
962# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
965#ifdef RUBY_ASAN_ENABLED
967asan_death_callback(
void)
970 rb_bug_without_die(
"ASAN error");
978rb_objspace_alloc(
void)
981 ruby_modular_gc_init();
984 void *
objspace = rb_gc_impl_objspace_alloc();
985 ruby_current_vm_ptr->gc.objspace =
objspace;
988 rb_gc_impl_stress_set(
objspace, initial_stress);
990#ifdef RUBY_ASAN_ENABLED
991 __sanitizer_set_death_callback(asan_death_callback);
1000 rb_gc_impl_objspace_free(
objspace);
1004rb_gc_obj_slot_size(
VALUE obj)
1006 return rb_gc_impl_obj_slot_size(obj);
1010gc_validate_pc(
void) {
1012 rb_execution_context_t *ec = GET_EC();
1013 const rb_control_frame_t *cfp = ec->cfp;
1014 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc) {
1015 RUBY_ASSERT(cfp->pc >= ISEQ_BODY(cfp->iseq)->iseq_encoded);
1016 RUBY_ASSERT(cfp->pc <= ISEQ_BODY(cfp->iseq)->iseq_encoded + ISEQ_BODY(cfp->iseq)->iseq_size);
1024 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
1030 RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
1032 memset((
char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
1039 bool gc_disabled =
RTEST(rb_gc_disable_no_rest());
1043 if (!gc_disabled) rb_gc_enable();
1045 RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
1052rb_wb_unprotected_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
1055 return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
1059rb_wb_protected_newobj_of(rb_execution_context_t *ec,
VALUE klass,
VALUE flags,
size_t size)
1062 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
1065#define UNEXPECTED_NODE(func) \
1066 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1067 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1070rb_data_object_check(
VALUE klass)
1072 if (klass != rb_cObject && (
rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
1074 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1082 if (klass) rb_data_object_check(klass);
1089 VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
1097 RBIMPL_NONNULL_ARG(
type);
1098 if (klass) rb_data_object_check(klass);
1100 return newobj_of(GET_RACTOR(), klass,
T_DATA, (
VALUE)
type, 1 | typed_flag, (
VALUE)datap, wb_protected, size);
1106 if (UNLIKELY(
type->flags & RUBY_TYPED_EMBEDDABLE)) {
1107 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1110 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
1116 if (
type->flags & RUBY_TYPED_EMBEDDABLE) {
1117 if (!(
type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1118 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1121 size_t embed_size = offsetof(
struct RTypedData, data) + size;
1122 if (rb_gc_size_allocatable_p(embed_size)) {
1123 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1124 memset((
char *)obj + offsetof(
struct RTypedData, data), 0, size);
1135rb_objspace_data_type_memsize(
VALUE obj)
1140 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1142 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1143#ifdef HAVE_MALLOC_USABLE_SIZE
1144 size += malloc_usable_size((
void *)ptr);
1148 if (ptr &&
type->function.dsize) {
1149 size +=
type->function.dsize(ptr);
1157rb_objspace_data_type_name(
VALUE obj)
1167static enum rb_id_table_iterator_result
1168cvar_table_free_i(
VALUE value,
void *ctx)
1170 xfree((
void *)value);
1171 return ID_TABLE_CONTINUE;
1177 rb_io_t *fptr =
RFILE(obj)->fptr;
1178 rb_gc_impl_make_zombie(
objspace, obj, rb_io_fptr_finalize_internal, fptr);
1186 int free_immediately =
false;
1187 void (*dfree)(
void *);
1190 free_immediately = (
RTYPEDDATA(obj)->type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1191 dfree =
RTYPEDDATA(obj)->type->function.dfree;
1194 dfree =
RDATA(obj)->dfree;
1199 if (!
RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1201 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1204 else if (free_immediately) {
1206 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1210 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1213 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, dfree, data);
1214 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1219 RB_DEBUG_COUNTER_INC(obj_data_empty);
1227rb_gc_obj_free_vm_weak_references(
VALUE obj)
1236 if (
FL_TEST(obj, RSTRING_FSTR)) {
1237 st_data_t fstr = (st_data_t)obj;
1238 st_delete(rb_vm_fstring_table(), &fstr, NULL);
1239 RB_DEBUG_COUNTER_INC(obj_str_fstr);
1245 rb_gc_free_dsymbol(obj);
1248 switch (imemo_type(obj)) {
1249 case imemo_callinfo:
1253 rb_free_method_entry_vm_weak_references((
const rb_method_entry_t *)obj);
1267 RB_DEBUG_COUNTER_INC(obj_free);
1274 rb_bug(
"obj_free() called for broken object");
1282 if (rb_shape_obj_too_complex(obj)) {
1283 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1284 st_free_table(ROBJECT_IV_HASH(obj));
1286 else if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1287 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1291 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1296 rb_id_table_free(RCLASS_M_TBL(obj));
1297 rb_cc_table_free(obj);
1298 if (rb_shape_obj_too_complex(obj)) {
1299 st_free_table((st_table *)RCLASS_IVPTR(obj));
1302 xfree(RCLASS_IVPTR(obj));
1305 if (RCLASS_CONST_TBL(obj)) {
1306 rb_free_const_table(RCLASS_CONST_TBL(obj));
1308 if (RCLASS_CVC_TBL(obj)) {
1309 rb_id_table_foreach_values(RCLASS_CVC_TBL(obj), cvar_table_free_i, NULL);
1310 rb_id_table_free(RCLASS_CVC_TBL(obj));
1312 rb_class_remove_subclass_head(obj);
1313 rb_class_remove_from_module_subclasses(obj);
1314 rb_class_remove_from_super_subclasses(obj);
1315 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1316 xfree(RCLASS_SUPERCLASSES(obj));
1329#if USE_DEBUG_COUNTER
1332 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1335 RB_DEBUG_COUNTER_INC(obj_hash_1);
1338 RB_DEBUG_COUNTER_INC(obj_hash_2);
1341 RB_DEBUG_COUNTER_INC(obj_hash_3);
1344 RB_DEBUG_COUNTER_INC(obj_hash_4);
1350 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1354 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1357 if (RHASH_AR_TABLE_P(obj)) {
1358 if (RHASH_AR_TABLE(obj) == NULL) {
1359 RB_DEBUG_COUNTER_INC(obj_hash_null);
1362 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1366 RB_DEBUG_COUNTER_INC(obj_hash_st);
1375 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1379 if (!rb_data_free(
objspace, obj))
return false;
1383 rb_matchext_t *rm = RMATCH_EXT(obj);
1384#if USE_DEBUG_COUNTER
1385 if (rm->
regs.num_regs >= 8) {
1386 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1388 else if (rm->
regs.num_regs >= 4) {
1389 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1391 else if (rm->
regs.num_regs >= 1) {
1392 RB_DEBUG_COUNTER_INC(obj_match_under4);
1395 onig_region_free(&rm->
regs, 0);
1398 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1402 if (
RFILE(obj)->fptr) {
1404 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1409 RB_DEBUG_COUNTER_INC(obj_rational);
1412 RB_DEBUG_COUNTER_INC(obj_complex);
1418 if (RICLASS_OWNS_M_TBL_P(obj)) {
1420 rb_id_table_free(RCLASS_M_TBL(obj));
1422 if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
1423 rb_id_table_free(RCLASS_CALLABLE_M_TBL(obj));
1425 rb_class_remove_subclass_head(obj);
1426 rb_cc_table_free(obj);
1427 rb_class_remove_from_module_subclasses(obj);
1428 rb_class_remove_from_super_subclasses(obj);
1430 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1434 RB_DEBUG_COUNTER_INC(obj_float);
1438 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1439 xfree(BIGNUM_DIGITS(obj));
1440 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1443 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1448 UNEXPECTED_NODE(obj_free);
1452 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1453 RSTRUCT(obj)->as.heap.ptr == NULL) {
1454 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1457 xfree((
void *)RSTRUCT(obj)->as.heap.ptr);
1458 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1463 RB_DEBUG_COUNTER_INC(obj_symbol);
1467 rb_imemo_free((
VALUE)obj);
1471 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1476 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, 0, 0);
1487 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1491internal_object_p(
VALUE obj)
1493 void *ptr = asan_unpoison_object_temporary(obj);
1495 if (
RBASIC(obj)->flags) {
1498 UNEXPECTED_NODE(internal_object_p);
1507 if (!
RBASIC(obj)->klass)
break;
1508 if (RCLASS_SINGLETON_P(obj)) {
1509 return rb_singleton_class_internal_p(obj);
1513 if (!
RBASIC(obj)->klass)
break;
1517 if (ptr || !
RBASIC(obj)->flags) {
1518 rb_asan_poison_object(obj);
1524rb_objspace_internal_object_p(
VALUE obj)
1526 return internal_object_p(obj);
1535os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1540 for (; v != (
VALUE)vend; v += stride) {
1541 if (!internal_object_p(v)) {
1561 rb_objspace_each_objects(os_obj_of_i, &oes);
1608 return os_obj_of(of);
1622 return rb_undefine_finalizer(obj);
1628 rb_check_frozen(obj);
1630 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1636should_be_callable(
VALUE block)
1639 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
1645should_be_finalizable(
VALUE obj)
1648 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
1651 rb_check_frozen(obj);
1657 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1732 if (rb_callable_receiver(block) == obj) {
1733 rb_warn(
"finalizer references object to be finalized");
1736 return rb_define_finalizer(obj, block);
1742 should_be_finalizable(obj);
1743 should_be_callable(block);
1745 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1753rb_objspace_call_finalizer(
void)
1755 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1759rb_objspace_free_objects(
void *
objspace)
1761 rb_gc_impl_shutdown_free_objects(
objspace);
1765rb_objspace_garbage_object_p(
VALUE obj)
1767 return rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1771rb_gc_pointer_to_heap_p(
VALUE obj)
1773 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj);
1794#if SIZEOF_LONG == SIZEOF_VOIDP
1795#define NUM2PTR(x) NUM2ULONG(x)
1796#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1797#define NUM2PTR(x) NUM2ULL(x)
1800 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
1801 VALUE ptr = NUM2PTR(objid);
1811 if (rb_static_id_valid_p(
SYM2ID(ptr))) {
1815 rb_raise(
rb_eRangeError,
"%p is not symbol id value", (
void *)ptr);
1819 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is not id value", rb_int2str(objid, 10));
1823 VALUE obj = rb_gc_impl_object_id_to_ref(rb_gc_get_objspace(), objid);
1828 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is id of the unshareable object on multi-ractor", rb_int2str(objid, 10));
1836 return id2ref(objid);
1843#if SIZEOF_LONG == SIZEOF_VOIDP
1850 return get_heap_object_id(
objspace, obj);
1854nonspecial_obj_id(
void *_objspace,
VALUE obj)
1856#if SIZEOF_LONG == SIZEOF_VOIDP
1858#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1861# error not supported
1868 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
1905 return rb_find_object_id(rb_gc_get_objspace(), obj, rb_gc_impl_object_id);
1908static enum rb_id_table_iterator_result
1909cc_table_memsize_i(
VALUE ccs_ptr,
void *data_ptr)
1911 size_t *total_size = data_ptr;
1913 *total_size +=
sizeof(*ccs);
1914 *total_size +=
sizeof(ccs->entries[0]) * ccs->capa;
1915 return ID_TABLE_CONTINUE;
1921 size_t total = rb_id_table_memsize(cc_table);
1922 rb_id_table_foreach_values(cc_table, cc_table_memsize_i, &total);
1927rb_obj_memsize_of(
VALUE obj)
1936 size += rb_generic_ivar_memsize(obj);
1941 if (rb_shape_obj_too_complex(obj)) {
1942 size += rb_st_memsize(ROBJECT_IV_HASH(obj));
1944 else if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1945 size += ROBJECT_IV_CAPACITY(obj) *
sizeof(
VALUE);
1950 if (RCLASS_M_TBL(obj)) {
1951 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1954 size +=
SIZEOF_VALUE << bit_length(RCLASS_IV_COUNT(obj));
1955 if (RCLASS_CVC_TBL(obj)) {
1956 size += rb_id_table_memsize(RCLASS_CVC_TBL(obj));
1958 if (RCLASS_EXT(obj)->const_tbl) {
1959 size += rb_id_table_memsize(RCLASS_EXT(obj)->const_tbl);
1961 if (RCLASS_CC_TBL(obj)) {
1962 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1964 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1965 size += (RCLASS_SUPERCLASS_DEPTH(obj) + 1) *
sizeof(
VALUE);
1969 if (RICLASS_OWNS_M_TBL_P(obj)) {
1970 if (RCLASS_M_TBL(obj)) {
1971 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1974 if (RCLASS_CC_TBL(obj)) {
1975 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1979 size += rb_str_memsize(obj);
1982 size += rb_ary_memsize(obj);
1985 if (RHASH_ST_TABLE_P(obj)) {
1986 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
1988 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(st_table);
1997 size += rb_objspace_data_type_memsize(obj);
2001 rb_matchext_t *rm = RMATCH_EXT(obj);
2002 size += onig_region_memsize(&rm->
regs);
2007 if (
RFILE(obj)->fptr) {
2008 size += rb_io_memsize(
RFILE(obj)->fptr);
2015 size += rb_imemo_memsize(obj);
2023 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2024 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
2029 UNEXPECTED_NODE(obj_memsize_of);
2033 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
2034 RSTRUCT(obj)->as.heap.ptr) {
2035 size +=
sizeof(
VALUE) * RSTRUCT_LEN(obj);
2044 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
2048 return size + rb_gc_obj_slot_size(obj);
2052set_zero(st_data_t key, st_data_t val, st_data_t arg)
2056 rb_hash_aset(hash, k,
INT2FIX(0));
2067count_objects_i(
VALUE obj,
void *d)
2071 if (
RBASIC(obj)->flags) {
2117count_objects(
int argc,
VALUE *argv,
VALUE os)
2128 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2131 hash = rb_hash_new();
2134 rb_hash_stlike_foreach(hash, set_zero, hash);
2136 rb_hash_aset(hash,
ID2SYM(rb_intern(
"TOTAL")),
SIZET2NUM(data.total));
2139 for (
size_t i = 0; i <=
T_MASK; i++) {
2148#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2150#define STACK_START (ec->machine.stack_start)
2151#define STACK_END (ec->machine.stack_end)
2152#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2154#if STACK_GROW_DIRECTION < 0
2155# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2156#elif STACK_GROW_DIRECTION > 0
2157# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2159# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2160 : (size_t)(STACK_END - STACK_START + 1))
2162#if !STACK_GROW_DIRECTION
2163int ruby_stack_grow_direction;
2165ruby_get_stack_grow_direction(
volatile VALUE *addr)
2168 SET_MACHINE_STACK_END(&end);
2170 if (end > addr)
return ruby_stack_grow_direction = 1;
2171 return ruby_stack_grow_direction = -1;
2178 rb_execution_context_t *ec = GET_EC();
2180 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2181 return STACK_LENGTH;
2184#define PREVENT_STACK_OVERFLOW 1
2185#ifndef PREVENT_STACK_OVERFLOW
2186#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2187# define PREVENT_STACK_OVERFLOW 1
2189# define PREVENT_STACK_OVERFLOW 0
2192#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2194stack_check(rb_execution_context_t *ec,
int water_mark)
2198 size_t length = STACK_LENGTH;
2199 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2201 return length > maximum_length;
2204#define stack_check(ec, water_mark) FALSE
2207#define STACKFRAME_FOR_CALL_CFUNC 2048
2210rb_ec_stack_check(rb_execution_context_t *ec)
2212 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2218 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2223#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2224 if (!RB_SPECIAL_CONST_P(obj)) { \
2225 rb_vm_t *vm = GET_VM(); \
2226 void *objspace = vm->gc.objspace; \
2227 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2228 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2229 (func)(objspace, (obj_or_ptr)); \
2231 else if (check_obj ? \
2232 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2233 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2235 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2236 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2237 vm->gc.mark_func_data = NULL; \
2238 mark_func_data->mark_func((obj), mark_func_data->data); \
2239 vm->gc.mark_func_data = mark_func_data; \
2245gc_mark_internal(
VALUE obj)
2247 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2253 gc_mark_internal(obj);
2257rb_gc_mark_and_move(
VALUE *ptr)
2259 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr,
false);
2263gc_mark_and_pin_internal(
VALUE obj)
2265 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2271 gc_mark_and_pin_internal(obj);
2275gc_mark_maybe_internal(
VALUE obj)
2277 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2283 gc_mark_maybe_internal(obj);
2287rb_gc_mark_weak(
VALUE *ptr)
2291 rb_vm_t *vm = GET_VM();
2293 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2294 GC_ASSERT(rb_gc_impl_during_gc_p(
objspace));
2296 rb_gc_impl_mark_weak(
objspace, ptr);
2299 GC_ASSERT(!rb_gc_impl_during_gc_p(
objspace));
2304rb_gc_remove_weak(
VALUE parent_obj,
VALUE *ptr)
2306 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj, ptr);
2309ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2311each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2322each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2324 if (end <= start)
return;
2325 each_location(start, end - start, cb, data);
2329gc_mark_maybe_each_location(
VALUE obj,
void *data)
2331 gc_mark_maybe_internal(obj);
2337 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2341rb_gc_mark_values(
long n,
const VALUE *values)
2343 for (
long i = 0; i < n; i++) {
2344 gc_mark_internal(values[i]);
2349rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
2351 for (
long i = 0; i < n; i++) {
2352 gc_mark_and_pin_internal(values[i]);
2357mark_key(st_data_t key, st_data_t value, st_data_t data)
2359 gc_mark_and_pin_internal((
VALUE)key);
2365rb_mark_set(st_table *tbl)
2369 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2373mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2375 gc_mark_internal((
VALUE)key);
2376 gc_mark_internal((
VALUE)value);
2382pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2384 gc_mark_and_pin_internal((
VALUE)key);
2385 gc_mark_and_pin_internal((
VALUE)value);
2391pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2393 gc_mark_and_pin_internal((
VALUE)key);
2394 gc_mark_internal((
VALUE)value);
2400mark_hash(
VALUE hash)
2402 if (rb_hash_compare_by_id_p(hash)) {
2403 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2406 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2409 gc_mark_internal(RHASH(hash)->ifnone);
2413rb_mark_hash(st_table *tbl)
2417 st_foreach(tbl, pin_key_pin_value, 0);
2420static enum rb_id_table_iterator_result
2423 gc_mark_internal(me);
2425 return ID_TABLE_CONTINUE;
2432 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
2436#if STACK_GROW_DIRECTION < 0
2437#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2438#elif STACK_GROW_DIRECTION > 0
2439#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2441#define GET_STACK_BOUNDS(start, end, appendix) \
2442 ((STACK_END < STACK_START) ? \
2443 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2447gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
2449 gc_mark_maybe_internal(obj);
2451#ifdef RUBY_ASAN_ENABLED
2452 const rb_execution_context_t *ec = (
const rb_execution_context_t *)data;
2453 void *fake_frame_start;
2454 void *fake_frame_end;
2455 bool is_fake_frame = asan_get_fake_stack_extents(
2456 ec->machine.asan_fake_stack_handle, obj,
2457 ec->machine.stack_start, ec->machine.stack_end,
2458 &fake_frame_start, &fake_frame_end
2460 if (is_fake_frame) {
2461 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2473 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)value));
2475 return rb_gc_impl_location(
objspace, value);
2481 return gc_location_internal(rb_gc_get_objspace(), value);
2484#if defined(__wasm__)
2487static VALUE *rb_stack_range_tmp[2];
2490rb_mark_locations(
void *begin,
void *end)
2492 rb_stack_range_tmp[0] = begin;
2493 rb_stack_range_tmp[1] = end;
2497rb_gc_save_machine_context(
void)
2502# if defined(__EMSCRIPTEN__)
2505mark_current_machine_context(
const rb_execution_context_t *ec)
2507 emscripten_scan_stack(rb_mark_locations);
2508 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2510 emscripten_scan_registers(rb_mark_locations);
2511 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2516mark_current_machine_context(
const rb_execution_context_t *ec)
2518 VALUE *stack_start, *stack_end;
2520 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2521 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2523 rb_wasm_scan_locals(rb_mark_locations);
2524 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2532rb_gc_save_machine_context(
void)
2534 rb_thread_t *thread = GET_THREAD();
2536 RB_VM_SAVE_MACHINE_CONTEXT(thread);
2541mark_current_machine_context(
const rb_execution_context_t *ec)
2543 rb_gc_mark_machine_context(ec);
2548rb_gc_mark_machine_context(
const rb_execution_context_t *ec)
2550 VALUE *stack_start, *stack_end;
2552 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2553 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2556#ifdef RUBY_ASAN_ENABLED
2558 (rb_execution_context_t *)ec;
2563 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2564 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
2565 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2569rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2571 gc_mark_and_pin_internal((
VALUE)value);
2577rb_mark_tbl(st_table *tbl)
2579 if (!tbl || tbl->num_entries == 0)
return;
2581 st_foreach(tbl, rb_mark_tbl_i, 0);
2585gc_mark_tbl_no_pin(st_table *tbl)
2587 if (!tbl || tbl->num_entries == 0)
return;
2589 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
2593rb_mark_tbl_no_pin(st_table *tbl)
2595 gc_mark_tbl_no_pin(tbl);
2598static enum rb_id_table_iterator_result
2606 gc_mark_internal((
VALUE)entry->cref);
2608 return ID_TABLE_CONTINUE;
2616 rb_id_table_foreach_values(tbl, mark_cvc_tbl_i,
objspace);
2623 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
2626static enum rb_id_table_iterator_result
2629 const rb_const_entry_t *ce = (
const rb_const_entry_t *)value;
2631 gc_mark_internal(ce->value);
2632 gc_mark_internal(ce->file);
2634 return ID_TABLE_CONTINUE;
2638rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
2640 rb_execution_context_t *ec = GET_EC();
2641 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2643#define MARK_CHECKPOINT(category) do { \
2644 if (categoryp) *categoryp = category; \
2647 MARK_CHECKPOINT(
"vm");
2649 if (vm->self) gc_mark_internal(vm->self);
2651 MARK_CHECKPOINT(
"end_proc");
2654 MARK_CHECKPOINT(
"global_tbl");
2655 rb_gc_mark_global_tbl();
2658 void rb_yjit_root_mark(
void);
2660 if (rb_yjit_enabled_p) {
2661 MARK_CHECKPOINT(
"YJIT");
2662 rb_yjit_root_mark();
2666 MARK_CHECKPOINT(
"machine_context");
2667 mark_current_machine_context(ec);
2669 MARK_CHECKPOINT(
"finish");
2671#undef MARK_CHECKPOINT
2674#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA(d)->type->function.dmark
2680 rb_mark_generic_ivar(obj);
2695 rb_bug(
"rb_gc_mark() called for broken object");
2699 UNEXPECTED_NODE(rb_gc_mark);
2703 rb_imemo_mark_and_move(obj,
false);
2710 gc_mark_internal(
RBASIC(obj)->klass);
2715 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
2723 mark_m_tbl(
objspace, RCLASS_M_TBL(obj));
2725 rb_cc_table_mark(obj);
2726 if (rb_shape_obj_too_complex(obj)) {
2727 gc_mark_tbl_no_pin((st_table *)RCLASS_IVPTR(obj));
2730 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
2731 gc_mark_internal(RCLASS_IVPTR(obj)[i]);
2735 if (RCLASS_CONST_TBL(obj)) {
2736 rb_id_table_foreach_values(RCLASS_CONST_TBL(obj), mark_const_table_i,
objspace);
2739 gc_mark_internal(RCLASS_EXT(obj)->classpath);
2743 if (RICLASS_OWNS_M_TBL_P(obj)) {
2744 mark_m_tbl(
objspace, RCLASS_M_TBL(obj));
2750 if (RCLASS_INCLUDER(obj)) {
2751 gc_mark_internal(RCLASS_INCLUDER(obj));
2753 mark_m_tbl(
objspace, RCLASS_CALLABLE_M_TBL(obj));
2754 rb_cc_table_mark(obj);
2758 if (ARY_SHARED_P(obj)) {
2759 VALUE root = ARY_SHARED_ROOT(obj);
2760 gc_mark_internal(root);
2765 for (
long i = 0; i <
len; i++) {
2766 gc_mark_internal(ptr[i]);
2776 if (STR_SHARED_P(obj)) {
2777 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
2782 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
2785 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
2795 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
2797 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
2798 gc_mark_internal(*(
VALUE *)((
char *)ptr + offset));
2805 if (mark_func) (*mark_func)(ptr);
2813 rb_shape_t *shape = rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj));
2815 if (rb_shape_obj_too_complex(obj)) {
2816 gc_mark_tbl_no_pin(ROBJECT_IV_HASH(obj));
2821 uint32_t
len = ROBJECT_IV_COUNT(obj);
2822 for (uint32_t i = 0; i <
len; i++) {
2823 gc_mark_internal(ptr[i]);
2831 attr_index_t num_of_ivs = shape->next_iv_index;
2832 if (RCLASS_EXT(klass)->max_iv_count < num_of_ivs) {
2833 RCLASS_EXT(klass)->max_iv_count = num_of_ivs;
2841 if (
RFILE(obj)->fptr) {
2842 gc_mark_internal(
RFILE(obj)->fptr->self);
2843 gc_mark_internal(
RFILE(obj)->fptr->pathv);
2844 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
2845 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
2846 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
2847 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
2848 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
2849 gc_mark_internal(
RFILE(obj)->fptr->timeout);
2854 gc_mark_internal(
RREGEXP(obj)->src);
2858 gc_mark_internal(
RMATCH(obj)->regexp);
2860 gc_mark_internal(
RMATCH(obj)->str);
2865 gc_mark_internal(RRATIONAL(obj)->num);
2866 gc_mark_internal(RRATIONAL(obj)->den);
2870 gc_mark_internal(RCOMPLEX(obj)->real);
2871 gc_mark_internal(RCOMPLEX(obj)->imag);
2875 const long len = RSTRUCT_LEN(obj);
2876 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
2878 for (
long i = 0; i <
len; i++) {
2879 gc_mark_internal(ptr[i]);
2889 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
2891 rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj) ?
"corrupted object" :
"non object");
2896rb_gc_obj_optimal_size(
VALUE obj)
2900 return rb_ary_size_as_embedded(obj);
2903 if (rb_shape_obj_too_complex(obj)) {
2904 return sizeof(
struct RObject);
2911 return rb_str_size_as_embedded(obj);
2914 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));
2922rb_gc_writebarrier(VALUE a, VALUE b)
2924 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
2930 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
2937rb_gc_writebarrier_remember(
VALUE obj)
2939 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
2945 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
2949rb_gc_modular_gc_loaded_p(
void)
2952 return rb_gc_functions.modular_gc_loaded_p;
2959rb_gc_active_gc_name(
void)
2961 const char *gc_name = rb_gc_impl_active_gc_name();
2963 const size_t len = strlen(gc_name);
2964 if (
len > RB_GC_MAX_NAME_LEN) {
2965 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
2966 RB_GC_MAX_NAME_LEN,
len, gc_name);
2974rb_obj_gc_flags(
VALUE obj,
ID* flags,
size_t max)
2976 return rb_gc_impl_obj_flags(rb_gc_get_objspace(), obj, flags, max);
2982rb_gc_ractor_cache_alloc(rb_ractor_t *ractor)
2984 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
2988rb_gc_ractor_cache_free(
void *cache)
2990 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
2996 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
2999 rb_vm_register_global_object(obj);
3005 rb_vm_t *vm = GET_VM();
3010 tmp->next = vm->global_object_list;
3012 vm->global_object_list = tmp;
3020 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
3022 rb_print_backtrace(stderr);
3029 rb_vm_t *vm = GET_VM();
3032 if (tmp->varptr == addr) {
3033 vm->global_object_list = tmp->next;
3038 if (tmp->next->varptr == addr) {
3041 tmp->next = tmp->next->next;
3052 rb_gc_register_address(var);
3056gc_start_internal(rb_execution_context_t *ec,
VALUE self,
VALUE full_mark,
VALUE immediate_mark,
VALUE immediate_sweep,
VALUE compact)
3058 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
3102rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
3104 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3110 if (ARY_SHARED_P(v)) {
3111 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
3115 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
3117 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3118 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
3119 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
3120 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
3128 for (
long i = 0; i <
len; i++) {
3133 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3134 if (rb_ary_embeddable_p(v)) {
3135 rb_ary_make_embedded(v);
3146 if (rb_shape_obj_too_complex(v)) {
3147 gc_ref_update_table_values_only(ROBJECT_IV_HASH(v));
3151 size_t slot_size = rb_gc_obj_slot_size(v);
3153 if (slot_size >= embed_size && !
RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
3155 memcpy(
ROBJECT(v)->as.ary, ptr,
sizeof(
VALUE) * ROBJECT_IV_COUNT(v));
3161 for (uint32_t i = 0; i < ROBJECT_IV_COUNT(v); i++) {
3167rb_gc_ref_update_table_values_only(st_table *tbl)
3169 gc_ref_update_table_values_only(tbl);
3174rb_gc_update_tbl_refs(st_table *ptr)
3176 gc_update_table_refs(ptr);
3182 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
3188 for (
long i = 0; i < n; i++) {
3189 UPDATE_IF_MOVED(
objspace, values[i]);
3194rb_gc_update_values(
long n,
VALUE *values)
3196 gc_update_values(rb_gc_get_objspace(), n, values);
3199static enum rb_id_table_iterator_result
3200check_id_table_move(
VALUE value,
void *data)
3205 return ID_TABLE_REPLACE;
3208 return ID_TABLE_CONTINUE;
3212rb_gc_prepare_heap_process_object(
VALUE obj)
3226rb_gc_prepare_heap(
void)
3228 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3232rb_gc_heap_id_for_size(
size_t size)
3234 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3238rb_gc_size_allocatable_p(
size_t size)
3240 return rb_gc_impl_size_allocatable_p(size);
3243static enum rb_id_table_iterator_result
3244update_id_table(
VALUE *value,
void *data,
int existing)
3252 return ID_TABLE_CONTINUE;
3259 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
3263static enum rb_id_table_iterator_result
3267 VM_ASSERT(vm_ccs_p(ccs));
3270 ccs->cme = (
const rb_callable_method_entry_t *)gc_location_internal(
objspace, (
VALUE)ccs->cme);
3273 for (
int i=0; i<ccs->len; i++) {
3274 if (rb_gc_impl_object_moved_p(
objspace, (
VALUE)ccs->entries[i].cc)) {
3280 return ID_TABLE_CONTINUE;
3288 rb_id_table_foreach_values(tbl, update_cc_tbl_i,
objspace);
3292static enum rb_id_table_iterator_result
3300 TYPED_UPDATE_IF_MOVED(
objspace, rb_cref_t *, entry->cref);
3303 entry->class_value = gc_location_internal(
objspace, entry->class_value);
3305 return ID_TABLE_CONTINUE;
3313 rb_id_table_foreach_values(tbl, update_cvc_tbl_i,
objspace);
3317static enum rb_id_table_iterator_result
3320 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3322 if (rb_gc_impl_object_moved_p(
objspace, ce->value)) {
3323 ce->value = gc_location_internal(
objspace, ce->value);
3326 if (rb_gc_impl_object_moved_p(
objspace, ce->file)) {
3327 ce->file = gc_location_internal(
objspace, ce->file);
3330 return ID_TABLE_CONTINUE;
3337 rb_id_table_foreach_values(tbl, update_const_table,
objspace);
3341update_subclass_entries(
void *
objspace, rb_subclass_entry_t *entry)
3344 UPDATE_IF_MOVED(
objspace, entry->klass);
3345 entry = entry->next;
3350update_class_ext(
void *
objspace, rb_classext_t *ext)
3352 UPDATE_IF_MOVED(
objspace, ext->origin_);
3353 UPDATE_IF_MOVED(
objspace, ext->includer);
3354 UPDATE_IF_MOVED(
objspace, ext->refined_class);
3355 update_subclass_entries(
objspace, ext->subclasses);
3361 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
3362 for (
size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj) + 1; i++) {
3363 UPDATE_IF_MOVED(
objspace, RCLASS_SUPERCLASSES(obj)[i]);
3369#define global_symbols ruby_global_symbols
3372struct global_vm_table_foreach_data {
3373 vm_table_foreach_callback_func callback;
3374 vm_table_update_callback_func update_callback;
3379vm_weak_table_foreach_key(st_data_t key, st_data_t value, st_data_t data,
int error)
3381 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3383 return iter_data->callback((
VALUE)key, iter_data->data);
3387vm_weak_table_foreach_update_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3389 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3391 return iter_data->update_callback((
VALUE *)key, iter_data->data);
3395vm_weak_table_str_sym_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3397 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3403 return iter_data->callback((
VALUE)value, iter_data->data);
3408vm_weak_table_foreach_update_value(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3410 struct global_vm_table_foreach_data *iter_data = (
struct global_vm_table_foreach_data *)data;
3412 return iter_data->update_callback((
VALUE *)value, iter_data->data);
3416vm_weak_table_gen_ivar_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3418 int retval = vm_weak_table_foreach_key(key, value, data, error);
3419 if (retval == ST_DELETE) {
3426vm_weak_table_frozen_strings_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3430 int retval = vm_weak_table_foreach_key(key, value, data, error);
3431 if (retval == ST_DELETE) {
3437struct st_table *rb_generic_ivtbl_get(
void);
3440rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
3441 vm_table_update_callback_func update_callback,
3443 enum rb_gc_vm_weak_tables table)
3445 rb_vm_t *vm = GET_VM();
3447 struct global_vm_table_foreach_data foreach_data = {
3448 .callback = callback,
3449 .update_callback = update_callback,
3454 case RB_GC_VM_CI_TABLE: {
3455 st_foreach_with_replace(
3457 vm_weak_table_foreach_key,
3458 vm_weak_table_foreach_update_key,
3459 (st_data_t)&foreach_data
3463 case RB_GC_VM_OVERLOADED_CME_TABLE: {
3464 st_foreach_with_replace(
3465 vm->overloaded_cme_table,
3466 vm_weak_table_foreach_key,
3467 vm_weak_table_foreach_update_key,
3468 (st_data_t)&foreach_data
3472 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
3473 st_foreach_with_replace(
3474 global_symbols.str_sym,
3475 vm_weak_table_str_sym_foreach,
3476 vm_weak_table_foreach_update_value,
3477 (st_data_t)&foreach_data
3481 case RB_GC_VM_GENERIC_IV_TABLE: {
3482 st_table *generic_iv_tbl = rb_generic_ivtbl_get();
3483 st_foreach_with_replace(
3485 vm_weak_table_gen_ivar_foreach,
3486 vm_weak_table_foreach_update_key,
3487 (st_data_t)&foreach_data
3491 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
3492 st_table *frozen_strings = GET_VM()->frozen_strings;
3493 st_foreach_with_replace(
3495 vm_weak_table_frozen_strings_foreach,
3496 vm_weak_table_foreach_update_key,
3497 (st_data_t)&foreach_data
3502 rb_bug(
"rb_gc_vm_weak_table_foreach: unknown table %d", table);
3508rb_gc_update_vm_references(
void *
objspace)
3510 rb_execution_context_t *ec = GET_EC();
3511 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3513 rb_vm_update_references(vm);
3514 rb_gc_update_global_tbl();
3515 global_symbols.ids = gc_location_internal(
objspace, global_symbols.ids);
3516 global_symbols.dsymbol_fstr_hash = gc_location_internal(
objspace, global_symbols.dsymbol_fstr_hash);
3517 gc_update_table_refs(global_symbols.str_sym);
3520 void rb_yjit_root_update_references(
void);
3522 if (rb_yjit_enabled_p) {
3523 rb_yjit_root_update_references();
3532 rb_ref_update_generic_ivar(obj);
3538 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
3545 update_m_tbl(
objspace, RCLASS_M_TBL(obj));
3548 update_superclasses(
objspace, obj);
3550 if (rb_shape_obj_too_complex(obj)) {
3551 gc_ref_update_table_values_only(RCLASS_IV_HASH(obj));
3554 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
3555 UPDATE_IF_MOVED(
objspace, RCLASS_IVPTR(obj)[i]);
3559 update_class_ext(
objspace, RCLASS_EXT(obj));
3560 update_const_tbl(
objspace, RCLASS_CONST_TBL(obj));
3562 UPDATE_IF_MOVED(
objspace, RCLASS_EXT(obj)->classpath);
3566 if (RICLASS_OWNS_M_TBL_P(obj)) {
3567 update_m_tbl(
objspace, RCLASS_M_TBL(obj));
3572 update_class_ext(
objspace, RCLASS_EXT(obj));
3573 update_m_tbl(
objspace, RCLASS_CALLABLE_M_TBL(obj));
3578 rb_imemo_mark_and_move(obj,
true);
3590 gc_ref_update_array(
objspace, obj);
3595 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
3600 if (STR_SHARED_P(obj)) {
3606 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
3607 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
3608 rb_str_make_embedded(obj);
3620 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3622 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3623 VALUE *ref = (
VALUE *)((
char *)ptr + offset);
3624 *ref = gc_location_internal(
objspace, *ref);
3629 if (compact_func) (*compact_func)(ptr);
3636 gc_ref_update_object(
objspace, obj);
3640 if (
RFILE(obj)->fptr) {
3643 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
3644 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
3645 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
3655 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
3671 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
3672 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
3676 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
3677 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
3683 long i,
len = RSTRUCT_LEN(obj);
3684 VALUE *ptr = (
VALUE *)RSTRUCT_CONST_PTR(obj);
3686 for (i = 0; i <
len; i++) {
3692 rb_bug(
"unreachable");
3709 unless_objspace(
objspace) {
return; }
3711 rb_gc_impl_start(
objspace,
true,
true,
true,
false);
3717 unless_objspace(
objspace) {
return FALSE; }
3719 return rb_gc_impl_during_gc_p(
objspace);
3725 return rb_gc_impl_gc_count(rb_gc_get_objspace());
3729gc_count(rb_execution_context_t *ec,
VALUE self)
3741 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
3744 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
3751gc_stat(rb_execution_context_t *ec,
VALUE self,
VALUE arg)
3754 arg = rb_hash_new();
3760 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3765 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3778 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3783 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3795gc_stat_heap(rb_execution_context_t *ec,
VALUE self,
VALUE heap_name,
VALUE arg)
3798 arg = rb_hash_new();
3801 if (
NIL_P(heap_name)) {
3812 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
3815 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
3820 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
3827gc_config_get(rb_execution_context_t *ec,
VALUE self)
3829 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
3830 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
3836gc_config_set(rb_execution_context_t *ec,
VALUE self,
VALUE hash)
3838 void *
objspace = rb_gc_get_objspace();
3840 rb_gc_impl_config_set(
objspace, hash);
3842 return rb_gc_impl_config_get(
objspace);
3846gc_stress_get(rb_execution_context_t *ec,
VALUE self)
3848 return rb_gc_impl_stress_get(rb_gc_get_objspace());
3852gc_stress_set_m(rb_execution_context_t *ec,
VALUE self,
VALUE flag)
3854 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
3860rb_gc_initial_stress_set(
VALUE flag)
3862 initial_stress = flag;
3866rb_gc_heap_sizes(
void)
3868 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
3874 return rb_objspace_gc_enable(rb_gc_get_objspace());
3878rb_objspace_gc_enable(
void *
objspace)
3880 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3882 return RBOOL(disabled);
3886gc_enable(rb_execution_context_t *ec,
VALUE _)
3888 return rb_gc_enable();
3894 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3895 rb_gc_impl_gc_disable(
objspace,
false);
3896 return RBOOL(disabled);
3900rb_gc_disable_no_rest(
void)
3902 return gc_disable_no_rest(rb_gc_get_objspace());
3908 return rb_objspace_gc_disable(rb_gc_get_objspace());
3912rb_objspace_gc_disable(
void *
objspace)
3914 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
3915 rb_gc_impl_gc_disable(
objspace,
true);
3916 return RBOOL(disabled);
3920gc_disable(rb_execution_context_t *ec,
VALUE _)
3922 return rb_gc_disable();
3927ruby_gc_set_params(
void)
3929 rb_gc_impl_set_params(rb_gc_get_objspace());
3933rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
3937 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
3940 rb_vm_t *vm = GET_VM();
3941 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3942 struct gc_mark_func_data_struct mfd = {
3947 vm->gc.mark_func_data = &mfd;
3948 rb_gc_mark_children(rb_gc_get_objspace(), obj);
3949 vm->gc.mark_func_data = prev_mfd;
3956 const char *category;
3957 void (*func)(
const char *category,
VALUE,
void *);
3962root_objects_from(
VALUE obj,
void *ptr)
3965 (*data->func)(data->category, obj, data->data);
3969rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
3971 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");
3973 rb_vm_t *vm = GET_VM();
3977 .data = passing_data,
3980 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3981 struct gc_mark_func_data_struct mfd = {
3982 .mark_func = root_objects_from,
3986 vm->gc.mark_func_data = &mfd;
3987 rb_gc_save_machine_context();
3988 rb_gc_mark_roots(vm->gc.objspace, &data.category);
3989 vm->gc.mark_func_data = prev_mfd;
4000#define TYPE_NAME(t) case (t): return #t;
4027 if (obj && rb_objspace_data_type_name(obj)) {
4028 return rb_objspace_data_type_name(obj);
4037obj_type_name(
VALUE obj)
4039 return type_name(
TYPE(obj), obj);
4043rb_method_type_name(rb_method_type_t
type)
4046 case VM_METHOD_TYPE_ISEQ:
return "iseq";
4047 case VM_METHOD_TYPE_ATTRSET:
return "attrest";
4048 case VM_METHOD_TYPE_IVAR:
return "ivar";
4049 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
4050 case VM_METHOD_TYPE_ALIAS:
return "alias";
4051 case VM_METHOD_TYPE_REFINED:
return "refined";
4052 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
4053 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
4054 case VM_METHOD_TYPE_MISSING:
return "missing";
4055 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
4056 case VM_METHOD_TYPE_UNDEF:
return "undef";
4057 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
4059 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
4063rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
4065 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
4066 VALUE path = rb_iseq_path(iseq);
4067 int n = ISEQ_BODY(iseq)->location.first_lineno;
4068 snprintf(buff, buff_size,
" %s@%s:%d",
4069 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4070 RSTRING_PTR(path), n);
4075str_len_no_raise(
VALUE str)
4077 long len = RSTRING_LEN(str);
4078 if (
len < 0)
return 0;
4079 if (
len > INT_MAX)
return INT_MAX;
4083#define BUFF_ARGS buff + pos, buff_size - pos
4084#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4085#define APPEND_S(s) do { \
4086 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4090 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4093#define C(c, s) ((c) != 0 ? (s) : " ")
4096rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
4101 APPEND_F(
"%s", obj_type_name(obj));
4107 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
4113 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
4132 if (internal_object_p(obj)) {
4135 else if (
RBASIC(obj)->klass == 0) {
4136 APPEND_S(
"(temporary internal)");
4140 if (!
NIL_P(class_path)) {
4141 APPEND_F(
"(%s)", RSTRING_PTR(class_path));
4150const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
4153rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
4160 UNEXPECTED_NODE(rb_raw_obj_info);
4163 if (ARY_SHARED_P(obj)) {
4164 APPEND_S(
"shared -> ");
4165 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4167 else if (ARY_EMBED_P(obj)) {
4168 APPEND_F(
"[%s%s] len: %ld (embed)",
4169 C(ARY_EMBED_P(obj),
"E"),
4170 C(ARY_SHARED_P(obj),
"S"),
4174 APPEND_F(
"[%s%s] len: %ld, capa:%ld ptr:%p",
4175 C(ARY_EMBED_P(obj),
"E"),
4176 C(ARY_SHARED_P(obj),
"S"),
4178 ARY_EMBED_P(obj) ? -1L :
RARRAY(obj)->as.heap.aux.capa,
4183 if (STR_SHARED_P(obj)) {
4184 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
4187 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
4189 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
4191 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
4195 VALUE fstr = RSYMBOL(obj)->fstr;
4196 ID id = RSYMBOL(obj)->id;
4198 APPEND_F(
":%s id:%d", RSTRING_PTR(fstr), (
unsigned int)
id);
4201 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
4206 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
4210 APPEND_F(
"[%c] %"PRIdSIZE,
4211 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
4219 if (!
NIL_P(class_path)) {
4220 APPEND_F(
"%s", RSTRING_PTR(class_path));
4230 if (!
NIL_P(class_path)) {
4231 APPEND_F(
"src:%s", RSTRING_PTR(class_path));
4237 if (rb_shape_obj_too_complex(obj)) {
4238 size_t hash_len = rb_st_table_size(ROBJECT_IV_HASH(obj));
4239 APPEND_F(
"(too_complex) len:%zu", hash_len);
4242 uint32_t
len = ROBJECT_IV_CAPACITY(obj);
4244 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
4245 APPEND_F(
"(embed) len:%d",
len);
4249 APPEND_F(
"len:%d ptr:%p",
len, (
void *)ptr);
4256 const rb_iseq_t *iseq;
4258 (block = vm_proc_block(obj)) != NULL &&
4259 (vm_block_type(block) == block_type_iseq) &&
4260 (iseq = vm_block_iseq(block)) != NULL) {
4261 rb_raw_iseq_info(BUFF_ARGS, iseq);
4263 else if (rb_ractor_p(obj)) {
4264 rb_ractor_t *r = (
void *)
DATA_PTR(obj);
4266 APPEND_F(
"r:%d", r->pub.id);
4270 const char *
const type_name = rb_objspace_data_type_name(obj);
4272 APPEND_F(
"%s", type_name);
4278 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
4280 switch (imemo_type(obj)) {
4283 const rb_method_entry_t *me = (
const rb_method_entry_t *)obj;
4285 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
4286 rb_id2name(me->called_id),
4287 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
4288 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
4289 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
4290 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
4291 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
4292 me->def ? rb_method_type_name(me->def->type) :
"NULL",
4293 me->def ? me->def->aliased : -1,
4295 (
void *)me->defined_class);
4298 switch (me->def->type) {
4299 case VM_METHOD_TYPE_ISEQ:
4300 APPEND_S(
" (iseq:");
4301 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
4312 const rb_iseq_t *iseq = (
const rb_iseq_t *)obj;
4313 rb_raw_iseq_info(BUFF_ARGS, iseq);
4316 case imemo_callinfo:
4319 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
4320 rb_id2name(vm_ci_mid(ci)),
4323 vm_ci_kwarg(ci) ?
"available" :
"NULL");
4326 case imemo_callcache:
4330 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4332 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
4333 NIL_P(class_path) ? (cc->klass ?
"??" :
"<NULL>") : RSTRING_PTR(class_path),
4334 cme ? rb_id2name(cme->called_id) :
"<NULL>",
4335 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
4337 (
void *)(uintptr_t)vm_cc_call(cc));
4356rb_asan_poison_object(
VALUE obj)
4358 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4359 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
4363rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
4365 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4366 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
4370rb_asan_poisoned_object_p(
VALUE obj)
4372 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4373 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
4376#define asan_unpoisoning_object(obj) \
4377 for (void *poisoned = asan_unpoison_object_temporary(obj), \
4378 *unpoisoning = &poisoned; \
4380 unpoisoning = asan_poison_object_restore(obj, poisoned))
4383rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
4385 asan_unpoisoning_object(obj) {
4386 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4387 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4388 if (pos >= buff_size) {}
4399#define OBJ_INFO_BUFFERS_NUM 10
4400#define OBJ_INFO_BUFFERS_SIZE 0x100
4402static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
4410 if (RB_UNLIKELY(oldval >= maxval - 1)) {
4421 rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
4422 char *
const buff = obj_info_buffers[index];
4423 return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
4429 return obj_type_name(obj);
4447 rb_vraise(argv->exc, argv->fmt, *argv->ap);
4452gc_raise(
VALUE exc,
const char *fmt, ...)
4460 if (ruby_thread_has_gvl_p()) {
4470 fprintf(stderr,
"%s",
"[FATAL] ");
4471 vfprintf(stderr, fmt, ap);
4478NORETURN(
static void negative_size_allocation_error(
const char *));
4480negative_size_allocation_error(
const char *msg)
4486ruby_memerror_body(
void *dummy)
4492NORETURN(
static void ruby_memerror(
void));
4497 if (ruby_thread_has_gvl_p()) {
4506 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4523 rb_execution_context_t *ec = GET_EC();
4524 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
4527 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
4528 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
4529 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4532 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4533 rb_ec_raised_clear(ec);
4536 rb_ec_raised_set(ec, RAISED_NOMEMORY);
4537 exc = ruby_vm_special_exception_copy(exc);
4540 EC_JUMP_TAG(ec, TAG_RAISE);
4544rb_memerror_reentered(
void)
4546 rb_execution_context_t *ec = GET_EC();
4547 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
4551rb_malloc_info_show_results(
void)
4556handle_malloc_failure(
void *ptr)
4567static void *ruby_xmalloc_body(
size_t size);
4570ruby_xmalloc(
size_t size)
4572 return handle_malloc_failure(ruby_xmalloc_body(size));
4576ruby_xmalloc_body(
size_t size)
4578 if ((ssize_t)size < 0) {
4579 negative_size_allocation_error(
"too large allocation size");
4582 return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
4586ruby_malloc_size_overflow(
size_t count,
size_t elsize)
4588 rb_raise(rb_eArgError,
4589 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
4593static void *ruby_xmalloc2_body(
size_t n,
size_t size);
4596ruby_xmalloc2(
size_t n,
size_t size)
4598 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
4602ruby_xmalloc2_body(
size_t n,
size_t size)
4604 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4607static void *ruby_xcalloc_body(
size_t n,
size_t size);
4610ruby_xcalloc(
size_t n,
size_t size)
4612 return handle_malloc_failure(ruby_xcalloc_body(n, size));
4616ruby_xcalloc_body(
size_t n,
size_t size)
4618 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4621static void *ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size);
4623#ifdef ruby_sized_xrealloc
4624#undef ruby_sized_xrealloc
4627ruby_sized_xrealloc(
void *ptr,
size_t new_size,
size_t old_size)
4629 return handle_malloc_failure(ruby_sized_xrealloc_body(ptr, new_size, old_size));
4633ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size)
4635 if ((ssize_t)new_size < 0) {
4636 negative_size_allocation_error(
"too large allocation size");
4639 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size);
4643ruby_xrealloc(
void *ptr,
size_t new_size)
4645 return ruby_sized_xrealloc(ptr, new_size, 0);
4648static void *ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n);
4650#ifdef ruby_sized_xrealloc2
4651#undef ruby_sized_xrealloc2
4654ruby_sized_xrealloc2(
void *ptr,
size_t n,
size_t size,
size_t old_n)
4656 return handle_malloc_failure(ruby_sized_xrealloc2_body(ptr, n, size, old_n));
4660ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n)
4662 size_t len = xmalloc2_size(n, size);
4663 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr,
len, old_n * size);
4667ruby_xrealloc2(
void *ptr,
size_t n,
size_t size)
4669 return ruby_sized_xrealloc2(ptr, n, size, 0);
4672#ifdef ruby_sized_xfree
4673#undef ruby_sized_xfree
4676ruby_sized_xfree(
void *x,
size_t size)
4682 if (LIKELY(GET_VM())) {
4683 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
4694 ruby_sized_xfree(x, 0);
4698rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
4700 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4701 return ruby_xmalloc(w);
4705rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
4707 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4708 return ruby_xcalloc(w, 1);
4712rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
4714 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4715 return ruby_xrealloc((
void *)p, w);
4719rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4721 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4722 return ruby_xmalloc(u);
4726rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4728 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4729 return ruby_xcalloc(u, 1);
4736ruby_mimmalloc(
size_t size)
4739#if CALC_EXACT_MALLOC_SIZE
4743#if CALC_EXACT_MALLOC_SIZE
4759ruby_mimcalloc(
size_t num,
size_t size)
4762#if CALC_EXACT_MALLOC_SIZE
4763 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
4764 if (UNLIKELY(t.left)) {
4768 mem = calloc1(size);
4780 mem = calloc(num, size);
4786ruby_mimfree(
void *ptr)
4788#if CALC_EXACT_MALLOC_SIZE
4796rb_gc_adjust_memory_usage(ssize_t diff)
4798 unless_objspace(
objspace) {
return; }
4800 rb_gc_impl_adjust_memory_usage(
objspace, diff);
4804rb_obj_info(
VALUE obj)
4806 return obj_info(obj);
4810rb_obj_info_dump(
VALUE obj)
4813 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
4817rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
4820 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
4824rb_gc_before_fork(
void)
4826 rb_gc_impl_before_fork(rb_gc_get_objspace());
4830rb_gc_after_fork(rb_pid_t pid)
4832 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
4886 malloc_offset = gc_compute_malloc_offset();
4899 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
4922ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
4924#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
4927 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (
unsigned long)addr, size, name);
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
VALUE rb_define_module(const char *name)
Defines a top-level module.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define T_FILE
Old name of RUBY_T_FILE.
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define T_MASK
Old name of RUBY_T_MASK.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define T_DATA
Old name of RUBY_T_DATA.
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define LL2NUM
Old name of RB_LL2NUM.
#define T_NONE
Old name of RUBY_T_NONE.
#define T_NODE
Old name of RUBY_T_NODE.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_ABLE
Old name of RB_FL_ABLE.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define T_UNDEF
Old name of RUBY_T_UNDEF.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_MATCH
Old name of RUBY_T_MATCH.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define T_MOVED
Old name of RUBY_T_MOVED.
#define FL_TEST
Old name of RB_FL_TEST.
#define xcalloc
Old name of ruby_xcalloc.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
int ruby_stack_check(void)
Checks for stack overflow.
VALUE rb_eNoMemError
NoMemoryError exception.
VALUE rb_eRangeError
RangeError exception.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
size_t rb_obj_embedded_size(uint32_t numiv)
Internal header for Object.
VALUE rb_mKernel
Kernel module.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
void rb_str_free(VALUE str)
Destroys the given string for no reason.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_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).
int len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
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.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RCLASS(obj)
Convenient casting macro.
#define DATA_PTR(obj)
Convenient getter macro.
#define RDATA(obj)
Convenient casting macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
#define RFILE(obj)
Convenient casting macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RMATCH(obj)
Convenient casting macro.
#define ROBJECT(obj)
Convenient casting macro.
static VALUE * ROBJECT_IVPTR(VALUE obj)
Queries the instance variables.
#define RREGEXP(obj)
Convenient casting macro.
#define RREGEXP_PTR(obj)
Convenient accessor macro.
#define RSTRING(obj)
Convenient casting macro.
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define RTYPEDDATA(obj)
Convenient casting macro.
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define errno
Ractor-aware version of errno.
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
const char * wrap_struct_name
Name of structs of this kind.
struct rmatch_offset * char_offset
Capture group offsets, in C array.
int char_offset_num_allocated
Number of rmatch_offset that ::rmatch::char_offset holds.
struct re_registers regs
"Registers" of a match.
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Represents the region of a capture group.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.