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
33 # ifdef RUBY_ALTERNATIVE_MALLOC_HEADER
35 # elif defined(HAVE_MALLOC_H)
37 # elif defined(HAVE_MALLOC_NP_H)
38 # include <malloc_np.h>
39 # elif defined(HAVE_MALLOC_MALLOC_H)
40 # include <malloc/malloc.h>
44 # define HAVE_MALLOC_USABLE_SIZE
45 # define malloc_usable_size(a) _msize(a)
46 # elif defined HAVE_MALLOC_SIZE
47 # define HAVE_MALLOC_USABLE_SIZE
48 # define malloc_usable_size(a) malloc_size(a)
56 #ifdef HAVE_SYS_TIME_H
57 # include <sys/time.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)
71 #include <sys/types.h>
74 #include <emscripten.h>
79 #include <linux/prctl.h>
80 #include <sys/prctl.h>
87 #include "debug_counter.h"
88 #include "eval_intern.h"
92 #include "internal/class.h"
93 #include "internal/compile.h"
94 #include "internal/complex.h"
95 #include "internal/cont.h"
96 #include "internal/error.h"
97 #include "internal/eval.h"
98 #include "internal/gc.h"
99 #include "internal/hash.h"
100 #include "internal/imemo.h"
101 #include "internal/io.h"
102 #include "internal/numeric.h"
103 #include "internal/object.h"
104 #include "internal/proc.h"
105 #include "internal/rational.h"
106 #include "internal/sanitizers.h"
107 #include "internal/struct.h"
108 #include "internal/symbol.h"
109 #include "internal/thread.h"
110 #include "internal/variable.h"
111 #include "internal/warnings.h"
122 #include "ruby_assert.h"
123 #include "ruby_atomic.h"
127 #include "vm_callinfo.h"
128 #include "ractor_core.h"
138 RB_VM_LOCK_ENTER_LEV(&lev);
143 rb_gc_vm_unlock(
unsigned int lev)
145 RB_VM_LOCK_LEAVE_LEV(&lev);
152 RB_VM_LOCK_ENTER_CR_LEV(GET_RACTOR(), &lev);
157 rb_gc_cr_unlock(
unsigned int lev)
159 RB_VM_LOCK_LEAVE_CR_LEV(GET_RACTOR(), &lev);
163 rb_gc_vm_lock_no_barrier(
void)
165 unsigned int lev = 0;
166 RB_VM_LOCK_ENTER_LEV_NB(&lev);
171 rb_gc_vm_unlock_no_barrier(
unsigned int lev)
173 RB_VM_LOCK_LEAVE_LEV(&lev);
177 rb_gc_vm_barrier(
void)
185 if (LIKELY(!(ruby_vm_event_flags & event)))
return;
188 if (!ec->cfp)
return;
190 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
194 rb_gc_get_objspace(
void)
196 return GET_VM()->gc.objspace;
200 rb_gc_ractor_newobj_cache_foreach(
void (*func)(
void *cache,
void *data),
void *data)
203 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
204 func(r->newobj_cache, data);
209 rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
220 #define RESTORE_FINALIZER() (\
221 ec->cfp = saved.cfp, \
222 ec->cfp->sp = saved.sp, \
223 ec->errinfo = saved.errinfo)
225 saved.errinfo = ec->errinfo;
227 saved.sp = ec->cfp->sp;
232 enum ruby_tag_type state = EC_EXEC_TAG();
233 if (state != TAG_NONE) {
236 VALUE failed_final = saved.final;
239 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
240 rb_ec_error_print(ec, ec->errinfo);
244 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
245 saved.final = callback(i, data);
249 #undef RESTORE_FINALIZER
253 rb_gc_set_pending_interrupt(
void)
256 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
260 rb_gc_unset_pending_interrupt(
void)
263 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
267 rb_gc_multi_ractor_p(
void)
269 return rb_multi_ractor_p();
272 bool rb_obj_is_main_ractor(
VALUE gv);
275 rb_gc_shutdown_call_finalizer_p(
VALUE obj)
280 if (rb_obj_is_thread(obj))
return false;
281 if (rb_obj_is_mutex(obj))
return false;
283 if (rb_obj_is_main_ractor(obj))
return false;
291 if (RSYMBOL(obj)->fstr &&
294 RSYMBOL(obj)->fstr = 0;
307 rb_gc_get_shape(
VALUE obj)
309 return (uint32_t)rb_shape_get_shape_id(obj);
313 rb_gc_set_shape(
VALUE obj, uint32_t shape_id)
315 rb_shape_set_shape_id(obj, (uint32_t)shape_id);
319 rb_gc_rebuild_shape(
VALUE obj,
size_t heap_id)
321 rb_shape_t *orig_shape = rb_shape_get_shape(obj);
323 if (rb_shape_obj_too_complex(obj))
return (uint32_t)OBJ_TOO_COMPLEX_SHAPE_ID;
325 rb_shape_t *initial_shape = rb_shape_get_shape_by_id((shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID));
326 rb_shape_t *new_shape = rb_shape_traverse_from_new_root(initial_shape, orig_shape);
328 if (!new_shape)
return 0;
330 return (uint32_t)rb_shape_id(new_shape);
333 void rb_vm_update_references(
void *
ptr);
335 #define rb_setjmp(env) RUBY_SETJMP(env)
336 #define rb_jmp_buf rb_jmpbuf_t
337 #undef rb_data_object_wrap
339 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
340 #define MAP_ANONYMOUS MAP_ANON
343 #define unless_objspace(objspace) \
345 rb_vm_t *unless_objspace_vm = GET_VM(); \
346 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
349 #define RMOVED(obj) ((struct RMoved *)(obj))
351 #define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
352 if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
353 *(_type *)&(_thing) = (_type)rb_gc_impl_location(_objspace, (VALUE)_thing); \
357 #define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
359 #if RUBY_MARK_FREE_DEBUG
360 int ruby_gc_debug_indent = 0;
363 #ifndef RGENGC_OBJ_INFO
364 # define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
367 #ifndef CALC_EXACT_MALLOC_SIZE
368 # define CALC_EXACT_MALLOC_SIZE 0
373 static size_t malloc_offset = 0;
374 #if defined(HAVE_MALLOC_USABLE_SIZE)
376 gc_compute_malloc_offset(
void)
387 for (offset = 0; offset <= 16; offset += 8) {
388 size_t allocated = (64 - offset);
389 void *test_ptr = malloc(allocated);
390 size_t wasted = malloc_usable_size(test_ptr) - allocated;
401 gc_compute_malloc_offset(
void)
409 rb_malloc_grow_capa(
size_t current,
size_t type_size)
411 size_t current_capacity = current;
412 if (current_capacity < 4) {
413 current_capacity = 4;
415 current_capacity *= type_size;
418 size_t new_capacity = (current_capacity * 2);
421 if (rb_popcount64(new_capacity) != 1) {
422 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
425 new_capacity -= malloc_offset;
426 new_capacity /= type_size;
427 if (current > new_capacity) {
428 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
434 static inline struct rbimpl_size_mul_overflow_tag
435 size_add_overflow(size_t x, size_t y)
441 #elif defined(ckd_add)
442 p = ckd_add(&z, x, y);
444 #elif __has_builtin(__builtin_add_overflow)
445 p = __builtin_add_overflow(x, y, &z);
447 #elif defined(DSIZE_T)
459 return (
struct rbimpl_size_mul_overflow_tag) { p, z, };
462 static inline struct rbimpl_size_mul_overflow_tag
463 size_mul_add_overflow(size_t x, size_t y, size_t z)
465 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
466 struct rbimpl_size_mul_overflow_tag u = size_add_overflow(t.right, z);
467 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
470 static inline struct rbimpl_size_mul_overflow_tag
471 size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
473 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
474 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
475 struct rbimpl_size_mul_overflow_tag v = size_add_overflow(t.right, u.right);
476 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
479 PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
482 size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
484 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
485 if (LIKELY(!t.left)) {
494 "integer overflow: %"PRIuSIZE
497 x, y, (
size_t)SIZE_MAX);
502 rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
504 return size_mul_or_raise(x, y, exc);
508 size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
510 struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
511 if (LIKELY(!t.left)) {
520 "integer overflow: %"PRIuSIZE
524 x, y, z, (
size_t)SIZE_MAX);
529 rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
531 return size_mul_add_or_raise(x, y, z, exc);
535 size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
537 struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
538 if (LIKELY(!t.left)) {
547 "integer overflow: %"PRIdSIZE
552 x, y, z, w, (
size_t)SIZE_MAX);
556 #if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
558 volatile VALUE rb_gc_guarded_val;
562 rb_gc_guarded_val = val;
568 static const char *obj_type_name(
VALUE obj);
569 #define RB_AMALGAMATED_DEFAULT_GC
570 #include "gc/default.c"
571 static int external_gc_loaded = FALSE;
574 #if USE_SHARED_GC && !defined(HAVE_DLOPEN)
575 # error "Shared GC requires dlopen"
579 typedef struct gc_function_map {
581 void *(*objspace_alloc)(void);
582 void (*objspace_init)(
void *objspace_ptr);
583 void (*objspace_free)(
void *objspace_ptr);
584 void *(*ractor_cache_alloc)(
void *objspace_ptr);
585 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
586 void (*set_params)(
void *objspace_ptr);
588 size_t *(*heap_sizes)(
void *objspace_ptr);
590 void (*shutdown_free_objects)(
void *objspace_ptr);
592 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
593 bool (*during_gc_p)(
void *objspace_ptr);
594 void (*prepare_heap)(
void *objspace_ptr);
595 void (*gc_enable)(
void *objspace_ptr);
596 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
597 bool (*gc_enabled_p)(
void *objspace_ptr);
598 VALUE (*config_get)(
void *objpace_ptr);
599 void (*config_set)(
void *objspace_ptr,
VALUE hash);
600 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
601 VALUE (*stress_get)(
void *objspace_ptr);
603 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);
604 size_t (*obj_slot_size)(
VALUE obj);
605 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
606 bool (*size_allocatable_p)(
size_t size);
608 void *(*malloc)(
void *objspace_ptr,
size_t size);
609 void *(*calloc)(
void *objspace_ptr,
size_t size);
610 void *(*realloc)(
void *objspace_ptr,
void *
ptr,
size_t new_size,
size_t old_size);
611 void (*free)(
void *objspace_ptr,
void *
ptr,
size_t old_size);
612 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
614 void (*mark)(
void *objspace_ptr,
VALUE obj);
615 void (*mark_and_move)(
void *objspace_ptr,
VALUE *
ptr);
616 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
617 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
618 void (*mark_weak)(
void *objspace_ptr,
VALUE *
ptr);
619 void (*remove_weak)(
void *objspace_ptr,
VALUE parent_obj,
VALUE *
ptr);
621 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
622 VALUE (*location)(
void *objspace_ptr,
VALUE value);
624 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
625 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
626 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
628 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
629 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
631 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
633 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
634 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
635 void (*shutdown_call_finalizer)(
void *objspace_ptr);
637 VALUE (*object_id)(
void *objspace_ptr,
VALUE obj);
638 VALUE (*object_id_to_ref)(
void *objspace_ptr,
VALUE object_id);
640 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
641 bool (*get_measure_total_time)(
void *objspace_ptr);
642 unsigned long long (*get_total_time)(
void *objspace_ptr);
643 size_t (*gc_count)(
void *objspace_ptr);
644 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
645 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
646 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
648 size_t (*obj_flags)(
void *objspace_ptr,
VALUE obj,
ID* flags,
size_t max);
649 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *
ptr);
650 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
651 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
652 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
654 const char *(*active_gc_name)(void);
655 } rb_gc_function_map_t;
657 static rb_gc_function_map_t rb_gc_functions;
659 # define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
662 ruby_external_gc_init(
void)
667 char *gc_so_file = getenv(RUBY_GC_LIBRARY);
669 char *gc_so_path = NULL;
674 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
675 char c = gc_so_file[i];
676 if (isalnum(c))
continue;
682 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
687 size_t gc_so_path_size = strlen(SHARED_GC_DIR
"librubygc." SOEXT) + strlen(gc_so_file) + 1;
688 gc_so_path = alloca(gc_so_path_size);
690 size_t gc_so_path_idx = 0;
691 #define GC_SO_PATH_APPEND(str) do { \
692 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
694 GC_SO_PATH_APPEND(SHARED_GC_DIR);
695 GC_SO_PATH_APPEND(
"librubygc.");
696 GC_SO_PATH_APPEND(gc_so_file);
697 GC_SO_PATH_APPEND(SOEXT);
698 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
699 #undef GC_SO_PATH_APPEND
702 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
704 fprintf(stderr,
"ruby_external_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
707 external_gc_loaded = TRUE;
710 rb_gc_function_map_t gc_functions;
712 # define load_external_gc_func(name) do { \
714 const char *func_name = "rb_gc_impl_" #name; \
715 gc_functions.name = dlsym(handle, func_name); \
716 if (!gc_functions.name) { \
717 fprintf(stderr, "ruby_external_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
722 gc_functions.name = rb_gc_impl_##name; \
727 load_external_gc_func(objspace_alloc);
728 load_external_gc_func(objspace_init);
729 load_external_gc_func(objspace_free);
730 load_external_gc_func(ractor_cache_alloc);
731 load_external_gc_func(ractor_cache_free);
732 load_external_gc_func(set_params);
733 load_external_gc_func(init);
734 load_external_gc_func(heap_sizes);
736 load_external_gc_func(shutdown_free_objects);
738 load_external_gc_func(start);
739 load_external_gc_func(during_gc_p);
740 load_external_gc_func(prepare_heap);
741 load_external_gc_func(gc_enable);
742 load_external_gc_func(gc_disable);
743 load_external_gc_func(gc_enabled_p);
744 load_external_gc_func(config_set);
745 load_external_gc_func(config_get);
746 load_external_gc_func(stress_set);
747 load_external_gc_func(stress_get);
749 load_external_gc_func(new_obj);
750 load_external_gc_func(obj_slot_size);
751 load_external_gc_func(heap_id_for_size);
752 load_external_gc_func(size_allocatable_p);
754 load_external_gc_func(malloc);
755 load_external_gc_func(calloc);
756 load_external_gc_func(realloc);
757 load_external_gc_func(free);
758 load_external_gc_func(adjust_memory_usage);
760 load_external_gc_func(mark);
761 load_external_gc_func(mark_and_move);
762 load_external_gc_func(mark_and_pin);
763 load_external_gc_func(mark_maybe);
764 load_external_gc_func(mark_weak);
765 load_external_gc_func(remove_weak);
767 load_external_gc_func(object_moved_p);
768 load_external_gc_func(location);
770 load_external_gc_func(writebarrier);
771 load_external_gc_func(writebarrier_unprotect);
772 load_external_gc_func(writebarrier_remember);
774 load_external_gc_func(each_objects);
775 load_external_gc_func(each_object);
777 load_external_gc_func(make_zombie);
778 load_external_gc_func(define_finalizer);
779 load_external_gc_func(undefine_finalizer);
780 load_external_gc_func(copy_finalizer);
781 load_external_gc_func(shutdown_call_finalizer);
783 load_external_gc_func(object_id);
784 load_external_gc_func(object_id_to_ref);
786 load_external_gc_func(set_measure_total_time);
787 load_external_gc_func(get_measure_total_time);
788 load_external_gc_func(get_total_time);
789 load_external_gc_func(gc_count);
790 load_external_gc_func(latest_gc_info);
791 load_external_gc_func(stat);
792 load_external_gc_func(stat_heap);
794 load_external_gc_func(obj_flags);
795 load_external_gc_func(pointer_to_heap_p);
796 load_external_gc_func(garbage_object_p);
797 load_external_gc_func(set_event_hook);
798 load_external_gc_func(copy_attributes);
800 load_external_gc_func(active_gc_name);
802 # undef load_external_gc_func
804 rb_gc_functions = gc_functions;
808 # define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
809 # define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
810 # define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
811 # define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
812 # define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
813 # define rb_gc_impl_set_params rb_gc_functions.set_params
814 # define rb_gc_impl_init rb_gc_functions.init
815 # define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
817 # define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
819 # define rb_gc_impl_start rb_gc_functions.start
820 # define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
821 # define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
822 # define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
823 # define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
824 # define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
825 # define rb_gc_impl_config_get rb_gc_functions.config_get
826 # define rb_gc_impl_config_set rb_gc_functions.config_set
827 # define rb_gc_impl_stress_set rb_gc_functions.stress_set
828 # define rb_gc_impl_stress_get rb_gc_functions.stress_get
830 # define rb_gc_impl_new_obj rb_gc_functions.new_obj
831 # define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
832 # define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
833 # define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
835 # define rb_gc_impl_malloc rb_gc_functions.malloc
836 # define rb_gc_impl_calloc rb_gc_functions.calloc
837 # define rb_gc_impl_realloc rb_gc_functions.realloc
838 # define rb_gc_impl_free rb_gc_functions.free
839 # define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
841 # define rb_gc_impl_mark rb_gc_functions.mark
842 # define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
843 # define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
844 # define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
845 # define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
846 # define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
848 # define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
849 # define rb_gc_impl_location rb_gc_functions.location
851 # define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
852 # define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
853 # define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
855 # define rb_gc_impl_each_objects rb_gc_functions.each_objects
856 # define rb_gc_impl_each_object rb_gc_functions.each_object
858 # define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
859 # define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
860 # define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
861 # define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
862 # define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
864 # define rb_gc_impl_object_id rb_gc_functions.object_id
865 # define rb_gc_impl_object_id_to_ref rb_gc_functions.object_id_to_ref
867 # define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
868 # define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
869 # define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
870 # define rb_gc_impl_gc_count rb_gc_functions.gc_count
871 # define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
872 # define rb_gc_impl_stat rb_gc_functions.stat
873 # define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
875 # define rb_gc_impl_obj_flags rb_gc_functions.obj_flags
876 # define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
877 # define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
878 # define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
879 # define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
881 # define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
887 rb_objspace_alloc(
void)
890 ruby_external_gc_init();
893 void *objspace = rb_gc_impl_objspace_alloc();
894 ruby_current_vm_ptr->gc.objspace = objspace;
896 rb_gc_impl_objspace_init(objspace);
897 rb_gc_impl_stress_set(objspace, initial_stress);
903 rb_objspace_free(
void *objspace)
905 rb_gc_impl_objspace_free(objspace);
909 rb_gc_obj_slot_size(
VALUE obj)
911 return rb_gc_impl_obj_slot_size(obj);
917 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
921 RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
923 memset((
char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
927 RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
934 rb_wb_unprotected_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
937 return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
944 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
947 #define UNEXPECTED_NODE(func) \
948 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
949 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
952 rb_data_object_check(
VALUE klass)
954 if (klass != rb_cObject && (
rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
956 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
964 if (klass) rb_data_object_check(klass);
979 RBIMPL_NONNULL_ARG(
type);
980 if (klass) rb_data_object_check(klass);
982 return newobj_of(GET_RACTOR(), klass,
T_DATA, (
VALUE)
type, 1 | typed_flag, (
VALUE)datap, wb_protected, size);
988 if (UNLIKELY(
type->flags & RUBY_TYPED_EMBEDDABLE)) {
992 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
998 if (
type->flags & RUBY_TYPED_EMBEDDABLE) {
999 if (!(
type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1003 size_t embed_size = offsetof(
struct RTypedData, data) + size;
1004 if (rb_gc_size_allocatable_p(embed_size)) {
1005 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
1006 memset((
char *)obj + offsetof(
struct RTypedData, data), 0, size);
1017 rb_objspace_data_type_memsize(
VALUE obj)
1022 const void *
ptr = RTYPEDDATA_GET_DATA(obj);
1024 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1025 #ifdef HAVE_MALLOC_USABLE_SIZE
1026 size += malloc_usable_size((
void *)
ptr);
1030 if (
ptr &&
type->function.dsize) {
1031 size +=
type->function.dsize(
ptr);
1039 rb_objspace_data_type_name(
VALUE obj)
1049 static enum rb_id_table_iterator_result
1050 cvar_table_free_i(
VALUE value,
void *ctx)
1052 xfree((
void *)value);
1053 return ID_TABLE_CONTINUE;
1057 make_io_zombie(
void *objspace,
VALUE obj)
1060 rb_gc_impl_make_zombie(objspace, obj, rb_io_fptr_finalize_internal, fptr);
1064 rb_data_free(
void *objspace,
VALUE obj)
1068 int free_immediately =
false;
1069 void (*dfree)(
void *);
1072 free_immediately = (
RTYPEDDATA(obj)->type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1073 dfree =
RTYPEDDATA(obj)->type->function.dfree;
1076 dfree =
RDATA(obj)->dfree;
1081 if (!
RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1083 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1086 else if (free_immediately) {
1092 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1095 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, dfree, data);
1096 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1101 RB_DEBUG_COUNTER_INC(obj_data_empty);
1109 rb_gc_obj_free(
void *objspace,
VALUE obj)
1111 RB_DEBUG_COUNTER_INC(obj_free);
1118 rb_bug(
"obj_free() called for broken object");
1131 if (rb_shape_obj_too_complex(obj)) {
1132 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1133 st_free_table(ROBJECT_IV_HASH(obj));
1135 else if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1136 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1140 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1145 rb_id_table_free(RCLASS_M_TBL(obj));
1146 rb_cc_table_free(obj);
1147 if (rb_shape_obj_too_complex(obj)) {
1148 st_free_table((
st_table *)RCLASS_IVPTR(obj));
1151 xfree(RCLASS_IVPTR(obj));
1154 if (RCLASS_CONST_TBL(obj)) {
1155 rb_free_const_table(RCLASS_CONST_TBL(obj));
1157 if (RCLASS_CVC_TBL(obj)) {
1158 rb_id_table_foreach_values(RCLASS_CVC_TBL(obj), cvar_table_free_i, NULL);
1159 rb_id_table_free(RCLASS_CVC_TBL(obj));
1161 rb_class_remove_subclass_head(obj);
1162 rb_class_remove_from_module_subclasses(obj);
1163 rb_class_remove_from_super_subclasses(obj);
1164 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1165 xfree(RCLASS_SUPERCLASSES(obj));
1178 #if USE_DEBUG_COUNTER
1181 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1184 RB_DEBUG_COUNTER_INC(obj_hash_1);
1187 RB_DEBUG_COUNTER_INC(obj_hash_2);
1190 RB_DEBUG_COUNTER_INC(obj_hash_3);
1193 RB_DEBUG_COUNTER_INC(obj_hash_4);
1199 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1203 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1206 if (RHASH_AR_TABLE_P(obj)) {
1207 if (RHASH_AR_TABLE(obj) == NULL) {
1208 RB_DEBUG_COUNTER_INC(obj_hash_null);
1211 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1215 RB_DEBUG_COUNTER_INC(obj_hash_st);
1224 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1228 if (!rb_data_free(objspace, obj))
return false;
1233 #if USE_DEBUG_COUNTER
1234 if (rm->
regs.num_regs >= 8) {
1235 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1237 else if (rm->
regs.num_regs >= 4) {
1238 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1240 else if (rm->
regs.num_regs >= 1) {
1241 RB_DEBUG_COUNTER_INC(obj_match_under4);
1244 onig_region_free(&rm->
regs, 0);
1247 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1251 if (
RFILE(obj)->fptr) {
1252 make_io_zombie(objspace, obj);
1253 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1258 RB_DEBUG_COUNTER_INC(obj_rational);
1261 RB_DEBUG_COUNTER_INC(obj_complex);
1267 if (RICLASS_OWNS_M_TBL_P(obj)) {
1269 rb_id_table_free(RCLASS_M_TBL(obj));
1271 if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
1272 rb_id_table_free(RCLASS_CALLABLE_M_TBL(obj));
1274 rb_class_remove_subclass_head(obj);
1275 rb_cc_table_free(obj);
1276 rb_class_remove_from_module_subclasses(obj);
1277 rb_class_remove_from_super_subclasses(obj);
1279 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1283 RB_DEBUG_COUNTER_INC(obj_float);
1287 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1288 xfree(BIGNUM_DIGITS(obj));
1289 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1292 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1297 UNEXPECTED_NODE(obj_free);
1301 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1302 RSTRUCT(obj)->as.heap.ptr == NULL) {
1303 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1306 xfree((
void *)RSTRUCT(obj)->as.heap.ptr);
1307 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1313 rb_gc_free_dsymbol(obj);
1314 RB_DEBUG_COUNTER_INC(obj_symbol);
1319 rb_imemo_free((
VALUE)obj);
1323 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1328 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, 0, 0);
1339 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1343 internal_object_p(
VALUE obj)
1345 void *
ptr = asan_unpoison_object_temporary(obj);
1347 if (
RBASIC(obj)->flags) {
1350 UNEXPECTED_NODE(internal_object_p);
1359 if (!
RBASIC(obj)->klass)
break;
1360 if (RCLASS_SINGLETON_P(obj)) {
1361 return rb_singleton_class_internal_p(obj);
1365 if (!
RBASIC(obj)->klass)
break;
1370 asan_poison_object(obj);
1376 rb_objspace_internal_object_p(
VALUE obj)
1378 return internal_object_p(obj);
1387 os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1392 for (; v != (
VALUE)vend; v += stride) {
1393 if (!internal_object_p(v)) {
1413 rb_objspace_each_objects(os_obj_of_i, &oes);
1460 return os_obj_of(of);
1474 rb_check_frozen(obj);
1476 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1482 should_be_callable(
VALUE block)
1491 should_be_finalizable(
VALUE obj)
1497 rb_check_frozen(obj);
1503 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1569 define_final(
int argc,
VALUE *argv,
VALUE os)
1578 if (rb_callable_receiver(block) == obj) {
1579 rb_warn(
"finalizer references object to be finalized");
1588 should_be_finalizable(obj);
1589 should_be_callable(block);
1591 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1599 rb_objspace_call_finalizer(
void)
1601 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1605 rb_objspace_free_objects(
void *objspace)
1607 rb_gc_impl_shutdown_free_objects(objspace);
1611 rb_objspace_garbage_object_p(
VALUE obj)
1613 return rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1634 #if SIZEOF_LONG == SIZEOF_VOIDP
1635 #define NUM2PTR(x) NUM2ULONG(x)
1636 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1637 #define NUM2PTR(x) NUM2ULL(x)
1640 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
1651 if (rb_static_id_valid_p(
SYM2ID(
ptr))) {
1663 VALUE obj = rb_gc_impl_object_id_to_ref(rb_gc_get_objspace(), objid);
1668 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is id of the unshareable object on multi-ractor", rb_int2str(objid, 10));
1676 return id2ref(objid);
1680 rb_find_object_id(
void *objspace,
VALUE obj,
VALUE (*get_heap_object_id)(
void *,
VALUE))
1683 #if SIZEOF_LONG == SIZEOF_VOIDP
1690 return get_heap_object_id(objspace, obj);
1694 nonspecial_obj_id(
void *_objspace,
VALUE obj)
1696 #if SIZEOF_LONG == SIZEOF_VOIDP
1698 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1701 # error not supported
1708 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
1745 return rb_find_object_id(rb_gc_get_objspace(), obj, rb_gc_impl_object_id);
1748 static enum rb_id_table_iterator_result
1749 cc_table_memsize_i(
VALUE ccs_ptr,
void *data_ptr)
1751 size_t *total_size = data_ptr;
1753 *total_size +=
sizeof(*ccs);
1754 *total_size +=
sizeof(ccs->entries[0]) * ccs->capa;
1755 return ID_TABLE_CONTINUE;
1761 size_t total = rb_id_table_memsize(cc_table);
1762 rb_id_table_foreach_values(cc_table, cc_table_memsize_i, &total);
1767 rb_obj_memsize_of(
VALUE obj)
1776 size += rb_generic_ivar_memsize(obj);
1781 if (rb_shape_obj_too_complex(obj)) {
1782 size += rb_st_memsize(ROBJECT_IV_HASH(obj));
1784 else if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1785 size += ROBJECT_IV_CAPACITY(obj) *
sizeof(
VALUE);
1790 if (RCLASS_M_TBL(obj)) {
1791 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1794 size +=
SIZEOF_VALUE << bit_length(RCLASS_IV_COUNT(obj));
1795 if (RCLASS_CVC_TBL(obj)) {
1796 size += rb_id_table_memsize(RCLASS_CVC_TBL(obj));
1798 if (RCLASS_EXT(obj)->const_tbl) {
1799 size += rb_id_table_memsize(RCLASS_EXT(obj)->const_tbl);
1801 if (RCLASS_CC_TBL(obj)) {
1802 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1804 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1805 size += (RCLASS_SUPERCLASS_DEPTH(obj) + 1) *
sizeof(
VALUE);
1809 if (RICLASS_OWNS_M_TBL_P(obj)) {
1810 if (RCLASS_M_TBL(obj)) {
1811 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1814 if (RCLASS_CC_TBL(obj)) {
1815 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1819 size += rb_str_memsize(obj);
1822 size += rb_ary_memsize(obj);
1825 if (RHASH_ST_TABLE_P(obj)) {
1826 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
1828 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(
st_table);
1837 size += rb_objspace_data_type_memsize(obj);
1842 size += onig_region_memsize(&rm->
regs);
1847 if (
RFILE(obj)->fptr) {
1848 size += rb_io_memsize(
RFILE(obj)->fptr);
1855 size += rb_imemo_memsize(obj);
1863 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
1864 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
1869 UNEXPECTED_NODE(obj_memsize_of);
1873 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
1874 RSTRUCT(obj)->as.heap.ptr) {
1884 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
1888 return size + rb_gc_obj_slot_size(obj);
1892 set_zero(st_data_t key, st_data_t val, st_data_t arg)
1907 count_objects_i(
VALUE obj,
void *d)
1911 if (
RBASIC(obj)->flags) {
1957 count_objects(
int argc,
VALUE *argv,
VALUE os)
1968 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
1974 rb_hash_stlike_foreach(hash, set_zero, hash);
1979 for (
size_t i = 0; i <=
T_MASK; i++) {
1988 #define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
1990 #define STACK_START (ec->machine.stack_start)
1991 #define STACK_END (ec->machine.stack_end)
1992 #define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
1994 #if STACK_GROW_DIRECTION < 0
1995 # define STACK_LENGTH (size_t)(STACK_START - STACK_END)
1996 #elif STACK_GROW_DIRECTION > 0
1997 # define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
1999 # define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2000 : (size_t)(STACK_END - STACK_START + 1))
2002 #if !STACK_GROW_DIRECTION
2003 int ruby_stack_grow_direction;
2005 ruby_get_stack_grow_direction(
volatile VALUE *addr)
2008 SET_MACHINE_STACK_END(&end);
2010 if (end > addr)
return ruby_stack_grow_direction = 1;
2011 return ruby_stack_grow_direction = -1;
2020 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2021 return STACK_LENGTH;
2024 #define PREVENT_STACK_OVERFLOW 1
2025 #ifndef PREVENT_STACK_OVERFLOW
2026 #if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2027 # define PREVENT_STACK_OVERFLOW 1
2029 # define PREVENT_STACK_OVERFLOW 0
2032 #if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2038 size_t length = STACK_LENGTH;
2039 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2041 return length > maximum_length;
2044 #define stack_check(ec, water_mark) FALSE
2047 #define STACKFRAME_FOR_CALL_CFUNC 2048
2052 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2058 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2063 #define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2064 if (!RB_SPECIAL_CONST_P(obj)) { \
2065 rb_vm_t *vm = GET_VM(); \
2066 void *objspace = vm->gc.objspace; \
2067 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2068 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2069 (func)(objspace, (obj_or_ptr)); \
2071 else if (check_obj ? \
2072 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2073 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2075 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2076 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2077 vm->gc.mark_func_data = NULL; \
2078 mark_func_data->mark_func((obj), mark_func_data->data); \
2079 vm->gc.mark_func_data = mark_func_data; \
2085 gc_mark_internal(
VALUE obj)
2087 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2093 gc_mark_internal(obj);
2099 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move,
ptr, *
ptr,
false);
2103 gc_mark_and_pin_internal(
VALUE obj)
2105 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2111 gc_mark_and_pin_internal(obj);
2115 gc_mark_maybe_internal(
VALUE obj)
2117 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2123 gc_mark_maybe_internal(obj);
2132 void *objspace = vm->gc.objspace;
2133 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2134 GC_ASSERT(rb_gc_impl_during_gc_p(objspace));
2136 rb_gc_impl_mark_weak(objspace,
ptr);
2139 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace));
2146 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj,
ptr);
2149 ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2151 each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2162 each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2164 if (end <= start)
return;
2165 each_location(start, end - start, cb, data);
2169 gc_mark_maybe_each_location(
VALUE obj,
void *data)
2171 gc_mark_maybe_internal(obj);
2177 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2181 rb_gc_mark_values(
long n,
const VALUE *values)
2183 for (
long i = 0; i < n; i++) {
2184 gc_mark_internal(values[i]);
2189 rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
2191 for (
long i = 0; i < n; i++) {
2192 gc_mark_and_pin_internal(values[i]);
2197 mark_key(st_data_t key, st_data_t value, st_data_t data)
2199 gc_mark_and_pin_internal((
VALUE)key);
2209 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2213 mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2215 gc_mark_internal((
VALUE)key);
2216 gc_mark_internal((
VALUE)value);
2222 pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2224 gc_mark_and_pin_internal((
VALUE)key);
2225 gc_mark_and_pin_internal((
VALUE)value);
2231 pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2233 gc_mark_and_pin_internal((
VALUE)key);
2234 gc_mark_internal((
VALUE)value);
2240 mark_hash(
VALUE hash)
2242 if (rb_hash_compare_by_id_p(hash)) {
2243 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2246 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2249 gc_mark_internal(RHASH(hash)->ifnone);
2260 static enum rb_id_table_iterator_result
2261 mark_method_entry_i(
VALUE me,
void *objspace)
2263 gc_mark_internal(me);
2265 return ID_TABLE_CONTINUE;
2269 mark_m_tbl(
void *objspace,
struct rb_id_table *tbl)
2272 rb_id_table_foreach_values(tbl, mark_method_entry_i, objspace);
2276 #if STACK_GROW_DIRECTION < 0
2277 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2278 #elif STACK_GROW_DIRECTION > 0
2279 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2281 #define GET_STACK_BOUNDS(start, end, appendix) \
2282 ((STACK_END < STACK_START) ? \
2283 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2287 gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
2289 gc_mark_maybe_internal(obj);
2291 #ifdef RUBY_ASAN_ENABLED
2293 void *fake_frame_start;
2294 void *fake_frame_end;
2295 bool is_fake_frame = asan_get_fake_stack_extents(
2296 ec->machine.asan_fake_stack_handle, obj,
2297 ec->machine.stack_start, ec->machine.stack_end,
2298 &fake_frame_start, &fake_frame_end
2300 if (is_fake_frame) {
2301 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2306 #if defined(__wasm__)
2309 static VALUE *rb_stack_range_tmp[2];
2312 rb_mark_locations(
void *begin,
void *end)
2314 rb_stack_range_tmp[0] = begin;
2315 rb_stack_range_tmp[1] = end;
2318 # if defined(__EMSCRIPTEN__)
2323 emscripten_scan_stack(rb_mark_locations);
2324 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2326 emscripten_scan_registers(rb_mark_locations);
2327 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2334 VALUE *stack_start, *stack_end;
2336 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2337 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2339 rb_wasm_scan_locals(rb_mark_locations);
2340 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2352 VALUE v[
sizeof(rb_jmp_buf) / (
sizeof(
VALUE))];
2353 } save_regs_gc_mark;
2354 VALUE *stack_start, *stack_end;
2356 FLUSH_REGISTER_WINDOWS;
2357 memset(&save_regs_gc_mark, 0,
sizeof(save_regs_gc_mark));
2359 rb_setjmp(save_regs_gc_mark.j);
2365 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2368 #ifdef RUBY_ASAN_ENABLED
2374 each_location(save_regs_gc_mark.v, numberof(save_regs_gc_mark.v), gc_mark_machine_stack_location_maybe, data);
2375 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2382 VALUE *stack_start, *stack_end;
2384 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2385 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2388 #ifdef RUBY_ASAN_ENABLED
2395 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2396 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
2397 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2401 rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2403 gc_mark_and_pin_internal((
VALUE)value);
2411 if (!tbl || tbl->num_entries == 0)
return;
2419 if (!tbl || tbl->num_entries == 0)
return;
2427 gc_mark_tbl_no_pin(tbl);
2430 static enum rb_id_table_iterator_result
2431 mark_cvc_tbl_i(
VALUE cvc_entry,
void *objspace)
2438 gc_mark_internal((
VALUE)entry->cref);
2440 return ID_TABLE_CONTINUE;
2444 mark_cvc_tbl(
void *objspace,
VALUE klass)
2448 rb_id_table_foreach_values(tbl, mark_cvc_tbl_i, objspace);
2455 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
2458 static enum rb_id_table_iterator_result
2459 mark_const_table_i(
VALUE value,
void *objspace)
2463 gc_mark_internal(ce->value);
2464 gc_mark_internal(ce->file);
2466 return ID_TABLE_CONTINUE;
2470 rb_gc_mark_roots(
void *objspace,
const char **categoryp)
2473 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2475 #define MARK_CHECKPOINT(category) do { \
2476 if (categoryp) *categoryp = category; \
2479 MARK_CHECKPOINT(
"vm");
2481 if (vm->self) gc_mark_internal(vm->self);
2483 MARK_CHECKPOINT(
"machine_context");
2484 mark_current_machine_context(ec);
2486 MARK_CHECKPOINT(
"end_proc");
2489 MARK_CHECKPOINT(
"global_tbl");
2490 rb_gc_mark_global_tbl();
2493 void rb_yjit_root_mark(
void);
2495 if (rb_yjit_enabled_p) {
2496 MARK_CHECKPOINT(
"YJIT");
2497 rb_yjit_root_mark();
2501 MARK_CHECKPOINT(
"finish");
2502 #undef MARK_CHECKPOINT
2505 #define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA(d)->type->function.dmark
2508 rb_gc_mark_children(
void *objspace,
VALUE obj)
2511 rb_mark_generic_ivar(obj);
2526 rb_bug(
"rb_gc_mark() called for broken object");
2534 rb_imemo_mark_and_move(obj,
false);
2541 gc_mark_internal(
RBASIC(obj)->klass);
2546 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
2554 mark_m_tbl(objspace, RCLASS_M_TBL(obj));
2555 mark_cvc_tbl(objspace, obj);
2556 rb_cc_table_mark(obj);
2557 if (rb_shape_obj_too_complex(obj)) {
2558 gc_mark_tbl_no_pin((
st_table *)RCLASS_IVPTR(obj));
2561 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
2562 gc_mark_internal(RCLASS_IVPTR(obj)[i]);
2566 if (RCLASS_CONST_TBL(obj)) {
2567 rb_id_table_foreach_values(RCLASS_CONST_TBL(obj), mark_const_table_i, objspace);
2570 gc_mark_internal(RCLASS_EXT(obj)->classpath);
2574 if (RICLASS_OWNS_M_TBL_P(obj)) {
2575 mark_m_tbl(objspace, RCLASS_M_TBL(obj));
2581 if (RCLASS_INCLUDER(obj)) {
2582 gc_mark_internal(RCLASS_INCLUDER(obj));
2584 mark_m_tbl(objspace, RCLASS_CALLABLE_M_TBL(obj));
2585 rb_cc_table_mark(obj);
2589 if (ARY_SHARED_P(obj)) {
2590 VALUE root = ARY_SHARED_ROOT(obj);
2591 gc_mark_internal(root);
2596 for (
long i = 0; i <
len; i++) {
2597 gc_mark_internal(
ptr[i]);
2607 if (STR_SHARED_P(obj)) {
2608 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
2613 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
2616 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
2626 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
2628 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
2629 gc_mark_internal(*(
VALUE *)((
char *)
ptr + offset));
2636 if (mark_func) (*mark_func)(
ptr);
2644 rb_shape_t *shape = rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj));
2646 if (rb_shape_obj_too_complex(obj)) {
2647 gc_mark_tbl_no_pin(ROBJECT_IV_HASH(obj));
2652 uint32_t
len = ROBJECT_IV_COUNT(obj);
2653 for (uint32_t i = 0; i <
len; i++) {
2654 gc_mark_internal(
ptr[i]);
2662 attr_index_t num_of_ivs = shape->next_iv_index;
2663 if (RCLASS_EXT(klass)->max_iv_count < num_of_ivs) {
2664 RCLASS_EXT(klass)->max_iv_count = num_of_ivs;
2672 if (
RFILE(obj)->fptr) {
2673 gc_mark_internal(
RFILE(obj)->fptr->self);
2674 gc_mark_internal(
RFILE(obj)->fptr->pathv);
2675 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
2676 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
2677 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
2678 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
2679 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
2680 gc_mark_internal(
RFILE(obj)->fptr->timeout);
2685 gc_mark_internal(
RREGEXP(obj)->src);
2689 gc_mark_internal(
RMATCH(obj)->regexp);
2691 gc_mark_internal(
RMATCH(obj)->str);
2696 gc_mark_internal(RRATIONAL(obj)->num);
2697 gc_mark_internal(RRATIONAL(obj)->den);
2701 gc_mark_internal(RCOMPLEX(obj)->real);
2702 gc_mark_internal(RCOMPLEX(obj)->imag);
2707 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
2709 for (
long i = 0; i <
len; i++) {
2710 gc_mark_internal(
ptr[i]);
2720 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
2722 rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj) ?
"corrupted object" :
"non object");
2727 rb_gc_obj_optimal_size(
VALUE obj)
2731 return rb_ary_size_as_embedded(obj);
2734 if (rb_shape_obj_too_complex(obj)) {
2735 return sizeof(
struct RObject);
2742 return rb_str_size_as_embedded(obj);
2745 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(
st_table) : sizeof(ar_table));
2755 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
2761 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
2768 rb_gc_writebarrier_remember(
VALUE obj)
2770 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
2776 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
2780 rb_gc_external_gc_loaded_p(
void)
2782 return external_gc_loaded;
2786 rb_gc_active_gc_name(
void)
2788 const char *gc_name = rb_gc_impl_active_gc_name();
2789 const size_t len = strlen(gc_name);
2790 if (
len > RB_GC_MAX_NAME_LEN) {
2791 rb_bug(
"GC should have a name no more than %d chars long. Currently: %zu (%s)",
2792 RB_GC_MAX_NAME_LEN,
len, gc_name);
2800 rb_obj_gc_flags(
VALUE obj,
ID* flags,
size_t max)
2802 return rb_gc_impl_obj_flags(rb_gc_get_objspace(), obj, flags, max);
2808 rb_gc_ractor_cache_alloc(
void)
2810 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace());
2814 rb_gc_ractor_cache_free(
void *cache)
2816 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
2822 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
2825 rb_vm_register_global_object(obj);
2836 tmp->next = vm->global_object_list;
2838 vm->global_object_list = tmp;
2846 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
2848 rb_print_backtrace(stderr);
2858 if (tmp->varptr == addr) {
2859 vm->global_object_list = tmp->next;
2864 if (tmp->next->varptr == addr) {
2867 tmp->next = tmp->next->next;
2884 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
2928 rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
2930 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
2934 gc_ref_update_array(
void *objspace,
VALUE v)
2936 if (ARY_SHARED_P(v)) {
2937 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
2939 UPDATE_IF_MOVED(objspace,
RARRAY(v)->as.heap.aux.shared_root);
2941 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
2943 if (ARY_EMBED_P(new_root) && new_root != old_root) {
2944 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
2945 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
2946 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
2954 for (
long i = 0; i <
len; i++) {
2955 UPDATE_IF_MOVED(objspace,
ptr[i]);
2959 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
2960 if (rb_ary_embeddable_p(v)) {
2961 rb_ary_make_embedded(v);
2968 gc_ref_update_object(
void *objspace,
VALUE v)
2972 if (rb_shape_obj_too_complex(v)) {
2973 gc_ref_update_table_values_only(ROBJECT_IV_HASH(v));
2977 size_t slot_size = rb_gc_obj_slot_size(v);
2979 if (slot_size >= embed_size && !
RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
2987 for (uint32_t i = 0; i < ROBJECT_IV_COUNT(v); i++) {
2988 UPDATE_IF_MOVED(objspace,
ptr[i]);
2993 rb_gc_ref_update_table_values_only(
st_table *tbl)
2995 gc_ref_update_table_values_only(tbl);
3002 gc_update_table_refs(
ptr);
3006 gc_ref_update_hash(
void *objspace,
VALUE v)
3008 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)objspace);
3012 gc_update_values(
void *objspace,
long n,
VALUE *values)
3014 for (
long i = 0; i < n; i++) {
3015 UPDATE_IF_MOVED(objspace, values[i]);
3020 rb_gc_update_values(
long n,
VALUE *values)
3022 gc_update_values(rb_gc_get_objspace(), n, values);
3025 static enum rb_id_table_iterator_result
3026 check_id_table_move(
VALUE value,
void *data)
3028 void *objspace = (
void *)data;
3030 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)value)) {
3031 return ID_TABLE_REPLACE;
3034 return ID_TABLE_CONTINUE;
3040 return rb_gc_impl_location(rb_gc_get_objspace(), value);
3044 rb_gc_prepare_heap_process_object(
VALUE obj)
3058 rb_gc_prepare_heap(
void)
3060 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3064 rb_gc_heap_id_for_size(
size_t size)
3066 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3070 rb_gc_size_allocatable_p(
size_t size)
3072 return rb_gc_impl_size_allocatable_p(size);
3075 static enum rb_id_table_iterator_result
3076 update_id_table(
VALUE *value,
void *data,
int existing)
3078 void *objspace = (
void *)data;
3080 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)*value)) {
3081 *value = rb_gc_impl_location(objspace, (
VALUE)*value);
3084 return ID_TABLE_CONTINUE;
3088 update_m_tbl(
void *objspace,
struct rb_id_table *tbl)
3091 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table, objspace);
3095 static enum rb_id_table_iterator_result
3096 update_cc_tbl_i(
VALUE ccs_ptr,
void *objspace)
3099 VM_ASSERT(vm_ccs_p(ccs));
3101 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)ccs->cme)) {
3105 for (
int i=0; i<ccs->len; i++) {
3106 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)ccs->entries[i].cc)) {
3112 return ID_TABLE_CONTINUE;
3116 update_cc_tbl(
void *objspace,
VALUE klass)
3120 rb_id_table_foreach_values(tbl, update_cc_tbl_i, objspace);
3124 static enum rb_id_table_iterator_result
3125 update_cvc_tbl_i(
VALUE cvc_entry,
void *objspace)
3132 TYPED_UPDATE_IF_MOVED(objspace,
rb_cref_t *, entry->cref);
3135 entry->class_value = rb_gc_impl_location(objspace, entry->class_value);
3137 return ID_TABLE_CONTINUE;
3141 update_cvc_tbl(
void *objspace,
VALUE klass)
3145 rb_id_table_foreach_values(tbl, update_cvc_tbl_i, objspace);
3149 static enum rb_id_table_iterator_result
3150 update_const_table(
VALUE value,
void *objspace)
3154 if (rb_gc_impl_object_moved_p(objspace, ce->value)) {
3155 ce->value = rb_gc_impl_location(objspace, ce->value);
3158 if (rb_gc_impl_object_moved_p(objspace, ce->file)) {
3159 ce->file = rb_gc_impl_location(objspace, ce->file);
3162 return ID_TABLE_CONTINUE;
3166 update_const_tbl(
void *objspace,
struct rb_id_table *tbl)
3169 rb_id_table_foreach_values(tbl, update_const_table, objspace);
3176 UPDATE_IF_MOVED(objspace, entry->klass);
3177 entry = entry->next;
3184 UPDATE_IF_MOVED(objspace, ext->origin_);
3185 UPDATE_IF_MOVED(objspace, ext->includer);
3186 UPDATE_IF_MOVED(objspace, ext->refined_class);
3187 update_subclass_entries(objspace, ext->subclasses);
3191 update_superclasses(
void *objspace,
VALUE obj)
3193 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
3194 for (
size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj) + 1; i++) {
3195 UPDATE_IF_MOVED(objspace, RCLASS_SUPERCLASSES(obj)[i]);
3201 #define global_symbols ruby_global_symbols
3204 rb_gc_update_vm_references(
void *objspace)
3207 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3209 rb_vm_update_references(vm);
3210 rb_gc_update_global_tbl();
3211 global_symbols.ids = rb_gc_impl_location(objspace, global_symbols.ids);
3212 global_symbols.dsymbol_fstr_hash = rb_gc_impl_location(objspace, global_symbols.dsymbol_fstr_hash);
3213 gc_update_table_refs(global_symbols.str_sym);
3216 void rb_yjit_root_update_references(
void);
3218 if (rb_yjit_enabled_p) {
3219 rb_yjit_root_update_references();
3225 rb_gc_update_object_references(
void *objspace,
VALUE obj)
3228 rb_ref_update_generic_ivar(obj);
3234 UPDATE_IF_MOVED(objspace, RCLASS_ATTACHED_OBJECT(obj));
3239 UPDATE_IF_MOVED(objspace,
RCLASS(obj)->super);
3241 update_m_tbl(objspace, RCLASS_M_TBL(obj));
3242 update_cc_tbl(objspace, obj);
3243 update_cvc_tbl(objspace, obj);
3244 update_superclasses(objspace, obj);
3246 if (rb_shape_obj_too_complex(obj)) {
3247 gc_ref_update_table_values_only(RCLASS_IV_HASH(obj));
3250 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
3251 UPDATE_IF_MOVED(objspace, RCLASS_IVPTR(obj)[i]);
3255 update_class_ext(objspace, RCLASS_EXT(obj));
3256 update_const_tbl(objspace, RCLASS_CONST_TBL(obj));
3258 UPDATE_IF_MOVED(objspace, RCLASS_EXT(obj)->classpath);
3262 if (RICLASS_OWNS_M_TBL_P(obj)) {
3263 update_m_tbl(objspace, RCLASS_M_TBL(obj));
3266 UPDATE_IF_MOVED(objspace,
RCLASS(obj)->super);
3268 update_class_ext(objspace, RCLASS_EXT(obj));
3269 update_m_tbl(objspace, RCLASS_CALLABLE_M_TBL(obj));
3270 update_cc_tbl(objspace, obj);
3274 rb_imemo_mark_and_move(obj,
true);
3286 gc_ref_update_array(objspace, obj);
3290 gc_ref_update_hash(objspace, obj);
3291 UPDATE_IF_MOVED(objspace, RHASH(obj)->ifnone);
3296 if (STR_SHARED_P(obj)) {
3297 UPDATE_IF_MOVED(objspace,
RSTRING(obj)->as.heap.aux.shared);
3302 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
3303 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
3304 rb_str_make_embedded(obj);
3316 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3318 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3321 *ref = rb_gc_impl_location(objspace, *ref);
3326 if (compact_func) (*compact_func)(
ptr);
3333 gc_ref_update_object(objspace, obj);
3337 if (
RFILE(obj)->fptr) {
3338 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->self);
3339 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->pathv);
3340 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->tied_io_for_writing);
3341 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
3342 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
3343 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->encs.ecopts);
3344 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->write_lock);
3348 UPDATE_IF_MOVED(objspace,
RREGEXP(obj)->src);
3352 UPDATE_IF_MOVED(objspace, RSYMBOL(obj)->fstr);
3360 UPDATE_IF_MOVED(objspace,
RMATCH(obj)->regexp);
3363 UPDATE_IF_MOVED(objspace,
RMATCH(obj)->str);
3368 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->num);
3369 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->den);
3373 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->real);
3374 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->imag);
3383 for (i = 0; i <
len; i++) {
3384 UPDATE_IF_MOVED(objspace,
ptr[i]);
3393 UPDATE_IF_MOVED(objspace,
RBASIC(obj)->klass);
3406 unless_objspace(objspace) {
return; }
3408 rb_gc_impl_start(objspace,
true,
true,
true,
false);
3414 unless_objspace(objspace) {
return FALSE; }
3416 return rb_gc_impl_during_gc_p(objspace);
3422 return rb_gc_impl_gc_count(rb_gc_get_objspace());
3438 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
3457 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3475 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3498 if (
NIL_P(heap_name)) {
3512 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
3526 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
3527 rb_hash_aset(cfg_hash, sym(
"implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
3535 void *objspace = rb_gc_get_objspace();
3537 rb_gc_impl_config_set(objspace, hash);
3539 return rb_gc_impl_config_get(objspace);
3545 return rb_gc_impl_stress_get(rb_gc_get_objspace());
3551 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
3557 rb_gc_initial_stress_set(
VALUE flag)
3559 initial_stress = flag;
3563 rb_gc_heap_sizes(
void)
3565 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
3571 return rb_objspace_gc_enable(rb_gc_get_objspace());
3575 rb_objspace_gc_enable(
void *objspace)
3577 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3578 rb_gc_impl_gc_enable(objspace);
3579 return RBOOL(disabled);
3589 gc_disable_no_rest(
void *objspace)
3591 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3592 rb_gc_impl_gc_disable(objspace,
false);
3593 return RBOOL(disabled);
3597 rb_gc_disable_no_rest(
void)
3599 return gc_disable_no_rest(rb_gc_get_objspace());
3605 return rb_objspace_gc_disable(rb_gc_get_objspace());
3609 rb_objspace_gc_disable(
void *objspace)
3611 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3612 rb_gc_impl_gc_disable(objspace,
true);
3613 return RBOOL(disabled);
3624 ruby_gc_set_params(
void)
3626 rb_gc_impl_set_params(rb_gc_get_objspace());
3630 rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
3634 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace()))
rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
3638 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3639 struct gc_mark_func_data_struct mfd = {
3644 vm->gc.mark_func_data = &mfd;
3645 rb_gc_mark_children(rb_gc_get_objspace(), obj);
3646 vm->gc.mark_func_data = prev_mfd;
3653 const char *category;
3654 void (*func)(
const char *category,
VALUE,
void *);
3659 root_objects_from(
VALUE obj,
void *
ptr)
3662 (*data->func)(data->category, obj, data->data);
3666 rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
3668 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");
3674 .data = passing_data,
3677 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3678 struct gc_mark_func_data_struct mfd = {
3679 .mark_func = root_objects_from,
3683 vm->gc.mark_func_data = &mfd;
3684 rb_gc_mark_roots(rb_gc_get_objspace(), &data.category);
3685 vm->gc.mark_func_data = prev_mfd;
3696 #define TYPE_NAME(t) case (t): return #t;
3723 if (obj && rb_objspace_data_type_name(obj)) {
3724 return rb_objspace_data_type_name(obj);
3733 obj_type_name(
VALUE obj)
3735 return type_name(
TYPE(obj), obj);
3739 rb_method_type_name(rb_method_type_t
type)
3742 case VM_METHOD_TYPE_ISEQ:
return "iseq";
3743 case VM_METHOD_TYPE_ATTRSET:
return "attrest";
3744 case VM_METHOD_TYPE_IVAR:
return "ivar";
3745 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
3746 case VM_METHOD_TYPE_ALIAS:
return "alias";
3747 case VM_METHOD_TYPE_REFINED:
return "refined";
3748 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
3749 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
3750 case VM_METHOD_TYPE_MISSING:
return "missing";
3751 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
3752 case VM_METHOD_TYPE_UNDEF:
return "undef";
3753 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
3755 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
3759 rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
3761 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
3762 VALUE path = rb_iseq_path(iseq);
3763 int n = ISEQ_BODY(iseq)->location.first_lineno;
3764 snprintf(buff, buff_size,
" %s@%s:%d",
3771 str_len_no_raise(
VALUE str)
3774 if (
len < 0)
return 0;
3775 if (
len > INT_MAX)
return INT_MAX;
3779 #define BUFF_ARGS buff + pos, buff_size - pos
3780 #define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
3781 #define APPEND_S(s) do { \
3782 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
3786 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
3789 #define C(c, s) ((c) != 0 ? (s) : " ")
3792 rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
3797 APPEND_F(
"%s", obj_type_name(obj));
3809 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
3828 if (internal_object_p(obj)) {
3831 else if (
RBASIC(obj)->klass == 0) {
3832 APPEND_S(
"(temporary internal)");
3836 if (!
NIL_P(class_path)) {
3846 const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
3849 rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
3856 UNEXPECTED_NODE(rb_raw_obj_info);
3859 if (ARY_SHARED_P(obj)) {
3860 APPEND_S(
"shared -> ");
3861 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
3863 else if (ARY_EMBED_P(obj)) {
3864 APPEND_F(
"[%s%s] len: %ld (embed)",
3865 C(ARY_EMBED_P(obj),
"E"),
3866 C(ARY_SHARED_P(obj),
"S"),
3870 APPEND_F(
"[%s%s] len: %ld, capa:%ld ptr:%p",
3871 C(ARY_EMBED_P(obj),
"E"),
3872 C(ARY_SHARED_P(obj),
"S"),
3874 ARY_EMBED_P(obj) ? -1L :
RARRAY(obj)->as.heap.aux.capa,
3879 if (STR_SHARED_P(obj)) {
3883 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
3887 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj),
RSTRING_PTR(obj));
3891 VALUE fstr = RSYMBOL(obj)->fstr;
3892 ID id = RSYMBOL(obj)->id;
3894 APPEND_F(
":%s id:%d",
RSTRING_PTR(fstr), (
unsigned int)
id);
3897 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
3902 APPEND_F(
"-> %p", (
void*)rb_gc_impl_location(rb_gc_get_objspace(), obj));
3906 APPEND_F(
"[%c] %"PRIdSIZE,
3907 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
3915 if (!
NIL_P(class_path)) {
3926 if (!
NIL_P(class_path)) {
3933 if (rb_shape_obj_too_complex(obj)) {
3934 size_t hash_len = rb_st_table_size(ROBJECT_IV_HASH(obj));
3935 APPEND_F(
"(too_complex) len:%zu", hash_len);
3938 uint32_t
len = ROBJECT_IV_CAPACITY(obj);
3940 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
3941 APPEND_F(
"(embed) len:%d",
len);
3945 APPEND_F(
"len:%d ptr:%p",
len, (
void *)
ptr);
3954 (block = vm_proc_block(obj)) != NULL &&
3955 (vm_block_type(block) == block_type_iseq) &&
3956 (iseq = vm_block_iseq(block)) != NULL) {
3957 rb_raw_iseq_info(BUFF_ARGS, iseq);
3959 else if (rb_ractor_p(obj)) {
3962 APPEND_F(
"r:%d", r->pub.id);
3966 const char *
const type_name = rb_objspace_data_type_name(obj);
3968 APPEND_F(
"%s", type_name);
3974 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
3976 switch (imemo_type(obj)) {
3981 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
3983 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
3984 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
3985 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
3986 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
3987 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
3988 me->def ? rb_method_type_name(me->def->type) :
"NULL",
3989 me->def ? me->def->aliased : -1,
3991 (
void *)me->defined_class);
3994 switch (me->def->type) {
3995 case VM_METHOD_TYPE_ISEQ:
3996 APPEND_S(
" (iseq:");
3997 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
4009 rb_raw_iseq_info(BUFF_ARGS, iseq);
4012 case imemo_callinfo:
4015 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
4019 vm_ci_kwarg(ci) ?
"available" :
"NULL");
4022 case imemo_callcache:
4028 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
4029 NIL_P(class_path) ? (cc->klass ?
"??" :
"<NULL>") :
RSTRING_PTR(class_path),
4031 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
4033 (
void *)(uintptr_t)vm_cc_call(cc));
4051 #define asan_unpoisoning_object(obj) \
4052 for (void *poisoned = asan_unpoison_object_temporary(obj), \
4053 *unpoisoning = &poisoned; \
4055 unpoisoning = asan_poison_object_restore(obj, poisoned))
4058 rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
4060 asan_unpoisoning_object(obj) {
4061 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4062 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4063 if (pos >= buff_size) {}
4074 #define OBJ_INFO_BUFFERS_NUM 10
4075 #define OBJ_INFO_BUFFERS_SIZE 0x100
4077 static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
4096 rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
4097 char *
const buff = obj_info_buffers[index];
4098 return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
4104 return obj_type_name(obj);
4119 gc_vraise(
void *
ptr)
4122 rb_vraise(argv->exc, argv->fmt, *argv->ap);
4127 gc_raise(
VALUE exc,
const char *fmt, ...)
4135 if (ruby_thread_has_gvl_p()) {
4145 fprintf(stderr,
"%s",
"[FATAL] ");
4146 vfprintf(stderr, fmt, ap);
4153 NORETURN(
static void negative_size_allocation_error(
const char *));
4155 negative_size_allocation_error(
const char *msg)
4161 ruby_memerror_body(
void *dummy)
4167 NORETURN(
static void ruby_memerror(
void));
4172 if (ruby_thread_has_gvl_p()) {
4181 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4191 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
4194 rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4195 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4198 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4199 rb_ec_raised_clear(ec);
4202 rb_ec_raised_set(ec, RAISED_NOMEMORY);
4203 exc = ruby_vm_special_exception_copy(exc);
4206 EC_JUMP_TAG(ec, TAG_RAISE);
4210 rb_malloc_info_show_results(
void)
4217 if ((ssize_t)size < 0) {
4218 negative_size_allocation_error(
"too large allocation size");
4221 return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
4225 ruby_malloc_size_overflow(
size_t count,
size_t elsize)
4228 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
4235 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4241 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4244 #ifdef ruby_sized_xrealloc
4245 #undef ruby_sized_xrealloc
4248 ruby_sized_xrealloc(
void *
ptr,
size_t new_size,
size_t old_size)
4250 if ((ssize_t)new_size < 0) {
4251 negative_size_allocation_error(
"too large allocation size");
4254 return rb_gc_impl_realloc(rb_gc_get_objspace(),
ptr, new_size, old_size);
4260 return ruby_sized_xrealloc(
ptr, new_size, 0);
4263 #ifdef ruby_sized_xrealloc2
4264 #undef ruby_sized_xrealloc2
4267 ruby_sized_xrealloc2(
void *
ptr,
size_t n,
size_t size,
size_t old_n)
4269 size_t len = xmalloc2_size(n, size);
4270 return rb_gc_impl_realloc(rb_gc_get_objspace(),
ptr,
len, old_n * size);
4276 return ruby_sized_xrealloc2(
ptr, n, size, 0);
4279 #ifdef ruby_sized_xfree
4280 #undef ruby_sized_xfree
4283 ruby_sized_xfree(
void *x,
size_t size)
4289 if (LIKELY(GET_VM())) {
4290 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
4301 ruby_sized_xfree(x, 0);
4305 rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
4307 size_t w = size_mul_add_or_raise(x, y, z,
rb_eArgError);
4312 rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
4314 size_t w = size_mul_add_or_raise(x, y, z,
rb_eArgError);
4319 rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
4321 size_t w = size_mul_add_or_raise(x, y, z,
rb_eArgError);
4326 rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4328 size_t u = size_mul_add_mul_or_raise(x, y, z, w,
rb_eArgError);
4333 rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4335 size_t u = size_mul_add_mul_or_raise(x, y, z, w,
rb_eArgError);
4343 ruby_mimmalloc(
size_t size)
4346 #if CALC_EXACT_MALLOC_SIZE
4350 #if CALC_EXACT_MALLOC_SIZE
4366 ruby_mimcalloc(
size_t num,
size_t size)
4369 #if CALC_EXACT_MALLOC_SIZE
4370 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
4371 if (UNLIKELY(t.left)) {
4375 mem = calloc1(size);
4387 mem = calloc(num, size);
4393 ruby_mimfree(
void *
ptr)
4395 #if CALC_EXACT_MALLOC_SIZE
4405 unless_objspace(objspace) {
return; }
4407 rb_gc_impl_adjust_memory_usage(objspace, diff);
4411 rb_obj_info(
VALUE obj)
4413 return obj_info(obj);
4417 rb_obj_info_dump(
VALUE obj)
4420 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
4424 rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
4427 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
4481 malloc_offset = gc_compute_malloc_offset();
4494 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
4517 ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name)
4519 #if defined(__linux__) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
4522 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (
unsigned long)addr, size, name);
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define RB_UNLIKELY(x)
Asserts that the given Boolean expression likely doesn't hold.
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.
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for a 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.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define T_FILE
Old name of RUBY_T_FILE.
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define T_MASK
Old name of RUBY_T_MASK.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define T_DATA
Old name of RUBY_T_DATA.
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define LL2NUM
Old name of RB_LL2NUM.
#define T_NONE
Old name of RUBY_T_NONE.
#define T_NODE
Old name of RUBY_T_NODE.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_ABLE
Old name of RB_FL_ABLE.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define T_UNDEF
Old name of RUBY_T_UNDEF.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_MATCH
Old name of RUBY_T_MATCH.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define T_MOVED
Old name of RUBY_T_MOVED.
#define FL_TEST
Old name of RB_FL_TEST.
#define xcalloc
Old name of ruby_xcalloc.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
int ruby_stack_check(void)
Checks for stack overflow.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
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.
VALUE rb_eArgError
ArgumentError exception.
size_t rb_obj_embedded_size(uint32_t numiv)
Internal header for Object.
VALUE rb_mKernel
Kernel module.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
void rb_gc_mark(VALUE obj)
Marks an object.
void rb_mark_tbl_no_pin(struct st_table *tbl)
Identical to rb_mark_tbl(), except it marks objects using rb_gc_mark_movable().
void rb_memerror(void)
Triggers out-of-memory error.
size_t rb_gc_stat(VALUE key_or_buf)
Obtains various GC related profiles.
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
VALUE rb_gc_disable(void)
Disables GC.
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
VALUE rb_gc_latest_gc_info(VALUE key_or_buf)
Obtains various info regarding the most recent GC run.
void rb_mark_tbl(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only values of the table and leave their associated keys...
VALUE rb_gc_enable(void)
(Re-) enables GC.
void rb_mark_hash(struct st_table *tbl)
Marks keys and values associated inside of the given table.
int rb_during_gc(void)
Queries if the GC is busy.
void rb_gc_register_address(VALUE *valptr)
Inform the garbage collector that the global or static variable pointed by valptr stores a live Ruby ...
void rb_gc_unregister_address(VALUE *valptr)
Inform the garbage collector that a pointer previously passed to rb_gc_register_address() no longer p...
void rb_gc_mark_maybe(VALUE obj)
Identical to rb_gc_mark(), except it allows the passed value be a non-object.
void rb_gc_writebarrier(VALUE old, VALUE young)
This is the implementation of RB_OBJ_WRITE().
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
void rb_gc_writebarrier_unprotect(VALUE obj)
This is the implementation of RB_OBJ_WB_UNPROTECT().
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Marks objects between the two pointers.
void rb_gc(void)
Triggers a GC process.
void rb_global_variable(VALUE *)
An alias for rb_gc_register_address().
void rb_gc_register_mark_object(VALUE object)
Inform the garbage collector that object is a live Ruby object that should not be moved.
void rb_gc_update_tbl_refs(st_table *ptr)
Updates references inside of tables.
void rb_mark_set(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only keys of the table and leave their associated values...
VALUE rb_define_finalizer(VALUE obj, VALUE block)
Assigns a finaliser for an object.
void rb_gc_copy_finalizer(VALUE dst, VALUE src)
Copy&paste an object's finaliser to another.
void rb_gc_adjust_memory_usage(ssize_t diff)
Informs that there are external memory usages.
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
Defines RBIMPL_HAS_BUILTIN.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
VALUE rb_obj_is_fiber(VALUE obj)
Queries if an object is a fiber.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_obj_id(VALUE obj)
Finds or creates an integer primary key of the given object.
VALUE rb_memory_id(VALUE obj)
Identical to rb_obj_id(), except it hesitates from allocating a new instance of rb_cInteger.
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.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
bool ruby_free_at_exit_p(void)
Returns whether the Ruby VM will free all memory at shutdown.
VALUE rb_yield(VALUE val)
Yields the block.
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY(obj)
Convenient casting macro.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RCLASS(obj)
Convenient casting macro.
VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
This is the primitive way to wrap an existing C struct into RData.
#define DATA_PTR(obj)
Convenient getter macro.
VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
Identical to rb_data_object_wrap(), except it allocates a new data region internally instead of takin...
#define RDATA(obj)
Convenient casting macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
#define RFILE(obj)
Convenient casting macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RMATCH(obj)
Convenient casting macro.
#define ROBJECT(obj)
Convenient casting macro.
static VALUE * ROBJECT_IVPTR(VALUE obj)
Queries the instance variables.
#define RREGEXP(obj)
Convenient casting macro.
#define RREGEXP_PTR(obj)
Convenient accessor macro.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define RSTRING(obj)
Convenient casting macro.
static long RSTRUCT_LEN(VALUE st)
Returns the number of struct members.
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
This is the primitive way to wrap an existing C struct into RTypedData.
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
Identical to rb_data_typed_object_wrap(), except it allocates a new data region internally instead of...
#define RTYPEDDATA(obj)
Convenient casting macro.
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define errno
Ractor-aware version of errno.
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
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
Internal header for Class.
Represents the region of a capture group.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.
void * ruby_xmalloc2(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc(), except it allocates nelems * elemsiz bytes.
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
void ruby_xfree(void *ptr)
Deallocates a storage instance.
void * ruby_xcalloc(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc2(), except it returns a zero-filled storage instance.
void * ruby_xrealloc(void *ptr, size_t newsiz)
Resize the storage instance.
void * ruby_xrealloc2(void *ptr, size_t newelems, size_t newsiz)
Identical to ruby_xrealloc(), except it resizes the given storage instance to newelems * newsiz bytes...