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>
81 #include "debug_counter.h"
82 #include "eval_intern.h"
86 #include "internal/class.h"
87 #include "internal/compile.h"
88 #include "internal/complex.h"
89 #include "internal/cont.h"
90 #include "internal/error.h"
91 #include "internal/eval.h"
92 #include "internal/gc.h"
93 #include "internal/hash.h"
94 #include "internal/imemo.h"
95 #include "internal/io.h"
96 #include "internal/numeric.h"
97 #include "internal/object.h"
98 #include "internal/proc.h"
99 #include "internal/rational.h"
100 #include "internal/sanitizers.h"
101 #include "internal/struct.h"
102 #include "internal/symbol.h"
103 #include "internal/thread.h"
104 #include "internal/variable.h"
105 #include "internal/warnings.h"
116 #include "ruby_assert.h"
117 #include "ruby_atomic.h"
121 #include "vm_callinfo.h"
122 #include "ractor_core.h"
132 RB_VM_LOCK_ENTER_LEV(&lev);
137 rb_gc_vm_unlock(
unsigned int lev)
139 RB_VM_LOCK_LEAVE_LEV(&lev);
146 RB_VM_LOCK_ENTER_CR_LEV(GET_RACTOR(), &lev);
151 rb_gc_cr_unlock(
unsigned int lev)
153 RB_VM_LOCK_LEAVE_CR_LEV(GET_RACTOR(), &lev);
157 rb_gc_vm_lock_no_barrier(
void)
159 unsigned int lev = 0;
160 RB_VM_LOCK_ENTER_LEV_NB(&lev);
165 rb_gc_vm_unlock_no_barrier(
unsigned int lev)
167 RB_VM_LOCK_LEAVE_LEV(&lev);
171 rb_gc_vm_barrier(
void)
179 if (LIKELY(!(ruby_vm_event_flags & event)))
return;
182 if (!ec->cfp)
return;
184 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
188 rb_gc_get_objspace(
void)
190 return GET_VM()->gc.objspace;
194 rb_gc_ractor_newobj_cache_foreach(
void (*func)(
void *cache,
void *data),
void *data)
197 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
198 func(r->newobj_cache, data);
203 rb_gc_run_obj_finalizer(
VALUE objid,
long count,
VALUE (*callback)(
long i,
void *data),
void *data)
214 #define RESTORE_FINALIZER() (\
215 ec->cfp = saved.cfp, \
216 ec->cfp->sp = saved.sp, \
217 ec->errinfo = saved.errinfo)
219 saved.errinfo = ec->errinfo;
221 saved.sp = ec->cfp->sp;
226 enum ruby_tag_type state = EC_EXEC_TAG();
227 if (state != TAG_NONE) {
230 VALUE failed_final = saved.final;
233 rb_warn(
"Exception in finalizer %+"PRIsVALUE, failed_final);
234 rb_ec_error_print(ec, ec->errinfo);
238 for (
long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
239 saved.final = callback(i, data);
243 #undef RESTORE_FINALIZER
247 rb_gc_set_pending_interrupt(
void)
250 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
254 rb_gc_unset_pending_interrupt(
void)
257 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
261 rb_gc_multi_ractor_p(
void)
263 return rb_multi_ractor_p();
266 bool rb_obj_is_main_ractor(
VALUE gv);
269 rb_gc_shutdown_call_finalizer_p(
VALUE obj)
274 if (rb_obj_is_thread(obj))
return false;
275 if (rb_obj_is_mutex(obj))
return false;
277 if (rb_obj_is_main_ractor(obj))
return false;
285 if (RSYMBOL(obj)->fstr &&
288 RSYMBOL(obj)->fstr = 0;
301 rb_gc_get_shape(
VALUE obj)
303 return (uint32_t)rb_shape_get_shape_id(obj);
307 rb_gc_set_shape(
VALUE obj, uint32_t shape_id)
309 rb_shape_set_shape_id(obj, (uint32_t)shape_id);
313 rb_gc_rebuild_shape(
VALUE obj,
size_t heap_id)
315 rb_shape_t *orig_shape = rb_shape_get_shape(obj);
317 if (rb_shape_obj_too_complex(obj))
return (uint32_t)OBJ_TOO_COMPLEX_SHAPE_ID;
319 rb_shape_t *initial_shape = rb_shape_get_shape_by_id((shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID));
320 rb_shape_t *new_shape = rb_shape_traverse_from_new_root(initial_shape, orig_shape);
322 if (!new_shape)
return 0;
324 return (uint32_t)rb_shape_id(new_shape);
327 void rb_vm_update_references(
void *
ptr);
329 #define rb_setjmp(env) RUBY_SETJMP(env)
330 #define rb_jmp_buf rb_jmpbuf_t
331 #undef rb_data_object_wrap
333 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
334 #define MAP_ANONYMOUS MAP_ANON
337 #define unless_objspace(objspace) \
339 rb_vm_t *unless_objspace_vm = GET_VM(); \
340 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
343 #define RMOVED(obj) ((struct RMoved *)(obj))
345 #define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
346 if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
347 *(_type *)&(_thing) = (_type)rb_gc_impl_location(_objspace, (VALUE)_thing); \
351 #define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
353 #if RUBY_MARK_FREE_DEBUG
354 int ruby_gc_debug_indent = 0;
357 #ifndef RGENGC_OBJ_INFO
358 # define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
361 #ifndef CALC_EXACT_MALLOC_SIZE
362 # define CALC_EXACT_MALLOC_SIZE 0
367 static size_t malloc_offset = 0;
368 #if defined(HAVE_MALLOC_USABLE_SIZE)
370 gc_compute_malloc_offset(
void)
381 for (offset = 0; offset <= 16; offset += 8) {
382 size_t allocated = (64 - offset);
383 void *test_ptr = malloc(allocated);
384 size_t wasted = malloc_usable_size(test_ptr) - allocated;
395 gc_compute_malloc_offset(
void)
403 rb_malloc_grow_capa(
size_t current,
size_t type_size)
405 size_t current_capacity = current;
406 if (current_capacity < 4) {
407 current_capacity = 4;
409 current_capacity *= type_size;
412 size_t new_capacity = (current_capacity * 2);
415 if (rb_popcount64(new_capacity) != 1) {
416 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
419 new_capacity -= malloc_offset;
420 new_capacity /= type_size;
421 if (current > new_capacity) {
422 rb_bug(
"rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
428 static inline struct rbimpl_size_mul_overflow_tag
429 size_add_overflow(size_t x, size_t y)
435 #elif defined(ckd_add)
436 p = ckd_add(&z, x, y);
438 #elif __has_builtin(__builtin_add_overflow)
439 p = __builtin_add_overflow(x, y, &z);
441 #elif defined(DSIZE_T)
453 return (
struct rbimpl_size_mul_overflow_tag) { p, z, };
456 static inline struct rbimpl_size_mul_overflow_tag
457 size_mul_add_overflow(size_t x, size_t y, size_t z)
459 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
460 struct rbimpl_size_mul_overflow_tag u = size_add_overflow(t.right, z);
461 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
464 static inline struct rbimpl_size_mul_overflow_tag
465 size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w)
467 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
468 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
469 struct rbimpl_size_mul_overflow_tag v = size_add_overflow(t.right, u.right);
470 return (
struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
473 PRINTF_ARGS(NORETURN(
static void gc_raise(
VALUE,
const char*, ...)), 2, 3);
476 size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
478 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
479 if (LIKELY(!t.left)) {
488 "integer overflow: %"PRIuSIZE
491 x, y, (
size_t)SIZE_MAX);
496 rb_size_mul_or_raise(
size_t x,
size_t y,
VALUE exc)
498 return size_mul_or_raise(x, y, exc);
502 size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
504 struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
505 if (LIKELY(!t.left)) {
514 "integer overflow: %"PRIuSIZE
518 x, y, z, (
size_t)SIZE_MAX);
523 rb_size_mul_add_or_raise(
size_t x,
size_t y,
size_t z,
VALUE exc)
525 return size_mul_add_or_raise(x, y, z, exc);
529 size_mul_add_mul_or_raise(
size_t x,
size_t y,
size_t z,
size_t w,
VALUE exc)
531 struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
532 if (LIKELY(!t.left)) {
541 "integer overflow: %"PRIdSIZE
546 x, y, z, w, (
size_t)SIZE_MAX);
550 #if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
552 volatile VALUE rb_gc_guarded_val;
556 rb_gc_guarded_val = val;
562 static const char *obj_type_name(
VALUE obj);
563 #define RB_AMALGAMATED_DEFAULT_GC
564 #include "gc/default.c"
566 #if USE_SHARED_GC && !defined(HAVE_DLOPEN)
567 # error "Shared GC requires dlopen"
571 typedef struct gc_function_map {
573 void *(*objspace_alloc)(void);
574 void (*objspace_init)(
void *objspace_ptr);
575 void (*objspace_free)(
void *objspace_ptr);
576 void *(*ractor_cache_alloc)(
void *objspace_ptr);
577 void (*ractor_cache_free)(
void *objspace_ptr,
void *cache);
578 void (*set_params)(
void *objspace_ptr);
580 size_t *(*heap_sizes)(
void *objspace_ptr);
582 void (*shutdown_free_objects)(
void *objspace_ptr);
584 void (*start)(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact);
585 bool (*during_gc_p)(
void *objspace_ptr);
586 void (*prepare_heap)(
void *objspace_ptr);
587 void (*gc_enable)(
void *objspace_ptr);
588 void (*gc_disable)(
void *objspace_ptr,
bool finish_current_gc);
589 bool (*gc_enabled_p)(
void *objspace_ptr);
590 VALUE (*config_get)(
void *objpace_ptr);
591 void (*config_set)(
void *objspace_ptr,
VALUE hash);
592 void (*stress_set)(
void *objspace_ptr,
VALUE flag);
593 VALUE (*stress_get)(
void *objspace_ptr);
595 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);
596 size_t (*obj_slot_size)(
VALUE obj);
597 size_t (*heap_id_for_size)(
void *objspace_ptr,
size_t size);
598 bool (*size_allocatable_p)(
size_t size);
600 void *(*malloc)(
void *objspace_ptr,
size_t size);
601 void *(*calloc)(
void *objspace_ptr,
size_t size);
602 void *(*realloc)(
void *objspace_ptr,
void *
ptr,
size_t new_size,
size_t old_size);
603 void (*free)(
void *objspace_ptr,
void *
ptr,
size_t old_size);
604 void (*adjust_memory_usage)(
void *objspace_ptr, ssize_t diff);
606 void (*mark)(
void *objspace_ptr,
VALUE obj);
607 void (*mark_and_move)(
void *objspace_ptr,
VALUE *
ptr);
608 void (*mark_and_pin)(
void *objspace_ptr,
VALUE obj);
609 void (*mark_maybe)(
void *objspace_ptr,
VALUE obj);
610 void (*mark_weak)(
void *objspace_ptr,
VALUE *
ptr);
611 void (*remove_weak)(
void *objspace_ptr,
VALUE parent_obj,
VALUE *
ptr);
613 bool (*object_moved_p)(
void *objspace_ptr,
VALUE obj);
614 VALUE (*location)(
void *objspace_ptr,
VALUE value);
616 void (*writebarrier)(
void *objspace_ptr,
VALUE a,
VALUE b);
617 void (*writebarrier_unprotect)(
void *objspace_ptr,
VALUE obj);
618 void (*writebarrier_remember)(
void *objspace_ptr,
VALUE obj);
620 void (*each_objects)(
void *objspace_ptr, int (*callback)(
void *,
void *, size_t,
void *),
void *data);
621 void (*each_object)(
void *objspace_ptr, void (*func)(
VALUE obj,
void *data),
void *data);
623 void (*make_zombie)(
void *objspace_ptr,
VALUE obj, void (*dfree)(
void *),
void *data);
625 void (*undefine_finalizer)(
void *objspace_ptr,
VALUE obj);
626 void (*copy_finalizer)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
627 void (*shutdown_call_finalizer)(
void *objspace_ptr);
629 VALUE (*object_id)(
void *objspace_ptr,
VALUE obj);
630 VALUE (*object_id_to_ref)(
void *objspace_ptr,
VALUE object_id);
632 void (*set_measure_total_time)(
void *objspace_ptr,
VALUE flag);
633 bool (*get_measure_total_time)(
void *objspace_ptr);
634 unsigned long long (*get_total_time)(
void *objspace_ptr);
635 size_t (*gc_count)(
void *objspace_ptr);
636 VALUE (*latest_gc_info)(
void *objspace_ptr,
VALUE key);
637 VALUE (*stat)(
void *objspace_ptr,
VALUE hash_or_sym);
638 VALUE (*stat_heap)(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym);
640 size_t (*obj_flags)(
void *objspace_ptr,
VALUE obj,
ID* flags,
size_t max);
641 bool (*pointer_to_heap_p)(
void *objspace_ptr,
const void *
ptr);
642 bool (*garbage_object_p)(
void *objspace_ptr,
VALUE obj);
643 void (*set_event_hook)(
void *objspace_ptr,
const rb_event_flag_t event);
644 void (*copy_attributes)(
void *objspace_ptr,
VALUE dest,
VALUE obj);
645 } rb_gc_function_map_t;
647 static rb_gc_function_map_t rb_gc_functions;
649 # define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
652 ruby_external_gc_init(
void)
657 char *gc_so_file = getenv(RUBY_GC_LIBRARY);
659 char *gc_so_path = NULL;
664 for (
size_t i = 0; i < strlen(gc_so_file); i++) {
665 char c = gc_so_file[i];
666 if (isalnum(c))
continue;
672 fprintf(stderr,
"Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY
"\n");
677 size_t gc_so_path_size = strlen(SHARED_GC_DIR
"librubygc." SOEXT) + strlen(gc_so_file) + 1;
678 gc_so_path = alloca(gc_so_path_size);
680 size_t gc_so_path_idx = 0;
681 #define GC_SO_PATH_APPEND(str) do { \
682 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
684 GC_SO_PATH_APPEND(SHARED_GC_DIR);
685 GC_SO_PATH_APPEND(
"librubygc.");
686 GC_SO_PATH_APPEND(gc_so_file);
687 GC_SO_PATH_APPEND(SOEXT);
688 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
689 #undef GC_SO_PATH_APPEND
692 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
694 fprintf(stderr,
"ruby_external_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
699 rb_gc_function_map_t gc_functions;
701 # define load_external_gc_func(name) do { \
703 const char *func_name = "rb_gc_impl_" #name; \
704 gc_functions.name = dlsym(handle, func_name); \
705 if (!gc_functions.name) { \
706 fprintf(stderr, "ruby_external_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
711 gc_functions.name = rb_gc_impl_##name; \
716 load_external_gc_func(objspace_alloc);
717 load_external_gc_func(objspace_init);
718 load_external_gc_func(objspace_free);
719 load_external_gc_func(ractor_cache_alloc);
720 load_external_gc_func(ractor_cache_free);
721 load_external_gc_func(set_params);
722 load_external_gc_func(init);
723 load_external_gc_func(heap_sizes);
725 load_external_gc_func(shutdown_free_objects);
727 load_external_gc_func(start);
728 load_external_gc_func(during_gc_p);
729 load_external_gc_func(prepare_heap);
730 load_external_gc_func(gc_enable);
731 load_external_gc_func(gc_disable);
732 load_external_gc_func(gc_enabled_p);
733 load_external_gc_func(config_set);
734 load_external_gc_func(config_get);
735 load_external_gc_func(stress_set);
736 load_external_gc_func(stress_get);
738 load_external_gc_func(new_obj);
739 load_external_gc_func(obj_slot_size);
740 load_external_gc_func(heap_id_for_size);
741 load_external_gc_func(size_allocatable_p);
743 load_external_gc_func(malloc);
744 load_external_gc_func(calloc);
745 load_external_gc_func(realloc);
746 load_external_gc_func(free);
747 load_external_gc_func(adjust_memory_usage);
749 load_external_gc_func(mark);
750 load_external_gc_func(mark_and_move);
751 load_external_gc_func(mark_and_pin);
752 load_external_gc_func(mark_maybe);
753 load_external_gc_func(mark_weak);
754 load_external_gc_func(remove_weak);
756 load_external_gc_func(object_moved_p);
757 load_external_gc_func(location);
759 load_external_gc_func(writebarrier);
760 load_external_gc_func(writebarrier_unprotect);
761 load_external_gc_func(writebarrier_remember);
763 load_external_gc_func(each_objects);
764 load_external_gc_func(each_object);
766 load_external_gc_func(make_zombie);
767 load_external_gc_func(define_finalizer);
768 load_external_gc_func(undefine_finalizer);
769 load_external_gc_func(copy_finalizer);
770 load_external_gc_func(shutdown_call_finalizer);
772 load_external_gc_func(object_id);
773 load_external_gc_func(object_id_to_ref);
775 load_external_gc_func(set_measure_total_time);
776 load_external_gc_func(get_measure_total_time);
777 load_external_gc_func(get_total_time);
778 load_external_gc_func(gc_count);
779 load_external_gc_func(latest_gc_info);
780 load_external_gc_func(stat);
781 load_external_gc_func(stat_heap);
783 load_external_gc_func(obj_flags);
784 load_external_gc_func(pointer_to_heap_p);
785 load_external_gc_func(garbage_object_p);
786 load_external_gc_func(set_event_hook);
787 load_external_gc_func(copy_attributes);
789 # undef load_external_gc_func
791 rb_gc_functions = gc_functions;
795 # define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
796 # define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
797 # define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
798 # define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
799 # define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
800 # define rb_gc_impl_set_params rb_gc_functions.set_params
801 # define rb_gc_impl_init rb_gc_functions.init
802 # define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
804 # define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
806 # define rb_gc_impl_start rb_gc_functions.start
807 # define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
808 # define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
809 # define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
810 # define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
811 # define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
812 # define rb_gc_impl_config_get rb_gc_functions.config_get
813 # define rb_gc_impl_config_set rb_gc_functions.config_set
814 # define rb_gc_impl_stress_set rb_gc_functions.stress_set
815 # define rb_gc_impl_stress_get rb_gc_functions.stress_get
817 # define rb_gc_impl_new_obj rb_gc_functions.new_obj
818 # define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
819 # define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
820 # define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
822 # define rb_gc_impl_malloc rb_gc_functions.malloc
823 # define rb_gc_impl_calloc rb_gc_functions.calloc
824 # define rb_gc_impl_realloc rb_gc_functions.realloc
825 # define rb_gc_impl_free rb_gc_functions.free
826 # define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
828 # define rb_gc_impl_mark rb_gc_functions.mark
829 # define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
830 # define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
831 # define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
832 # define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
833 # define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
835 # define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
836 # define rb_gc_impl_location rb_gc_functions.location
838 # define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
839 # define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
840 # define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
842 # define rb_gc_impl_each_objects rb_gc_functions.each_objects
843 # define rb_gc_impl_each_object rb_gc_functions.each_object
845 # define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
846 # define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
847 # define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
848 # define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
849 # define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
851 # define rb_gc_impl_object_id rb_gc_functions.object_id
852 # define rb_gc_impl_object_id_to_ref rb_gc_functions.object_id_to_ref
854 # define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
855 # define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
856 # define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
857 # define rb_gc_impl_gc_count rb_gc_functions.gc_count
858 # define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
859 # define rb_gc_impl_stat rb_gc_functions.stat
860 # define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
862 # define rb_gc_impl_obj_flags rb_gc_functions.obj_flags
863 # define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
864 # define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
865 # define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
866 # define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
872 rb_objspace_alloc(
void)
875 ruby_external_gc_init();
878 void *objspace = rb_gc_impl_objspace_alloc();
879 ruby_current_vm_ptr->gc.objspace = objspace;
881 rb_gc_impl_objspace_init(objspace);
882 rb_gc_impl_stress_set(objspace, initial_stress);
888 rb_objspace_free(
void *objspace)
890 rb_gc_impl_objspace_free(objspace);
894 rb_gc_obj_slot_size(
VALUE obj)
896 return rb_gc_impl_obj_slot_size(obj);
902 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
906 RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
908 memset((
char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
912 RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
919 rb_wb_unprotected_newobj_of(
VALUE klass,
VALUE flags,
size_t size)
922 return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
929 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
932 #define UNEXPECTED_NODE(func) \
933 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
934 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
937 rb_data_object_check(
VALUE klass)
939 if (klass != rb_cObject && (
rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
941 rb_warn(
"undefining the allocator of T_DATA class %"PRIsVALUE, klass);
949 if (klass) rb_data_object_check(klass);
964 RBIMPL_NONNULL_ARG(
type);
965 if (klass) rb_data_object_check(klass);
967 return newobj_of(GET_RACTOR(), klass,
T_DATA, (
VALUE)
type, 1 | typed_flag, (
VALUE)datap, wb_protected, size);
973 if (UNLIKELY(
type->flags & RUBY_TYPED_EMBEDDABLE)) {
977 return typed_data_alloc(klass, 0, datap,
type,
sizeof(
struct RTypedData));
983 if (
type->flags & RUBY_TYPED_EMBEDDABLE) {
984 if (!(
type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
988 size_t embed_size = offsetof(
struct RTypedData, data) + size;
989 if (rb_gc_size_allocatable_p(embed_size)) {
990 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0,
type, embed_size);
991 memset((
char *)obj + offsetof(
struct RTypedData, data), 0, size);
1002 rb_objspace_data_type_memsize(
VALUE obj)
1007 const void *
ptr = RTYPEDDATA_GET_DATA(obj);
1009 if (
RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1010 #ifdef HAVE_MALLOC_USABLE_SIZE
1011 size += malloc_usable_size((
void *)
ptr);
1015 if (
ptr &&
type->function.dsize) {
1016 size +=
type->function.dsize(
ptr);
1024 rb_objspace_data_type_name(
VALUE obj)
1034 static enum rb_id_table_iterator_result
1035 cvar_table_free_i(
VALUE value,
void *ctx)
1037 xfree((
void *)value);
1038 return ID_TABLE_CONTINUE;
1042 make_io_zombie(
void *objspace,
VALUE obj)
1045 rb_gc_impl_make_zombie(objspace, obj, rb_io_fptr_finalize_internal, fptr);
1049 rb_data_free(
void *objspace,
VALUE obj)
1053 int free_immediately =
false;
1054 void (*dfree)(
void *);
1057 free_immediately = (
RTYPEDDATA(obj)->type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1058 dfree =
RTYPEDDATA(obj)->type->function.dfree;
1061 dfree =
RDATA(obj)->dfree;
1066 if (!
RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1068 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1071 else if (free_immediately) {
1077 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1080 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, dfree, data);
1081 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1086 RB_DEBUG_COUNTER_INC(obj_data_empty);
1094 rb_gc_obj_free(
void *objspace,
VALUE obj)
1096 RB_DEBUG_COUNTER_INC(obj_free);
1103 rb_bug(
"obj_free() called for broken object");
1116 if (rb_shape_obj_too_complex(obj)) {
1117 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1118 st_free_table(ROBJECT_IV_HASH(obj));
1120 else if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
1121 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1125 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1130 rb_id_table_free(RCLASS_M_TBL(obj));
1131 rb_cc_table_free(obj);
1132 if (rb_shape_obj_too_complex(obj)) {
1133 st_free_table((
st_table *)RCLASS_IVPTR(obj));
1136 xfree(RCLASS_IVPTR(obj));
1139 if (RCLASS_CONST_TBL(obj)) {
1140 rb_free_const_table(RCLASS_CONST_TBL(obj));
1142 if (RCLASS_CVC_TBL(obj)) {
1143 rb_id_table_foreach_values(RCLASS_CVC_TBL(obj), cvar_table_free_i, NULL);
1144 rb_id_table_free(RCLASS_CVC_TBL(obj));
1146 rb_class_remove_subclass_head(obj);
1147 rb_class_remove_from_module_subclasses(obj);
1148 rb_class_remove_from_super_subclasses(obj);
1149 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1150 xfree(RCLASS_SUPERCLASSES(obj));
1163 #if USE_DEBUG_COUNTER
1166 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1169 RB_DEBUG_COUNTER_INC(obj_hash_1);
1172 RB_DEBUG_COUNTER_INC(obj_hash_2);
1175 RB_DEBUG_COUNTER_INC(obj_hash_3);
1178 RB_DEBUG_COUNTER_INC(obj_hash_4);
1184 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1188 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1191 if (RHASH_AR_TABLE_P(obj)) {
1192 if (RHASH_AR_TABLE(obj) == NULL) {
1193 RB_DEBUG_COUNTER_INC(obj_hash_null);
1196 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1200 RB_DEBUG_COUNTER_INC(obj_hash_st);
1209 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1213 if (!rb_data_free(objspace, obj))
return false;
1218 #if USE_DEBUG_COUNTER
1219 if (rm->
regs.num_regs >= 8) {
1220 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1222 else if (rm->
regs.num_regs >= 4) {
1223 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1225 else if (rm->
regs.num_regs >= 1) {
1226 RB_DEBUG_COUNTER_INC(obj_match_under4);
1229 onig_region_free(&rm->
regs, 0);
1232 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1236 if (
RFILE(obj)->fptr) {
1237 make_io_zombie(objspace, obj);
1238 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1243 RB_DEBUG_COUNTER_INC(obj_rational);
1246 RB_DEBUG_COUNTER_INC(obj_complex);
1252 if (RICLASS_OWNS_M_TBL_P(obj)) {
1254 rb_id_table_free(RCLASS_M_TBL(obj));
1256 if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
1257 rb_id_table_free(RCLASS_CALLABLE_M_TBL(obj));
1259 rb_class_remove_subclass_head(obj);
1260 rb_cc_table_free(obj);
1261 rb_class_remove_from_module_subclasses(obj);
1262 rb_class_remove_from_super_subclasses(obj);
1264 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1268 RB_DEBUG_COUNTER_INC(obj_float);
1272 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1273 xfree(BIGNUM_DIGITS(obj));
1274 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1277 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1282 UNEXPECTED_NODE(obj_free);
1286 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1287 RSTRUCT(obj)->as.heap.ptr == NULL) {
1288 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1291 xfree((
void *)RSTRUCT(obj)->as.heap.ptr);
1292 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1298 rb_gc_free_dsymbol(obj);
1299 RB_DEBUG_COUNTER_INC(obj_symbol);
1304 rb_imemo_free((
VALUE)obj);
1308 rb_bug(
"gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1313 rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, 0, 0);
1324 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1328 internal_object_p(
VALUE obj)
1330 void *
ptr = asan_unpoison_object_temporary(obj);
1332 if (
RBASIC(obj)->flags) {
1335 UNEXPECTED_NODE(internal_object_p);
1344 if (!
RBASIC(obj)->klass)
break;
1345 if (RCLASS_SINGLETON_P(obj)) {
1346 return rb_singleton_class_internal_p(obj);
1350 if (!
RBASIC(obj)->klass)
break;
1355 asan_poison_object(obj);
1361 rb_objspace_internal_object_p(
VALUE obj)
1363 return internal_object_p(obj);
1372 os_obj_of_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1377 for (; v != (
VALUE)vend; v += stride) {
1378 if (!internal_object_p(v)) {
1398 rb_objspace_each_objects(os_obj_of_i, &oes);
1445 return os_obj_of(of);
1459 rb_check_frozen(obj);
1461 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1467 should_be_callable(
VALUE block)
1476 should_be_finalizable(
VALUE obj)
1482 rb_check_frozen(obj);
1488 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1554 define_final(
int argc,
VALUE *argv,
VALUE os)
1563 if (rb_callable_receiver(block) == obj) {
1564 rb_warn(
"finalizer references object to be finalized");
1573 should_be_finalizable(obj);
1574 should_be_callable(block);
1576 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1584 rb_objspace_call_finalizer(
void)
1586 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1590 rb_objspace_free_objects(
void *objspace)
1592 rb_gc_impl_shutdown_free_objects(objspace);
1596 rb_objspace_garbage_object_p(
VALUE obj)
1598 return rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1619 #if SIZEOF_LONG == SIZEOF_VOIDP
1620 #define NUM2PTR(x) NUM2ULONG(x)
1621 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1622 #define NUM2PTR(x) NUM2ULL(x)
1625 if (
FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
1636 if (rb_static_id_valid_p(
SYM2ID(
ptr))) {
1648 VALUE obj = rb_gc_impl_object_id_to_ref(rb_gc_get_objspace(), objid);
1653 rb_raise(
rb_eRangeError,
"%+"PRIsVALUE
" is id of the unshareable object on multi-ractor", rb_int2str(objid, 10));
1661 return id2ref(objid);
1665 rb_find_object_id(
void *objspace,
VALUE obj,
VALUE (*get_heap_object_id)(
void *,
VALUE))
1668 #if SIZEOF_LONG == SIZEOF_VOIDP
1675 return get_heap_object_id(objspace, obj);
1679 nonspecial_obj_id(
void *_objspace,
VALUE obj)
1681 #if SIZEOF_LONG == SIZEOF_VOIDP
1683 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1686 # error not supported
1693 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
1730 return rb_find_object_id(rb_gc_get_objspace(), obj, rb_gc_impl_object_id);
1733 static enum rb_id_table_iterator_result
1734 cc_table_memsize_i(
VALUE ccs_ptr,
void *data_ptr)
1736 size_t *total_size = data_ptr;
1738 *total_size +=
sizeof(*ccs);
1739 *total_size +=
sizeof(ccs->entries[0]) * ccs->capa;
1740 return ID_TABLE_CONTINUE;
1746 size_t total = rb_id_table_memsize(cc_table);
1747 rb_id_table_foreach_values(cc_table, cc_table_memsize_i, &total);
1752 rb_obj_memsize_of(
VALUE obj)
1761 size += rb_generic_ivar_memsize(obj);
1766 if (rb_shape_obj_too_complex(obj)) {
1767 size += rb_st_memsize(ROBJECT_IV_HASH(obj));
1769 else if (!(
RBASIC(obj)->flags & ROBJECT_EMBED)) {
1770 size += ROBJECT_IV_CAPACITY(obj) *
sizeof(
VALUE);
1775 if (RCLASS_M_TBL(obj)) {
1776 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1779 size +=
SIZEOF_VALUE << bit_length(RCLASS_IV_COUNT(obj));
1780 if (RCLASS_CVC_TBL(obj)) {
1781 size += rb_id_table_memsize(RCLASS_CVC_TBL(obj));
1783 if (RCLASS_EXT(obj)->const_tbl) {
1784 size += rb_id_table_memsize(RCLASS_EXT(obj)->const_tbl);
1786 if (RCLASS_CC_TBL(obj)) {
1787 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1789 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1790 size += (RCLASS_SUPERCLASS_DEPTH(obj) + 1) *
sizeof(
VALUE);
1794 if (RICLASS_OWNS_M_TBL_P(obj)) {
1795 if (RCLASS_M_TBL(obj)) {
1796 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1799 if (RCLASS_CC_TBL(obj)) {
1800 size += cc_table_memsize(RCLASS_CC_TBL(obj));
1804 size += rb_str_memsize(obj);
1807 size += rb_ary_memsize(obj);
1810 if (RHASH_ST_TABLE_P(obj)) {
1811 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
1813 size += st_memsize(RHASH_ST_TABLE(obj)) -
sizeof(
st_table);
1822 size += rb_objspace_data_type_memsize(obj);
1827 size += onig_region_memsize(&rm->
regs);
1832 if (
RFILE(obj)->fptr) {
1833 size += rb_io_memsize(
RFILE(obj)->fptr);
1840 size += rb_imemo_memsize(obj);
1848 if (!(
RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
1849 size += BIGNUM_LEN(obj) *
sizeof(BDIGIT);
1854 UNEXPECTED_NODE(obj_memsize_of);
1858 if ((
RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
1859 RSTRUCT(obj)->as.heap.ptr) {
1869 rb_bug(
"objspace/memsize_of(): unknown data type 0x%x(%p)",
1873 return size + rb_gc_obj_slot_size(obj);
1877 set_zero(st_data_t key, st_data_t val, st_data_t arg)
1892 count_objects_i(
VALUE obj,
void *d)
1896 if (
RBASIC(obj)->flags) {
1942 count_objects(
int argc,
VALUE *argv,
VALUE os)
1953 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
1959 rb_hash_stlike_foreach(hash, set_zero, hash);
1964 for (
size_t i = 0; i <=
T_MASK; i++) {
1973 #define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
1975 #define STACK_START (ec->machine.stack_start)
1976 #define STACK_END (ec->machine.stack_end)
1977 #define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
1979 #if STACK_GROW_DIRECTION < 0
1980 # define STACK_LENGTH (size_t)(STACK_START - STACK_END)
1981 #elif STACK_GROW_DIRECTION > 0
1982 # define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
1984 # define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
1985 : (size_t)(STACK_END - STACK_START + 1))
1987 #if !STACK_GROW_DIRECTION
1988 int ruby_stack_grow_direction;
1990 ruby_get_stack_grow_direction(
volatile VALUE *addr)
1993 SET_MACHINE_STACK_END(&end);
1995 if (end > addr)
return ruby_stack_grow_direction = 1;
1996 return ruby_stack_grow_direction = -1;
2005 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2006 return STACK_LENGTH;
2009 #define PREVENT_STACK_OVERFLOW 1
2010 #ifndef PREVENT_STACK_OVERFLOW
2011 #if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2012 # define PREVENT_STACK_OVERFLOW 1
2014 # define PREVENT_STACK_OVERFLOW 0
2017 #if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2023 size_t length = STACK_LENGTH;
2024 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2026 return length > maximum_length;
2029 #define stack_check(ec, water_mark) FALSE
2032 #define STACKFRAME_FOR_CALL_CFUNC 2048
2037 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2043 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2048 #define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2049 if (!RB_SPECIAL_CONST_P(obj)) { \
2050 rb_vm_t *vm = GET_VM(); \
2051 void *objspace = vm->gc.objspace; \
2052 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2053 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2054 (func)(objspace, (obj_or_ptr)); \
2056 else if (check_obj ? \
2057 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2058 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2060 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2061 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2062 vm->gc.mark_func_data = NULL; \
2063 mark_func_data->mark_func((obj), mark_func_data->data); \
2064 vm->gc.mark_func_data = mark_func_data; \
2070 gc_mark_internal(
VALUE obj)
2072 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj,
false);
2078 gc_mark_internal(obj);
2084 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move,
ptr, *
ptr,
false);
2088 gc_mark_and_pin_internal(
VALUE obj)
2090 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj,
false);
2096 gc_mark_and_pin_internal(obj);
2100 gc_mark_maybe_internal(
VALUE obj)
2102 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj,
true);
2108 gc_mark_maybe_internal(obj);
2117 void *objspace = vm->gc.objspace;
2118 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2119 GC_ASSERT(rb_gc_impl_during_gc_p(objspace));
2121 rb_gc_impl_mark_weak(objspace,
ptr);
2124 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace));
2131 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj,
ptr);
2134 ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(
static void each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data));
2136 each_location(
register const VALUE *x,
register long n,
void (*cb)(
VALUE,
void *),
void *data)
2147 each_location_ptr(
const VALUE *start,
const VALUE *end,
void (*cb)(
VALUE,
void *),
void *data)
2149 if (end <= start)
return;
2150 each_location(start, end - start, cb, data);
2154 gc_mark_maybe_each_location(
VALUE obj,
void *data)
2156 gc_mark_maybe_internal(obj);
2162 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2166 rb_gc_mark_values(
long n,
const VALUE *values)
2168 for (
long i = 0; i < n; i++) {
2169 gc_mark_internal(values[i]);
2174 rb_gc_mark_vm_stack_values(
long n,
const VALUE *values)
2176 for (
long i = 0; i < n; i++) {
2177 gc_mark_and_pin_internal(values[i]);
2182 mark_key(st_data_t key, st_data_t value, st_data_t data)
2184 gc_mark_and_pin_internal((
VALUE)key);
2194 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2198 mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2200 gc_mark_internal((
VALUE)key);
2201 gc_mark_internal((
VALUE)value);
2207 pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2209 gc_mark_and_pin_internal((
VALUE)key);
2210 gc_mark_and_pin_internal((
VALUE)value);
2216 pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2218 gc_mark_and_pin_internal((
VALUE)key);
2219 gc_mark_internal((
VALUE)value);
2225 mark_hash(
VALUE hash)
2227 if (rb_hash_compare_by_id_p(hash)) {
2228 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2231 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2234 gc_mark_internal(RHASH(hash)->ifnone);
2245 static enum rb_id_table_iterator_result
2246 mark_method_entry_i(
VALUE me,
void *objspace)
2248 gc_mark_internal(me);
2250 return ID_TABLE_CONTINUE;
2254 mark_m_tbl(
void *objspace,
struct rb_id_table *tbl)
2257 rb_id_table_foreach_values(tbl, mark_method_entry_i, objspace);
2261 #if STACK_GROW_DIRECTION < 0
2262 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2263 #elif STACK_GROW_DIRECTION > 0
2264 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2266 #define GET_STACK_BOUNDS(start, end, appendix) \
2267 ((STACK_END < STACK_START) ? \
2268 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2272 gc_mark_machine_stack_location_maybe(
VALUE obj,
void *data)
2274 gc_mark_maybe_internal(obj);
2276 #ifdef RUBY_ASAN_ENABLED
2278 void *fake_frame_start;
2279 void *fake_frame_end;
2280 bool is_fake_frame = asan_get_fake_stack_extents(
2281 ec->machine.asan_fake_stack_handle, obj,
2282 ec->machine.stack_start, ec->machine.stack_end,
2283 &fake_frame_start, &fake_frame_end
2285 if (is_fake_frame) {
2286 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2291 #if defined(__wasm__)
2294 static VALUE *rb_stack_range_tmp[2];
2297 rb_mark_locations(
void *begin,
void *end)
2299 rb_stack_range_tmp[0] = begin;
2300 rb_stack_range_tmp[1] = end;
2303 # if defined(__EMSCRIPTEN__)
2308 emscripten_scan_stack(rb_mark_locations);
2309 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2311 emscripten_scan_registers(rb_mark_locations);
2312 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2319 VALUE *stack_start, *stack_end;
2321 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2322 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2324 rb_wasm_scan_locals(rb_mark_locations);
2325 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2337 VALUE v[
sizeof(rb_jmp_buf) / (
sizeof(
VALUE))];
2338 } save_regs_gc_mark;
2339 VALUE *stack_start, *stack_end;
2341 FLUSH_REGISTER_WINDOWS;
2342 memset(&save_regs_gc_mark, 0,
sizeof(save_regs_gc_mark));
2344 rb_setjmp(save_regs_gc_mark.j);
2350 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2353 #ifdef RUBY_ASAN_ENABLED
2359 each_location(save_regs_gc_mark.v, numberof(save_regs_gc_mark.v), gc_mark_machine_stack_location_maybe, data);
2360 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2367 VALUE *stack_start, *stack_end;
2369 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2370 RUBY_DEBUG_LOG(
"ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2373 #ifdef RUBY_ASAN_ENABLED
2380 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2381 int num_regs =
sizeof(ec->machine.regs)/(
sizeof(
VALUE));
2382 each_location((
VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2386 rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2388 gc_mark_and_pin_internal((
VALUE)value);
2396 if (!tbl || tbl->num_entries == 0)
return;
2404 if (!tbl || tbl->num_entries == 0)
return;
2412 gc_mark_tbl_no_pin(tbl);
2415 static enum rb_id_table_iterator_result
2416 mark_cvc_tbl_i(
VALUE cvc_entry,
void *objspace)
2423 gc_mark_internal((
VALUE)entry->cref);
2425 return ID_TABLE_CONTINUE;
2429 mark_cvc_tbl(
void *objspace,
VALUE klass)
2433 rb_id_table_foreach_values(tbl, mark_cvc_tbl_i, objspace);
2440 return (
type->flags & RUBY_TYPED_DECL_MARKING) != 0;
2443 static enum rb_id_table_iterator_result
2444 mark_const_table_i(
VALUE value,
void *objspace)
2448 gc_mark_internal(ce->value);
2449 gc_mark_internal(ce->file);
2451 return ID_TABLE_CONTINUE;
2455 rb_gc_mark_roots(
void *objspace,
const char **categoryp)
2458 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2460 #define MARK_CHECKPOINT(category) do { \
2461 if (categoryp) *categoryp = category; \
2464 MARK_CHECKPOINT(
"vm");
2466 if (vm->self) gc_mark_internal(vm->self);
2468 MARK_CHECKPOINT(
"machine_context");
2469 mark_current_machine_context(ec);
2471 MARK_CHECKPOINT(
"end_proc");
2474 MARK_CHECKPOINT(
"global_tbl");
2475 rb_gc_mark_global_tbl();
2478 void rb_yjit_root_mark(
void);
2480 if (rb_yjit_enabled_p) {
2481 MARK_CHECKPOINT(
"YJIT");
2482 rb_yjit_root_mark();
2486 MARK_CHECKPOINT(
"finish");
2487 #undef MARK_CHECKPOINT
2490 #define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA(d)->type->function.dmark
2493 rb_gc_mark_children(
void *objspace,
VALUE obj)
2496 rb_mark_generic_ivar(obj);
2511 rb_bug(
"rb_gc_mark() called for broken object");
2519 rb_imemo_mark_and_move(obj,
false);
2526 gc_mark_internal(
RBASIC(obj)->klass);
2531 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
2539 mark_m_tbl(objspace, RCLASS_M_TBL(obj));
2540 mark_cvc_tbl(objspace, obj);
2541 rb_cc_table_mark(obj);
2542 if (rb_shape_obj_too_complex(obj)) {
2543 gc_mark_tbl_no_pin((
st_table *)RCLASS_IVPTR(obj));
2546 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
2547 gc_mark_internal(RCLASS_IVPTR(obj)[i]);
2551 if (RCLASS_CONST_TBL(obj)) {
2552 rb_id_table_foreach_values(RCLASS_CONST_TBL(obj), mark_const_table_i, objspace);
2555 gc_mark_internal(RCLASS_EXT(obj)->classpath);
2559 if (RICLASS_OWNS_M_TBL_P(obj)) {
2560 mark_m_tbl(objspace, RCLASS_M_TBL(obj));
2566 if (RCLASS_INCLUDER(obj)) {
2567 gc_mark_internal(RCLASS_INCLUDER(obj));
2569 mark_m_tbl(objspace, RCLASS_CALLABLE_M_TBL(obj));
2570 rb_cc_table_mark(obj);
2574 if (ARY_SHARED_P(obj)) {
2575 VALUE root = ARY_SHARED_ROOT(obj);
2576 gc_mark_internal(root);
2581 for (
long i = 0; i <
len; i++) {
2582 gc_mark_internal(
ptr[i]);
2592 if (STR_SHARED_P(obj)) {
2593 if (STR_EMBED_P(
RSTRING(obj)->as.heap.aux.shared)) {
2598 gc_mark_and_pin_internal(
RSTRING(obj)->as.heap.aux.shared);
2601 gc_mark_internal(
RSTRING(obj)->as.heap.aux.shared);
2611 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
2613 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
2614 gc_mark_internal(*(
VALUE *)((
char *)
ptr + offset));
2621 if (mark_func) (*mark_func)(
ptr);
2629 rb_shape_t *shape = rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj));
2631 if (rb_shape_obj_too_complex(obj)) {
2632 gc_mark_tbl_no_pin(ROBJECT_IV_HASH(obj));
2637 uint32_t
len = ROBJECT_IV_COUNT(obj);
2638 for (uint32_t i = 0; i <
len; i++) {
2639 gc_mark_internal(
ptr[i]);
2647 attr_index_t num_of_ivs = shape->next_iv_index;
2648 if (RCLASS_EXT(klass)->max_iv_count < num_of_ivs) {
2649 RCLASS_EXT(klass)->max_iv_count = num_of_ivs;
2657 if (
RFILE(obj)->fptr) {
2658 gc_mark_internal(
RFILE(obj)->fptr->self);
2659 gc_mark_internal(
RFILE(obj)->fptr->pathv);
2660 gc_mark_internal(
RFILE(obj)->fptr->tied_io_for_writing);
2661 gc_mark_internal(
RFILE(obj)->fptr->writeconv_asciicompat);
2662 gc_mark_internal(
RFILE(obj)->fptr->writeconv_pre_ecopts);
2663 gc_mark_internal(
RFILE(obj)->fptr->encs.ecopts);
2664 gc_mark_internal(
RFILE(obj)->fptr->write_lock);
2665 gc_mark_internal(
RFILE(obj)->fptr->timeout);
2670 gc_mark_internal(
RREGEXP(obj)->src);
2674 gc_mark_internal(
RMATCH(obj)->regexp);
2676 gc_mark_internal(
RMATCH(obj)->str);
2681 gc_mark_internal(RRATIONAL(obj)->num);
2682 gc_mark_internal(RRATIONAL(obj)->den);
2686 gc_mark_internal(RCOMPLEX(obj)->real);
2687 gc_mark_internal(RCOMPLEX(obj)->imag);
2692 const VALUE *
const ptr = RSTRUCT_CONST_PTR(obj);
2694 for (
long i = 0; i <
len; i++) {
2695 gc_mark_internal(
ptr[i]);
2705 rb_bug(
"rb_gc_mark(): unknown data type 0x%x(%p) %s",
2707 rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj) ?
"corrupted object" :
"non object");
2712 rb_gc_obj_optimal_size(
VALUE obj)
2716 return rb_ary_size_as_embedded(obj);
2719 if (rb_shape_obj_too_complex(obj)) {
2720 return sizeof(
struct RObject);
2727 return rb_str_size_as_embedded(obj);
2730 return sizeof(
struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(
st_table) : sizeof(ar_table));
2740 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
2746 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
2753 rb_gc_writebarrier_remember(
VALUE obj)
2755 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
2761 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
2766 rb_obj_gc_flags(
VALUE obj,
ID* flags,
size_t max)
2768 return rb_gc_impl_obj_flags(rb_gc_get_objspace(), obj, flags, max);
2774 rb_gc_ractor_cache_alloc(
void)
2776 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace());
2780 rb_gc_ractor_cache_free(
void *cache)
2782 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
2788 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj))
2791 rb_vm_register_global_object(obj);
2802 tmp->next = vm->global_object_list;
2804 vm->global_object_list = tmp;
2812 rb_warn(
"Object is assigned to registering address already: %"PRIsVALUE,
2814 rb_print_backtrace(stderr);
2824 if (tmp->varptr == addr) {
2825 vm->global_object_list = tmp->next;
2830 if (tmp->next->varptr == addr) {
2833 tmp->next = tmp->next->next;
2850 rb_gc_impl_start(rb_gc_get_objspace(),
RTEST(full_mark),
RTEST(immediate_mark),
RTEST(immediate_sweep),
RTEST(compact));
2894 rb_objspace_each_objects(
int (*callback)(
void *,
void *,
size_t,
void *),
void *data)
2896 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
2900 gc_ref_update_array(
void *objspace,
VALUE v)
2902 if (ARY_SHARED_P(v)) {
2903 VALUE old_root =
RARRAY(v)->as.heap.aux.shared_root;
2905 UPDATE_IF_MOVED(objspace,
RARRAY(v)->as.heap.aux.shared_root);
2907 VALUE new_root =
RARRAY(v)->as.heap.aux.shared_root;
2909 if (ARY_EMBED_P(new_root) && new_root != old_root) {
2910 size_t offset = (size_t)(
RARRAY(v)->as.heap.ptr -
RARRAY(old_root)->as.ary);
2911 GC_ASSERT(
RARRAY(v)->as.heap.ptr >=
RARRAY(old_root)->as.ary);
2912 RARRAY(v)->as.heap.ptr =
RARRAY(new_root)->as.ary + offset;
2920 for (
long i = 0; i <
len; i++) {
2921 UPDATE_IF_MOVED(objspace,
ptr[i]);
2925 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
2926 if (rb_ary_embeddable_p(v)) {
2927 rb_ary_make_embedded(v);
2934 gc_ref_update_object(
void *objspace,
VALUE v)
2938 if (rb_shape_obj_too_complex(v)) {
2939 gc_ref_update_table_values_only(ROBJECT_IV_HASH(v));
2943 size_t slot_size = rb_gc_obj_slot_size(v);
2945 if (slot_size >= embed_size && !
RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
2953 for (uint32_t i = 0; i < ROBJECT_IV_COUNT(v); i++) {
2954 UPDATE_IF_MOVED(objspace,
ptr[i]);
2959 rb_gc_ref_update_table_values_only(
st_table *tbl)
2961 gc_ref_update_table_values_only(tbl);
2968 gc_update_table_refs(
ptr);
2972 gc_ref_update_hash(
void *objspace,
VALUE v)
2974 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)objspace);
2978 gc_update_values(
void *objspace,
long n,
VALUE *values)
2980 for (
long i = 0; i < n; i++) {
2981 UPDATE_IF_MOVED(objspace, values[i]);
2986 rb_gc_update_values(
long n,
VALUE *values)
2988 gc_update_values(rb_gc_get_objspace(), n, values);
2991 static enum rb_id_table_iterator_result
2992 check_id_table_move(
VALUE value,
void *data)
2994 void *objspace = (
void *)data;
2996 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)value)) {
2997 return ID_TABLE_REPLACE;
3000 return ID_TABLE_CONTINUE;
3006 return rb_gc_impl_location(rb_gc_get_objspace(), value);
3010 rb_gc_prepare_heap_process_object(
VALUE obj)
3024 rb_gc_prepare_heap(
void)
3026 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3030 rb_gc_heap_id_for_size(
size_t size)
3032 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3036 rb_gc_size_allocatable_p(
size_t size)
3038 return rb_gc_impl_size_allocatable_p(size);
3041 static enum rb_id_table_iterator_result
3042 update_id_table(
VALUE *value,
void *data,
int existing)
3044 void *objspace = (
void *)data;
3046 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)*value)) {
3047 *value = rb_gc_impl_location(objspace, (
VALUE)*value);
3050 return ID_TABLE_CONTINUE;
3054 update_m_tbl(
void *objspace,
struct rb_id_table *tbl)
3057 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table, objspace);
3061 static enum rb_id_table_iterator_result
3062 update_cc_tbl_i(
VALUE ccs_ptr,
void *objspace)
3065 VM_ASSERT(vm_ccs_p(ccs));
3067 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)ccs->cme)) {
3071 for (
int i=0; i<ccs->len; i++) {
3072 if (rb_gc_impl_object_moved_p(objspace, (
VALUE)ccs->entries[i].cc)) {
3078 return ID_TABLE_CONTINUE;
3082 update_cc_tbl(
void *objspace,
VALUE klass)
3086 rb_id_table_foreach_values(tbl, update_cc_tbl_i, objspace);
3090 static enum rb_id_table_iterator_result
3091 update_cvc_tbl_i(
VALUE cvc_entry,
void *objspace)
3098 TYPED_UPDATE_IF_MOVED(objspace,
rb_cref_t *, entry->cref);
3101 entry->class_value = rb_gc_impl_location(objspace, entry->class_value);
3103 return ID_TABLE_CONTINUE;
3107 update_cvc_tbl(
void *objspace,
VALUE klass)
3111 rb_id_table_foreach_values(tbl, update_cvc_tbl_i, objspace);
3115 static enum rb_id_table_iterator_result
3116 update_const_table(
VALUE value,
void *objspace)
3120 if (rb_gc_impl_object_moved_p(objspace, ce->value)) {
3121 ce->value = rb_gc_impl_location(objspace, ce->value);
3124 if (rb_gc_impl_object_moved_p(objspace, ce->file)) {
3125 ce->file = rb_gc_impl_location(objspace, ce->file);
3128 return ID_TABLE_CONTINUE;
3132 update_const_tbl(
void *objspace,
struct rb_id_table *tbl)
3135 rb_id_table_foreach_values(tbl, update_const_table, objspace);
3142 UPDATE_IF_MOVED(objspace, entry->klass);
3143 entry = entry->next;
3150 UPDATE_IF_MOVED(objspace, ext->origin_);
3151 UPDATE_IF_MOVED(objspace, ext->includer);
3152 UPDATE_IF_MOVED(objspace, ext->refined_class);
3153 update_subclass_entries(objspace, ext->subclasses);
3157 update_superclasses(
void *objspace,
VALUE obj)
3159 if (
FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
3160 for (
size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj) + 1; i++) {
3161 UPDATE_IF_MOVED(objspace, RCLASS_SUPERCLASSES(obj)[i]);
3167 #define global_symbols ruby_global_symbols
3170 rb_gc_update_vm_references(
void *objspace)
3173 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3175 rb_vm_update_references(vm);
3176 rb_gc_update_global_tbl();
3177 global_symbols.ids = rb_gc_impl_location(objspace, global_symbols.ids);
3178 global_symbols.dsymbol_fstr_hash = rb_gc_impl_location(objspace, global_symbols.dsymbol_fstr_hash);
3179 gc_update_table_refs(global_symbols.str_sym);
3182 void rb_yjit_root_update_references(
void);
3184 if (rb_yjit_enabled_p) {
3185 rb_yjit_root_update_references();
3191 rb_gc_update_object_references(
void *objspace,
VALUE obj)
3194 rb_ref_update_generic_ivar(obj);
3200 UPDATE_IF_MOVED(objspace, RCLASS_ATTACHED_OBJECT(obj));
3205 UPDATE_IF_MOVED(objspace,
RCLASS(obj)->super);
3207 update_m_tbl(objspace, RCLASS_M_TBL(obj));
3208 update_cc_tbl(objspace, obj);
3209 update_cvc_tbl(objspace, obj);
3210 update_superclasses(objspace, obj);
3212 if (rb_shape_obj_too_complex(obj)) {
3213 gc_ref_update_table_values_only(RCLASS_IV_HASH(obj));
3216 for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
3217 UPDATE_IF_MOVED(objspace, RCLASS_IVPTR(obj)[i]);
3221 update_class_ext(objspace, RCLASS_EXT(obj));
3222 update_const_tbl(objspace, RCLASS_CONST_TBL(obj));
3224 UPDATE_IF_MOVED(objspace, RCLASS_EXT(obj)->classpath);
3228 if (RICLASS_OWNS_M_TBL_P(obj)) {
3229 update_m_tbl(objspace, RCLASS_M_TBL(obj));
3232 UPDATE_IF_MOVED(objspace,
RCLASS(obj)->super);
3234 update_class_ext(objspace, RCLASS_EXT(obj));
3235 update_m_tbl(objspace, RCLASS_CALLABLE_M_TBL(obj));
3236 update_cc_tbl(objspace, obj);
3240 rb_imemo_mark_and_move(obj,
true);
3252 gc_ref_update_array(objspace, obj);
3256 gc_ref_update_hash(objspace, obj);
3257 UPDATE_IF_MOVED(objspace, RHASH(obj)->ifnone);
3262 if (STR_SHARED_P(obj)) {
3263 UPDATE_IF_MOVED(objspace,
RSTRING(obj)->as.heap.aux.shared);
3268 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
3269 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
3270 rb_str_make_embedded(obj);
3282 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3284 for (
size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3287 *ref = rb_gc_impl_location(objspace, *ref);
3292 if (compact_func) (*compact_func)(
ptr);
3299 gc_ref_update_object(objspace, obj);
3303 if (
RFILE(obj)->fptr) {
3304 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->self);
3305 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->pathv);
3306 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->tied_io_for_writing);
3307 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->writeconv_asciicompat);
3308 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->writeconv_pre_ecopts);
3309 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->encs.ecopts);
3310 UPDATE_IF_MOVED(objspace,
RFILE(obj)->fptr->write_lock);
3314 UPDATE_IF_MOVED(objspace,
RREGEXP(obj)->src);
3318 UPDATE_IF_MOVED(objspace, RSYMBOL(obj)->fstr);
3326 UPDATE_IF_MOVED(objspace,
RMATCH(obj)->regexp);
3329 UPDATE_IF_MOVED(objspace,
RMATCH(obj)->str);
3334 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->num);
3335 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->den);
3339 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->real);
3340 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->imag);
3349 for (i = 0; i <
len; i++) {
3350 UPDATE_IF_MOVED(objspace,
ptr[i]);
3359 UPDATE_IF_MOVED(objspace,
RBASIC(obj)->klass);
3372 unless_objspace(objspace) {
return; }
3374 rb_gc_impl_start(objspace,
true,
true,
true,
false);
3380 unless_objspace(objspace) {
return FALSE; }
3382 return rb_gc_impl_during_gc_p(objspace);
3388 return rb_gc_impl_gc_count(rb_gc_get_objspace());
3404 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
3423 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3441 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3464 if (
NIL_P(heap_name)) {
3478 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
3492 return rb_gc_impl_config_get(rb_gc_get_objspace());
3498 void *objspace = rb_gc_get_objspace();
3500 rb_gc_impl_config_set(objspace, hash);
3502 return rb_gc_impl_config_get(objspace);
3508 return rb_gc_impl_stress_get(rb_gc_get_objspace());
3514 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
3520 rb_gc_initial_stress_set(
VALUE flag)
3522 initial_stress = flag;
3526 rb_gc_heap_sizes(
void)
3528 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
3534 return rb_objspace_gc_enable(rb_gc_get_objspace());
3538 rb_objspace_gc_enable(
void *objspace)
3540 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3541 rb_gc_impl_gc_enable(objspace);
3542 return RBOOL(disabled);
3552 gc_disable_no_rest(
void *objspace)
3554 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3555 rb_gc_impl_gc_disable(objspace,
false);
3556 return RBOOL(disabled);
3560 rb_gc_disable_no_rest(
void)
3562 return gc_disable_no_rest(rb_gc_get_objspace());
3568 return rb_objspace_gc_disable(rb_gc_get_objspace());
3572 rb_objspace_gc_disable(
void *objspace)
3574 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3575 rb_gc_impl_gc_disable(objspace,
true);
3576 return RBOOL(disabled);
3589 ruby_gc_set_params(
void)
3591 rb_gc_impl_set_params(rb_gc_get_objspace());
3595 rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data)
3599 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace()))
rb_bug(
"rb_objspace_reachable_objects_from() is not supported while during GC");
3603 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3604 struct gc_mark_func_data_struct mfd = {
3609 vm->gc.mark_func_data = &mfd;
3610 rb_gc_mark_children(rb_gc_get_objspace(), obj);
3611 vm->gc.mark_func_data = prev_mfd;
3618 const char *category;
3619 void (*func)(
const char *category,
VALUE,
void *);
3624 root_objects_from(
VALUE obj,
void *
ptr)
3627 (*data->func)(data->category, obj, data->data);
3631 rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *passing_data)
3633 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");
3639 .data = passing_data,
3642 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3643 struct gc_mark_func_data_struct mfd = {
3644 .mark_func = root_objects_from,
3648 vm->gc.mark_func_data = &mfd;
3649 rb_gc_mark_roots(rb_gc_get_objspace(), &data.category);
3650 vm->gc.mark_func_data = prev_mfd;
3661 #define TYPE_NAME(t) case (t): return #t;
3688 if (obj && rb_objspace_data_type_name(obj)) {
3689 return rb_objspace_data_type_name(obj);
3698 obj_type_name(
VALUE obj)
3700 return type_name(
TYPE(obj), obj);
3704 rb_method_type_name(rb_method_type_t
type)
3707 case VM_METHOD_TYPE_ISEQ:
return "iseq";
3708 case VM_METHOD_TYPE_ATTRSET:
return "attrest";
3709 case VM_METHOD_TYPE_IVAR:
return "ivar";
3710 case VM_METHOD_TYPE_BMETHOD:
return "bmethod";
3711 case VM_METHOD_TYPE_ALIAS:
return "alias";
3712 case VM_METHOD_TYPE_REFINED:
return "refined";
3713 case VM_METHOD_TYPE_CFUNC:
return "cfunc";
3714 case VM_METHOD_TYPE_ZSUPER:
return "zsuper";
3715 case VM_METHOD_TYPE_MISSING:
return "missing";
3716 case VM_METHOD_TYPE_OPTIMIZED:
return "optimized";
3717 case VM_METHOD_TYPE_UNDEF:
return "undef";
3718 case VM_METHOD_TYPE_NOTIMPLEMENTED:
return "notimplemented";
3720 rb_bug(
"rb_method_type_name: unreachable (type: %d)",
type);
3724 rb_raw_iseq_info(
char *
const buff,
const size_t buff_size,
const rb_iseq_t *iseq)
3726 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !
RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj,
T_MOVED)) {
3727 VALUE path = rb_iseq_path(iseq);
3728 int n = ISEQ_BODY(iseq)->location.first_lineno;
3729 snprintf(buff, buff_size,
" %s@%s:%d",
3736 str_len_no_raise(
VALUE str)
3739 if (
len < 0)
return 0;
3740 if (
len > INT_MAX)
return INT_MAX;
3744 #define BUFF_ARGS buff + pos, buff_size - pos
3745 #define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
3746 #define APPEND_S(s) do { \
3747 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
3751 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
3754 #define C(c, s) ((c) != 0 ? (s) : " ")
3757 rb_raw_obj_info_common(
char *
const buff,
const size_t buff_size,
const VALUE obj)
3762 APPEND_F(
"%s", obj_type_name(obj));
3774 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (
void *)obj)) {
3793 if (internal_object_p(obj)) {
3796 else if (
RBASIC(obj)->klass == 0) {
3797 APPEND_S(
"(temporary internal)");
3801 if (!
NIL_P(class_path)) {
3811 const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
3814 rb_raw_obj_info_buitin_type(
char *
const buff,
const size_t buff_size,
const VALUE obj,
size_t pos)
3821 UNEXPECTED_NODE(rb_raw_obj_info);
3824 if (ARY_SHARED_P(obj)) {
3825 APPEND_S(
"shared -> ");
3826 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
3828 else if (ARY_EMBED_P(obj)) {
3829 APPEND_F(
"[%s%s] len: %ld (embed)",
3830 C(ARY_EMBED_P(obj),
"E"),
3831 C(ARY_SHARED_P(obj),
"S"),
3835 APPEND_F(
"[%s%s] len: %ld, capa:%ld ptr:%p",
3836 C(ARY_EMBED_P(obj),
"E"),
3837 C(ARY_SHARED_P(obj),
"S"),
3839 ARY_EMBED_P(obj) ? -1L :
RARRAY(obj)->as.heap.aux.capa,
3844 if (STR_SHARED_P(obj)) {
3848 if (STR_EMBED_P(obj)) APPEND_S(
" [embed]");
3852 APPEND_F(
" \"%.*s\"", str_len_no_raise(obj),
RSTRING_PTR(obj));
3856 VALUE fstr = RSYMBOL(obj)->fstr;
3857 ID id = RSYMBOL(obj)->id;
3859 APPEND_F(
":%s id:%d",
RSTRING_PTR(fstr), (
unsigned int)
id);
3862 APPEND_F(
"(%p) id:%d", (
void *)fstr, (
unsigned int)
id);
3867 APPEND_F(
"-> %p", (
void*)rb_gc_impl_location(rb_gc_get_objspace(), obj));
3871 APPEND_F(
"[%c] %"PRIdSIZE,
3872 RHASH_AR_TABLE_P(obj) ?
'A' :
'S',
3880 if (!
NIL_P(class_path)) {
3891 if (!
NIL_P(class_path)) {
3898 if (rb_shape_obj_too_complex(obj)) {
3899 size_t hash_len = rb_st_table_size(ROBJECT_IV_HASH(obj));
3900 APPEND_F(
"(too_complex) len:%zu", hash_len);
3903 uint32_t
len = ROBJECT_IV_CAPACITY(obj);
3905 if (
RBASIC(obj)->flags & ROBJECT_EMBED) {
3906 APPEND_F(
"(embed) len:%d",
len);
3910 APPEND_F(
"len:%d ptr:%p",
len, (
void *)
ptr);
3919 (block = vm_proc_block(obj)) != NULL &&
3920 (vm_block_type(block) == block_type_iseq) &&
3921 (iseq = vm_block_iseq(block)) != NULL) {
3922 rb_raw_iseq_info(BUFF_ARGS, iseq);
3924 else if (rb_ractor_p(obj)) {
3927 APPEND_F(
"r:%d", r->pub.id);
3931 const char *
const type_name = rb_objspace_data_type_name(obj);
3933 APPEND_F(
"%s", type_name);
3939 APPEND_F(
"<%s> ", rb_imemo_name(imemo_type(obj)));
3941 switch (imemo_type(obj)) {
3946 APPEND_F(
":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
3948 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ?
"pub" :
3949 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ?
"pri" :
"pro",
3950 METHOD_ENTRY_COMPLEMENTED(me) ?
",cmp" :
"",
3951 METHOD_ENTRY_CACHED(me) ?
",cc" :
"",
3952 METHOD_ENTRY_INVALIDATED(me) ?
",inv" :
"",
3953 me->def ? rb_method_type_name(me->def->type) :
"NULL",
3954 me->def ? me->def->aliased : -1,
3956 (
void *)me->defined_class);
3959 switch (me->def->type) {
3960 case VM_METHOD_TYPE_ISEQ:
3961 APPEND_S(
" (iseq:");
3962 rb_raw_obj_info(BUFF_ARGS, (
VALUE)me->def->body.iseq.
iseqptr);
3974 rb_raw_iseq_info(BUFF_ARGS, iseq);
3977 case imemo_callinfo:
3980 APPEND_F(
"(mid:%s, flag:%x argc:%d, kwarg:%s)",
3984 vm_ci_kwarg(ci) ?
"available" :
"NULL");
3987 case imemo_callcache:
3993 APPEND_F(
"(klass:%s cme:%s%s (%p) call:%p",
3994 NIL_P(class_path) ? (cc->klass ?
"??" :
"<NULL>") :
RSTRING_PTR(class_path),
3996 cme ? (METHOD_ENTRY_INVALIDATED(cme) ?
" [inv]" :
"") :
"",
3998 (
void *)(uintptr_t)vm_cc_call(cc));
4016 #define asan_unpoisoning_object(obj) \
4017 for (void *poisoned = asan_unpoison_object_temporary(obj), \
4018 *unpoisoning = &poisoned; \
4020 unpoisoning = asan_poison_object_restore(obj, poisoned))
4023 rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj)
4025 asan_unpoisoning_object(obj) {
4026 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4027 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4028 if (pos >= buff_size) {}
4039 #define OBJ_INFO_BUFFERS_NUM 10
4040 #define OBJ_INFO_BUFFERS_SIZE 0x100
4042 static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
4061 rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
4062 char *
const buff = obj_info_buffers[index];
4063 return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
4069 return obj_type_name(obj);
4084 gc_vraise(
void *
ptr)
4087 rb_vraise(argv->exc, argv->fmt, *argv->ap);
4092 gc_raise(
VALUE exc,
const char *fmt, ...)
4100 if (ruby_thread_has_gvl_p()) {
4110 fprintf(stderr,
"%s",
"[FATAL] ");
4111 vfprintf(stderr, fmt, ap);
4118 NORETURN(
static void negative_size_allocation_error(
const char *));
4120 negative_size_allocation_error(
const char *msg)
4126 ruby_memerror_body(
void *dummy)
4132 NORETURN(
static void ruby_memerror(
void));
4137 if (ruby_thread_has_gvl_p()) {
4146 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4156 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
4159 rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4160 fprintf(stderr,
"[FATAL] failed to allocate memory\n");
4163 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4164 rb_ec_raised_clear(ec);
4167 rb_ec_raised_set(ec, RAISED_NOMEMORY);
4168 exc = ruby_vm_special_exception_copy(exc);
4171 EC_JUMP_TAG(ec, TAG_RAISE);
4175 rb_malloc_info_show_results(
void)
4182 if ((ssize_t)size < 0) {
4183 negative_size_allocation_error(
"too large allocation size");
4186 return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
4190 ruby_malloc_size_overflow(
size_t count,
size_t elsize)
4193 "malloc: possible integer overflow (%"PRIuSIZE
"*%"PRIuSIZE
")",
4200 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4206 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4209 #ifdef ruby_sized_xrealloc
4210 #undef ruby_sized_xrealloc
4213 ruby_sized_xrealloc(
void *
ptr,
size_t new_size,
size_t old_size)
4215 if ((ssize_t)new_size < 0) {
4216 negative_size_allocation_error(
"too large allocation size");
4219 return rb_gc_impl_realloc(rb_gc_get_objspace(),
ptr, new_size, old_size);
4225 return ruby_sized_xrealloc(
ptr, new_size, 0);
4228 #ifdef ruby_sized_xrealloc2
4229 #undef ruby_sized_xrealloc2
4232 ruby_sized_xrealloc2(
void *
ptr,
size_t n,
size_t size,
size_t old_n)
4234 size_t len = xmalloc2_size(n, size);
4235 return rb_gc_impl_realloc(rb_gc_get_objspace(),
ptr,
len, old_n * size);
4241 return ruby_sized_xrealloc2(
ptr, n, size, 0);
4244 #ifdef ruby_sized_xfree
4245 #undef ruby_sized_xfree
4248 ruby_sized_xfree(
void *x,
size_t size)
4254 if (LIKELY(GET_VM())) {
4255 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
4266 ruby_sized_xfree(x, 0);
4270 rb_xmalloc_mul_add(
size_t x,
size_t y,
size_t z)
4272 size_t w = size_mul_add_or_raise(x, y, z,
rb_eArgError);
4277 rb_xcalloc_mul_add(
size_t x,
size_t y,
size_t z)
4279 size_t w = size_mul_add_or_raise(x, y, z,
rb_eArgError);
4284 rb_xrealloc_mul_add(
const void *p,
size_t x,
size_t y,
size_t z)
4286 size_t w = size_mul_add_or_raise(x, y, z,
rb_eArgError);
4291 rb_xmalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4293 size_t u = size_mul_add_mul_or_raise(x, y, z, w,
rb_eArgError);
4298 rb_xcalloc_mul_add_mul(
size_t x,
size_t y,
size_t z,
size_t w)
4300 size_t u = size_mul_add_mul_or_raise(x, y, z, w,
rb_eArgError);
4308 ruby_mimmalloc(
size_t size)
4311 #if CALC_EXACT_MALLOC_SIZE
4315 #if CALC_EXACT_MALLOC_SIZE
4331 ruby_mimcalloc(
size_t num,
size_t size)
4334 #if CALC_EXACT_MALLOC_SIZE
4335 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
4336 if (UNLIKELY(t.left)) {
4340 mem = calloc1(size);
4352 mem = calloc(num, size);
4358 ruby_mimfree(
void *
ptr)
4360 #if CALC_EXACT_MALLOC_SIZE
4370 unless_objspace(objspace) {
return; }
4372 rb_gc_impl_adjust_memory_usage(objspace, diff);
4376 rb_obj_info(
VALUE obj)
4378 return obj_info(obj);
4382 rb_obj_info_dump(
VALUE obj)
4385 fprintf(stderr,
"rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
4389 rb_obj_info_dump_loc(
VALUE obj,
const char *file,
int line,
const char *func)
4392 fprintf(stderr,
"<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
4446 malloc_offset = gc_compute_malloc_offset();
4459 rb_vm_register_special_exception(ruby_error_nomemory,
rb_eNoMemError,
"failed to allocate memory");
#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.
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...