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"
120#include "ruby_assert.h"
121#include "ruby_atomic.h"
126#include "vm_callinfo.h"
127#include "ractor_core.h"
136 unsigned int lev = 0;
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_NB(&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;
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)
252 if (RB_LIKELY(ruby_single_main_ractor)) {
254 ccan_list_empty(&GET_VM()->ractor.set) ||
255 (ccan_list_top(&GET_VM()->ractor.set,
rb_ractor_t, vmlr_node) == ruby_single_main_ractor &&
256 ccan_list_tail(&GET_VM()->ractor.set,
rb_ractor_t, vmlr_node) == ruby_single_main_ractor)
259 func(ruby_single_main_ractor->newobj_cache, data);
262 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
263 func(r->newobj_cache, data);
269rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
280#define RESTORE_FINALIZER() (\
281 ec->cfp = saved.cfp, \
282 ec->cfp->sp = saved.sp, \
283 ec->errinfo = saved.errinfo)
285 saved.errinfo = ec->errinfo;
287 saved.sp = ec->cfp->sp;
292 enum ruby_tag_type state = EC_EXEC_TAG();
293 if (state != TAG_NONE) {
296 VALUE failed_final = saved.final;
299 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
300 rb_ec_error_print(ec, ec->errinfo);
304 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
305 saved.final = callback(i, data);
309#undef RESTORE_FINALIZER
313rb_gc_set_pending_interrupt(
void)
316 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
320rb_gc_unset_pending_interrupt(
void)
323 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
327rb_gc_multi_ractor_p(
void)
329 return rb_multi_ractor_p();
332bool rb_obj_is_main_ractor(
VALUE gv);
335rb_gc_shutdown_call_finalizer_p(
VALUE obj)
339 if (!ruby_free_at_exit_p() && (!
DATA_PTR(obj) || !
RDATA(obj)->dfree))
return false;
340 if (rb_obj_is_thread(obj))
return false;
341 if (rb_obj_is_mutex(obj))
return false;
342 if (rb_obj_is_fiber(obj))
return false;
343 if (rb_obj_is_main_ractor(obj))
return false;
344 if (rb_obj_is_fstring_table(obj))
return false;
352 if (RSYMBOL(obj)->fstr &&
355 RSYMBOL(obj)->fstr = 0;
363 return ruby_free_at_exit_p();
368rb_gc_get_shape(
VALUE obj)
370 return (uint32_t)rb_obj_shape_id(obj);
374rb_gc_set_shape(
VALUE obj, uint32_t shape_id)
376 rb_obj_set_shape_id(obj, (uint32_t)shape_id);
380rb_gc_rebuild_shape(
VALUE obj,
size_t heap_id)
382 shape_id_t orig_shape_id = rb_obj_shape_id(obj);
383 if (rb_shape_too_complex_p(orig_shape_id)) {
384 return (uint32_t)orig_shape_id;
387 shape_id_t initial_shape_id = rb_shape_root(heap_id);
388 shape_id_t new_shape_id = rb_shape_traverse_from_new_root(initial_shape_id, orig_shape_id);
390 if (new_shape_id == INVALID_SHAPE_ID) {
394 return (uint32_t)new_shape_id;
397void rb_vm_update_references(
void *ptr);
399#define rb_setjmp(env) RUBY_SETJMP(env)
400#define rb_jmp_buf rb_jmpbuf_t
401#undef rb_data_object_wrap
403#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
404#define MAP_ANONYMOUS MAP_ANON
407#define unless_objspace(objspace) \
409 rb_vm_t *unless_objspace_vm = GET_VM(); \
410 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
413#define RMOVED(obj) ((struct RMoved *)(obj))
415#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
416 if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
417 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
421#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
423#if RUBY_MARK_FREE_DEBUG
424int ruby_gc_debug_indent = 0;
427#ifndef RGENGC_OBJ_INFO
428# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
431#ifndef CALC_EXACT_MALLOC_SIZE
432# define CALC_EXACT_MALLOC_SIZE 0
437static size_t malloc_offset = 0;
438#if defined(HAVE_MALLOC_USABLE_SIZE)
440gc_compute_malloc_offset(
void)
451 for (offset = 0; offset <= 16; offset += 8) {
452 size_t allocated = (64 - offset);
453 void *test_ptr = malloc(allocated);
454 size_t wasted = malloc_usable_size(test_ptr) - allocated;
465gc_compute_malloc_offset(
void)
473rb_malloc_grow_capa(
size_t current,
size_t type_size)
475 size_t current_capacity = current;
476 if (current_capacity < 4) {
477 current_capacity = 4;
479 current_capacity *= type_size;
482 size_t new_capacity = (current_capacity * 2);
485 if (rb_popcount64(new_capacity) != 1) {
486 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
489 new_capacity -= malloc_offset;
490 new_capacity /= type_size;
491 if (current > new_capacity) {
492 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
498static inline struct rbimpl_size_mul_overflow_tag
499size_mul_add_overflow(size_t x, size_t y, size_t z)
501 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
502 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_add_overflow(t.right, z);
503 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
506static inline struct rbimpl_size_mul_overflow_tag
507size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
509 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
510 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
511 struct rbimpl_size_mul_overflow_tag v = rbimpl_size_add_overflow(t.right, u.right);
512 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
515PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
518size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
520 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
521 if (LIKELY(!t.left)) {
524 else if (rb_during_gc()) {
530 "integer overflow: %"PRIuSIZE
533 x, y, (
size_t)SIZE_MAX);
538rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
540 return size_mul_or_raise(x, y, exc);
544size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
546 struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
547 if (LIKELY(!t.left)) {
550 else if (rb_during_gc()) {
556 "integer overflow: %"PRIuSIZE
560 x, y, z, (
size_t)SIZE_MAX);
565rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
567 return size_mul_add_or_raise(x, y, z, exc);
571size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
573 struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
574 if (LIKELY(!t.left)) {
577 else if (rb_during_gc()) {
583 "integer overflow: %"PRIdSIZE
588 x, y, z, w, (
size_t)SIZE_MAX);
592#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
594volatile VALUE rb_gc_guarded_val;
596rb_gc_guarded_ptr_val(
volatile VALUE *ptr,
VALUE val)
598 rb_gc_guarded_val = val;
604static const char *obj_type_name(
VALUE obj);
605#include "gc/default/default.c"
607#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
608# error "Modular GC requires dlopen"
612typedef struct gc_function_map {
614 void *(*objspace_alloc)(void);
615 void (*objspace_init)(
void *objspace_ptr);
616 void *(*ractor_cache_alloc)(
void *objspace_ptr,
void *ractor);
617 void (*set_params)(
void *objspace_ptr);
619 size_t *(*heap_sizes)(
void *objspace_ptr);
621 void (*shutdown_free_objects)(
void *objspace_ptr);
622 void (*objspace_free)(
void *objspace_ptr);
623 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
625 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
626 bool (*during_gc_p)(
void *objspace_ptr);
627 void (*prepare_heap)(
void *objspace_ptr);
628 void (*gc_enable)(
void *objspace_ptr);
629 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
630 bool (*gc_enabled_p)(
void *objspace_ptr);
631 VALUE (*config_get)(
void *objpace_ptr);
632 void (*config_set)(
void *objspace_ptr,
VALUE hash);
633 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
634 VALUE (*stress_get)(
void *objspace_ptr);
636 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);
637 size_t (*obj_slot_size)(
VALUE obj);
638 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
639 bool (*size_allocatable_p)(
size_t size);
641 void *(*malloc)(
void *objspace_ptr,
size_t size);
642 void *(*calloc)(
void *objspace_ptr,
size_t size);
643 void *(*realloc)(
void *objspace_ptr,
void *ptr,
size_t new_size,
size_t old_size);
644 void (*free)(
void *objspace_ptr,
void *ptr,
size_t old_size);
645 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
647 void (*mark)(
void *objspace_ptr,
VALUE obj);
648 void (*mark_and_move)(
void *objspace_ptr,
VALUE *ptr);
649 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
650 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
651 void (*mark_weak)(
void *objspace_ptr,
VALUE *ptr);
652 void (*remove_weak)(
void *objspace_ptr,
VALUE parent_obj,
VALUE *ptr);
654 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
655 VALUE (*location)(
void *objspace_ptr,
VALUE value);
657 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
658 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
659 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
661 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
662 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
664 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
666 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
667 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
668 void (*shutdown_call_finalizer)(
void *objspace_ptr);
670 VALUE (*object_id)(
void *objspace_ptr,
VALUE obj);
671 VALUE (*object_id_to_ref)(
void *objspace_ptr,
VALUE object_id);
673 void (*before_fork)(
void *objspace_ptr);
674 void (*after_fork)(
void *objspace_ptr, rb_pid_t pid);
676 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
677 bool (*get_measure_total_time)(
void *objspace_ptr);
678 unsigned long long (*get_total_time)(
void *objspace_ptr);
679 size_t (*gc_count)(
void *objspace_ptr);
680 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
681 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
682 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
683 const char *(*active_gc_name)(void);
686 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *ptr);
687 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
688 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
689 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
691 bool modular_gc_loaded_p;
692} rb_gc_function_map_t;
694static rb_gc_function_map_t rb_gc_functions;
696# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
697# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
700ruby_modular_gc_init(
void)
705 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
707 rb_gc_function_map_t gc_functions = { 0 };
709 char *gc_so_path = NULL;
714 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
715 char c = gc_so_file[i];
716 if (isalnum(c))
continue;
722 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
727 size_t gc_so_path_size = strlen(MODULAR_GC_DIR
"librubygc." DLEXT) + strlen(gc_so_file) + 1;
730 size_t prefix_len = 0;
731 if (dladdr((
void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
732 const char *base = strrchr(dli.dli_fname,
'/');
735# define end_with_p(lit) \
736 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
737 memcmp(base - tail, lit, tail) == 0)
739 prefix_len = base - dli.dli_fname;
740 if (end_with_p(
"/bin") || end_with_p(
"/lib")) {
743 prefix_len += MODULAR_GC_DIR[0] !=
'/';
744 gc_so_path_size += prefix_len;
748 gc_so_path = alloca(gc_so_path_size);
750 size_t gc_so_path_idx = 0;
751#define GC_SO_PATH_APPEND(str) do { \
752 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
755 if (prefix_len > 0) {
756 memcpy(gc_so_path, dli.dli_fname, prefix_len);
757 gc_so_path_idx = prefix_len;
760 GC_SO_PATH_APPEND(MODULAR_GC_DIR
"librubygc.");
761 GC_SO_PATH_APPEND(gc_so_file);
762 GC_SO_PATH_APPEND(DLEXT);
763 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
764#undef GC_SO_PATH_APPEND
767 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
769 fprintf(stderr,
"ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
773 gc_functions.modular_gc_loaded_p =
true;
776# define load_modular_gc_func(name) do { \
778 const char *func_name = "rb_gc_impl_" #name; \
779 gc_functions.name = dlsym(handle, func_name); \
780 if (!gc_functions.name) { \
781 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
786 gc_functions.name = rb_gc_impl_##name; \
791 load_modular_gc_func(objspace_alloc);
792 load_modular_gc_func(objspace_init);
793 load_modular_gc_func(ractor_cache_alloc);
794 load_modular_gc_func(set_params);
795 load_modular_gc_func(init);
796 load_modular_gc_func(heap_sizes);
798 load_modular_gc_func(shutdown_free_objects);
799 load_modular_gc_func(objspace_free);
800 load_modular_gc_func(ractor_cache_free);
802 load_modular_gc_func(start);
803 load_modular_gc_func(during_gc_p);
804 load_modular_gc_func(prepare_heap);
805 load_modular_gc_func(gc_enable);
806 load_modular_gc_func(gc_disable);
807 load_modular_gc_func(gc_enabled_p);
808 load_modular_gc_func(config_set);
809 load_modular_gc_func(config_get);
810 load_modular_gc_func(stress_set);
811 load_modular_gc_func(stress_get);
813 load_modular_gc_func(new_obj);
814 load_modular_gc_func(obj_slot_size);
815 load_modular_gc_func(heap_id_for_size);
816 load_modular_gc_func(size_allocatable_p);
818 load_modular_gc_func(malloc);
819 load_modular_gc_func(calloc);
820 load_modular_gc_func(realloc);
821 load_modular_gc_func(free);
822 load_modular_gc_func(adjust_memory_usage);
824 load_modular_gc_func(mark);
825 load_modular_gc_func(mark_and_move);
826 load_modular_gc_func(mark_and_pin);
827 load_modular_gc_func(mark_maybe);
828 load_modular_gc_func(mark_weak);
829 load_modular_gc_func(remove_weak);
831 load_modular_gc_func(object_moved_p);
832 load_modular_gc_func(location);
834 load_modular_gc_func(writebarrier);
835 load_modular_gc_func(writebarrier_unprotect);
836 load_modular_gc_func(writebarrier_remember);
838 load_modular_gc_func(each_objects);
839 load_modular_gc_func(each_object);
841 load_modular_gc_func(make_zombie);
842 load_modular_gc_func(define_finalizer);
843 load_modular_gc_func(undefine_finalizer);
844 load_modular_gc_func(copy_finalizer);
845 load_modular_gc_func(shutdown_call_finalizer);
847 load_modular_gc_func(before_fork);
848 load_modular_gc_func(after_fork);
850 load_modular_gc_func(set_measure_total_time);
851 load_modular_gc_func(get_measure_total_time);
852 load_modular_gc_func(get_total_time);
853 load_modular_gc_func(gc_count);
854 load_modular_gc_func(latest_gc_info);
855 load_modular_gc_func(stat);
856 load_modular_gc_func(stat_heap);
857 load_modular_gc_func(active_gc_name);
859 load_modular_gc_func(object_metadata);
860 load_modular_gc_func(pointer_to_heap_p);
861 load_modular_gc_func(garbage_object_p);
862 load_modular_gc_func(set_event_hook);
863 load_modular_gc_func(copy_attributes);
865# undef load_modular_gc_func
867 rb_gc_functions = gc_functions;
871# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
872# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
873# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
874# define rb_gc_impl_set_params rb_gc_functions.set_params
875# define rb_gc_impl_init rb_gc_functions.init
876# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
878# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
879# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
880# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
882# define rb_gc_impl_start rb_gc_functions.start
883# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
884# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
885# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
886# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
887# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
888# define rb_gc_impl_config_get rb_gc_functions.config_get
889# define rb_gc_impl_config_set rb_gc_functions.config_set
890# define rb_gc_impl_stress_set rb_gc_functions.stress_set
891# define rb_gc_impl_stress_get rb_gc_functions.stress_get
893# define rb_gc_impl_new_obj rb_gc_functions.new_obj
894# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
895# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
896# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
898# define rb_gc_impl_malloc rb_gc_functions.malloc
899# define rb_gc_impl_calloc rb_gc_functions.calloc
900# define rb_gc_impl_realloc rb_gc_functions.realloc
901# define rb_gc_impl_free rb_gc_functions.free
902# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
904# define rb_gc_impl_mark rb_gc_functions.mark
905# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
906# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
907# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
908# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
909# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
911# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
912# define rb_gc_impl_location rb_gc_functions.location
914# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
915# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
916# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
918# define rb_gc_impl_each_objects rb_gc_functions.each_objects
919# define rb_gc_impl_each_object rb_gc_functions.each_object
921# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
922# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
923# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
924# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
925# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
927# define rb_gc_impl_before_fork rb_gc_functions.before_fork
928# define rb_gc_impl_after_fork rb_gc_functions.after_fork
930# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
931# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
932# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
933# define rb_gc_impl_gc_count rb_gc_functions.gc_count
934# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
935# define rb_gc_impl_stat rb_gc_functions.stat
936# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
937# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
939# define rb_gc_impl_object_metadata rb_gc_functions.object_metadata
940# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
941# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
942# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
943# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
946#ifdef RUBY_ASAN_ENABLED
948asan_death_callback(
void)
951 rb_bug_without_die(
"ASAN error");
959rb_objspace_alloc(
void)
962 ruby_modular_gc_init();
965 void *
objspace = rb_gc_impl_objspace_alloc();
966 ruby_current_vm_ptr->gc.objspace =
objspace;
968 rb_gc_impl_stress_set(
objspace, initial_stress);
970#ifdef RUBY_ASAN_ENABLED
971 __sanitizer_set_death_callback(asan_death_callback);
984rb_gc_obj_slot_size(
VALUE obj)
986 return rb_gc_impl_obj_slot_size(obj);
995 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc) {
996 RUBY_ASSERT(cfp->pc >= ISEQ_BODY(cfp->iseq)->iseq_encoded);
997 RUBY_ASSERT(cfp->pc <= ISEQ_BODY(cfp->iseq)->iseq_encoded + ISEQ_BODY(cfp->iseq)->iseq_size);
1005 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
1011 RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
1013 memset((
char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
1020 bool gc_disabled =
RTEST(rb_gc_disable_no_rest());
1024 if (!gc_disabled) rb_gc_enable();
1026 RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
1033rb_wb_unprotected_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
1036 return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
1043 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
1046#define UNEXPECTED_NODE(func) \
1047 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1048 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1051rb_data_object_check(
VALUE klass)
1053 if (klass != rb_cObject && (
rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
1055 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1063 if (klass) rb_data_object_check(klass);
1070 VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
1078 RBIMPL_NONNULL_ARG(
type);
1079 if (klass) rb_data_object_check(klass);
1081 return newobj_of(GET_RACTOR(), klass,
T_DATA, ((
VALUE)
type) | IS_TYPED_DATA | typed_flag, (
VALUE)datap, 0, wb_protected, size);
1087 if (UNLIKELY(
type->flags & RUBY_TYPED_EMBEDDABLE)) {
1088 rb_raise(
rb_eTypeError,
"Cannot wrap an embeddable TypedData");
1091 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
1097 if (
type->flags & RUBY_TYPED_EMBEDDABLE) {
1098 if (!(
type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1099 rb_raise(
rb_eTypeError,
"Embeddable TypedData must be freed immediately");
1102 size_t embed_size = offsetof(
struct RTypedData, data) + size;
1103 if (rb_gc_size_allocatable_p(embed_size)) {
1104 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1105 memset((
char *)obj + offsetof(
struct RTypedData, data), 0, size);
1116rb_objspace_data_type_memsize(
VALUE obj)
1121 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1123 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1124#ifdef HAVE_MALLOC_USABLE_SIZE
1125 size += malloc_usable_size((
void *)ptr);
1129 if (ptr &&
type->function.dsize) {
1130 size +=
type->function.dsize(ptr);
1138rb_objspace_data_type_name(
VALUE obj)
1148static enum rb_id_table_iterator_result
1149cvar_table_free_i(
VALUE value,
void *ctx)
1151 xfree((
void *)value);
1152 return ID_TABLE_CONTINUE;
1156io_fptr_finalize(
void *fptr)
1165 rb_gc_impl_make_zombie(
objspace, obj, io_fptr_finalize, fptr);
1173 int free_immediately =
false;
1174 void (*dfree)(
void *);
1181 dfree =
RDATA(obj)->dfree;
1186 if (!
RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1188 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1191 else if (free_immediately) {
1197 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1200 rb_gc_impl_make_zombie(
objspace, obj, dfree, data);
1201 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1206 RB_DEBUG_COUNTER_INC(obj_data_empty);
1215 bool obj_too_complex;
1225 rb_id_table_free(RCLASSEXT_M_TBL(ext));
1226 rb_cc_tbl_free(RCLASSEXT_CC_TBL(ext), args->klass);
1227 if (args->obj_too_complex) {
1228 st_free_table((
st_table *)RCLASSEXT_FIELDS(ext));
1231 xfree(RCLASSEXT_FIELDS(ext));
1233 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
1234 rb_free_const_table(tbl);
1236 if ((tbl = RCLASSEXT_CVC_TBL(ext)) != NULL) {
1237 rb_id_table_foreach_values(tbl, cvar_table_free_i, NULL);
1238 rb_id_table_free(tbl);
1240 rb_class_classext_free_subclasses(ext, args->klass);
1241 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
1243 xfree(RCLASSEXT_SUPERCLASSES(ext));
1255 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
1257 rb_id_table_free(RCLASSEXT_M_TBL(ext));
1259 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
1260 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
1262 rb_cc_tbl_free(RCLASSEXT_CC_TBL(ext), args->klass);
1264 rb_class_classext_free_subclasses(ext, args->klass);
1276 RB_DEBUG_COUNTER_INC(obj_free);
1283 rb_bug(
"obj_free() called for broken object");
1291 if (rb_shape_obj_too_complex_p(obj)) {
1292 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1293 st_free_table(ROBJECT_FIELDS_HASH(obj));
1295 else if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1296 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1300 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1306 args.obj_too_complex = rb_shape_obj_too_complex_p(obj) ? true :
false;
1308 rb_class_classext_foreach(obj, classext_free, (
void *)&args);
1309 if (
RCLASS(obj)->ns_classext_tbl) {
1310 st_free_table(
RCLASS(obj)->ns_classext_tbl);
1322#if USE_DEBUG_COUNTER
1325 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1328 RB_DEBUG_COUNTER_INC(obj_hash_1);
1331 RB_DEBUG_COUNTER_INC(obj_hash_2);
1334 RB_DEBUG_COUNTER_INC(obj_hash_3);
1337 RB_DEBUG_COUNTER_INC(obj_hash_4);
1343 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1347 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1350 if (RHASH_AR_TABLE_P(obj)) {
1351 if (RHASH_AR_TABLE(obj) == NULL) {
1352 RB_DEBUG_COUNTER_INC(obj_hash_null);
1355 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1359 RB_DEBUG_COUNTER_INC(obj_hash_st);
1368 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1372 if (!rb_data_free(
objspace, obj))
return false;
1377#if USE_DEBUG_COUNTER
1378 if (rm->
regs.num_regs >= 8) {
1379 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1381 else if (rm->
regs.num_regs >= 4) {
1382 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1384 else if (rm->
regs.num_regs >= 1) {
1385 RB_DEBUG_COUNTER_INC(obj_match_under4);
1388 onig_region_free(&rm->
regs, 0);
1391 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1395 if (
RFILE(obj)->fptr) {
1397 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1402 RB_DEBUG_COUNTER_INC(obj_rational);
1405 RB_DEBUG_COUNTER_INC(obj_complex);
1412 rb_class_classext_foreach(obj, classext_iclass_free, (
void *)&args);
1413 if (
RCLASS(obj)->ns_classext_tbl) {
1414 st_free_table(
RCLASS(obj)->ns_classext_tbl);
1417 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1421 RB_DEBUG_COUNTER_INC(obj_float);
1425 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1426 xfree(BIGNUM_DIGITS(obj));
1427 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1430 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1435 UNEXPECTED_NODE(obj_free);
1439 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1440 RSTRUCT(obj)->as.heap.ptr == NULL) {
1441 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1444 xfree((
void *)RSTRUCT(obj)->as.heap.ptr);
1445 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1450 RB_DEBUG_COUNTER_INC(obj_symbol);
1454 rb_imemo_free((
VALUE)obj);
1458 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1463 rb_gc_impl_make_zombie(
objspace, obj, 0, 0);
1474 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1478internal_object_p(
VALUE obj)
1480 void *ptr = asan_unpoison_object_temporary(obj);
1482 if (
RBASIC(obj)->flags) {
1485 UNEXPECTED_NODE(internal_object_p);
1494 if (obj == rb_mRubyVMFrozenCore)
1498 if (RCLASS_SINGLETON_P(obj)) {
1499 return rb_singleton_class_internal_p(obj);
1503 if (!
RBASIC(obj)->klass)
break;
1507 if (ptr || !
RBASIC(obj)->flags) {
1508 rb_asan_poison_object(obj);
1514rb_objspace_internal_object_p(
VALUE obj)
1516 return internal_object_p(obj);
1525os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1530 for (; v != (
VALUE)vend; v += stride) {
1531 if (!internal_object_p(v)) {
1551 rb_objspace_each_objects(os_obj_of_i, &oes);
1610 return os_obj_of(of);
1624 return rb_undefine_finalizer(obj);
1630 rb_check_frozen(obj);
1632 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1638should_be_callable(
VALUE block)
1641 rb_raise(rb_eArgError,
"wrong type argument %"PRIsVALUE
" (should be callable)",
1647should_be_finalizable(
VALUE obj)
1650 rb_raise(rb_eArgError,
"cannot define finalizer for %s",
1653 rb_check_frozen(obj);
1659 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1734 if (rb_callable_receiver(block) == obj) {
1735 rb_warn(
"finalizer references object to be finalized");
1738 return rb_define_finalizer(obj, block);
1744 should_be_finalizable(obj);
1745 should_be_callable(block);
1747 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1755rb_objspace_call_finalizer(
void)
1757 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1761rb_objspace_free_objects(
void *
objspace)
1763 rb_gc_impl_shutdown_free_objects(
objspace);
1767rb_objspace_garbage_object_p(
VALUE obj)
1769 return rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1773rb_gc_pointer_to_heap_p(
VALUE obj)
1775 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj);
1778#define OBJ_ID_INCREMENT (RUBY_IMMEDIATE_MASK + 1)
1779#define LAST_OBJECT_ID() (object_id_counter * OBJ_ID_INCREMENT)
1780static VALUE id2ref_value = 0;
1782static bool id2ref_tbl_built =
false;
1784#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1785static size_t object_id_counter = 1;
1787static unsigned long long object_id_counter = 1;
1791generate_next_object_id(
void)
1793#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1797 unsigned int lock_lev = rb_gc_vm_lock();
1798 VALUE id =
ULL2NUM(++object_id_counter * OBJ_ID_INCREMENT);
1799 rb_gc_vm_unlock(lock_lev);
1805rb_gc_obj_id_moved(
VALUE obj)
1807 if (UNLIKELY(id2ref_tbl)) {
1808 st_insert(id2ref_tbl, (st_data_t)rb_obj_id(obj), (st_data_t)obj);
1813object_id_cmp(st_data_t x, st_data_t y)
1816 return !rb_big_eql(x, y);
1824object_id_hash(st_data_t n)
1829static const struct st_hash_type object_id_hash_type = {
1834static void gc_mark_tbl_no_pin(
st_table *table);
1837id2ref_tbl_mark(
void *data)
1849id2ref_tbl_memsize(
const void *data)
1851 return rb_st_memsize(data);
1855id2ref_tbl_free(
void *data)
1859 st_free_table(table);
1865 .dmark = id2ref_tbl_mark,
1866 .dfree = id2ref_tbl_free,
1867 .dsize = id2ref_tbl_memsize,
1871 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
1874#define RUBY_ATOMIC_VALUE_LOAD(x) (VALUE)(RUBY_ATOMIC_PTR_LOAD(x))
1877class_object_id(
VALUE klass)
1879 VALUE id = RUBY_ATOMIC_VALUE_LOAD(
RCLASS(klass)->object_id);
1881 unsigned int lock_lev = rb_gc_vm_lock();
1882 id = generate_next_object_id();
1887 else if (RB_UNLIKELY(id2ref_tbl)) {
1888 st_insert(id2ref_tbl,
id, klass);
1890 rb_gc_vm_unlock(lock_lev);
1896object_id0(
VALUE obj)
1900 if (rb_shape_has_object_id(RBASIC_SHAPE_ID(obj))) {
1901 shape_id_t object_id_shape_id = rb_shape_transition_object_id(obj);
1902 id = rb_obj_field_get(obj, object_id_shape_id);
1909 shape_id_t object_id_shape_id = rb_shape_transition_object_id(obj);
1911 id = generate_next_object_id();
1912 rb_obj_field_set(obj, object_id_shape_id,
id);
1913 if (RB_UNLIKELY(id2ref_tbl)) {
1914 st_insert(id2ref_tbl, (st_data_t)
id, (st_data_t)obj);
1928 return class_object_id(obj);
1930 rb_bug(
"T_IMEMO can't have an object_id");
1937 unsigned int lock_lev = rb_gc_vm_lock();
1938 VALUE id = object_id0(obj);
1939 rb_gc_vm_unlock(lock_lev);
1943 return object_id0(obj);
1947build_id2ref_i(
VALUE obj,
void *data)
1954 if (
RCLASS(obj)->object_id) {
1955 st_insert(id2ref_tbl,
RCLASS(obj)->object_id, obj);
1962 if (rb_shape_obj_has_id(obj)) {
1963 st_insert(id2ref_tbl, rb_obj_id(obj), obj);
1970object_id_to_ref(
void *objspace_ptr,
VALUE object_id)
1974 unsigned int lev = rb_gc_vm_lock();
1982 id2ref_tbl = st_init_table(&object_id_hash_type);
1987 bool gc_disabled =
RTEST(rb_gc_disable_no_rest());
1989 rb_gc_impl_each_object(
objspace, build_id2ref_i, (
void *)id2ref_tbl);
1991 if (!gc_disabled) rb_gc_enable();
1992 id2ref_tbl_built =
true;
1996 bool found = st_lookup(id2ref_tbl, object_id, &obj) && !rb_gc_impl_garbage_object_p(
objspace, obj);
1998 rb_gc_vm_unlock(lev);
2013obj_free_object_id(
VALUE obj)
2020 if (RB_UNLIKELY(id2ref_tbl)) {
2024 if (
RCLASS(obj)->object_id) {
2025 obj_id =
RCLASS(obj)->object_id;
2029 if (rb_shape_obj_has_id(obj)) {
2030 obj_id = object_id(obj);
2036 if (RB_UNLIKELY(obj_id)) {
2039 if (!st_delete(id2ref_tbl, (st_data_t *)&obj_id, NULL)) {
2041 if (id2ref_tbl_built) {
2042 rb_bug(
"Object ID seen, but not in _id2ref table: object_id=%llu object=%s",
NUM2ULL(obj_id), rb_obj_info(obj));
2049rb_gc_obj_free_vm_weak_references(
VALUE obj)
2051 obj_free_object_id(obj);
2061 rb_gc_free_fstring(obj);
2065 rb_gc_free_dsymbol(obj);
2068 switch (imemo_type(obj)) {
2069 case imemo_callinfo:
2104#if SIZEOF_LONG == SIZEOF_VOIDP
2105#define NUM2PTR(x) NUM2ULONG(x)
2106#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2107#define NUM2PTR(x) NUM2ULL(x)
2110 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
2111 VALUE ptr = NUM2PTR(objid);
2121 if (rb_static_id_valid_p(
SYM2ID(ptr))) {
2125 rb_raise(
rb_eRangeError,
"%p is not a symbol id value", (
void *)ptr);
2129 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is not an id value", rb_int2str(objid, 10));
2133 VALUE obj = object_id_to_ref(rb_gc_get_objspace(), objid);
2138 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is the id of an unshareable object on multi-ractor", rb_int2str(objid, 10));
2147 return id2ref(objid);
2154#if SIZEOF_LONG == SIZEOF_VOIDP
2161 return get_heap_object_id(obj);
2165nonspecial_obj_id(
VALUE obj)
2167#if SIZEOF_LONG == SIZEOF_VOIDP
2169#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2172# error not supported
2179 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
2216 return rb_find_object_id(rb_gc_get_objspace(), obj, object_id);
2220rb_obj_id_p(
VALUE obj)
2225static enum rb_id_table_iterator_result
2226cc_table_memsize_i(
VALUE ccs_ptr,
void *data_ptr)
2228 size_t *total_size = data_ptr;
2230 *total_size +=
sizeof(*ccs);
2231 *total_size +=
sizeof(ccs->entries[0]) * ccs->capa;
2232 return ID_TABLE_CONTINUE;
2238 size_t total = rb_id_table_memsize(cc_table);
2239 rb_id_table_foreach_values(cc_table, cc_table_memsize_i, &total);
2246 size_t *size = (
size_t *)arg;
2249 if (RCLASSEXT_M_TBL(ext)) {
2250 s += rb_id_table_memsize(RCLASSEXT_M_TBL(ext));
2252 if (RCLASSEXT_CVC_TBL(ext)) {
2253 s += rb_id_table_memsize(RCLASSEXT_CVC_TBL(ext));
2255 if (RCLASSEXT_CONST_TBL(ext)) {
2256 s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
2258 if (RCLASSEXT_CC_TBL(ext)) {
2259 s += cc_table_memsize(RCLASSEXT_CC_TBL(ext));
2261 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2262 s += (RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1) *
sizeof(
VALUE);
2271classext_fields_hash_memsize(
rb_classext_t *ext,
bool prime,
VALUE namespace,
void *arg)
2273 size_t *size = (
size_t *)arg;
2276 count = rb_st_table_size((
st_table *)RCLASSEXT_FIELDS(ext));
2283classext_superclasses_memsize(
rb_classext_t *ext,
bool prime,
VALUE namespace,
void *arg)
2285 size_t *size = (
size_t *)arg;
2287 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2289 array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
2290 *size += array_size *
sizeof(
VALUE);
2295rb_obj_memsize_of(
VALUE obj)
2304 size += rb_generic_ivar_memsize(obj);
2309 if (rb_shape_obj_too_complex_p(obj)) {
2310 size += rb_st_memsize(ROBJECT_FIELDS_HASH(obj));
2312 else if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
2313 size += ROBJECT_FIELDS_CAPACITY(obj) *
sizeof(
VALUE);
2318 rb_class_classext_foreach(obj, classext_memsize, (
void *)&size);
2320 if (rb_shape_obj_too_complex_p(obj)) {
2321 rb_class_classext_foreach(obj, classext_fields_hash_memsize, (
void *)&size);
2325 size +=
SIZEOF_VALUE << bit_length(RCLASS_FIELDS_COUNT(obj));
2328 rb_class_classext_foreach(obj, classext_superclasses_memsize, (
void *)&size);
2331 if (RICLASS_OWNS_M_TBL_P(obj)) {
2332 if (RCLASS_M_TBL(obj)) {
2333 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
2336 if (RCLASS_WRITABLE_CC_TBL(obj)) {
2337 size += cc_table_memsize(RCLASS_WRITABLE_CC_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);
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)
2490 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2493 hash = rb_hash_new();
2496 rb_hash_stlike_foreach(hash, set_zero, hash);
2498 rb_hash_aset(hash,
ID2SYM(rb_intern(
"TOTAL")),
SIZET2NUM(data.total));
2501 for (
size_t i = 0; i <=
T_MASK; i++) {
2510#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2512#define STACK_START (ec->machine.stack_start)
2513#define STACK_END (ec->machine.stack_end)
2514#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2516#if STACK_GROW_DIRECTION < 0
2517# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2518#elif STACK_GROW_DIRECTION > 0
2519# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2521# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2522 : (size_t)(STACK_END - STACK_START + 1))
2524#if !STACK_GROW_DIRECTION
2525int ruby_stack_grow_direction;
2527ruby_get_stack_grow_direction(
volatile VALUE *addr)
2530 SET_MACHINE_STACK_END(&end);
2532 if (end > addr)
return ruby_stack_grow_direction = 1;
2533 return ruby_stack_grow_direction = -1;
2542 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2543 return STACK_LENGTH;
2546#define PREVENT_STACK_OVERFLOW 1
2547#ifndef PREVENT_STACK_OVERFLOW
2548#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2549# define PREVENT_STACK_OVERFLOW 1
2551# define PREVENT_STACK_OVERFLOW 0
2554#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2560 size_t length = STACK_LENGTH;
2561 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2563 return length > maximum_length;
2566#define stack_check(ec, water_mark) FALSE
2569#define STACKFRAME_FOR_CALL_CFUNC 2048
2574 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2580 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2585#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2586 if (!RB_SPECIAL_CONST_P(obj)) { \
2587 rb_vm_t *vm = GET_VM(); \
2588 void *objspace = vm->gc.objspace; \
2589 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2590 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2591 (func)(objspace, (obj_or_ptr)); \
2593 else if (check_obj ? \
2594 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2595 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2597 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2598 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2599 vm->gc.mark_func_data = NULL; \
2600 mark_func_data->mark_func((obj), mark_func_data->data); \
2601 vm->gc.mark_func_data = mark_func_data; \
2607gc_mark_internal(
VALUE obj)
2609 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2615 gc_mark_internal(obj);
2619rb_gc_mark_and_move(
VALUE *ptr)
2621 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr,
false);
2625gc_mark_and_pin_internal(
VALUE obj)
2627 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2633 gc_mark_and_pin_internal(obj);
2637gc_mark_maybe_internal(
VALUE obj)
2639 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2645 gc_mark_maybe_internal(obj);
2649rb_gc_mark_weak(
VALUE *ptr)
2655 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2656 GC_ASSERT(rb_gc_impl_during_gc_p(
objspace));
2658 rb_gc_impl_mark_weak(
objspace, ptr);
2661 GC_ASSERT(!rb_gc_impl_during_gc_p(
objspace));
2666rb_gc_remove_weak(
VALUE parent_obj,
VALUE *ptr)
2668 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj, ptr);
2671ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2673each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2684each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2686 if (end <= start)
return;
2687 each_location(start, end - start, cb, data);
2691gc_mark_maybe_each_location(
VALUE obj,
void *data)
2693 gc_mark_maybe_internal(obj);
2699 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2703rb_gc_mark_values(
long n,
const VALUE *values)
2705 for (
long i = 0; i < n; i++) {
2706 gc_mark_internal(values[i]);
2711rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
2713 for (
long i = 0; i < n; i++) {
2714 gc_mark_and_pin_internal(values[i]);
2719mark_key(st_data_t key, st_data_t value, st_data_t data)
2721 gc_mark_and_pin_internal((
VALUE)key);
2731 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2735mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2737 gc_mark_internal((
VALUE)key);
2738 gc_mark_internal((
VALUE)value);
2744pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2746 gc_mark_and_pin_internal((
VALUE)key);
2747 gc_mark_and_pin_internal((
VALUE)value);
2753pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2755 gc_mark_and_pin_internal((
VALUE)key);
2756 gc_mark_internal((
VALUE)value);
2762mark_hash(
VALUE hash)
2764 if (rb_hash_compare_by_id_p(hash)) {
2765 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2768 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2771 gc_mark_internal(RHASH(hash)->ifnone);
2779 st_foreach(tbl, pin_key_pin_value, 0);
2782static enum rb_id_table_iterator_result
2785 gc_mark_internal(me);
2787 return ID_TABLE_CONTINUE;
2794 rb_id_table_foreach_values(tbl, mark_method_entry_i,
objspace);
2798static enum rb_id_table_iterator_result
2803 gc_mark_internal(ce->value);
2804 gc_mark_internal(ce->file);
2805 return ID_TABLE_CONTINUE;
2812 rb_id_table_foreach_values(tbl, mark_const_entry_i,
objspace);
2820static enum rb_id_table_iterator_result
2821mark_cc_entry_i(
VALUE ccs_ptr,
void *data)
2825 VM_ASSERT(vm_ccs_p(ccs));
2827 if (METHOD_ENTRY_INVALIDATED(ccs->cme)) {
2828 rb_vm_ccs_free(ccs);
2829 return ID_TABLE_DELETE;
2832 gc_mark_internal((
VALUE)ccs->cme);
2834 for (
int i=0; i<ccs->len; i++) {
2836 VM_ASSERT(vm_cc_check_cme(ccs->entries[i].cc, ccs->cme));
2838 gc_mark_internal((
VALUE)ccs->entries[i].cc);
2840 return ID_TABLE_CONTINUE;
2853 rb_id_table_foreach_values(tbl, mark_cc_entry_i, (
void *)&args);
2856static enum rb_id_table_iterator_result
2864 gc_mark_internal((
VALUE)entry->cref);
2866 return ID_TABLE_CONTINUE;
2873 rb_id_table_foreach_values(tbl, mark_cvc_tbl_i,
objspace);
2876#if STACK_GROW_DIRECTION < 0
2877#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2878#elif STACK_GROW_DIRECTION > 0
2879#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2881#define GET_STACK_BOUNDS(start, end, appendix) \
2882 ((STACK_END < STACK_START) ? \
2883 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2887gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
2889 gc_mark_maybe_internal(obj);
2891#ifdef RUBY_ASAN_ENABLED
2893 void *fake_frame_start;
2894 void *fake_frame_end;
2895 bool is_fake_frame = asan_get_fake_stack_extents(
2896 ec->machine.asan_fake_stack_handle, obj,
2897 ec->machine.stack_start, ec->machine.stack_end,
2898 &fake_frame_start, &fake_frame_end
2900 if (is_fake_frame) {
2901 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2913 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)value));
2915 return rb_gc_impl_location(
objspace, value);
2921 return gc_location_internal(rb_gc_get_objspace(), value);
2924#if defined(__wasm__)
2927static VALUE *rb_stack_range_tmp[2];
2930rb_mark_locations(
void *begin,
void *end)
2932 rb_stack_range_tmp[0] = begin;
2933 rb_stack_range_tmp[1] = end;
2937rb_gc_save_machine_context(
void)
2942# if defined(__EMSCRIPTEN__)
2947 emscripten_scan_stack(rb_mark_locations);
2948 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2950 emscripten_scan_registers(rb_mark_locations);
2951 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2958 VALUE *stack_start, *stack_end;
2960 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2961 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2963 rb_wasm_scan_locals(rb_mark_locations);
2964 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2972rb_gc_save_machine_context(
void)
2976 RB_VM_SAVE_MACHINE_CONTEXT(thread);
2983 rb_gc_mark_machine_context(ec);
2990 VALUE *stack_start, *stack_end;
2992 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2993 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2996#ifdef RUBY_ASAN_ENABLED
3003 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
3004 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
3005 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
3009rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
3011 gc_mark_and_pin_internal((
VALUE)value);
3019 if (!tbl || tbl->num_entries == 0)
return;
3021 st_foreach(tbl, rb_mark_tbl_i, 0);
3027 if (!tbl || tbl->num_entries == 0)
return;
3029 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
3035 gc_mark_tbl_no_pin(tbl);
3041 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
3045rb_gc_mark_roots(
void *
objspace,
const char **categoryp)
3048 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3050#define MARK_CHECKPOINT(category) do { \
3051 if (categoryp) *categoryp = category; \
3054 MARK_CHECKPOINT(
"vm");
3056 if (vm->self) gc_mark_internal(vm->self);
3058 MARK_CHECKPOINT(
"end_proc");
3061 MARK_CHECKPOINT(
"global_tbl");
3062 rb_gc_mark_global_tbl();
3065 void rb_yjit_root_mark(
void);
3067 if (rb_yjit_enabled_p) {
3068 MARK_CHECKPOINT(
"YJIT");
3069 rb_yjit_root_mark();
3073 MARK_CHECKPOINT(
"machine_context");
3074 mark_current_machine_context(ec);
3076 MARK_CHECKPOINT(
"global_symbols");
3077 rb_sym_global_symbols_mark();
3079 MARK_CHECKPOINT(
"finish");
3081#undef MARK_CHECKPOINT
3096 if (RCLASSEXT_SUPER(ext)) {
3097 gc_mark_internal(RCLASSEXT_SUPER(ext));
3099 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3100 if (rb_shape_obj_too_complex_p(obj)) {
3101 gc_mark_tbl_no_pin((
st_table *)RCLASSEXT_FIELDS(ext));
3104 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
3105 mark_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
3107 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3108 mark_cc_tbl(
objspace, RCLASSEXT_CC_TBL(ext), obj);
3109 mark_cvc_tbl(
objspace, RCLASSEXT_CVC_TBL(ext));
3110 gc_mark_internal(RCLASSEXT_CLASSPATH(ext));
3120 if (RCLASSEXT_SUPER(ext)) {
3121 gc_mark_internal(RCLASSEXT_SUPER(ext));
3123 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
3124 mark_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3126 if (RCLASSEXT_INCLUDER(ext)) {
3127 gc_mark_internal(RCLASSEXT_INCLUDER(ext));
3129 mark_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3130 mark_cc_tbl(
objspace, RCLASSEXT_CC_TBL(ext), iclass);
3133#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA_TYPE(d)->function.dmark
3141 rb_mark_generic_ivar(obj);
3156 rb_bug(
"rb_gc_mark() called for broken object");
3160 UNEXPECTED_NODE(rb_gc_mark);
3164 rb_imemo_mark_and_move(obj,
false);
3171 gc_mark_internal(
RBASIC(obj)->klass);
3176 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
3181 foreach_args.obj = obj;
3182 rb_class_classext_foreach(obj, gc_mark_classext_module, (
void *)&foreach_args);
3184 if (!rb_shape_obj_too_complex_p(obj)) {
3185 for (attr_index_t i = 0; i < RCLASS_FIELDS_COUNT(obj); i++) {
3186 gc_mark_internal(RCLASS_PRIME_FIELDS(obj)[i]);
3193 foreach_args.obj = obj;
3194 rb_class_classext_foreach(obj, gc_mark_classext_iclass, (
void *)&foreach_args);
3198 if (ARY_SHARED_P(obj)) {
3199 VALUE root = ARY_SHARED_ROOT(obj);
3200 gc_mark_internal(root);
3205 for (
long i = 0; i <
len; i++) {
3206 gc_mark_internal(ptr[i]);
3216 if (STR_SHARED_P(obj)) {
3217 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
3222 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
3225 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
3235 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3237 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3238 gc_mark_internal(*(
VALUE *)((
char *)ptr + offset));
3245 if (mark_func) (*mark_func)(ptr);
3253 if (rb_shape_obj_too_complex_p(obj)) {
3254 gc_mark_tbl_no_pin(ROBJECT_FIELDS_HASH(obj));
3259 uint32_t
len = ROBJECT_FIELDS_COUNT(obj);
3260 for (uint32_t i = 0; i <
len; i++) {
3261 gc_mark_internal(ptr[i]);
3265 attr_index_t fields_count = ROBJECT_FIELDS_COUNT(obj);
3270 if (RCLASS_MAX_IV_COUNT(klass) < fields_count) {
3271 RCLASS_SET_MAX_IV_COUNT(klass, fields_count);
3279 if (
RFILE(obj)->fptr) {
3280 gc_mark_internal(
RFILE(obj)->fptr->self);
3281 gc_mark_internal(
RFILE(obj)->fptr->pathv);
3282 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
3283 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
3284 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
3285 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
3286 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
3287 gc_mark_internal(
RFILE(obj)->fptr->timeout);
3288 gc_mark_internal(
RFILE(obj)->fptr->wakeup_mutex);
3293 gc_mark_internal(
RREGEXP(obj)->src);
3297 gc_mark_internal(
RMATCH(obj)->regexp);
3299 gc_mark_internal(
RMATCH(obj)->str);
3304 gc_mark_internal(RRATIONAL(obj)->num);
3305 gc_mark_internal(RRATIONAL(obj)->den);
3309 gc_mark_internal(RCOMPLEX(obj)->real);
3310 gc_mark_internal(RCOMPLEX(obj)->imag);
3314 const long len = RSTRUCT_LEN(obj);
3315 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
3317 for (
long i = 0; i <
len; i++) {
3318 gc_mark_internal(ptr[i]);
3328 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
3330 rb_gc_impl_pointer_to_heap_p(
objspace, (
void *)obj) ?
"corrupted object" :
"non object");
3335rb_gc_obj_optimal_size(
VALUE obj)
3339 return rb_ary_size_as_embedded(obj);
3342 if (rb_shape_obj_too_complex_p(obj)) {
3343 return sizeof(
struct RObject);
3350 return rb_str_size_as_embedded(obj);
3353 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(
st_table) : sizeof(ar_table));
3361rb_gc_writebarrier(VALUE a, VALUE b)
3363 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
3369 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
3376rb_gc_writebarrier_remember(
VALUE obj)
3378 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
3384 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
3388rb_gc_modular_gc_loaded_p(
void)
3391 return rb_gc_functions.modular_gc_loaded_p;
3398rb_gc_active_gc_name(
void)
3400 const char *gc_name = rb_gc_impl_active_gc_name();
3402 const size_t len = strlen(gc_name);
3403 if (
len > RB_GC_MAX_NAME_LEN) {
3404 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
3405 RB_GC_MAX_NAME_LEN,
len, gc_name);
3412rb_gc_object_metadata(
VALUE obj)
3414 return rb_gc_impl_object_metadata(rb_gc_get_objspace(), obj);
3422 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
3426rb_gc_ractor_cache_free(
void *cache)
3428 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3434 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
3437 rb_vm_register_global_object(obj);
3448 tmp->next = vm->global_object_list;
3450 vm->global_object_list = tmp;
3458 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
3460 rb_print_backtrace(stderr);
3470 if (tmp->varptr == addr) {
3471 vm->global_object_list = tmp->next;
3476 if (tmp->next->varptr == addr) {
3479 tmp->next = tmp->next->next;
3490 rb_gc_register_address(var);
3496 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
3540rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
3542 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3548 if (ARY_SHARED_P(v)) {
3549 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
3553 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
3555 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3556 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
3557 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
3558 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
3566 for (
long i = 0; i <
len; i++) {
3571 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3572 if (rb_ary_embeddable_p(v)) {
3573 rb_ary_make_embedded(v);
3584 if (rb_shape_obj_too_complex_p(v)) {
3585 gc_ref_update_table_values_only(ROBJECT_FIELDS_HASH(v));
3589 size_t slot_size = rb_gc_obj_slot_size(v);
3591 if (slot_size >= embed_size && !
RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
3593 memcpy(
ROBJECT(v)->as.ary, ptr,
sizeof(
VALUE) * ROBJECT_FIELDS_COUNT(v));
3599 for (uint32_t i = 0; i < ROBJECT_FIELDS_COUNT(v); i++) {
3605rb_gc_ref_update_table_values_only(
st_table *tbl)
3607 gc_ref_update_table_values_only(tbl);
3614 gc_update_table_refs(ptr);
3620 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)
objspace);
3626 for (
long i = 0; i < n; i++) {
3627 UPDATE_IF_MOVED(
objspace, values[i]);
3632rb_gc_update_values(
long n,
VALUE *values)
3634 gc_update_values(rb_gc_get_objspace(), n, values);
3637static enum rb_id_table_iterator_result
3638check_id_table_move(
VALUE value,
void *data)
3643 return ID_TABLE_REPLACE;
3646 return ID_TABLE_CONTINUE;
3650rb_gc_prepare_heap_process_object(
VALUE obj)
3664rb_gc_prepare_heap(
void)
3666 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3670rb_gc_heap_id_for_size(
size_t size)
3672 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3676rb_gc_size_allocatable_p(
size_t size)
3678 return rb_gc_impl_size_allocatable_p(size);
3681static enum rb_id_table_iterator_result
3682update_id_table(
VALUE *value,
void *data,
int existing)
3690 return ID_TABLE_CONTINUE;
3697 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table,
objspace);
3701static enum rb_id_table_iterator_result
3705 VM_ASSERT(vm_ccs_p(ccs));
3711 for (
int i=0; i<ccs->len; i++) {
3712 if (rb_gc_impl_object_moved_p(
objspace, (
VALUE)ccs->entries[i].cc)) {
3718 return ID_TABLE_CONTINUE;
3725 rb_id_table_foreach_values(tbl, update_cc_tbl_i,
objspace);
3728static enum rb_id_table_iterator_result
3739 entry->class_value = gc_location_internal(
objspace, entry->class_value);
3741 return ID_TABLE_CONTINUE;
3748 rb_id_table_foreach_values(tbl, update_cvc_tbl_i,
objspace);
3751static enum rb_id_table_iterator_result
3756 if (rb_gc_impl_object_moved_p(
objspace, ce->value)) {
3757 ce->value = gc_location_internal(
objspace, ce->value);
3760 if (rb_gc_impl_object_moved_p(
objspace, ce->file)) {
3761 ce->file = gc_location_internal(
objspace, ce->file);
3764 return ID_TABLE_CONTINUE;
3771 rb_id_table_foreach_values(tbl, update_const_tbl_i,
objspace);
3779 if (!anchor)
return;
3780 entry = anchor->head;
3783 UPDATE_IF_MOVED(
objspace, entry->klass);
3784 entry = entry->next;
3791 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
3792 size_t array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
3793 for (
size_t i = 0; i < array_size; i++) {
3794 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPERCLASSES(ext)[i]);
3802 UPDATE_IF_MOVED(
objspace, RCLASSEXT_ORIGIN(ext));
3803 UPDATE_IF_MOVED(
objspace, RCLASSEXT_REFINED_CLASS(ext));
3804 UPDATE_IF_MOVED(
objspace, RCLASSEXT_CLASSPATH(ext));
3806 UPDATE_IF_MOVED(
objspace, RCLASSEXT_INCLUDER(ext));
3814 VALUE klass = args->klass;
3817 if (RCLASSEXT_SUPER(ext)) {
3818 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
3821 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3823 if (args->obj_too_complex) {
3824 gc_ref_update_table_values_only((
st_table *)RCLASSEXT_FIELDS(ext));
3828 for (attr_index_t i = 0; i < RCLASS_FIELDS_COUNT(klass); i++) {
3829 UPDATE_IF_MOVED(
objspace, RCLASSEXT_FIELDS(RCLASS_EXT_PRIME(klass))[i]);
3833 if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
3834 update_const_tbl(
objspace, RCLASSEXT_CONST_TBL(ext));
3836 update_cc_tbl(
objspace, RCLASSEXT_CC_TBL(ext));
3837 update_cvc_tbl(
objspace, RCLASSEXT_CVC_TBL(ext));
3838 update_superclasses(
objspace, ext);
3841 update_classext_values(
objspace, ext,
false);
3850 if (RCLASSEXT_SUPER(ext)) {
3851 UPDATE_IF_MOVED(
objspace, RCLASSEXT_SUPER(ext));
3853 update_m_tbl(
objspace, RCLASSEXT_M_TBL(ext));
3854 update_m_tbl(
objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3855 update_cc_tbl(
objspace, RCLASSEXT_CC_TBL(ext));
3858 update_classext_values(
objspace, ext,
true);
3862#define global_symbols ruby_global_symbols
3865 vm_table_foreach_callback_func callback;
3866 vm_table_update_callback_func update_callback;
3872vm_weak_table_foreach_weak_key(st_data_t key, st_data_t value, st_data_t data,
int error)
3876 int ret = iter_data->callback((
VALUE)key, iter_data->data);
3878 if (!iter_data->weak_only) {
3879 if (ret != ST_CONTINUE)
return ret;
3881 ret = iter_data->callback((
VALUE)value, iter_data->data);
3888vm_weak_table_foreach_update_weak_key(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3892 int ret = iter_data->update_callback((
VALUE *)key, iter_data->data);
3894 if (!iter_data->weak_only) {
3895 if (ret != ST_CONTINUE)
return ret;
3897 ret = iter_data->update_callback((
VALUE *)value, iter_data->data);
3904vm_weak_table_str_sym_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3908 if (!iter_data->weak_only) {
3909 int ret = iter_data->callback((
VALUE)key, iter_data->data);
3910 if (ret != ST_CONTINUE)
return ret;
3917 return iter_data->callback((
VALUE)value, iter_data->data);
3922vm_weak_table_foreach_update_weak_value(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3926 if (!iter_data->weak_only) {
3927 int ret = iter_data->update_callback((
VALUE *)key, iter_data->data);
3928 if (ret != ST_CONTINUE)
return ret;
3931 return iter_data->update_callback((
VALUE *)value, iter_data->data);
3937 if (UNLIKELY(rb_shape_obj_too_complex_p(obj))) {
3938 st_free_table(fields_tbl->as.complex.table);
3945vm_weak_table_gen_fields_foreach_too_complex_i(st_data_t _key, st_data_t value, st_data_t data,
int error)
3949 GC_ASSERT(!iter_data->weak_only);
3953 return iter_data->callback((
VALUE)value, iter_data->data);
3957vm_weak_table_gen_fields_foreach_too_complex_replace_i(st_data_t *_key, st_data_t *value, st_data_t data,
int existing)
3961 GC_ASSERT(!iter_data->weak_only);
3963 return iter_data->update_callback((
VALUE *)value, iter_data->data);
3966struct st_table *rb_generic_fields_tbl_get(
void);
3969vm_weak_table_id2ref_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
3974 int ret = iter_data->callback((
VALUE)key, iter_data->data);
3975 if (ret != ST_CONTINUE)
return ret;
3978 return iter_data->callback((
VALUE)value, iter_data->data);
3982vm_weak_table_id2ref_foreach_update(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
3986 iter_data->update_callback((
VALUE *)value, iter_data->data);
3989 iter_data->update_callback((
VALUE *)key, iter_data->data);
3996vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
4000 int ret = iter_data->callback((
VALUE)key, iter_data->data);
4014 ret = iter_data->update_callback(&new_key, iter_data->data);
4015 if (key != new_key) ret = ST_DELETE;
4016 DURING_GC_COULD_MALLOC_REGION_START();
4018 st_insert(rb_generic_fields_tbl_get(), (st_data_t)new_key, value);
4020 DURING_GC_COULD_MALLOC_REGION_END();
4021 key = (st_data_t)new_key;
4029 if (!iter_data->weak_only) {
4032 if (rb_shape_obj_too_complex_p((
VALUE)key)) {
4033 st_foreach_with_replace(
4034 fields_tbl->as.complex.table,
4035 vm_weak_table_gen_fields_foreach_too_complex_i,
4036 vm_weak_table_gen_fields_foreach_too_complex_replace_i,
4041 for (uint32_t i = 0; i < fields_tbl->as.shape.fields_count; i++) {
4044 int ivar_ret = iter_data->callback(fields_tbl->as.shape.fields[i], iter_data->data);
4049 iter_data->update_callback(&fields_tbl->as.shape.fields[i], iter_data->data);
4052 rb_bug(
"vm_weak_table_gen_fields_foreach: return value %d not supported", ivar_ret);
4062vm_weak_table_frozen_strings_foreach(st_data_t key, st_data_t value, st_data_t data,
int error)
4064 int retval = vm_weak_table_foreach_weak_key(key, value, data, error);
4065 if (retval == ST_DELETE) {
4071void rb_fstring_foreach_with_replace(st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg);
4073rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
4074 vm_table_update_callback_func update_callback,
4077 enum rb_gc_vm_weak_tables table)
4082 .callback = callback,
4083 .update_callback = update_callback,
4085 .weak_only = weak_only,
4089 case RB_GC_VM_CI_TABLE: {
4091 st_foreach_with_replace(
4093 vm_weak_table_foreach_weak_key,
4094 vm_weak_table_foreach_update_weak_key,
4095 (st_data_t)&foreach_data
4100 case RB_GC_VM_OVERLOADED_CME_TABLE: {
4101 if (vm->overloaded_cme_table) {
4102 st_foreach_with_replace(
4103 vm->overloaded_cme_table,
4104 vm_weak_table_foreach_weak_key,
4105 vm_weak_table_foreach_update_weak_key,
4106 (st_data_t)&foreach_data
4111 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
4112 if (global_symbols.str_sym) {
4113 st_foreach_with_replace(
4114 global_symbols.str_sym,
4115 vm_weak_table_str_sym_foreach,
4116 vm_weak_table_foreach_update_weak_value,
4117 (st_data_t)&foreach_data
4122 case RB_GC_VM_ID2REF_TABLE: {
4124 st_foreach_with_replace(
4126 vm_weak_table_id2ref_foreach,
4127 vm_weak_table_id2ref_foreach_update,
4128 (st_data_t)&foreach_data
4133 case RB_GC_VM_GENERIC_FIELDS_TABLE: {
4134 st_table *generic_fields_tbl = rb_generic_fields_tbl_get();
4135 if (generic_fields_tbl) {
4138 vm_weak_table_gen_fields_foreach,
4139 (st_data_t)&foreach_data
4144 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
4145 rb_fstring_foreach_with_replace(
4146 vm_weak_table_frozen_strings_foreach,
4147 vm_weak_table_foreach_update_weak_key,
4148 (st_data_t)&foreach_data
4152 case RB_GC_VM_WEAK_TABLE_COUNT:
4153 rb_bug(
"Unreacheable");
4158rb_gc_update_vm_references(
void *
objspace)
4161 rb_vm_t *vm = rb_ec_vm_ptr(ec);
4163 rb_vm_update_references(vm);
4164 rb_gc_update_global_tbl();
4165 rb_sym_global_symbols_update_references();
4168 void rb_yjit_root_update_references(
void);
4170 if (rb_yjit_enabled_p) {
4171 rb_yjit_root_update_references();
4184 UPDATE_IF_MOVED(
objspace, RCLASS_ATTACHED_OBJECT(obj));
4189 args.obj_too_complex = rb_shape_obj_too_complex_p(obj);
4191 rb_class_classext_foreach(obj, update_classext, (
void *)&args);
4196 rb_class_classext_foreach(obj, update_iclass_classext, (
void *)&args);
4200 rb_imemo_mark_and_move(obj,
true);
4212 gc_ref_update_array(
objspace, obj);
4217 UPDATE_IF_MOVED(
objspace, RHASH(obj)->ifnone);
4222 if (STR_SHARED_P(obj)) {
4228 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
4229 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
4230 rb_str_make_embedded(obj);
4242 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
4244 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
4245 VALUE *ref = (
VALUE *)((
char *)ptr + offset);
4246 *ref = gc_location_internal(
objspace, *ref);
4251 if (compact_func) (*compact_func)(ptr);
4258 gc_ref_update_object(
objspace, obj);
4262 if (
RFILE(obj)->fptr) {
4265 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->tied_io_for_writing);
4266 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
4267 UPDATE_IF_MOVED(
objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
4279 UPDATE_IF_MOVED(
objspace, RSYMBOL(obj)->fstr);
4295 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->num);
4296 UPDATE_IF_MOVED(
objspace, RRATIONAL(obj)->den);
4300 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->real);
4301 UPDATE_IF_MOVED(
objspace, RCOMPLEX(obj)->imag);
4307 long i,
len = RSTRUCT_LEN(obj);
4308 VALUE *ptr = (
VALUE *)RSTRUCT_CONST_PTR(obj);
4310 for (i = 0; i <
len; i++) {
4316 rb_bug(
"unreachable");
4333 unless_objspace(
objspace) {
return; }
4335 rb_gc_impl_start(
objspace,
true,
true,
true,
false);
4341 unless_objspace(
objspace) {
return FALSE; }
4343 return rb_gc_impl_during_gc_p(
objspace);
4349 return rb_gc_impl_gc_count(rb_gc_get_objspace());
4365 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
4368 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(key));
4378 arg = rb_hash_new();
4384 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4389 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4402 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4407 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4422 arg = rb_hash_new();
4425 if (
NIL_P(heap_name)) {
4436 rb_raise(
rb_eTypeError,
"heap_name must be nil or an Integer");
4439 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
4444 rb_raise(rb_eArgError,
"unknown key: %"PRIsVALUE,
rb_sym2str(arg));
4453 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
4454 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
4462 void *
objspace = rb_gc_get_objspace();
4464 rb_gc_impl_config_set(
objspace, hash);
4466 return rb_gc_impl_config_get(
objspace);
4472 return rb_gc_impl_stress_get(rb_gc_get_objspace());
4478 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
4484rb_gc_initial_stress_set(
VALUE flag)
4486 initial_stress = flag;
4490rb_gc_heap_sizes(
void)
4492 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
4498 return rb_objspace_gc_enable(rb_gc_get_objspace());
4502rb_objspace_gc_enable(
void *
objspace)
4504 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4506 return RBOOL(disabled);
4512 return rb_gc_enable();
4518 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4519 rb_gc_impl_gc_disable(
objspace,
false);
4520 return RBOOL(disabled);
4524rb_gc_disable_no_rest(
void)
4526 return gc_disable_no_rest(rb_gc_get_objspace());
4532 return rb_objspace_gc_disable(rb_gc_get_objspace());
4536rb_objspace_gc_disable(
void *
objspace)
4538 bool disabled = !rb_gc_impl_gc_enabled_p(
objspace);
4539 rb_gc_impl_gc_disable(
objspace,
true);
4540 return RBOOL(disabled);
4546 return rb_gc_disable();
4551ruby_gc_set_params(
void)
4553 rb_gc_impl_set_params(rb_gc_get_objspace());
4557rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
4560 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
4564 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4565 struct gc_mark_func_data_struct mfd = {
4570 vm->gc.mark_func_data = &mfd;
4571 rb_gc_mark_children(rb_gc_get_objspace(), obj);
4572 vm->gc.mark_func_data = prev_mfd;
4578 const char *category;
4579 void (*func)(
const char *category,
VALUE,
void *);
4584root_objects_from(
VALUE obj,
void *ptr)
4587 (*data->func)(data->category, obj, data->data);
4591rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
4593 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");
4599 .data = passing_data,
4602 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4603 struct gc_mark_func_data_struct mfd = {
4604 .mark_func = root_objects_from,
4608 vm->gc.mark_func_data = &mfd;
4609 rb_gc_save_machine_context();
4610 rb_gc_mark_roots(vm->gc.objspace, &data.category);
4611 vm->gc.mark_func_data = prev_mfd;
4622#define TYPE_NAME(t) case (t): return #t;
4649 if (obj && rb_objspace_data_type_name(obj)) {
4650 return rb_objspace_data_type_name(obj);
4659obj_type_name(
VALUE obj)
4661 return type_name(
TYPE(obj), obj);
4665rb_method_type_name(rb_method_type_t
type)
4668 case VM_METHOD_TYPE_ISEQ:
return "iseq";
4669 case VM_METHOD_TYPE_ATTRSET:
return "attrest";
4670 case VM_METHOD_TYPE_IVAR:
return "ivar";
4671 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
4672 case VM_METHOD_TYPE_ALIAS:
return "alias";
4673 case VM_METHOD_TYPE_REFINED:
return "refined";
4674 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
4675 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
4676 case VM_METHOD_TYPE_MISSING:
return "missing";
4677 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
4678 case VM_METHOD_TYPE_UNDEF:
return "undef";
4679 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
4681 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
4685rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
4687 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
4688 VALUE path = rb_iseq_path(iseq);
4689 int n = ISEQ_BODY(iseq)->location.first_lineno;
4690 snprintf(buff, buff_size,
" %s@%s:%d",
4691 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4692 RSTRING_PTR(path), n);
4697str_len_no_raise(
VALUE str)
4699 long len = RSTRING_LEN(str);
4700 if (
len < 0)
return 0;
4701 if (
len > INT_MAX)
return INT_MAX;
4705#define BUFF_ARGS buff + pos, buff_size - pos
4706#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4707#define APPEND_S(s) do { \
4708 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4712 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4715#define C(c, s) ((c) != 0 ? (s) : " ")
4718rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
4723 APPEND_F(
"%s", obj_type_name(obj));
4729 APPEND_F(
" %s", rb_id2name(
SYM2ID(obj)));
4735 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
4754 if (internal_object_p(obj)) {
4757 else if (
RBASIC(obj)->klass == 0) {
4758 APPEND_S(
"(temporary internal)");
4762 if (!
NIL_P(class_path)) {
4763 APPEND_F(
"(%s)", RSTRING_PTR(class_path));
4772const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
4775rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
4782 UNEXPECTED_NODE(rb_raw_obj_info);
4785 if (ARY_SHARED_P(obj)) {
4786 APPEND_S(
"shared -> ");
4787 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4789 else if (ARY_EMBED_P(obj)) {
4790 APPEND_F(
"[%s%s] len: %ld (embed)",
4791 C(ARY_EMBED_P(obj),
"E"),
4792 C(ARY_SHARED_P(obj),
"S"),
4796 APPEND_F(
"[%s%s] len: %ld, capa:%ld ptr:%p",
4797 C(ARY_EMBED_P(obj),
"E"),
4798 C(ARY_SHARED_P(obj),
"S"),
4800 ARY_EMBED_P(obj) ? -1L :
RARRAY(obj)->as.heap.aux.
capa,
4805 if (STR_SHARED_P(obj)) {
4806 APPEND_F(
" [shared] len: %ld", RSTRING_LEN(obj));
4809 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
4811 APPEND_F(
" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj),
rb_str_capacity(obj));
4813 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
4817 VALUE fstr = RSYMBOL(obj)->fstr;
4818 ID id = RSYMBOL(obj)->id;
4820 APPEND_F(
":%s id:%d", RSTRING_PTR(fstr), (
unsigned int)
id);
4823 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
4828 APPEND_F(
"-> %p", (
void*)gc_location_internal(rb_gc_get_objspace(), obj));
4832 APPEND_F(
"[%c] %"PRIdSIZE,
4833 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
4841 if (!
NIL_P(class_path)) {
4842 APPEND_F(
"%s", RSTRING_PTR(class_path));
4852 if (!
NIL_P(class_path)) {
4853 APPEND_F(
"src:%s", RSTRING_PTR(class_path));
4859 if (rb_shape_obj_too_complex_p(obj)) {
4860 size_t hash_len = rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
4861 APPEND_F(
"(too_complex) len:%zu", hash_len);
4864 uint32_t
len = ROBJECT_FIELDS_CAPACITY(obj);
4866 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
4867 APPEND_F(
"(embed) len:%d",
len);
4871 APPEND_F(
"len:%d ptr:%p",
len, (
void *)ptr);
4880 (block = vm_proc_block(obj)) != NULL &&
4881 (vm_block_type(block) == block_type_iseq) &&
4882 (iseq = vm_block_iseq(block)) != NULL) {
4883 rb_raw_iseq_info(BUFF_ARGS, iseq);
4885 else if (rb_ractor_p(obj)) {
4888 APPEND_F(
"r:%d", r->pub.id);
4892 const char *
const type_name = rb_objspace_data_type_name(obj);
4894 APPEND_F(
"%s", type_name);
4900 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
4902 switch (imemo_type(obj)) {
4907 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
4908 rb_id2name(me->called_id),
4909 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
4910 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
4911 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
4912 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
4913 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
4914 me->def ? rb_method_type_name(me->def->
type) :
"NULL",
4915 me->def ? me->def->aliased : -1,
4917 (void *)me->defined_class);
4920 switch (me->def->type) {
4921 case VM_METHOD_TYPE_ISEQ:
4922 APPEND_S(
" (iseq:");
4923 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
4935 rb_raw_iseq_info(BUFF_ARGS, iseq);
4938 case imemo_callinfo:
4941 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
4942 rb_id2name(vm_ci_mid(ci)),
4945 vm_ci_kwarg(ci) ?
"available" :
"NULL");
4948 case imemo_callcache:
4954 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
4955 NIL_P(class_path) ? (cc->klass ?
"??" :
"<NULL>") : RSTRING_PTR(class_path),
4956 cme ? rb_id2name(cme->called_id) :
"<NULL>",
4957 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
4959 (void *)(uintptr_t)vm_cc_call(cc));
4978rb_asan_poison_object(
VALUE obj)
4980 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4981 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
4985rb_asan_unpoison_object(
VALUE obj,
bool newobj_p)
4987 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4988 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
4992rb_asan_poisoned_object_p(
VALUE obj)
4994 MAYBE_UNUSED(
struct RVALUE *) ptr = (
void *)obj;
4995 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
4999rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
5001 asan_unpoisoning_object(obj) {
5002 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
5003 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
5004 if (pos >= buff_size) {}
5015#define OBJ_INFO_BUFFERS_NUM 10
5016#define OBJ_INFO_BUFFERS_SIZE 0x100
5018static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
5026 if (RB_UNLIKELY(oldval >= maxval - 1)) {
5037 rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
5038 char *
const buff = obj_info_buffers[index];
5039 return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
5045 return obj_type_name(obj);
5063 rb_vraise(argv->exc, argv->fmt, *argv->ap);
5068gc_raise(
VALUE exc,
const char *fmt, ...)
5076 if (ruby_thread_has_gvl_p()) {
5086 fprintf(stderr,
"%s",
"[FATAL] ");
5087 vfprintf(stderr, fmt, ap);
5094NORETURN(
static void negative_size_allocation_error(
const char *));
5096negative_size_allocation_error(
const char *msg)
5102ruby_memerror_body(
void *dummy)
5108NORETURN(
static void ruby_memerror(
void));
5113 if (ruby_thread_has_gvl_p()) {
5122 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
5140 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
5143 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
5144 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
5145 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
5148 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
5149 rb_ec_raised_clear(ec);
5152 rb_ec_raised_set(ec, RAISED_NOMEMORY);
5153 exc = ruby_vm_special_exception_copy(exc);
5156 EC_JUMP_TAG(ec, TAG_RAISE);
5160rb_memerror_reentered(
void)
5163 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
5167handle_malloc_failure(
void *ptr)
5178static void *ruby_xmalloc_body(
size_t size);
5181ruby_xmalloc(
size_t size)
5183 return handle_malloc_failure(ruby_xmalloc_body(size));
5187ruby_xmalloc_body(
size_t size)
5189 if ((ssize_t)size < 0) {
5190 negative_size_allocation_error(
"too large allocation size");
5193 return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
5197ruby_malloc_size_overflow(
size_t count,
size_t elsize)
5199 rb_raise(rb_eArgError,
5200 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
5205ruby_malloc_add_size_overflow(
size_t x,
size_t y)
5207 rb_raise(rb_eArgError,
5208 "malloc: possible integer overflow (%"PRIuSIZE
"+%"PRIuSIZE
")",
5212static void *ruby_xmalloc2_body(
size_t n,
size_t size);
5215ruby_xmalloc2(
size_t n,
size_t size)
5217 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
5221ruby_xmalloc2_body(
size_t n,
size_t size)
5223 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
5226static void *ruby_xcalloc_body(
size_t n,
size_t size);
5229ruby_xcalloc(
size_t n,
size_t size)
5231 return handle_malloc_failure(ruby_xcalloc_body(n, size));
5235ruby_xcalloc_body(
size_t n,
size_t size)
5237 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
5240static void *ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size);
5242#ifdef ruby_sized_xrealloc
5243#undef ruby_sized_xrealloc
5246ruby_sized_xrealloc(
void *ptr,
size_t new_size,
size_t old_size)
5248 return handle_malloc_failure(ruby_sized_xrealloc_body(ptr, new_size, old_size));
5252ruby_sized_xrealloc_body(
void *ptr,
size_t new_size,
size_t old_size)
5254 if ((ssize_t)new_size < 0) {
5255 negative_size_allocation_error(
"too large allocation size");
5258 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size);
5262ruby_xrealloc(
void *ptr,
size_t new_size)
5264 return ruby_sized_xrealloc(ptr, new_size, 0);
5267static void *ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n);
5269#ifdef ruby_sized_xrealloc2
5270#undef ruby_sized_xrealloc2
5273ruby_sized_xrealloc2(
void *ptr,
size_t n,
size_t size,
size_t old_n)
5275 return handle_malloc_failure(ruby_sized_xrealloc2_body(ptr, n, size, old_n));
5279ruby_sized_xrealloc2_body(
void *ptr,
size_t n,
size_t size,
size_t old_n)
5281 size_t len = xmalloc2_size(n, size);
5282 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr,
len, old_n * size);
5286ruby_xrealloc2(
void *ptr,
size_t n,
size_t size)
5288 return ruby_sized_xrealloc2(ptr, n, size, 0);
5291#ifdef ruby_sized_xfree
5292#undef ruby_sized_xfree
5295ruby_sized_xfree(
void *x,
size_t size)
5301 if (LIKELY(GET_VM())) {
5302 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
5313 ruby_sized_xfree(x, 0);
5317rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
5319 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5320 return ruby_xmalloc(w);
5324rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
5326 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5327 return ruby_xcalloc(w, 1);
5331rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
5333 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5334 return ruby_xrealloc((
void *)p, w);
5338rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
5340 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5341 return ruby_xmalloc(u);
5345rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
5347 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5348 return ruby_xcalloc(u, 1);
5355ruby_mimmalloc(
size_t size)
5358#if CALC_EXACT_MALLOC_SIZE
5362#if CALC_EXACT_MALLOC_SIZE
5378ruby_mimcalloc(
size_t num,
size_t size)
5381#if CALC_EXACT_MALLOC_SIZE
5382 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
5383 if (UNLIKELY(t.left)) {
5387 mem = calloc1(size);
5399 mem = calloc(num, size);
5405ruby_mimfree(
void *ptr)
5407#if CALC_EXACT_MALLOC_SIZE
5415rb_gc_adjust_memory_usage(ssize_t diff)
5417 unless_objspace(
objspace) {
return; }
5419 rb_gc_impl_adjust_memory_usage(
objspace, diff);
5423rb_obj_info(
VALUE obj)
5425 return obj_info(obj);
5429rb_obj_info_dump(
VALUE obj)
5432 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
5436rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
5439 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
5443rb_gc_before_fork(
void)
5445 rb_gc_impl_before_fork(rb_gc_get_objspace());
5449rb_gc_after_fork(rb_pid_t pid)
5451 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
5505 rb_gc_register_address(&id2ref_value);
5507 malloc_offset = gc_compute_malloc_offset();
5520 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
5543ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
5545#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
5548 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.
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_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#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 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 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.
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.
Defines RBIMPL_HAS_BUILTIN.
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.
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 capa
Designed capacity of the buffer.
int rb_io_fptr_finalize(rb_io_t *fptr)
Destroys the given IO.
int len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
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(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_FIELDS(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.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
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.
This is the struct that holds necessary info for a struct.
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
RUBY_DATA_FUNC dcompact
This function is called when the object is relocated.
const char * wrap_struct_name
Name of structs of this kind.
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
struct rb_data_type_struct::@55 function
Function pointers.
VALUE flags
Type-specific behavioural characteristics.
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.
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 enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the 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.