Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)
gc.c
1/**********************************************************************
2
3 gc.c -
4
5 $Author$
6 created at: Tue Oct 5 09:44:46 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#define rb_data_object_alloc rb_data_object_alloc
15#define rb_data_typed_object_alloc rb_data_typed_object_alloc
16
17#include "ruby/internal/config.h"
18#ifdef _WIN32
19# include "ruby/ruby.h"
20#endif
21
22#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
23# include "wasm/setjmp.h"
24# include "wasm/machine.h"
25#else
26# include <setjmp.h>
27#endif
28#include <stdarg.h>
29#include <stdio.h>
30
31/* MALLOC_HEADERS_BEGIN */
32#ifndef HAVE_MALLOC_USABLE_SIZE
33# ifdef _WIN32
34# define HAVE_MALLOC_USABLE_SIZE
35# define malloc_usable_size(a) _msize(a)
36# elif defined HAVE_MALLOC_SIZE
37# define HAVE_MALLOC_USABLE_SIZE
38# define malloc_usable_size(a) malloc_size(a)
39# endif
40#endif
41
42#ifdef HAVE_MALLOC_USABLE_SIZE
43# ifdef RUBY_ALTERNATIVE_MALLOC_HEADER
44/* Alternative malloc header is included in ruby/missing.h */
45# elif defined(HAVE_MALLOC_H)
46# include <malloc.h>
47# elif defined(HAVE_MALLOC_NP_H)
48# include <malloc_np.h>
49# elif defined(HAVE_MALLOC_MALLOC_H)
50# include <malloc/malloc.h>
51# endif
52#endif
53
54/* MALLOC_HEADERS_END */
55
56#ifdef HAVE_SYS_TIME_H
57# include <sys/time.h>
58#endif
59
60#ifdef HAVE_SYS_RESOURCE_H
61# include <sys/resource.h>
62#endif
63
64#if defined _WIN32 || defined __CYGWIN__
65# include <windows.h>
66#elif defined(HAVE_POSIX_MEMALIGN)
67#elif defined(HAVE_MEMALIGN)
68# include <malloc.h>
69#endif
70
71#include <sys/types.h>
72
73#ifdef __EMSCRIPTEN__
74#include <emscripten.h>
75#endif
76
77/* For ruby_annotate_mmap */
78#ifdef HAVE_SYS_PRCTL_H
79#include <sys/prctl.h>
80#endif
81
82#undef LIST_HEAD /* ccan/list conflicts with BSD-origin sys/queue.h. */
83
84#include "constant.h"
85#include "darray.h"
86#include "debug_counter.h"
87#include "eval_intern.h"
88#include "gc/gc.h"
89#include "id_table.h"
90#include "internal.h"
91#include "internal/class.h"
92#include "internal/compile.h"
93#include "internal/complex.h"
94#include "internal/concurrent_set.h"
95#include "internal/cont.h"
96#include "internal/error.h"
97#include "internal/eval.h"
98#include "internal/gc.h"
99#include "internal/hash.h"
100#include "internal/imemo.h"
101#include "internal/io.h"
102#include "internal/numeric.h"
103#include "internal/object.h"
104#include "internal/proc.h"
105#include "internal/rational.h"
106#include "internal/sanitizers.h"
107#include "internal/struct.h"
108#include "internal/symbol.h"
109#include "internal/thread.h"
110#include "internal/variable.h"
111#include "internal/warnings.h"
112#include "probes.h"
113#include "regint.h"
114#include "ruby/debug.h"
115#include "ruby/io.h"
116#include "ruby/re.h"
117#include "ruby/st.h"
118#include "ruby/thread.h"
119#include "ruby/util.h"
120#include "ruby/vm.h"
121#include "ruby_assert.h"
122#include "ruby_atomic.h"
123#include "symbol.h"
124#include "variable.h"
125#include "vm_core.h"
126#include "vm_sync.h"
127#include "vm_callinfo.h"
128#include "ractor_core.h"
129#include "yjit.h"
130#include "zjit.h"
131
132#include "builtin.h"
133#include "shape.h"
134
135unsigned int
136rb_gc_vm_lock(const char *file, int line)
137{
138 unsigned int lev = 0;
139 rb_vm_lock_enter(&lev, file, line);
140 return lev;
141}
142
143void
144rb_gc_vm_unlock(unsigned int lev, const char *file, int line)
145{
146 rb_vm_lock_leave(&lev, file, line);
147}
148
149unsigned int
150rb_gc_cr_lock(const char *file, int line)
151{
152 unsigned int lev;
153 rb_vm_lock_enter_cr(GET_RACTOR(), &lev, file, line);
154 return lev;
155}
156
157void
158rb_gc_cr_unlock(unsigned int lev, const char *file, int line)
159{
160 rb_vm_lock_leave_cr(GET_RACTOR(), &lev, file, line);
161}
162
163unsigned int
164rb_gc_vm_lock_no_barrier(const char *file, int line)
165{
166 unsigned int lev = 0;
167 rb_vm_lock_enter_nb(&lev, file, line);
168 return lev;
169}
170
171void
172rb_gc_vm_unlock_no_barrier(unsigned int lev, const char *file, int line)
173{
174 rb_vm_lock_leave_nb(&lev, file, line);
175}
176
177void
178rb_gc_vm_barrier(void)
179{
180 rb_vm_barrier();
181}
182
183void *
184rb_gc_get_ractor_newobj_cache(void)
185{
186 return GET_RACTOR()->newobj_cache;
187}
188
189#if USE_MODULAR_GC
190void
191rb_gc_initialize_vm_context(struct rb_gc_vm_context *context)
192{
193 rb_native_mutex_initialize(&context->lock);
194 context->ec = GET_EC();
195}
196
197void
198rb_gc_worker_thread_set_vm_context(struct rb_gc_vm_context *context)
199{
200 rb_native_mutex_lock(&context->lock);
201
202 GC_ASSERT(rb_current_execution_context(false) == NULL);
203
204#ifdef RB_THREAD_LOCAL_SPECIFIER
205 rb_current_ec_set(context->ec);
206#else
207 native_tls_set(ruby_current_ec_key, context->ec);
208#endif
209}
210
211void
212rb_gc_worker_thread_unset_vm_context(struct rb_gc_vm_context *context)
213{
214 rb_native_mutex_unlock(&context->lock);
215
216 GC_ASSERT(rb_current_execution_context(true) == context->ec);
217
218#ifdef RB_THREAD_LOCAL_SPECIFIER
219 rb_current_ec_set(NULL);
220#else
221 native_tls_set(ruby_current_ec_key, NULL);
222#endif
223}
224#endif
225
226bool
227rb_gc_event_hook_required_p(rb_event_flag_t event)
228{
229 return ruby_vm_event_flags & event;
230}
231
232void
233rb_gc_event_hook(VALUE obj, rb_event_flag_t event)
234{
235 if (LIKELY(!rb_gc_event_hook_required_p(event))) return;
236
237 rb_execution_context_t *ec = GET_EC();
238 if (!ec->cfp) return;
239
240 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
241}
242
243void *
244rb_gc_get_objspace(void)
245{
246 return GET_VM()->gc.objspace;
247}
248
249
250void
251rb_gc_ractor_newobj_cache_foreach(void (*func)(void *cache, void *data), void *data)
252{
253 rb_ractor_t *r = NULL;
254 if (RB_LIKELY(ruby_single_main_ractor)) {
255 GC_ASSERT(
256 ccan_list_empty(&GET_VM()->ractor.set) ||
257 (ccan_list_top(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor &&
258 ccan_list_tail(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor)
259 );
260
261 func(ruby_single_main_ractor->newobj_cache, data);
262 }
263 else {
264 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
265 func(r->newobj_cache, data);
266 }
267 }
268}
269
270void
271rb_gc_run_obj_finalizer(VALUE objid, long count, VALUE (*callback)(long i, void *data), void *data)
272{
273 volatile struct {
274 VALUE errinfo;
275 VALUE final;
276 rb_control_frame_t *cfp;
277 VALUE *sp;
278 long finished;
279 } saved;
280
281 rb_execution_context_t * volatile ec = GET_EC();
282#define RESTORE_FINALIZER() (\
283 ec->cfp = saved.cfp, \
284 ec->cfp->sp = saved.sp, \
285 ec->errinfo = saved.errinfo)
286
287 saved.errinfo = ec->errinfo;
288 saved.cfp = ec->cfp;
289 saved.sp = ec->cfp->sp;
290 saved.finished = 0;
291 saved.final = Qundef;
292
293 ASSERT_vm_unlocking();
294 rb_ractor_ignore_belonging(true);
295 EC_PUSH_TAG(ec);
296 enum ruby_tag_type state = EC_EXEC_TAG();
297 if (state != TAG_NONE) {
298 ++saved.finished; /* skip failed finalizer */
299
300 VALUE failed_final = saved.final;
301 saved.final = Qundef;
302 if (!UNDEF_P(failed_final) && !NIL_P(ruby_verbose)) {
303 rb_warn("Exception in finalizer %+"PRIsVALUE, failed_final);
304 rb_ec_error_print(ec, ec->errinfo);
305 }
306 }
307
308 for (long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
309 saved.final = callback(i, data);
310 rb_check_funcall(saved.final, idCall, 1, &objid);
311 }
312 EC_POP_TAG();
313 rb_ractor_ignore_belonging(false);
314#undef RESTORE_FINALIZER
315}
316
317void
318rb_gc_set_pending_interrupt(void)
319{
320 rb_execution_context_t *ec = GET_EC();
321 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
322}
323
324void
325rb_gc_unset_pending_interrupt(void)
326{
327 rb_execution_context_t *ec = GET_EC();
328 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
329}
330
331bool
332rb_gc_multi_ractor_p(void)
333{
334 return rb_multi_ractor_p();
335}
336
337bool
338rb_gc_shutdown_call_finalizer_p(VALUE obj)
339{
340 switch (BUILTIN_TYPE(obj)) {
341 case T_DATA:
342 if (!ruby_free_at_exit_p() && (!DATA_PTR(obj) || !RDATA(obj)->dfree)) return false;
343 if (rb_obj_is_thread(obj)) return false;
344 if (rb_obj_is_mutex(obj)) return false;
345 if (rb_obj_is_fiber(obj)) return false;
346 if (rb_ractor_p(obj)) return false;
347 if (rb_obj_is_fstring_table(obj)) return false;
348 if (rb_obj_is_symbol_table(obj)) return false;
349
350 return true;
351
352 case T_FILE:
353 return true;
354
355 case T_SYMBOL:
356 return true;
357
358 case T_NONE:
359 return false;
360
361 default:
362 return ruby_free_at_exit_p();
363 }
364}
365
366uint32_t
367rb_gc_get_shape(VALUE obj)
368{
369 return (uint32_t)rb_obj_shape_id(obj);
370}
371
372void
373rb_gc_set_shape(VALUE obj, uint32_t shape_id)
374{
375 RBASIC_SET_SHAPE_ID(obj, (uint32_t)shape_id);
376}
377
378uint32_t
379rb_gc_rebuild_shape(VALUE obj, size_t heap_id)
380{
382
383 return (uint32_t)rb_shape_transition_heap(obj, heap_id);
384}
385
386void rb_vm_update_references(void *ptr);
387
388#define rb_setjmp(env) RUBY_SETJMP(env)
389#define rb_jmp_buf rb_jmpbuf_t
390#undef rb_data_object_wrap
391
392#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
393#define MAP_ANONYMOUS MAP_ANON
394#endif
395
396#define unless_objspace(objspace) \
397 void *objspace; \
398 rb_vm_t *unless_objspace_vm = GET_VM(); \
399 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
400 else /* return; or objspace will be warned uninitialized */
401
402#define RMOVED(obj) ((struct RMoved *)(obj))
403
404#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
405 if (gc_object_moved_p_internal((_objspace), (VALUE)(_thing))) { \
406 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
407 } \
408} while (0)
409
410#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
411
412#if RUBY_MARK_FREE_DEBUG
413int ruby_gc_debug_indent = 0;
414#endif
415
416#ifndef RGENGC_OBJ_INFO
417# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
418#endif
419
420#ifndef CALC_EXACT_MALLOC_SIZE
421# define CALC_EXACT_MALLOC_SIZE 0
422#endif
423
425
426static size_t malloc_offset = 0;
427#if defined(HAVE_MALLOC_USABLE_SIZE)
428static size_t
429gc_compute_malloc_offset(void)
430{
431 // Different allocators use different metadata storage strategies which result in different
432 // ideal sizes.
433 // For instance malloc(64) will waste 8B with glibc, but waste 0B with jemalloc.
434 // But malloc(56) will waste 0B with glibc, but waste 8B with jemalloc.
435 // So we try allocating 64, 56 and 48 bytes and select the first offset that doesn't
436 // waste memory.
437 // This was tested on Linux with glibc 2.35 and jemalloc 5, and for both it result in
438 // no wasted memory.
439 size_t offset = 0;
440 for (offset = 0; offset <= 16; offset += 8) {
441 size_t allocated = (64 - offset);
442 void *test_ptr = malloc(allocated);
443 size_t wasted = malloc_usable_size(test_ptr) - allocated;
444 free(test_ptr);
445
446 if (wasted == 0) {
447 return offset;
448 }
449 }
450 return 0;
451}
452#else
453static size_t
454gc_compute_malloc_offset(void)
455{
456 // If we don't have malloc_usable_size, we use powers of 2.
457 return 0;
458}
459#endif
460
461size_t
462rb_malloc_grow_capa(size_t current, size_t type_size)
463{
464 size_t current_capacity = current;
465 if (current_capacity < 4) {
466 current_capacity = 4;
467 }
468 current_capacity *= type_size;
469
470 // We double the current capacity.
471 size_t new_capacity = (current_capacity * 2);
472
473 // And round up to the next power of 2 if it's not already one.
474 if (rb_popcount64(new_capacity) != 1) {
475 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
476 }
477
478 new_capacity -= malloc_offset;
479 new_capacity /= type_size;
480 if (current > new_capacity) {
481 rb_bug("rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
482 }
483 RUBY_ASSERT(new_capacity > current);
484 return new_capacity;
485}
486
487static inline struct rbimpl_size_overflow_tag
488size_mul_add_overflow(size_t x, size_t y, size_t z) /* x * y + z */
489{
490 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
491 struct rbimpl_size_overflow_tag u = rbimpl_size_add_overflow(t.result, z);
492 return (struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed, u.result };
493}
494
495static inline struct rbimpl_size_overflow_tag
496size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
497{
498 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
499 struct rbimpl_size_overflow_tag u = rbimpl_size_mul_overflow(z, w);
500 struct rbimpl_size_overflow_tag v = rbimpl_size_add_overflow(t.result, u.result);
501 return (struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed || v.overflowed, v.result };
502}
503
504PRINTF_ARGS(NORETURN(static void gc_raise(VALUE, const char*, ...)), 2, 3);
505
506static inline size_t
507size_mul_or_raise(size_t x, size_t y, VALUE exc)
508{
509 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
510 if (LIKELY(!t.overflowed)) {
511 return t.result;
512 }
513 else if (rb_during_gc()) {
514 rb_memerror(); /* or...? */
515 }
516 else {
517 gc_raise(
518 exc,
519 "integer overflow: %"PRIuSIZE
520 " * %"PRIuSIZE
521 " > %"PRIuSIZE,
522 x, y, (size_t)SIZE_MAX);
523 }
524}
525
526size_t
527rb_size_mul_or_raise(size_t x, size_t y, VALUE exc)
528{
529 return size_mul_or_raise(x, y, exc);
530}
531
532static inline size_t
533size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
534{
535 struct rbimpl_size_overflow_tag t = size_mul_add_overflow(x, y, z);
536 if (LIKELY(!t.overflowed)) {
537 return t.result;
538 }
539 else if (rb_during_gc()) {
540 rb_memerror(); /* or...? */
541 }
542 else {
543 gc_raise(
544 exc,
545 "integer overflow: %"PRIuSIZE
546 " * %"PRIuSIZE
547 " + %"PRIuSIZE
548 " > %"PRIuSIZE,
549 x, y, z, (size_t)SIZE_MAX);
550 }
551}
552
553size_t
554rb_size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
555{
556 return size_mul_add_or_raise(x, y, z, exc);
557}
558
559static inline size_t
560size_mul_add_mul_or_raise(size_t x, size_t y, size_t z, size_t w, VALUE exc)
561{
562 struct rbimpl_size_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
563 if (LIKELY(!t.overflowed)) {
564 return t.result;
565 }
566 else if (rb_during_gc()) {
567 rb_memerror(); /* or...? */
568 }
569 else {
570 gc_raise(
571 exc,
572 "integer overflow: %"PRIdSIZE
573 " * %"PRIdSIZE
574 " + %"PRIdSIZE
575 " * %"PRIdSIZE
576 " > %"PRIdSIZE,
577 x, y, z, w, (size_t)SIZE_MAX);
578 }
579}
580
581#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
582/* trick the compiler into thinking a external signal handler uses this */
583volatile VALUE rb_gc_guarded_val;
584volatile VALUE *
585rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val)
586{
587 rb_gc_guarded_val = val;
588
589 return ptr;
590}
591#endif
592
593static const char *obj_type_name(VALUE obj);
594#include "gc/default/default.c"
595
596#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
597# error "Modular GC requires dlopen"
598#elif USE_MODULAR_GC
599#include <dlfcn.h>
600
601typedef struct gc_function_map {
602 // Bootup
603 void *(*objspace_alloc)(void);
604 void (*objspace_init)(void *objspace_ptr);
605 void *(*ractor_cache_alloc)(void *objspace_ptr, void *ractor);
606 void (*set_params)(void *objspace_ptr);
607 void (*init)(void);
608 size_t *(*heap_sizes)(void *objspace_ptr);
609 // Shutdown
610 void (*shutdown_free_objects)(void *objspace_ptr);
611 void (*objspace_free)(void *objspace_ptr);
612 void (*ractor_cache_free)(void *objspace_ptr, void *cache);
613 // GC
614 void (*start)(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact);
615 bool (*during_gc_p)(void *objspace_ptr);
616 void (*prepare_heap)(void *objspace_ptr);
617 void (*gc_enable)(void *objspace_ptr);
618 void (*gc_disable)(void *objspace_ptr, bool finish_current_gc);
619 bool (*gc_enabled_p)(void *objspace_ptr);
620 VALUE (*config_get)(void *objpace_ptr);
621 void (*config_set)(void *objspace_ptr, VALUE hash);
622 void (*stress_set)(void *objspace_ptr, VALUE flag);
623 VALUE (*stress_get)(void *objspace_ptr);
624 // Object allocation
625 VALUE (*new_obj)(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags, bool wb_protected, size_t alloc_size);
626 size_t (*obj_slot_size)(VALUE obj);
627 size_t (*heap_id_for_size)(void *objspace_ptr, size_t size);
628 bool (*size_allocatable_p)(size_t size);
629 // Malloc
630 void *(*malloc)(void *objspace_ptr, size_t size, bool gc_allowed);
631 void *(*calloc)(void *objspace_ptr, size_t size, bool gc_allowed);
632 void *(*realloc)(void *objspace_ptr, void *ptr, size_t new_size, size_t old_size, bool gc_allowed);
633 void (*free)(void *objspace_ptr, void *ptr, size_t old_size);
634 void (*adjust_memory_usage)(void *objspace_ptr, ssize_t diff);
635 // Marking
636 void (*mark)(void *objspace_ptr, VALUE obj);
637 void (*mark_and_move)(void *objspace_ptr, VALUE *ptr);
638 void (*mark_and_pin)(void *objspace_ptr, VALUE obj);
639 void (*mark_maybe)(void *objspace_ptr, VALUE obj);
640 void (*mark_weak)(void *objspace_ptr, VALUE *ptr);
641 void (*remove_weak)(void *objspace_ptr, VALUE parent_obj, VALUE *ptr);
642 // Compaction
643 bool (*object_moved_p)(void *objspace_ptr, VALUE obj);
644 VALUE (*location)(void *objspace_ptr, VALUE value);
645 // Write barriers
646 void (*writebarrier)(void *objspace_ptr, VALUE a, VALUE b);
647 void (*writebarrier_unprotect)(void *objspace_ptr, VALUE obj);
648 void (*writebarrier_remember)(void *objspace_ptr, VALUE obj);
649 // Heap walking
650 void (*each_objects)(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data);
651 void (*each_object)(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data);
652 // Finalizers
653 void (*make_zombie)(void *objspace_ptr, VALUE obj, void (*dfree)(void *), void *data);
654 VALUE (*define_finalizer)(void *objspace_ptr, VALUE obj, VALUE block);
655 void (*undefine_finalizer)(void *objspace_ptr, VALUE obj);
656 void (*copy_finalizer)(void *objspace_ptr, VALUE dest, VALUE obj);
657 void (*shutdown_call_finalizer)(void *objspace_ptr);
658 // Forking
659 void (*before_fork)(void *objspace_ptr);
660 void (*after_fork)(void *objspace_ptr, rb_pid_t pid);
661 // Statistics
662 void (*set_measure_total_time)(void *objspace_ptr, VALUE flag);
663 bool (*get_measure_total_time)(void *objspace_ptr);
664 unsigned long long (*get_total_time)(void *objspace_ptr);
665 size_t (*gc_count)(void *objspace_ptr);
666 VALUE (*latest_gc_info)(void *objspace_ptr, VALUE key);
667 VALUE (*stat)(void *objspace_ptr, VALUE hash_or_sym);
668 VALUE (*stat_heap)(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym);
669 const char *(*active_gc_name)(void);
670 // Miscellaneous
671 struct rb_gc_object_metadata_entry *(*object_metadata)(void *objspace_ptr, VALUE obj);
672 bool (*pointer_to_heap_p)(void *objspace_ptr, const void *ptr);
673 bool (*garbage_object_p)(void *objspace_ptr, VALUE obj);
674 void (*set_event_hook)(void *objspace_ptr, const rb_event_flag_t event);
675 void (*copy_attributes)(void *objspace_ptr, VALUE dest, VALUE obj);
676
677 bool modular_gc_loaded_p;
678} rb_gc_function_map_t;
679
680static rb_gc_function_map_t rb_gc_functions;
681
682# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
683# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
684
685static void
686ruby_modular_gc_init(void)
687{
688 // Assert that the directory path ends with a /
689 RUBY_ASSERT_ALWAYS(MODULAR_GC_DIR[sizeof(MODULAR_GC_DIR) - 2] == '/');
690
691 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
692
693 rb_gc_function_map_t gc_functions = { 0 };
694
695 char *gc_so_path = NULL;
696 void *handle = NULL;
697 if (gc_so_file) {
698 /* Check to make sure that gc_so_file matches /[\w-_]+/ so that it does
699 * not load a shared object outside of the directory. */
700 for (size_t i = 0; i < strlen(gc_so_file); i++) {
701 char c = gc_so_file[i];
702 if (isalnum(c)) continue;
703 switch (c) {
704 case '-':
705 case '_':
706 break;
707 default:
708 fprintf(stderr, "Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY"\n");
709 exit(1);
710 }
711 }
712
713 size_t gc_so_path_size = strlen(MODULAR_GC_DIR "librubygc." DLEXT) + strlen(gc_so_file) + 1;
714#ifdef LOAD_RELATIVE
715 Dl_info dli;
716 size_t prefix_len = 0;
717 if (dladdr((void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
718 const char *base = strrchr(dli.dli_fname, '/');
719 if (base) {
720 size_t tail = 0;
721# define end_with_p(lit) \
722 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
723 memcmp(base - tail, lit, tail) == 0)
724
725 prefix_len = base - dli.dli_fname;
726 if (end_with_p("/bin") || end_with_p("/lib")) {
727 prefix_len -= tail;
728 }
729 prefix_len += MODULAR_GC_DIR[0] != '/';
730 gc_so_path_size += prefix_len;
731 }
732 }
733#endif
734 gc_so_path = alloca(gc_so_path_size);
735 {
736 size_t gc_so_path_idx = 0;
737#define GC_SO_PATH_APPEND(str) do { \
738 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
739} while (0)
740#ifdef LOAD_RELATIVE
741 if (prefix_len > 0) {
742 memcpy(gc_so_path, dli.dli_fname, prefix_len);
743 gc_so_path_idx = prefix_len;
744 }
745#endif
746 GC_SO_PATH_APPEND(MODULAR_GC_DIR "librubygc.");
747 GC_SO_PATH_APPEND(gc_so_file);
748 GC_SO_PATH_APPEND(DLEXT);
749 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
750#undef GC_SO_PATH_APPEND
751 }
752
753 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
754 if (!handle) {
755 fprintf(stderr, "ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
756 exit(1);
757 }
758
759 gc_functions.modular_gc_loaded_p = true;
760 }
761
762# define load_modular_gc_func(name) do { \
763 if (handle) { \
764 const char *func_name = "rb_gc_impl_" #name; \
765 gc_functions.name = dlsym(handle, func_name); \
766 if (!gc_functions.name) { \
767 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
768 exit(1); \
769 } \
770 } \
771 else { \
772 gc_functions.name = rb_gc_impl_##name; \
773 } \
774} while (0)
775
776 // Bootup
777 load_modular_gc_func(objspace_alloc);
778 load_modular_gc_func(objspace_init);
779 load_modular_gc_func(ractor_cache_alloc);
780 load_modular_gc_func(set_params);
781 load_modular_gc_func(init);
782 load_modular_gc_func(heap_sizes);
783 // Shutdown
784 load_modular_gc_func(shutdown_free_objects);
785 load_modular_gc_func(objspace_free);
786 load_modular_gc_func(ractor_cache_free);
787 // GC
788 load_modular_gc_func(start);
789 load_modular_gc_func(during_gc_p);
790 load_modular_gc_func(prepare_heap);
791 load_modular_gc_func(gc_enable);
792 load_modular_gc_func(gc_disable);
793 load_modular_gc_func(gc_enabled_p);
794 load_modular_gc_func(config_set);
795 load_modular_gc_func(config_get);
796 load_modular_gc_func(stress_set);
797 load_modular_gc_func(stress_get);
798 // Object allocation
799 load_modular_gc_func(new_obj);
800 load_modular_gc_func(obj_slot_size);
801 load_modular_gc_func(heap_id_for_size);
802 load_modular_gc_func(size_allocatable_p);
803 // Malloc
804 load_modular_gc_func(malloc);
805 load_modular_gc_func(calloc);
806 load_modular_gc_func(realloc);
807 load_modular_gc_func(free);
808 load_modular_gc_func(adjust_memory_usage);
809 // Marking
810 load_modular_gc_func(mark);
811 load_modular_gc_func(mark_and_move);
812 load_modular_gc_func(mark_and_pin);
813 load_modular_gc_func(mark_maybe);
814 load_modular_gc_func(mark_weak);
815 load_modular_gc_func(remove_weak);
816 // Compaction
817 load_modular_gc_func(object_moved_p);
818 load_modular_gc_func(location);
819 // Write barriers
820 load_modular_gc_func(writebarrier);
821 load_modular_gc_func(writebarrier_unprotect);
822 load_modular_gc_func(writebarrier_remember);
823 // Heap walking
824 load_modular_gc_func(each_objects);
825 load_modular_gc_func(each_object);
826 // Finalizers
827 load_modular_gc_func(make_zombie);
828 load_modular_gc_func(define_finalizer);
829 load_modular_gc_func(undefine_finalizer);
830 load_modular_gc_func(copy_finalizer);
831 load_modular_gc_func(shutdown_call_finalizer);
832 // Forking
833 load_modular_gc_func(before_fork);
834 load_modular_gc_func(after_fork);
835 // Statistics
836 load_modular_gc_func(set_measure_total_time);
837 load_modular_gc_func(get_measure_total_time);
838 load_modular_gc_func(get_total_time);
839 load_modular_gc_func(gc_count);
840 load_modular_gc_func(latest_gc_info);
841 load_modular_gc_func(stat);
842 load_modular_gc_func(stat_heap);
843 load_modular_gc_func(active_gc_name);
844 // Miscellaneous
845 load_modular_gc_func(object_metadata);
846 load_modular_gc_func(pointer_to_heap_p);
847 load_modular_gc_func(garbage_object_p);
848 load_modular_gc_func(set_event_hook);
849 load_modular_gc_func(copy_attributes);
850
851# undef load_modular_gc_func
852
853 rb_gc_functions = gc_functions;
854}
855
856// Bootup
857# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
858# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
859# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
860# define rb_gc_impl_set_params rb_gc_functions.set_params
861# define rb_gc_impl_init rb_gc_functions.init
862# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
863// Shutdown
864# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
865# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
866# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
867// GC
868# define rb_gc_impl_start rb_gc_functions.start
869# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
870# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
871# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
872# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
873# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
874# define rb_gc_impl_config_get rb_gc_functions.config_get
875# define rb_gc_impl_config_set rb_gc_functions.config_set
876# define rb_gc_impl_stress_set rb_gc_functions.stress_set
877# define rb_gc_impl_stress_get rb_gc_functions.stress_get
878// Object allocation
879# define rb_gc_impl_new_obj rb_gc_functions.new_obj
880# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
881# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
882# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
883// Malloc
884# define rb_gc_impl_malloc rb_gc_functions.malloc
885# define rb_gc_impl_calloc rb_gc_functions.calloc
886# define rb_gc_impl_realloc rb_gc_functions.realloc
887# define rb_gc_impl_free rb_gc_functions.free
888# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
889// Marking
890# define rb_gc_impl_mark rb_gc_functions.mark
891# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
892# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
893# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
894# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
895# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
896// Compaction
897# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
898# define rb_gc_impl_location rb_gc_functions.location
899// Write barriers
900# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
901# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
902# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
903// Heap walking
904# define rb_gc_impl_each_objects rb_gc_functions.each_objects
905# define rb_gc_impl_each_object rb_gc_functions.each_object
906// Finalizers
907# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
908# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
909# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
910# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
911# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
912// Forking
913# define rb_gc_impl_before_fork rb_gc_functions.before_fork
914# define rb_gc_impl_after_fork rb_gc_functions.after_fork
915// Statistics
916# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
917# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
918# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
919# define rb_gc_impl_gc_count rb_gc_functions.gc_count
920# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
921# define rb_gc_impl_stat rb_gc_functions.stat
922# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
923# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
924// Miscellaneous
925# define rb_gc_impl_object_metadata rb_gc_functions.object_metadata
926# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
927# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
928# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
929# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
930#endif
931
932#ifdef RUBY_ASAN_ENABLED
933static void
934asan_death_callback(void)
935{
936 if (GET_VM()) {
937 rb_bug_without_die("ASAN error");
938 }
939}
940#endif
941
942static VALUE initial_stress = Qfalse;
943
944void *
945rb_objspace_alloc(void)
946{
947#if USE_MODULAR_GC
948 ruby_modular_gc_init();
949#endif
950
951 void *objspace = rb_gc_impl_objspace_alloc();
952 ruby_current_vm_ptr->gc.objspace = objspace;
953 rb_gc_impl_objspace_init(objspace);
954 rb_gc_impl_stress_set(objspace, initial_stress);
955
956#ifdef RUBY_ASAN_ENABLED
957 __sanitizer_set_death_callback(asan_death_callback);
958#endif
959
960 return objspace;
961}
962
963void
964rb_objspace_free(void *objspace)
965{
966 rb_gc_impl_objspace_free(objspace);
967}
968
969size_t
970rb_gc_obj_slot_size(VALUE obj)
971{
972 return rb_gc_impl_obj_slot_size(obj);
973}
974
975static inline void
976gc_validate_pc(VALUE obj)
977{
978#if RUBY_DEBUG
979 // IMEMOs and objects without a class (e.g managed id table) are not traceable
980 if (RB_TYPE_P(obj, T_IMEMO) || !CLASS_OF(obj)) return;
981
982 rb_execution_context_t *ec = GET_EC();
983 const rb_control_frame_t *cfp = ec->cfp;
984 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc) {
985 const VALUE *iseq_encoded = ISEQ_BODY(cfp->iseq)->iseq_encoded;
986 const VALUE *iseq_encoded_end = iseq_encoded + ISEQ_BODY(cfp->iseq)->iseq_size;
987 RUBY_ASSERT(cfp->pc >= iseq_encoded, "PC not set when allocating, breaking tracing");
988 RUBY_ASSERT(cfp->pc <= iseq_encoded_end, "PC not set when allocating, breaking tracing");
989 }
990#endif
991}
992
993static inline VALUE
994newobj_of(rb_ractor_t *cr, VALUE klass, VALUE flags, shape_id_t shape_id, bool wb_protected, size_t size)
995{
996 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, wb_protected, size);
997 RBASIC_SET_SHAPE_ID_NO_CHECKS(obj, shape_id);
998
999 gc_validate_pc(obj);
1000
1001 if (UNLIKELY(rb_gc_event_hook_required_p(RUBY_INTERNAL_EVENT_NEWOBJ))) {
1002 int lev = RB_GC_VM_LOCK_NO_BARRIER();
1003 {
1004 size_t slot_size = rb_gc_obj_slot_size(obj);
1005 if (slot_size > RVALUE_SIZE) {
1006 memset((char *)obj + RVALUE_SIZE, 0, slot_size - RVALUE_SIZE);
1007 }
1008
1009 /* We must disable GC here because the callback could call xmalloc
1010 * which could potentially trigger a GC, and a lot of code is unsafe
1011 * to trigger a GC right after an object has been allocated because
1012 * they perform initialization for the object and assume that the
1013 * GC does not trigger before then. */
1014 bool gc_disabled = RTEST(rb_gc_disable_no_rest());
1015 {
1016 rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_NEWOBJ);
1017 }
1018 if (!gc_disabled) rb_gc_enable();
1019 }
1020 RB_GC_VM_UNLOCK_NO_BARRIER(lev);
1021 }
1022
1023#if RGENGC_CHECK_MODE
1024# ifndef GC_DEBUG_SLOT_FILL_SPECIAL_VALUE
1025# define GC_DEBUG_SLOT_FILL_SPECIAL_VALUE 255
1026# endif
1027
1028 memset(
1029 (void *)(obj + sizeof(struct RBasic)),
1030 GC_DEBUG_SLOT_FILL_SPECIAL_VALUE,
1031 rb_gc_obj_slot_size(obj) - sizeof(struct RBasic)
1032 );
1033#endif
1034
1035 return obj;
1036}
1037
1038VALUE
1039rb_wb_unprotected_newobj_of(VALUE klass, VALUE flags, shape_id_t shape_id, size_t size)
1040{
1041 GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
1042 return newobj_of(GET_RACTOR(), klass, flags, shape_id, FALSE, size);
1043}
1044
1045VALUE
1046rb_wb_protected_newobj_of(rb_execution_context_t *ec, VALUE klass, VALUE flags, shape_id_t shape_id, size_t size)
1047{
1048 GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
1049 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, shape_id, TRUE, size);
1050}
1051
1052#define UNEXPECTED_NODE(func) \
1053 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1054 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1055
1056static inline void
1057rb_data_object_check(VALUE klass)
1058{
1059 if (klass != rb_cObject && (rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
1060 rb_undef_alloc_func(klass);
1061 rb_warn("undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1062 }
1063}
1064
1065VALUE
1066rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
1067{
1069 if (klass) rb_data_object_check(klass);
1070 VALUE obj = newobj_of(GET_RACTOR(), klass, T_DATA, ROOT_SHAPE_ID, !dmark, sizeof(struct RTypedData));
1071
1072 struct RData *data = (struct RData *)obj;
1073 data->dmark = dmark;
1074 data->dfree = dfree;
1075 data->data = datap;
1076
1077 return obj;
1078}
1079
1080VALUE
1082{
1083 VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
1084 DATA_PTR(obj) = xcalloc(1, size);
1085 return obj;
1086}
1087
1088static VALUE
1089typed_data_alloc(VALUE klass, VALUE typed_flag, void *datap, const rb_data_type_t *type, size_t size)
1090{
1091 RBIMPL_NONNULL_ARG(type);
1092 if (klass) rb_data_object_check(klass);
1093 bool wb_protected = (type->flags & RUBY_FL_WB_PROTECTED) || !type->function.dmark;
1094 VALUE obj = newobj_of(GET_RACTOR(), klass, T_DATA | RUBY_TYPED_FL_IS_TYPED_DATA, ROOT_SHAPE_ID, wb_protected, size);
1095
1096 struct RTypedData *data = (struct RTypedData *)obj;
1097 data->fields_obj = 0;
1098 *(VALUE *)&data->type = ((VALUE)type) | typed_flag;
1099 data->data = datap;
1100
1101 return obj;
1102}
1103
1104VALUE
1106{
1107 if (UNLIKELY(type->flags & RUBY_TYPED_EMBEDDABLE)) {
1108 rb_raise(rb_eTypeError, "Cannot wrap an embeddable TypedData");
1109 }
1110
1111 return typed_data_alloc(klass, 0, datap, type, sizeof(struct RTypedData));
1112}
1113
1114VALUE
1116{
1117 if (type->flags & RUBY_TYPED_EMBEDDABLE) {
1118 if (!(type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1119 rb_raise(rb_eTypeError, "Embeddable TypedData must be freed immediately");
1120 }
1121
1122 size_t embed_size = offsetof(struct RTypedData, data) + size;
1123 if (rb_gc_size_allocatable_p(embed_size)) {
1124 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0, type, embed_size);
1125 memset((char *)obj + offsetof(struct RTypedData, data), 0, size);
1126 return obj;
1127 }
1128 }
1129
1130 VALUE obj = typed_data_alloc(klass, 0, NULL, type, sizeof(struct RTypedData));
1131 DATA_PTR(obj) = xcalloc(1, size);
1132 return obj;
1133}
1134
1135static size_t
1136rb_objspace_data_type_memsize(VALUE obj)
1137{
1138 size_t size = 0;
1139 if (RTYPEDDATA_P(obj)) {
1140 const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
1141 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1142
1143 if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1144#ifdef HAVE_MALLOC_USABLE_SIZE
1145 size += malloc_usable_size((void *)ptr);
1146#endif
1147 }
1148
1149 if (ptr && type->function.dsize) {
1150 size += type->function.dsize(ptr);
1151 }
1152 }
1153
1154 return size;
1155}
1156
1157const char *
1158rb_objspace_data_type_name(VALUE obj)
1159{
1160 if (RTYPEDDATA_P(obj)) {
1161 return RTYPEDDATA_TYPE(obj)->wrap_struct_name;
1162 }
1163 else {
1164 return 0;
1165 }
1166}
1167
1168static void
1169io_fptr_finalize(void *fptr)
1170{
1171 rb_io_fptr_finalize((struct rb_io *)fptr);
1172}
1173
1174static inline void
1175make_io_zombie(void *objspace, VALUE obj)
1176{
1177 rb_io_t *fptr = RFILE(obj)->fptr;
1178 rb_gc_impl_make_zombie(objspace, obj, io_fptr_finalize, fptr);
1179}
1180
1181static bool
1182rb_data_free(void *objspace, VALUE obj)
1183{
1184 void *data = RTYPEDDATA_P(obj) ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
1185 if (data) {
1186 int free_immediately = false;
1187 void (*dfree)(void *);
1188
1189 if (RTYPEDDATA_P(obj)) {
1190 free_immediately = (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1191 dfree = RTYPEDDATA_TYPE(obj)->function.dfree;
1192 }
1193 else {
1194 dfree = RDATA(obj)->dfree;
1195 }
1196
1197 if (dfree) {
1198 if (dfree == RUBY_DEFAULT_FREE) {
1199 if (!RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1200 xfree(data);
1201 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1202 }
1203 }
1204 else if (free_immediately) {
1205 (*dfree)(data);
1206 if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1207 xfree(data);
1208 }
1209
1210 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1211 }
1212 else {
1213 rb_gc_impl_make_zombie(objspace, obj, dfree, data);
1214 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1215 return FALSE;
1216 }
1217 }
1218 else {
1219 RB_DEBUG_COUNTER_INC(obj_data_empty);
1220 }
1221 }
1222
1223 return true;
1224}
1225
1227 VALUE klass;
1228 rb_objspace_t *objspace; // used for update_*
1229};
1230
1231static void
1232classext_free(rb_classext_t *ext, bool is_prime, VALUE box_value, void *arg)
1233{
1234 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
1235
1236 rb_class_classext_free(args->klass, ext, is_prime);
1237}
1238
1239static void
1240classext_iclass_free(rb_classext_t *ext, bool is_prime, VALUE box_value, void *arg)
1241{
1242 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
1243
1244 rb_iclass_classext_free(args->klass, ext, is_prime);
1245}
1246
1247bool
1248rb_gc_obj_free(void *objspace, VALUE obj)
1249{
1250 struct classext_foreach_args args;
1251
1252 RB_DEBUG_COUNTER_INC(obj_free);
1253
1254 switch (BUILTIN_TYPE(obj)) {
1255 case T_NIL:
1256 case T_FIXNUM:
1257 case T_TRUE:
1258 case T_FALSE:
1259 rb_bug("obj_free() called for broken object");
1260 break;
1261 default:
1262 break;
1263 }
1264
1265 switch (BUILTIN_TYPE(obj)) {
1266 case T_OBJECT:
1267 if (FL_TEST_RAW(obj, ROBJECT_HEAP)) {
1268 if (rb_shape_obj_too_complex_p(obj)) {
1269 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1270 st_free_table(ROBJECT_FIELDS_HASH(obj));
1271 }
1272 else {
1273 xfree(ROBJECT(obj)->as.heap.fields);
1274 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1275 }
1276 }
1277 else {
1278 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1279 }
1280 break;
1281 case T_MODULE:
1282 case T_CLASS:
1283#if USE_ZJIT
1284 rb_zjit_klass_free(obj);
1285#endif
1286 args.klass = obj;
1287 rb_class_classext_foreach(obj, classext_free, (void *)&args);
1288 if (RCLASS_CLASSEXT_TBL(obj)) {
1289 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1290 }
1291 (void)RB_DEBUG_COUNTER_INC_IF(obj_module_ptr, BUILTIN_TYPE(obj) == T_MODULE);
1292 (void)RB_DEBUG_COUNTER_INC_IF(obj_class_ptr, BUILTIN_TYPE(obj) == T_CLASS);
1293 break;
1294 case T_STRING:
1295 rb_str_free(obj);
1296 break;
1297 case T_ARRAY:
1298 rb_ary_free(obj);
1299 break;
1300 case T_HASH:
1301#if USE_DEBUG_COUNTER
1302 switch (RHASH_SIZE(obj)) {
1303 case 0:
1304 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1305 break;
1306 case 1:
1307 RB_DEBUG_COUNTER_INC(obj_hash_1);
1308 break;
1309 case 2:
1310 RB_DEBUG_COUNTER_INC(obj_hash_2);
1311 break;
1312 case 3:
1313 RB_DEBUG_COUNTER_INC(obj_hash_3);
1314 break;
1315 case 4:
1316 RB_DEBUG_COUNTER_INC(obj_hash_4);
1317 break;
1318 case 5:
1319 case 6:
1320 case 7:
1321 case 8:
1322 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1323 break;
1324 default:
1325 GC_ASSERT(RHASH_SIZE(obj) > 8);
1326 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1327 }
1328
1329 if (RHASH_AR_TABLE_P(obj)) {
1330 if (RHASH_AR_TABLE(obj) == NULL) {
1331 RB_DEBUG_COUNTER_INC(obj_hash_null);
1332 }
1333 else {
1334 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1335 }
1336 }
1337 else {
1338 RB_DEBUG_COUNTER_INC(obj_hash_st);
1339 }
1340#endif
1341
1342 rb_hash_free(obj);
1343 break;
1344 case T_REGEXP:
1345 if (RREGEXP(obj)->ptr) {
1346 onig_free(RREGEXP(obj)->ptr);
1347 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1348 }
1349 break;
1350 case T_DATA:
1351 if (!rb_data_free(objspace, obj)) return false;
1352 break;
1353 case T_MATCH:
1354 {
1355 rb_matchext_t *rm = RMATCH_EXT(obj);
1356#if USE_DEBUG_COUNTER
1357 if (rm->regs.num_regs >= 8) {
1358 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1359 }
1360 else if (rm->regs.num_regs >= 4) {
1361 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1362 }
1363 else if (rm->regs.num_regs >= 1) {
1364 RB_DEBUG_COUNTER_INC(obj_match_under4);
1365 }
1366#endif
1367 onig_region_free(&rm->regs, 0);
1368 xfree(rm->char_offset);
1369
1370 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1371 }
1372 break;
1373 case T_FILE:
1374 if (RFILE(obj)->fptr) {
1375 make_io_zombie(objspace, obj);
1376 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1377 return FALSE;
1378 }
1379 break;
1380 case T_RATIONAL:
1381 RB_DEBUG_COUNTER_INC(obj_rational);
1382 break;
1383 case T_COMPLEX:
1384 RB_DEBUG_COUNTER_INC(obj_complex);
1385 break;
1386 case T_MOVED:
1387 break;
1388 case T_ICLASS:
1389 args.klass = obj;
1390
1391 rb_class_classext_foreach(obj, classext_iclass_free, (void *)&args);
1392 if (RCLASS_CLASSEXT_TBL(obj)) {
1393 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1394 }
1395
1396 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1397 break;
1398
1399 case T_FLOAT:
1400 RB_DEBUG_COUNTER_INC(obj_float);
1401 break;
1402
1403 case T_BIGNUM:
1404 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1405 xfree(BIGNUM_DIGITS(obj));
1406 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1407 }
1408 else {
1409 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1410 }
1411 break;
1412
1413 case T_NODE:
1414 UNEXPECTED_NODE(obj_free);
1415 break;
1416
1417 case T_STRUCT:
1418 if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1419 RSTRUCT(obj)->as.heap.ptr == NULL) {
1420 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1421 }
1422 else {
1423 xfree((void *)RSTRUCT(obj)->as.heap.ptr);
1424 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1425 }
1426 break;
1427
1428 case T_SYMBOL:
1429 RB_DEBUG_COUNTER_INC(obj_symbol);
1430 break;
1431
1432 case T_IMEMO:
1433 rb_imemo_free((VALUE)obj);
1434 break;
1435
1436 default:
1437 rb_bug("gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1438 BUILTIN_TYPE(obj), (void*)obj, RBASIC(obj)->flags);
1439 }
1440
1441 if (FL_TEST_RAW(obj, FL_FINALIZE)) {
1442 rb_gc_impl_make_zombie(objspace, obj, 0, 0);
1443 return FALSE;
1444 }
1445 else {
1446 return TRUE;
1447 }
1448}
1449
1450void
1451rb_objspace_set_event_hook(const rb_event_flag_t event)
1452{
1453 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1454}
1455
1456static int
1457internal_object_p(VALUE obj)
1458{
1459 void *ptr = asan_unpoison_object_temporary(obj);
1460
1461 if (RBASIC(obj)->flags) {
1462 switch (BUILTIN_TYPE(obj)) {
1463 case T_NODE:
1464 UNEXPECTED_NODE(internal_object_p);
1465 break;
1466 case T_NONE:
1467 case T_MOVED:
1468 case T_IMEMO:
1469 case T_ICLASS:
1470 case T_ZOMBIE:
1471 break;
1472 case T_CLASS:
1473 if (obj == rb_mRubyVMFrozenCore)
1474 return 1;
1475
1476 if (!RBASIC_CLASS(obj)) break;
1477 if (RCLASS_SINGLETON_P(obj)) {
1478 return rb_singleton_class_internal_p(obj);
1479 }
1480 return 0;
1481 default:
1482 if (!RBASIC(obj)->klass) break;
1483 return 0;
1484 }
1485 }
1486 if (ptr || !RBASIC(obj)->flags) {
1487 rb_asan_poison_object(obj);
1488 }
1489 return 1;
1490}
1491
1492int
1493rb_objspace_internal_object_p(VALUE obj)
1494{
1495 return internal_object_p(obj);
1496}
1497
1499 size_t num;
1500 VALUE of;
1501};
1502
1503static int
1504os_obj_of_i(void *vstart, void *vend, size_t stride, void *data)
1505{
1506 struct os_each_struct *oes = (struct os_each_struct *)data;
1507
1508 VALUE v = (VALUE)vstart;
1509 for (; v != (VALUE)vend; v += stride) {
1510 if (!internal_object_p(v)) {
1511 if (!oes->of || rb_obj_is_kind_of(v, oes->of)) {
1512 if (!rb_multi_ractor_p() || rb_ractor_shareable_p(v)) {
1513 rb_yield(v);
1514 oes->num++;
1515 }
1516 }
1517 }
1518 }
1519
1520 return 0;
1521}
1522
1523static VALUE
1524os_obj_of(VALUE of)
1525{
1526 struct os_each_struct oes;
1527
1528 oes.num = 0;
1529 oes.of = of;
1530 rb_objspace_each_objects(os_obj_of_i, &oes);
1531 return SIZET2NUM(oes.num);
1532}
1533
1534/*
1535 * call-seq:
1536 * ObjectSpace.each_object([module]) {|obj| ... } -> integer
1537 * ObjectSpace.each_object([module]) -> an_enumerator
1538 *
1539 * Calls the block once for each living, nonimmediate object in this
1540 * Ruby process. If <i>module</i> is specified, calls the block
1541 * for only those classes or modules that match (or are a subclass of)
1542 * <i>module</i>. Returns the number of objects found. Immediate
1543 * objects (such as <code>Fixnum</code>s, static <code>Symbol</code>s
1544 * <code>true</code>, <code>false</code> and <code>nil</code>) are
1545 * never returned.
1546 *
1547 * If no block is given, an enumerator is returned instead.
1548 *
1549 * Job = Class.new
1550 * jobs = [Job.new, Job.new]
1551 * count = ObjectSpace.each_object(Job) {|x| p x }
1552 * puts "Total count: #{count}"
1553 *
1554 * <em>produces:</em>
1555 *
1556 * #<Job:0x000000011d6cbbf0>
1557 * #<Job:0x000000011d6cbc68>
1558 * Total count: 2
1559 *
1560 * Due to a current Ractor implementation issue, this method does not yield
1561 * Ractor-unshareable objects when the process is in multi-Ractor mode. Multi-ractor
1562 * mode is enabled when <code>Ractor.new</code> has been called for the first time.
1563 * See https://bugs.ruby-lang.org/issues/19387 for more information.
1564 *
1565 * a = 12345678987654321 # shareable
1566 * b = [].freeze # shareable
1567 * c = {} # not shareable
1568 * ObjectSpace.each_object {|x| x } # yields a, b, and c
1569 * Ractor.new {} # enter multi-Ractor mode
1570 * ObjectSpace.each_object {|x| x } # does not yield c
1571 *
1572 */
1573
1574static VALUE
1575os_each_obj(int argc, VALUE *argv, VALUE os)
1576{
1577 VALUE of;
1578
1579 of = (!rb_check_arity(argc, 0, 1) ? 0 : argv[0]);
1580 RETURN_ENUMERATOR(os, 1, &of);
1581 return os_obj_of(of);
1582}
1583
1584/*
1585 * call-seq:
1586 * ObjectSpace.undefine_finalizer(obj)
1587 *
1588 * Removes all finalizers for <i>obj</i>.
1589 *
1590 */
1591
1592static VALUE
1593undefine_final(VALUE os, VALUE obj)
1594{
1595 return rb_undefine_finalizer(obj);
1596}
1597
1598VALUE
1599rb_undefine_finalizer(VALUE obj)
1600{
1601 rb_check_frozen(obj);
1602
1603 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1604
1605 return obj;
1606}
1607
1608static void
1609should_be_callable(VALUE block)
1610{
1611 if (!rb_obj_respond_to(block, idCall, TRUE)) {
1612 rb_raise(rb_eArgError, "wrong type argument %"PRIsVALUE" (should be callable)",
1613 rb_obj_class(block));
1614 }
1615}
1616
1617static void
1618should_be_finalizable(VALUE obj)
1619{
1620 if (!FL_ABLE(obj)) {
1621 rb_raise(rb_eArgError, "cannot define finalizer for %s",
1622 rb_obj_classname(obj));
1623 }
1624 rb_check_frozen(obj);
1625}
1626
1627void
1628rb_gc_copy_finalizer(VALUE dest, VALUE obj)
1629{
1630 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1631}
1632
1633/*
1634 * call-seq:
1635 * ObjectSpace.define_finalizer(obj, aProc=proc())
1636 *
1637 * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
1638 * was destroyed. The object ID of the <i>obj</i> will be passed
1639 * as an argument to <i>aProc</i>. If <i>aProc</i> is a lambda or
1640 * method, make sure it can be called with a single argument.
1641 *
1642 * The return value is an array <code>[0, aProc]</code>.
1643 *
1644 * The two recommended patterns are to either create the finaliser proc
1645 * in a non-instance method where it can safely capture the needed state,
1646 * or to use a custom callable object that stores the needed state
1647 * explicitly as instance variables.
1648 *
1649 * class Foo
1650 * def initialize(data_needed_for_finalization)
1651 * ObjectSpace.define_finalizer(self, self.class.create_finalizer(data_needed_for_finalization))
1652 * end
1653 *
1654 * def self.create_finalizer(data_needed_for_finalization)
1655 * proc {
1656 * puts "finalizing #{data_needed_for_finalization}"
1657 * }
1658 * end
1659 * end
1660 *
1661 * class Bar
1662 * class Remover
1663 * def initialize(data_needed_for_finalization)
1664 * @data_needed_for_finalization = data_needed_for_finalization
1665 * end
1666 *
1667 * def call(id)
1668 * puts "finalizing #{@data_needed_for_finalization}"
1669 * end
1670 * end
1671 *
1672 * def initialize(data_needed_for_finalization)
1673 * ObjectSpace.define_finalizer(self, Remover.new(data_needed_for_finalization))
1674 * end
1675 * end
1676 *
1677 * Note that if your finalizer references the object to be
1678 * finalized it will never be run on GC, although it will still be
1679 * run at exit. You will get a warning if you capture the object
1680 * to be finalized as the receiver of the finalizer.
1681 *
1682 * class CapturesSelf
1683 * def initialize(name)
1684 * ObjectSpace.define_finalizer(self, proc {
1685 * # this finalizer will only be run on exit
1686 * puts "finalizing #{name}"
1687 * })
1688 * end
1689 * end
1690 *
1691 * Also note that finalization can be unpredictable and is never guaranteed
1692 * to be run except on exit.
1693 */
1694
1695static VALUE
1696define_final(int argc, VALUE *argv, VALUE os)
1697{
1698 VALUE obj, block;
1699
1700 rb_scan_args(argc, argv, "11", &obj, &block);
1701 if (argc == 1) {
1702 block = rb_block_proc();
1703 }
1704
1705 if (rb_callable_receiver(block) == obj) {
1706 rb_warn("finalizer references object to be finalized");
1707 }
1708
1709 return rb_define_finalizer(obj, block);
1710}
1711
1712VALUE
1713rb_define_finalizer(VALUE obj, VALUE block)
1714{
1715 should_be_finalizable(obj);
1716 should_be_callable(block);
1717
1718 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1719
1720 block = rb_ary_new3(2, INT2FIX(0), block);
1721 OBJ_FREEZE(block);
1722 return block;
1723}
1724
1725void
1726rb_objspace_call_finalizer(void)
1727{
1728 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1729}
1730
1731void
1732rb_objspace_free_objects(void *objspace)
1733{
1734 rb_gc_impl_shutdown_free_objects(objspace);
1735}
1736
1737int
1738rb_objspace_garbage_object_p(VALUE obj)
1739{
1740 return !SPECIAL_CONST_P(obj) && rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1741}
1742
1743bool
1744rb_gc_pointer_to_heap_p(VALUE obj)
1745{
1746 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj);
1747}
1748
1749#define OBJ_ID_INCREMENT (RUBY_IMMEDIATE_MASK + 1)
1750#define LAST_OBJECT_ID() (object_id_counter * OBJ_ID_INCREMENT)
1751static VALUE id2ref_value = 0;
1752static st_table *id2ref_tbl = NULL;
1753
1754#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1755static size_t object_id_counter = 1;
1756#else
1757static unsigned long long object_id_counter = 1;
1758#endif
1759
1760static inline VALUE
1761generate_next_object_id(void)
1762{
1763#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1764 // 64bit atomics are available
1765 return SIZET2NUM(RUBY_ATOMIC_SIZE_FETCH_ADD(object_id_counter, 1) * OBJ_ID_INCREMENT);
1766#else
1767 unsigned int lock_lev = RB_GC_VM_LOCK();
1768 VALUE id = ULL2NUM(++object_id_counter * OBJ_ID_INCREMENT);
1769 RB_GC_VM_UNLOCK(lock_lev);
1770 return id;
1771#endif
1772}
1773
1774void
1775rb_gc_obj_id_moved(VALUE obj)
1776{
1777 if (UNLIKELY(id2ref_tbl)) {
1778 st_insert(id2ref_tbl, (st_data_t)rb_obj_id(obj), (st_data_t)obj);
1779 }
1780}
1781
1782static int
1783object_id_cmp(st_data_t x, st_data_t y)
1784{
1785 if (RB_TYPE_P(x, T_BIGNUM)) {
1786 return !rb_big_eql(x, y);
1787 }
1788 else {
1789 return x != y;
1790 }
1791}
1792
1793static st_index_t
1794object_id_hash(st_data_t n)
1795{
1796 return FIX2LONG(rb_hash((VALUE)n));
1797}
1798
1799static const struct st_hash_type object_id_hash_type = {
1800 object_id_cmp,
1801 object_id_hash,
1802};
1803
1804static void gc_mark_tbl_no_pin(st_table *table);
1805
1806static void
1807id2ref_tbl_mark(void *data)
1808{
1809 st_table *table = (st_table *)data;
1810 if (UNLIKELY(!RB_POSFIXABLE(LAST_OBJECT_ID()))) {
1811 // It's very unlikely, but if enough object ids were generated, keys may be T_BIGNUM
1812 rb_mark_set(table);
1813 }
1814 // We purposely don't mark values, as they are weak references.
1815 // rb_gc_obj_free_vm_weak_references takes care of cleaning them up.
1816}
1817
1818static size_t
1819id2ref_tbl_memsize(const void *data)
1820{
1821 return rb_st_memsize(data);
1822}
1823
1824static void
1825id2ref_tbl_free(void *data)
1826{
1827 id2ref_tbl = NULL; // clear global ref
1828 st_table *table = (st_table *)data;
1829 st_free_table(table);
1830}
1831
1832static const rb_data_type_t id2ref_tbl_type = {
1833 .wrap_struct_name = "VM/_id2ref_table",
1834 .function = {
1835 .dmark = id2ref_tbl_mark,
1836 .dfree = id2ref_tbl_free,
1837 .dsize = id2ref_tbl_memsize,
1838 // dcompact function not required because the table is reference updated
1839 // in rb_gc_vm_weak_table_foreach
1840 },
1841 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
1842};
1843
1844static VALUE
1845class_object_id(VALUE klass)
1846{
1847 VALUE id = RUBY_ATOMIC_VALUE_LOAD(RCLASS(klass)->object_id);
1848 if (!id) {
1849 unsigned int lock_lev = RB_GC_VM_LOCK();
1850 id = generate_next_object_id();
1851 VALUE existing_id = RUBY_ATOMIC_VALUE_CAS(RCLASS(klass)->object_id, 0, id);
1852 if (existing_id) {
1853 id = existing_id;
1854 }
1855 else if (RB_UNLIKELY(id2ref_tbl)) {
1856 st_insert(id2ref_tbl, id, klass);
1857 }
1858 RB_GC_VM_UNLOCK(lock_lev);
1859 }
1860 return id;
1861}
1862
1863static inline VALUE
1864object_id_get(VALUE obj, shape_id_t shape_id)
1865{
1866 VALUE id;
1867 if (rb_shape_too_complex_p(shape_id)) {
1868 id = rb_obj_field_get(obj, ROOT_TOO_COMPLEX_WITH_OBJ_ID);
1869 }
1870 else {
1871 id = rb_obj_field_get(obj, rb_shape_object_id(shape_id));
1872 }
1873
1874#if RUBY_DEBUG
1875 if (!(FIXNUM_P(id) || RB_TYPE_P(id, T_BIGNUM))) {
1876 rb_p(obj);
1877 rb_bug("Object's shape includes object_id, but it's missing %s", rb_obj_info(obj));
1878 }
1879#endif
1880
1881 return id;
1882}
1883
1884static VALUE
1885object_id0(VALUE obj)
1886{
1887 VALUE id = Qfalse;
1888 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1889
1890 if (rb_shape_has_object_id(shape_id)) {
1891 return object_id_get(obj, shape_id);
1892 }
1893
1894 shape_id_t object_id_shape_id = rb_shape_transition_object_id(obj);
1895
1896 id = generate_next_object_id();
1897 rb_obj_field_set(obj, object_id_shape_id, 0, id);
1898
1899 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == object_id_shape_id);
1900 RUBY_ASSERT(rb_shape_obj_has_id(obj));
1901
1902 if (RB_UNLIKELY(id2ref_tbl)) {
1903 RB_VM_LOCKING() {
1904 st_insert(id2ref_tbl, (st_data_t)id, (st_data_t)obj);
1905 }
1906 }
1907 return id;
1908}
1909
1910static VALUE
1911object_id(VALUE obj)
1912{
1913 switch (BUILTIN_TYPE(obj)) {
1914 case T_CLASS:
1915 case T_MODULE:
1916 // With Ruby Box, classes and modules have different fields
1917 // in different boxes, so we cannot store the object id
1918 // in fields.
1919 return class_object_id(obj);
1920 case T_IMEMO:
1921 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
1922 break;
1923 default:
1924 break;
1925 }
1926
1927 if (UNLIKELY(rb_gc_multi_ractor_p() && rb_ractor_shareable_p(obj))) {
1928 unsigned int lock_lev = RB_GC_VM_LOCK();
1929 VALUE id = object_id0(obj);
1930 RB_GC_VM_UNLOCK(lock_lev);
1931 return id;
1932 }
1933
1934 return object_id0(obj);
1935}
1936
1937static void
1938build_id2ref_i(VALUE obj, void *data)
1939{
1940 st_table *id2ref_tbl = (st_table *)data;
1941
1942 switch (BUILTIN_TYPE(obj)) {
1943 case T_CLASS:
1944 case T_MODULE:
1945 RUBY_ASSERT(!rb_objspace_garbage_object_p(obj));
1946 if (RCLASS(obj)->object_id) {
1947 st_insert(id2ref_tbl, RCLASS(obj)->object_id, obj);
1948 }
1949 break;
1950 case T_IMEMO:
1951 RUBY_ASSERT(!rb_objspace_garbage_object_p(obj));
1952 if (IMEMO_TYPE_P(obj, imemo_fields) && rb_shape_obj_has_id(obj)) {
1953 st_insert(id2ref_tbl, rb_obj_id(obj), rb_imemo_fields_owner(obj));
1954 }
1955 break;
1956 case T_OBJECT:
1957 RUBY_ASSERT(!rb_objspace_garbage_object_p(obj));
1958 if (rb_shape_obj_has_id(obj)) {
1959 st_insert(id2ref_tbl, rb_obj_id(obj), obj);
1960 }
1961 break;
1962 default:
1963 // For generic_fields, the T_IMEMO/fields is responsible for populating the entry.
1964 break;
1965 }
1966}
1967
1968static VALUE
1969object_id_to_ref(void *objspace_ptr, VALUE object_id)
1970{
1971 rb_objspace_t *objspace = objspace_ptr;
1972
1973 unsigned int lev = RB_GC_VM_LOCK();
1974
1975 if (!id2ref_tbl) {
1976 rb_gc_vm_barrier(); // stop other ractors
1977
1978 // GC Must not trigger while we build the table, otherwise if we end
1979 // up freeing an object that had an ID, we might try to delete it from
1980 // the table even though it wasn't inserted yet.
1981 st_table *tmp_id2ref_tbl = st_init_table(&object_id_hash_type);
1982 VALUE tmp_id2ref_value = TypedData_Wrap_Struct(0, &id2ref_tbl_type, tmp_id2ref_tbl);
1983
1984 // build_id2ref_i will most certainly malloc, which could trigger GC and sweep
1985 // objects we just added to the table.
1986 // By calling rb_gc_disable() we also save having to handle potentially garbage objects.
1987 bool gc_disabled = RTEST(rb_gc_disable());
1988 {
1989 id2ref_tbl = tmp_id2ref_tbl;
1990 id2ref_value = tmp_id2ref_value;
1991
1992 rb_gc_impl_each_object(objspace, build_id2ref_i, (void *)id2ref_tbl);
1993 }
1994 if (!gc_disabled) rb_gc_enable();
1995 }
1996
1997 VALUE obj;
1998 bool found = st_lookup(id2ref_tbl, object_id, &obj) && !rb_gc_impl_garbage_object_p(objspace, obj);
1999
2000 RB_GC_VM_UNLOCK(lev);
2001
2002 if (found) {
2003 return obj;
2004 }
2005
2006 if (rb_funcall(object_id, rb_intern(">="), 1, ULL2NUM(LAST_OBJECT_ID()))) {
2007 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is not an id value", rb_funcall(object_id, rb_intern("to_s"), 1, INT2FIX(10)));
2008 }
2009 else {
2010 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is a recycled object", rb_funcall(object_id, rb_intern("to_s"), 1, INT2FIX(10)));
2011 }
2012}
2013
2014static inline void
2015obj_free_object_id(VALUE obj)
2016{
2017 VALUE obj_id = 0;
2018 if (RB_UNLIKELY(id2ref_tbl)) {
2019 switch (BUILTIN_TYPE(obj)) {
2020 case T_CLASS:
2021 case T_MODULE:
2022 obj_id = RCLASS(obj)->object_id;
2023 break;
2024 case T_IMEMO:
2025 if (!IMEMO_TYPE_P(obj, imemo_fields)) {
2026 return;
2027 }
2028 // fallthrough
2029 case T_OBJECT:
2030 {
2031 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2032 if (rb_shape_has_object_id(shape_id)) {
2033 obj_id = object_id_get(obj, shape_id);
2034 }
2035 break;
2036 }
2037 default:
2038 // For generic_fields, the T_IMEMO/fields is responsible for freeing the id.
2039 return;
2040 }
2041
2042 if (RB_UNLIKELY(obj_id)) {
2043 RUBY_ASSERT(FIXNUM_P(obj_id) || RB_TYPE_P(obj_id, T_BIGNUM));
2044
2045 if (!st_delete(id2ref_tbl, (st_data_t *)&obj_id, NULL)) {
2046 // The the object is a T_IMEMO/fields, then it's possible the actual object
2047 // has been garbage collected already.
2048 if (!RB_TYPE_P(obj, T_IMEMO)) {
2049 rb_bug("Object ID seen, but not in _id2ref table: object_id=%llu object=%s", NUM2ULL(obj_id), rb_obj_info(obj));
2050 }
2051 }
2052 }
2053 }
2054}
2055
2056void
2057rb_gc_obj_free_vm_weak_references(VALUE obj)
2058{
2060 obj_free_object_id(obj);
2061
2062 if (rb_obj_gen_fields_p(obj)) {
2064 }
2065
2066 switch (BUILTIN_TYPE(obj)) {
2067 case T_STRING:
2068 if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2069 rb_gc_free_fstring(obj);
2070 }
2071 break;
2072 case T_SYMBOL:
2073 rb_gc_free_dsymbol(obj);
2074 break;
2075 case T_IMEMO:
2076 switch (imemo_type(obj)) {
2077 case imemo_callcache: {
2078 const struct rb_callcache *cc = (const struct rb_callcache *)obj;
2079
2080 if (vm_cc_refinement_p(cc)) {
2081 rb_vm_delete_cc_refinement(cc);
2082 }
2083
2084 break;
2085 }
2086 case imemo_callinfo:
2087 rb_vm_ci_free((const struct rb_callinfo *)obj);
2088 break;
2089 case imemo_ment:
2090 rb_free_method_entry_vm_weak_references((const rb_method_entry_t *)obj);
2091 break;
2092 default:
2093 break;
2094 }
2095 break;
2096 default:
2097 break;
2098 }
2099}
2100
2101/*
2102 * call-seq:
2103 * ObjectSpace._id2ref(object_id) -> an_object
2104 *
2105 * Converts an object id to a reference to the object. May not be
2106 * called on an object id passed as a parameter to a finalizer.
2107 *
2108 * s = "I am a string" #=> "I am a string"
2109 * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
2110 * r == s #=> true
2111 *
2112 * On multi-ractor mode, if the object is not shareable, it raises
2113 * RangeError.
2114 *
2115 * This method is deprecated and should no longer be used.
2116 */
2117
2118static VALUE
2119id2ref(VALUE objid)
2120{
2121 objid = rb_to_int(objid);
2122 if (FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
2123 VALUE ptr = (VALUE)NUM2PTR(objid);
2124 if (SPECIAL_CONST_P(ptr)) {
2125 if (ptr == Qtrue) return Qtrue;
2126 if (ptr == Qfalse) return Qfalse;
2127 if (NIL_P(ptr)) return Qnil;
2128 if (FIXNUM_P(ptr)) return ptr;
2129 if (FLONUM_P(ptr)) return ptr;
2130
2131 if (SYMBOL_P(ptr)) {
2132 // Check that the symbol is valid
2133 if (rb_static_id_valid_p(SYM2ID(ptr))) {
2134 return ptr;
2135 }
2136 else {
2137 rb_raise(rb_eRangeError, "%p is not a symbol id value", (void *)ptr);
2138 }
2139 }
2140
2141 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is not an id value", rb_int2str(objid, 10));
2142 }
2143 }
2144
2145 VALUE obj = object_id_to_ref(rb_gc_get_objspace(), objid);
2146 if (!rb_multi_ractor_p() || rb_ractor_shareable_p(obj)) {
2147 return obj;
2148 }
2149 else {
2150 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is the id of an unshareable object on multi-ractor", rb_int2str(objid, 10));
2151 }
2152}
2153
2154/* :nodoc: */
2155static VALUE
2156os_id2ref(VALUE os, VALUE objid)
2157{
2158 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "ObjectSpace._id2ref is deprecated");
2159 return id2ref(objid);
2160}
2161
2162static VALUE
2163rb_find_object_id(void *objspace, VALUE obj, VALUE (*get_heap_object_id)(VALUE))
2164{
2165 if (SPECIAL_CONST_P(obj)) {
2166#if SIZEOF_LONG == SIZEOF_VOIDP
2167 return LONG2NUM((SIGNED_VALUE)obj);
2168#else
2169 return LL2NUM((SIGNED_VALUE)obj);
2170#endif
2171 }
2172
2173 return get_heap_object_id(obj);
2174}
2175
2176static VALUE
2177nonspecial_obj_id(VALUE obj)
2178{
2179#if SIZEOF_LONG == SIZEOF_VOIDP
2180 return (VALUE)((SIGNED_VALUE)(obj)|FIXNUM_FLAG);
2181#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2182 return LL2NUM((SIGNED_VALUE)(obj) / 2);
2183#else
2184# error not supported
2185#endif
2186}
2187
2188VALUE
2189rb_memory_id(VALUE obj)
2190{
2191 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
2192}
2193
2194/*
2195 * Document-method: __id__
2196 * Document-method: object_id
2197 *
2198 * call-seq:
2199 * obj.__id__ -> integer
2200 * obj.object_id -> integer
2201 *
2202 * Returns an integer identifier for +obj+.
2203 *
2204 * The same number will be returned on all calls to +object_id+ for a given
2205 * object, and no two active objects will share an id.
2206 *
2207 * Note: that some objects of builtin classes are reused for optimization.
2208 * This is the case for immediate values and frozen string literals.
2209 *
2210 * BasicObject implements +__id__+, Kernel implements +object_id+.
2211 *
2212 * Immediate values are not passed by reference but are passed by value:
2213 * +nil+, +true+, +false+, Fixnums, Symbols, and some Floats.
2214 *
2215 * Object.new.object_id == Object.new.object_id # => false
2216 * (21 * 2).object_id == (21 * 2).object_id # => true
2217 * "hello".object_id == "hello".object_id # => false
2218 * "hi".freeze.object_id == "hi".freeze.object_id # => true
2219 */
2220
2221VALUE
2222rb_obj_id(VALUE obj)
2223{
2224 /* If obj is an immediate, the object ID is obj directly converted to a Numeric.
2225 * Otherwise, the object ID is a Numeric that is a non-zero multiple of
2226 * (RUBY_IMMEDIATE_MASK + 1) which guarantees that it does not collide with
2227 * any immediates. */
2228 return rb_find_object_id(rb_gc_get_objspace(), obj, object_id);
2229}
2230
2231bool
2232rb_obj_id_p(VALUE obj)
2233{
2234 return !RB_TYPE_P(obj, T_IMEMO) && rb_shape_obj_has_id(obj);
2235}
2236
2237/*
2238 * GC implementations should call this function before the GC phase that updates references
2239 * embedded in the machine code generated by JIT compilers. JIT compilers usually enforce the
2240 * "W^X" policy and protect the code memory from being modified during execution. This function
2241 * makes the code memory writeable.
2242 */
2243void
2244rb_gc_before_updating_jit_code(void)
2245{
2246#if USE_YJIT
2247 rb_yjit_mark_all_writeable();
2248#endif
2249}
2250
2251/*
2252 * GC implementations should call this function before the GC phase that updates references
2253 * embedded in the machine code generated by JIT compilers. This function makes the code memory
2254 * executable again.
2255 */
2256void
2257rb_gc_after_updating_jit_code(void)
2258{
2259#if USE_YJIT
2260 rb_yjit_mark_all_executable();
2261#endif
2262}
2263
2264static void
2265classext_memsize(rb_classext_t *ext, bool prime, VALUE box_value, void *arg)
2266{
2267 size_t *size = (size_t *)arg;
2268 size_t s = 0;
2269
2270 if (RCLASSEXT_M_TBL(ext)) {
2271 s += rb_id_table_memsize(RCLASSEXT_M_TBL(ext));
2272 }
2273 if (RCLASSEXT_CONST_TBL(ext)) {
2274 s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
2275 }
2276 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2277 s += (RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1) * sizeof(VALUE);
2278 }
2279 if (!prime) {
2280 s += sizeof(rb_classext_t);
2281 }
2282 *size += s;
2283}
2284
2285static void
2286classext_superclasses_memsize(rb_classext_t *ext, bool prime, VALUE box_value, void *arg)
2287{
2288 size_t *size = (size_t *)arg;
2289 size_t array_size;
2290 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2291 RUBY_ASSERT(prime);
2292 array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
2293 *size += array_size * sizeof(VALUE);
2294 }
2295}
2296
2297size_t
2298rb_obj_memsize_of(VALUE obj)
2299{
2300 size_t size = 0;
2301
2302 if (SPECIAL_CONST_P(obj)) {
2303 return 0;
2304 }
2305
2306 switch (BUILTIN_TYPE(obj)) {
2307 case T_OBJECT:
2308 if (FL_TEST_RAW(obj, ROBJECT_HEAP)) {
2309 if (rb_shape_obj_too_complex_p(obj)) {
2310 size += rb_st_memsize(ROBJECT_FIELDS_HASH(obj));
2311 }
2312 else {
2313 size += ROBJECT_FIELDS_CAPACITY(obj) * sizeof(VALUE);
2314 }
2315 }
2316 break;
2317 case T_MODULE:
2318 case T_CLASS:
2319 rb_class_classext_foreach(obj, classext_memsize, (void *)&size);
2320 rb_class_classext_foreach(obj, classext_superclasses_memsize, (void *)&size);
2321 break;
2322 case T_ICLASS:
2323 if (RICLASS_OWNS_M_TBL_P(obj)) {
2324 if (RCLASS_M_TBL(obj)) {
2325 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
2326 }
2327 }
2328 break;
2329 case T_STRING:
2330 size += rb_str_memsize(obj);
2331 break;
2332 case T_ARRAY:
2333 size += rb_ary_memsize(obj);
2334 break;
2335 case T_HASH:
2336 if (RHASH_ST_TABLE_P(obj)) {
2337 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
2338 /* st_table is in the slot */
2339 size += st_memsize(RHASH_ST_TABLE(obj)) - sizeof(st_table);
2340 }
2341 break;
2342 case T_REGEXP:
2343 if (RREGEXP_PTR(obj)) {
2344 size += onig_memsize(RREGEXP_PTR(obj));
2345 }
2346 break;
2347 case T_DATA:
2348 size += rb_objspace_data_type_memsize(obj);
2349 break;
2350 case T_MATCH:
2351 {
2352 rb_matchext_t *rm = RMATCH_EXT(obj);
2353 size += onig_region_memsize(&rm->regs);
2354 size += sizeof(struct rmatch_offset) * rm->char_offset_num_allocated;
2355 }
2356 break;
2357 case T_FILE:
2358 if (RFILE(obj)->fptr) {
2359 size += rb_io_memsize(RFILE(obj)->fptr);
2360 }
2361 break;
2362 case T_RATIONAL:
2363 case T_COMPLEX:
2364 break;
2365 case T_IMEMO:
2366 size += rb_imemo_memsize(obj);
2367 break;
2368
2369 case T_FLOAT:
2370 case T_SYMBOL:
2371 break;
2372
2373 case T_BIGNUM:
2374 if (!(RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2375 size += BIGNUM_LEN(obj) * sizeof(BDIGIT);
2376 }
2377 break;
2378
2379 case T_NODE:
2380 UNEXPECTED_NODE(obj_memsize_of);
2381 break;
2382
2383 case T_STRUCT:
2384 if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
2385 RSTRUCT(obj)->as.heap.ptr) {
2386 size += sizeof(VALUE) * RSTRUCT_LEN(obj);
2387 }
2388 break;
2389
2390 case T_ZOMBIE:
2391 case T_MOVED:
2392 break;
2393
2394 default:
2395 rb_bug("objspace/memsize_of(): unknown data type 0x%x(%p)",
2396 BUILTIN_TYPE(obj), (void*)obj);
2397 }
2398
2399 return size + rb_gc_obj_slot_size(obj);
2400}
2401
2402static int
2403set_zero(st_data_t key, st_data_t val, st_data_t arg)
2404{
2405 VALUE k = (VALUE)key;
2406 VALUE hash = (VALUE)arg;
2407 rb_hash_aset(hash, k, INT2FIX(0));
2408 return ST_CONTINUE;
2409}
2410
2412 size_t counts[T_MASK+1];
2413 size_t freed;
2414 size_t total;
2415};
2416
2417static void
2418count_objects_i(VALUE obj, void *d)
2419{
2420 struct count_objects_data *data = (struct count_objects_data *)d;
2421
2422 if (RBASIC(obj)->flags) {
2423 data->counts[BUILTIN_TYPE(obj)]++;
2424 }
2425 else {
2426 data->freed++;
2427 }
2428
2429 data->total++;
2430}
2431
2432/*
2433 * call-seq:
2434 * ObjectSpace.count_objects([result_hash]) -> hash
2435 *
2436 * Counts all objects grouped by type.
2437 *
2438 * It returns a hash, such as:
2439 * {
2440 * :TOTAL=>10000,
2441 * :FREE=>3011,
2442 * :T_OBJECT=>6,
2443 * :T_CLASS=>404,
2444 * # ...
2445 * }
2446 *
2447 * The contents of the returned hash are implementation specific.
2448 * It may be changed in future.
2449 *
2450 * The keys starting with +:T_+ means live objects.
2451 * For example, +:T_ARRAY+ is the number of arrays.
2452 * +:FREE+ means object slots which is not used now.
2453 * +:TOTAL+ means sum of above.
2454 *
2455 * If the optional argument +result_hash+ is given,
2456 * it is overwritten and returned. This is intended to avoid probe effect.
2457 *
2458 * h = {}
2459 * ObjectSpace.count_objects(h)
2460 * puts h
2461 * # => { :TOTAL=>10000, :T_CLASS=>158280, :T_MODULE=>20672, :T_STRING=>527249 }
2462 *
2463 * This method is only expected to work on C Ruby.
2464 *
2465 */
2466
2467static VALUE
2468count_objects(int argc, VALUE *argv, VALUE os)
2469{
2470 struct count_objects_data data = { 0 };
2471 VALUE hash = Qnil;
2472 VALUE types[T_MASK + 1];
2473
2474 if (rb_check_arity(argc, 0, 1) == 1) {
2475 hash = argv[0];
2476 if (!RB_TYPE_P(hash, T_HASH))
2477 rb_raise(rb_eTypeError, "non-hash given");
2478 }
2479
2480 for (size_t i = 0; i <= T_MASK; i++) {
2481 // type_sym can allocate an object,
2482 // so we need to create all key symbols in advance
2483 // not to disturb the result
2484 types[i] = type_sym(i);
2485 }
2486
2487 // Same as type_sym, we need to create all key symbols in advance
2488 VALUE total = ID2SYM(rb_intern("TOTAL"));
2489 VALUE free = ID2SYM(rb_intern("FREE"));
2490
2491 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2492
2493 if (NIL_P(hash)) {
2494 hash = rb_hash_new();
2495 }
2496 else if (!RHASH_EMPTY_P(hash)) {
2497 rb_hash_stlike_foreach(hash, set_zero, hash);
2498 }
2499 rb_hash_aset(hash, total, SIZET2NUM(data.total));
2500 rb_hash_aset(hash, free, SIZET2NUM(data.freed));
2501
2502 for (size_t i = 0; i <= T_MASK; i++) {
2503 if (data.counts[i]) {
2504 rb_hash_aset(hash, types[i], SIZET2NUM(data.counts[i]));
2505 }
2506 }
2507
2508 return hash;
2509}
2510
2511#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2512
2513#define STACK_START (ec->machine.stack_start)
2514#define STACK_END (ec->machine.stack_end)
2515#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2516
2517#if STACK_GROW_DIRECTION < 0
2518# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2519#elif STACK_GROW_DIRECTION > 0
2520# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2521#else
2522# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2523 : (size_t)(STACK_END - STACK_START + 1))
2524#endif
2525#if !STACK_GROW_DIRECTION
2526int ruby_stack_grow_direction;
2527int
2528ruby_get_stack_grow_direction(volatile VALUE *addr)
2529{
2530 VALUE *end;
2531 SET_MACHINE_STACK_END(&end);
2532
2533 if (end > addr) return ruby_stack_grow_direction = 1;
2534 return ruby_stack_grow_direction = -1;
2535}
2536#endif
2537
2538size_t
2540{
2541 rb_execution_context_t *ec = GET_EC();
2542 SET_STACK_END;
2543 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2544 return STACK_LENGTH;
2545}
2546
2547#define PREVENT_STACK_OVERFLOW 1
2548#ifndef PREVENT_STACK_OVERFLOW
2549#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2550# define PREVENT_STACK_OVERFLOW 1
2551#else
2552# define PREVENT_STACK_OVERFLOW 0
2553#endif
2554#endif
2555#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2556static int
2557stack_check(rb_execution_context_t *ec, int water_mark)
2558{
2559 SET_STACK_END;
2560
2561 size_t length = STACK_LENGTH;
2562 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2563
2564 return length > maximum_length;
2565}
2566#else
2567#define stack_check(ec, water_mark) FALSE
2568#endif
2569
2570#define STACKFRAME_FOR_CALL_CFUNC 2048
2571
2572int
2573rb_ec_stack_check(rb_execution_context_t *ec)
2574{
2575 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2576}
2577
2578int
2580{
2581 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2582}
2583
2584/* ==================== Marking ==================== */
2585
2586#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2587 if (!RB_SPECIAL_CONST_P(obj)) { \
2588 rb_vm_t *vm = GET_VM(); \
2589 void *objspace = vm->gc.objspace; \
2590 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2591 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2592 (func)(objspace, (obj_or_ptr)); \
2593 } \
2594 else if (check_obj ? \
2595 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2596 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2597 true) { \
2598 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2599 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2600 vm->gc.mark_func_data = NULL; \
2601 mark_func_data->mark_func((obj), mark_func_data->data); \
2602 vm->gc.mark_func_data = mark_func_data; \
2603 } \
2604 } \
2605} while (0)
2606
2607static inline void
2608gc_mark_internal(VALUE obj)
2609{
2610 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj, false);
2611}
2612
2613void
2614rb_gc_mark_movable(VALUE obj)
2615{
2616 gc_mark_internal(obj);
2617}
2618
2619void
2620rb_gc_mark_and_move(VALUE *ptr)
2621{
2622 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr, false);
2623}
2624
2625static inline void
2626gc_mark_and_pin_internal(VALUE obj)
2627{
2628 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj, false);
2629}
2630
2631void
2632rb_gc_mark(VALUE obj)
2633{
2634 gc_mark_and_pin_internal(obj);
2635}
2636
2637static inline void
2638gc_mark_maybe_internal(VALUE obj)
2639{
2640 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj, true);
2641}
2642
2643void
2644rb_gc_mark_maybe(VALUE obj)
2645{
2646 gc_mark_maybe_internal(obj);
2647}
2648
2649void
2650rb_gc_mark_weak(VALUE *ptr)
2651{
2652 if (RB_SPECIAL_CONST_P(*ptr)) return;
2653
2654 rb_vm_t *vm = GET_VM();
2655 void *objspace = vm->gc.objspace;
2656 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2657 GC_ASSERT(rb_gc_impl_during_gc_p(objspace));
2658
2659 rb_gc_impl_mark_weak(objspace, ptr);
2660 }
2661 else {
2662 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace));
2663 }
2664}
2665
2666void
2667rb_gc_remove_weak(VALUE parent_obj, VALUE *ptr)
2668{
2669 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj, ptr);
2670}
2671
2672ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static void each_location(register const VALUE *x, register long n, void (*cb)(VALUE, void *), void *data));
2673static void
2674each_location(register const VALUE *x, register long n, void (*cb)(VALUE, void *), void *data)
2675{
2676 VALUE v;
2677 while (n--) {
2678 v = *x;
2679 cb(v, data);
2680 x++;
2681 }
2682}
2683
2684static void
2685each_location_ptr(const VALUE *start, const VALUE *end, void (*cb)(VALUE, void *), void *data)
2686{
2687 if (end <= start) return;
2688 each_location(start, end - start, cb, data);
2689}
2690
2691static void
2692gc_mark_maybe_each_location(VALUE obj, void *data)
2693{
2694 gc_mark_maybe_internal(obj);
2695}
2696
2697void
2698rb_gc_mark_locations(const VALUE *start, const VALUE *end)
2699{
2700 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2701}
2702
2703void
2704rb_gc_mark_values(long n, const VALUE *values)
2705{
2706 for (long i = 0; i < n; i++) {
2707 gc_mark_internal(values[i]);
2708 }
2709}
2710
2711void
2712rb_gc_mark_vm_stack_values(long n, const VALUE *values)
2713{
2714 for (long i = 0; i < n; i++) {
2715 gc_mark_and_pin_internal(values[i]);
2716 }
2717}
2718
2719static int
2720mark_key(st_data_t key, st_data_t value, st_data_t data)
2721{
2722 gc_mark_and_pin_internal((VALUE)key);
2723
2724 return ST_CONTINUE;
2725}
2726
2727void
2728rb_mark_set(st_table *tbl)
2729{
2730 if (!tbl) return;
2731
2732 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2733}
2734
2735static int
2736mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2737{
2738 gc_mark_internal((VALUE)key);
2739 gc_mark_internal((VALUE)value);
2740
2741 return ST_CONTINUE;
2742}
2743
2744static int
2745pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2746{
2747 gc_mark_and_pin_internal((VALUE)key);
2748 gc_mark_and_pin_internal((VALUE)value);
2749
2750 return ST_CONTINUE;
2751}
2752
2753static int
2754pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2755{
2756 gc_mark_and_pin_internal((VALUE)key);
2757 gc_mark_internal((VALUE)value);
2758
2759 return ST_CONTINUE;
2760}
2761
2762static void
2763mark_hash(VALUE hash)
2764{
2765 if (rb_hash_compare_by_id_p(hash)) {
2766 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2767 }
2768 else {
2769 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2770 }
2771
2772 gc_mark_internal(RHASH(hash)->ifnone);
2773}
2774
2775void
2776rb_mark_hash(st_table *tbl)
2777{
2778 if (!tbl) return;
2779
2780 st_foreach(tbl, pin_key_pin_value, 0);
2781}
2782
2783static enum rb_id_table_iterator_result
2784mark_method_entry_i(VALUE me, void *objspace)
2785{
2786 gc_mark_internal(me);
2787
2788 return ID_TABLE_CONTINUE;
2789}
2790
2791static void
2792mark_m_tbl(void *objspace, struct rb_id_table *tbl)
2793{
2794 if (tbl) {
2795 rb_id_table_foreach_values(tbl, mark_method_entry_i, objspace);
2796 }
2797}
2798
2799static enum rb_id_table_iterator_result
2800mark_const_entry_i(VALUE value, void *objspace)
2801{
2802 const rb_const_entry_t *ce = (const rb_const_entry_t *)value;
2803
2804 if (!rb_gc_checking_shareable()) {
2805 gc_mark_internal(ce->value);
2806 gc_mark_internal(ce->file); // TODO: ce->file should be shareable?
2807 }
2808 return ID_TABLE_CONTINUE;
2809}
2810
2811static void
2812mark_const_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
2813{
2814 if (!tbl) return;
2815 rb_id_table_foreach_values(tbl, mark_const_entry_i, objspace);
2816}
2817
2818#if STACK_GROW_DIRECTION < 0
2819#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2820#elif STACK_GROW_DIRECTION > 0
2821#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2822#else
2823#define GET_STACK_BOUNDS(start, end, appendix) \
2824 ((STACK_END < STACK_START) ? \
2825 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2826#endif
2827
2828static void
2829gc_mark_machine_stack_location_maybe(VALUE obj, void *data)
2830{
2831 gc_mark_maybe_internal(obj);
2832
2833#ifdef RUBY_ASAN_ENABLED
2834 const rb_execution_context_t *ec = (const rb_execution_context_t *)data;
2835 void *fake_frame_start;
2836 void *fake_frame_end;
2837 bool is_fake_frame = asan_get_fake_stack_extents(
2838 ec->machine.asan_fake_stack_handle, obj,
2839 ec->machine.stack_start, ec->machine.stack_end,
2840 &fake_frame_start, &fake_frame_end
2841 );
2842 if (is_fake_frame) {
2843 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2844 }
2845#endif
2846}
2847
2848static bool
2849gc_object_moved_p_internal(void *objspace, VALUE obj)
2850{
2851 if (SPECIAL_CONST_P(obj)) {
2852 return false;
2853 }
2854
2855 return rb_gc_impl_object_moved_p(objspace, obj);
2856}
2857
2858static VALUE
2859gc_location_internal(void *objspace, VALUE value)
2860{
2861 if (SPECIAL_CONST_P(value)) {
2862 return value;
2863 }
2864
2865 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(objspace, (void *)value));
2866
2867 return rb_gc_impl_location(objspace, value);
2868}
2869
2870VALUE
2871rb_gc_location(VALUE value)
2872{
2873 return gc_location_internal(rb_gc_get_objspace(), value);
2874}
2875
2876#if defined(__wasm__)
2877
2878
2879static VALUE *rb_stack_range_tmp[2];
2880
2881static void
2882rb_mark_locations(void *begin, void *end)
2883{
2884 rb_stack_range_tmp[0] = begin;
2885 rb_stack_range_tmp[1] = end;
2886}
2887
2888void
2889rb_gc_save_machine_context(void)
2890{
2891 // no-op
2892}
2893
2894# if defined(__EMSCRIPTEN__)
2895
2896static void
2897mark_current_machine_context(const rb_execution_context_t *ec)
2898{
2899 emscripten_scan_stack(rb_mark_locations);
2900 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2901
2902 emscripten_scan_registers(rb_mark_locations);
2903 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2904}
2905# else // use Asyncify version
2906
2907static void
2908mark_current_machine_context(rb_execution_context_t *ec)
2909{
2910 VALUE *stack_start, *stack_end;
2911 SET_STACK_END;
2912 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2913 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2914
2915 rb_wasm_scan_locals(rb_mark_locations);
2916 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2917}
2918
2919# endif
2920
2921#else // !defined(__wasm__)
2922
2923void
2924rb_gc_save_machine_context(void)
2925{
2926 rb_thread_t *thread = GET_THREAD();
2927
2928 RB_VM_SAVE_MACHINE_CONTEXT(thread);
2929}
2930
2931
2932static void
2933mark_current_machine_context(const rb_execution_context_t *ec)
2934{
2935 rb_gc_mark_machine_context(ec);
2936}
2937#endif
2938
2939void
2940rb_gc_mark_machine_context(const rb_execution_context_t *ec)
2941{
2942 VALUE *stack_start, *stack_end;
2943
2944 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2945 RUBY_DEBUG_LOG("ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2946
2947 void *data =
2948#ifdef RUBY_ASAN_ENABLED
2949 /* gc_mark_machine_stack_location_maybe() uses data as const */
2950 (rb_execution_context_t *)ec;
2951#else
2952 NULL;
2953#endif
2954
2955 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2956 int num_regs = sizeof(ec->machine.regs)/(sizeof(VALUE));
2957 each_location((VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2958}
2959
2960static int
2961rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2962{
2963 gc_mark_and_pin_internal((VALUE)value);
2964
2965 return ST_CONTINUE;
2966}
2967
2968void
2969rb_mark_tbl(st_table *tbl)
2970{
2971 if (!tbl || tbl->num_entries == 0) return;
2972
2973 st_foreach(tbl, rb_mark_tbl_i, 0);
2974}
2975
2976static void
2977gc_mark_tbl_no_pin(st_table *tbl)
2978{
2979 if (!tbl || tbl->num_entries == 0) return;
2980
2981 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
2982}
2983
2984void
2985rb_mark_tbl_no_pin(st_table *tbl)
2986{
2987 gc_mark_tbl_no_pin(tbl);
2988}
2989
2990static bool
2991gc_declarative_marking_p(const rb_data_type_t *type)
2992{
2993 return (type->flags & RUBY_TYPED_DECL_MARKING) != 0;
2994}
2995
2996void
2997rb_gc_mark_roots(void *objspace, const char **categoryp)
2998{
2999 rb_execution_context_t *ec = GET_EC();
3000 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3001
3002#define MARK_CHECKPOINT(category) do { \
3003 if (categoryp) *categoryp = category; \
3004} while (0)
3005
3006 MARK_CHECKPOINT("vm");
3007 rb_vm_mark(vm);
3008
3009 MARK_CHECKPOINT("end_proc");
3010 rb_mark_end_proc();
3011
3012 MARK_CHECKPOINT("global_tbl");
3013 rb_gc_mark_global_tbl();
3014
3015#if USE_YJIT
3016 void rb_yjit_root_mark(void); // in Rust
3017
3018 if (rb_yjit_enabled_p) {
3019 MARK_CHECKPOINT("YJIT");
3020 rb_yjit_root_mark();
3021 }
3022#endif
3023
3024#if USE_ZJIT
3025 void rb_zjit_root_mark(void);
3026 if (rb_zjit_enabled_p) {
3027 MARK_CHECKPOINT("ZJIT");
3028 rb_zjit_root_mark();
3029 }
3030#endif
3031
3032 MARK_CHECKPOINT("machine_context");
3033 mark_current_machine_context(ec);
3034
3035 MARK_CHECKPOINT("global_symbols");
3036 rb_sym_global_symbols_mark_and_move();
3037
3038 MARK_CHECKPOINT("finish");
3039
3040#undef MARK_CHECKPOINT
3041}
3042
3044 rb_objspace_t *objspace;
3045 VALUE obj;
3046};
3047
3048static void
3049gc_mark_classext_module(rb_classext_t *ext, bool prime, VALUE box_value, void *arg)
3050{
3052 rb_objspace_t *objspace = foreach_arg->objspace;
3053
3054 if (RCLASSEXT_SUPER(ext)) {
3055 gc_mark_internal(RCLASSEXT_SUPER(ext));
3056 }
3057 mark_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3058
3059 if (!rb_gc_checking_shareable()) {
3060 // unshareable
3061 gc_mark_internal(RCLASSEXT_FIELDS_OBJ(ext));
3062 gc_mark_internal(RCLASSEXT_CVC_TBL(ext));
3063 }
3064
3065 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
3066 mark_const_tbl(objspace, RCLASSEXT_CONST_TBL(ext));
3067 }
3068 mark_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3069 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3070 gc_mark_internal(RCLASSEXT_CLASSPATH(ext));
3071}
3072
3073static void
3074gc_mark_classext_iclass(rb_classext_t *ext, bool prime, VALUE box_value, void *arg)
3075{
3077 rb_objspace_t *objspace = foreach_arg->objspace;
3078
3079 if (RCLASSEXT_SUPER(ext)) {
3080 gc_mark_internal(RCLASSEXT_SUPER(ext));
3081 }
3082 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
3083 mark_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3084 }
3085 if (RCLASSEXT_INCLUDER(ext)) {
3086 gc_mark_internal(RCLASSEXT_INCLUDER(ext));
3087 }
3088 mark_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3089 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3090}
3091
3092#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA_TYPE(d)->function.dmark
3093
3094void
3095rb_gc_mark_children(void *objspace, VALUE obj)
3096{
3097 struct gc_mark_classext_foreach_arg foreach_args;
3098
3099 if (rb_obj_gen_fields_p(obj)) {
3100 rb_mark_generic_ivar(obj);
3101 }
3102
3103 switch (BUILTIN_TYPE(obj)) {
3104 case T_FLOAT:
3105 case T_BIGNUM:
3106 return;
3107
3108 case T_NIL:
3109 case T_FIXNUM:
3110 rb_bug("rb_gc_mark() called for broken object");
3111 break;
3112
3113 case T_NODE:
3114 UNEXPECTED_NODE(rb_gc_mark);
3115 break;
3116
3117 case T_IMEMO:
3118 rb_imemo_mark_and_move(obj, false);
3119 return;
3120
3121 default:
3122 break;
3123 }
3124
3125 gc_mark_internal(RBASIC(obj)->klass);
3126
3127 switch (BUILTIN_TYPE(obj)) {
3128 case T_CLASS:
3129 if (FL_TEST_RAW(obj, FL_SINGLETON) &&
3130 !rb_gc_checking_shareable()) {
3131 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
3132 }
3133 // Continue to the shared T_CLASS/T_MODULE
3134 case T_MODULE:
3135 foreach_args.objspace = objspace;
3136 foreach_args.obj = obj;
3137 rb_class_classext_foreach(obj, gc_mark_classext_module, (void *)&foreach_args);
3138 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3139 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3140 }
3141 break;
3142
3143 case T_ICLASS:
3144 foreach_args.objspace = objspace;
3145 foreach_args.obj = obj;
3146 rb_class_classext_foreach(obj, gc_mark_classext_iclass, (void *)&foreach_args);
3147 if (BOX_USER_P(RCLASS_PRIME_BOX(obj))) {
3148 gc_mark_internal(RCLASS_PRIME_BOX(obj)->box_object);
3149 }
3150 break;
3151
3152 case T_ARRAY:
3153 if (ARY_SHARED_P(obj)) {
3154 VALUE root = ARY_SHARED_ROOT(obj);
3155 gc_mark_internal(root);
3156 }
3157 else {
3158 long len = RARRAY_LEN(obj);
3159 const VALUE *ptr = RARRAY_CONST_PTR(obj);
3160 for (long i = 0; i < len; i++) {
3161 gc_mark_internal(ptr[i]);
3162 }
3163 }
3164 break;
3165
3166 case T_HASH:
3167 mark_hash(obj);
3168 break;
3169
3170 case T_SYMBOL:
3171 gc_mark_internal(RSYMBOL(obj)->fstr);
3172 break;
3173
3174 case T_STRING:
3175 if (STR_SHARED_P(obj)) {
3176 if (STR_EMBED_P(RSTRING(obj)->as.heap.aux.shared)) {
3177 /* Embedded shared strings cannot be moved because this string
3178 * points into the slot of the shared string. There may be code
3179 * using the RSTRING_PTR on the stack, which would pin this
3180 * string but not pin the shared string, causing it to move. */
3181 gc_mark_and_pin_internal(RSTRING(obj)->as.heap.aux.shared);
3182 }
3183 else {
3184 gc_mark_internal(RSTRING(obj)->as.heap.aux.shared);
3185 }
3186 }
3187 break;
3188
3189 case T_DATA: {
3190 bool typed_data = RTYPEDDATA_P(obj);
3191 void *const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
3192
3193 if (typed_data) {
3194 gc_mark_internal(RTYPEDDATA(obj)->fields_obj);
3195 }
3196
3197 if (ptr) {
3198 if (typed_data && gc_declarative_marking_p(RTYPEDDATA_TYPE(obj))) {
3199 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3200
3201 for (size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3202 gc_mark_internal(*(VALUE *)((char *)ptr + offset));
3203 }
3204 }
3205 else {
3206 RUBY_DATA_FUNC mark_func = typed_data ?
3208 RDATA(obj)->dmark;
3209 if (mark_func) (*mark_func)(ptr);
3210 }
3211 }
3212
3213 break;
3214 }
3215
3216 case T_OBJECT: {
3217 uint32_t len;
3218 if (rb_shape_obj_too_complex_p(obj)) {
3219 gc_mark_tbl_no_pin(ROBJECT_FIELDS_HASH(obj));
3220 len = ROBJECT_FIELDS_COUNT_COMPLEX(obj);
3221 }
3222 else {
3223 const VALUE * const ptr = ROBJECT_FIELDS(obj);
3224
3225 len = ROBJECT_FIELDS_COUNT_NOT_COMPLEX(obj);
3226 for (uint32_t i = 0; i < len; i++) {
3227 gc_mark_internal(ptr[i]);
3228 }
3229 }
3230
3231 attr_index_t fields_count = (attr_index_t)len;
3232 if (fields_count) {
3233 VALUE klass = RBASIC_CLASS(obj);
3234
3235 // Increment max_iv_count if applicable, used to determine size pool allocation
3236 if (RCLASS_MAX_IV_COUNT(klass) < fields_count) {
3237 RCLASS_SET_MAX_IV_COUNT(klass, fields_count);
3238 }
3239 }
3240
3241 break;
3242 }
3243
3244 case T_FILE:
3245 if (RFILE(obj)->fptr) {
3246 gc_mark_internal(RFILE(obj)->fptr->self);
3247 gc_mark_internal(RFILE(obj)->fptr->pathv);
3248 gc_mark_internal(RFILE(obj)->fptr->tied_io_for_writing);
3249 gc_mark_internal(RFILE(obj)->fptr->writeconv_asciicompat);
3250 gc_mark_internal(RFILE(obj)->fptr->writeconv_pre_ecopts);
3251 gc_mark_internal(RFILE(obj)->fptr->encs.ecopts);
3252 gc_mark_internal(RFILE(obj)->fptr->write_lock);
3253 gc_mark_internal(RFILE(obj)->fptr->timeout);
3254 gc_mark_internal(RFILE(obj)->fptr->wakeup_mutex);
3255 }
3256 break;
3257
3258 case T_REGEXP:
3259 gc_mark_internal(RREGEXP(obj)->src);
3260 break;
3261
3262 case T_MATCH:
3263 gc_mark_internal(RMATCH(obj)->regexp);
3264 if (RMATCH(obj)->str) {
3265 gc_mark_internal(RMATCH(obj)->str);
3266 }
3267 break;
3268
3269 case T_RATIONAL:
3270 gc_mark_internal(RRATIONAL(obj)->num);
3271 gc_mark_internal(RRATIONAL(obj)->den);
3272 break;
3273
3274 case T_COMPLEX:
3275 gc_mark_internal(RCOMPLEX(obj)->real);
3276 gc_mark_internal(RCOMPLEX(obj)->imag);
3277 break;
3278
3279 case T_STRUCT: {
3280 const long len = RSTRUCT_LEN(obj);
3281 const VALUE * const ptr = RSTRUCT_CONST_PTR(obj);
3282
3283 for (long i = 0; i < len; i++) {
3284 gc_mark_internal(ptr[i]);
3285 }
3286
3287 if (rb_shape_obj_has_fields(obj) && !FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
3288 gc_mark_internal(RSTRUCT_FIELDS_OBJ(obj));
3289 }
3290
3291 break;
3292 }
3293
3294 default:
3295 if (BUILTIN_TYPE(obj) == T_MOVED) rb_bug("rb_gc_mark(): %p is T_MOVED", (void *)obj);
3296 if (BUILTIN_TYPE(obj) == T_NONE) rb_bug("rb_gc_mark(): %p is T_NONE", (void *)obj);
3297 if (BUILTIN_TYPE(obj) == T_ZOMBIE) rb_bug("rb_gc_mark(): %p is T_ZOMBIE", (void *)obj);
3298 rb_bug("rb_gc_mark(): unknown data type 0x%x(%p) %s",
3299 BUILTIN_TYPE(obj), (void *)obj,
3300 rb_gc_impl_pointer_to_heap_p(objspace, (void *)obj) ? "corrupted object" : "non object");
3301 }
3302}
3303
3304size_t
3305rb_gc_obj_optimal_size(VALUE obj)
3306{
3307 switch (BUILTIN_TYPE(obj)) {
3308 case T_ARRAY:
3309 {
3310 size_t size = rb_ary_size_as_embedded(obj);
3311 if (rb_gc_size_allocatable_p(size)) {
3312 return size;
3313 }
3314 else {
3315 return sizeof(struct RArray);
3316 }
3317 }
3318
3319 case T_OBJECT:
3320 if (rb_shape_obj_too_complex_p(obj)) {
3321 return sizeof(struct RObject);
3322 }
3323 else {
3324 size_t size = rb_obj_embedded_size(ROBJECT_FIELDS_CAPACITY(obj));
3325 if (rb_gc_size_allocatable_p(size)) {
3326 return size;
3327 }
3328 else {
3329 return sizeof(struct RObject);
3330 }
3331 }
3332
3333 case T_STRING:
3334 {
3335 size_t size = rb_str_size_as_embedded(obj);
3336 if (rb_gc_size_allocatable_p(size)) {
3337 return size;
3338 }
3339 else {
3340 return sizeof(struct RString);
3341 }
3342 }
3343
3344 case T_HASH:
3345 return sizeof(struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));
3346
3347 default:
3348 return 0;
3349 }
3350}
3351
3352void
3353rb_gc_writebarrier(VALUE a, VALUE b)
3354{
3355 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
3356}
3357
3358void
3359rb_gc_writebarrier_unprotect(VALUE obj)
3360{
3361 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
3362}
3363
3364/*
3365 * remember `obj' if needed.
3366 */
3367void
3368rb_gc_writebarrier_remember(VALUE obj)
3369{
3370 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
3371}
3372
3373void
3374rb_gc_copy_attributes(VALUE dest, VALUE obj)
3375{
3376 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
3377}
3378
3379int
3380rb_gc_modular_gc_loaded_p(void)
3381{
3382#if USE_MODULAR_GC
3383 return rb_gc_functions.modular_gc_loaded_p;
3384#else
3385 return false;
3386#endif
3387}
3388
3389const char *
3390rb_gc_active_gc_name(void)
3391{
3392 const char *gc_name = rb_gc_impl_active_gc_name();
3393
3394 const size_t len = strlen(gc_name);
3395 if (len > RB_GC_MAX_NAME_LEN) {
3396 rb_bug("GC should have a name no more than %d chars long. Currently: %zu (%s)",
3397 RB_GC_MAX_NAME_LEN, len, gc_name);
3398 }
3399
3400 return gc_name;
3401}
3402
3404rb_gc_object_metadata(VALUE obj)
3405{
3406 return rb_gc_impl_object_metadata(rb_gc_get_objspace(), obj);
3407}
3408
3409/* GC */
3410
3411void *
3412rb_gc_ractor_cache_alloc(rb_ractor_t *ractor)
3413{
3414 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
3415}
3416
3417void
3418rb_gc_ractor_cache_free(void *cache)
3419{
3420 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3421}
3422
3423void
3424rb_gc_register_mark_object(VALUE obj)
3425{
3426 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj))
3427 return;
3428
3429 rb_vm_register_global_object(obj);
3430}
3431
3432void
3433rb_gc_register_address(VALUE *addr)
3434{
3435 rb_vm_t *vm = GET_VM();
3436
3437 VALUE obj = *addr;
3438
3439 struct global_object_list *tmp = ALLOC(struct global_object_list);
3440 tmp->next = vm->global_object_list;
3441 tmp->varptr = addr;
3442 vm->global_object_list = tmp;
3443
3444 /*
3445 * Because some C extensions have assignment-then-register bugs,
3446 * we guard `obj` here so that it would not get swept defensively.
3447 */
3448 RB_GC_GUARD(obj);
3449 if (0 && !SPECIAL_CONST_P(obj)) {
3450 rb_warn("Object is assigned to registering address already: %"PRIsVALUE,
3451 rb_obj_class(obj));
3452 rb_print_backtrace(stderr);
3453 }
3454}
3455
3456void
3457rb_gc_unregister_address(VALUE *addr)
3458{
3459 rb_vm_t *vm = GET_VM();
3460 struct global_object_list *tmp = vm->global_object_list;
3461
3462 if (tmp->varptr == addr) {
3463 vm->global_object_list = tmp->next;
3464 xfree(tmp);
3465 return;
3466 }
3467 while (tmp->next) {
3468 if (tmp->next->varptr == addr) {
3469 struct global_object_list *t = tmp->next;
3470
3471 tmp->next = tmp->next->next;
3472 xfree(t);
3473 break;
3474 }
3475 tmp = tmp->next;
3476 }
3477}
3478
3479void
3481{
3482 rb_gc_register_address(var);
3483}
3484
3485static VALUE
3486gc_start_internal(rb_execution_context_t *ec, VALUE self, VALUE full_mark, VALUE immediate_mark, VALUE immediate_sweep, VALUE compact)
3487{
3488 rb_gc_impl_start(rb_gc_get_objspace(), RTEST(full_mark), RTEST(immediate_mark), RTEST(immediate_sweep), RTEST(compact));
3489
3490 return Qnil;
3491}
3492
3493/*
3494 * rb_objspace_each_objects() is special C API to walk through
3495 * Ruby object space. This C API is too difficult to use it.
3496 * To be frank, you should not use it. Or you need to read the
3497 * source code of this function and understand what this function does.
3498 *
3499 * 'callback' will be called several times (the number of heap page,
3500 * at current implementation) with:
3501 * vstart: a pointer to the first living object of the heap_page.
3502 * vend: a pointer to next to the valid heap_page area.
3503 * stride: a distance to next VALUE.
3504 *
3505 * If callback() returns non-zero, the iteration will be stopped.
3506 *
3507 * This is a sample callback code to iterate liveness objects:
3508 *
3509 * static int
3510 * sample_callback(void *vstart, void *vend, int stride, void *data)
3511 * {
3512 * VALUE v = (VALUE)vstart;
3513 * for (; v != (VALUE)vend; v += stride) {
3514 * if (!rb_objspace_internal_object_p(v)) { // liveness check
3515 * // do something with live object 'v'
3516 * }
3517 * }
3518 * return 0; // continue to iteration
3519 * }
3520 *
3521 * Note: 'vstart' is not a top of heap_page. This point the first
3522 * living object to grasp at least one object to avoid GC issue.
3523 * This means that you can not walk through all Ruby object page
3524 * including freed object page.
3525 *
3526 * Note: On this implementation, 'stride' is the same as sizeof(RVALUE).
3527 * However, there are possibilities to pass variable values with
3528 * 'stride' with some reasons. You must use stride instead of
3529 * use some constant value in the iteration.
3530 */
3531void
3532rb_objspace_each_objects(int (*callback)(void *, void *, size_t, void *), void *data)
3533{
3534 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3535}
3536
3537static void
3538gc_ref_update_array(void *objspace, VALUE v)
3539{
3540 if (ARY_SHARED_P(v)) {
3541 VALUE old_root = RARRAY(v)->as.heap.aux.shared_root;
3542
3543 UPDATE_IF_MOVED(objspace, RARRAY(v)->as.heap.aux.shared_root);
3544
3545 VALUE new_root = RARRAY(v)->as.heap.aux.shared_root;
3546 // If the root is embedded and its location has changed
3547 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3548 size_t offset = (size_t)(RARRAY(v)->as.heap.ptr - RARRAY(old_root)->as.ary);
3549 GC_ASSERT(RARRAY(v)->as.heap.ptr >= RARRAY(old_root)->as.ary);
3550 RARRAY(v)->as.heap.ptr = RARRAY(new_root)->as.ary + offset;
3551 }
3552 }
3553 else {
3554 long len = RARRAY_LEN(v);
3555
3556 if (len > 0) {
3557 VALUE *ptr = (VALUE *)RARRAY_CONST_PTR(v);
3558 for (long i = 0; i < len; i++) {
3559 UPDATE_IF_MOVED(objspace, ptr[i]);
3560 }
3561 }
3562
3563 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3564 if (rb_ary_embeddable_p(v)) {
3565 rb_ary_make_embedded(v);
3566 }
3567 }
3568 }
3569}
3570
3571static void
3572gc_ref_update_object(void *objspace, VALUE v)
3573{
3574 VALUE *ptr = ROBJECT_FIELDS(v);
3575
3576 if (FL_TEST_RAW(v, ROBJECT_HEAP)) {
3577 if (rb_shape_obj_too_complex_p(v)) {
3578 gc_ref_update_table_values_only(ROBJECT_FIELDS_HASH(v));
3579 return;
3580 }
3581
3582 size_t slot_size = rb_gc_obj_slot_size(v);
3583 size_t embed_size = rb_obj_embedded_size(ROBJECT_FIELDS_CAPACITY(v));
3584 if (slot_size >= embed_size) {
3585 // Object can be re-embedded
3586 memcpy(ROBJECT(v)->as.ary, ptr, sizeof(VALUE) * ROBJECT_FIELDS_COUNT(v));
3587 FL_UNSET_RAW(v, ROBJECT_HEAP);
3588 xfree(ptr);
3589 ptr = ROBJECT(v)->as.ary;
3590 }
3591 }
3592
3593 for (uint32_t i = 0; i < ROBJECT_FIELDS_COUNT(v); i++) {
3594 UPDATE_IF_MOVED(objspace, ptr[i]);
3595 }
3596}
3597
3598void
3599rb_gc_ref_update_table_values_only(st_table *tbl)
3600{
3601 gc_ref_update_table_values_only(tbl);
3602}
3603
3604/* Update MOVED references in a VALUE=>VALUE st_table */
3605void
3606rb_gc_update_tbl_refs(st_table *ptr)
3607{
3608 gc_update_table_refs(ptr);
3609}
3610
3611static void
3612gc_ref_update_hash(void *objspace, VALUE v)
3613{
3614 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)objspace);
3615}
3616
3617static void
3618gc_update_values(void *objspace, long n, VALUE *values)
3619{
3620 for (long i = 0; i < n; i++) {
3621 UPDATE_IF_MOVED(objspace, values[i]);
3622 }
3623}
3624
3625void
3626rb_gc_update_values(long n, VALUE *values)
3627{
3628 gc_update_values(rb_gc_get_objspace(), n, values);
3629}
3630
3631static enum rb_id_table_iterator_result
3632check_id_table_move(VALUE value, void *data)
3633{
3634 void *objspace = (void *)data;
3635
3636 if (gc_object_moved_p_internal(objspace, (VALUE)value)) {
3637 return ID_TABLE_REPLACE;
3638 }
3639
3640 return ID_TABLE_CONTINUE;
3641}
3642
3643void
3644rb_gc_prepare_heap_process_object(VALUE obj)
3645{
3646 switch (BUILTIN_TYPE(obj)) {
3647 case T_STRING:
3648 // Precompute the string coderange. This both save time for when it will be
3649 // eventually needed, and avoid mutating heap pages after a potential fork.
3651 break;
3652 default:
3653 break;
3654 }
3655}
3656
3657void
3658rb_gc_prepare_heap(void)
3659{
3660 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3661}
3662
3663size_t
3664rb_gc_heap_id_for_size(size_t size)
3665{
3666 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3667}
3668
3669bool
3670rb_gc_size_allocatable_p(size_t size)
3671{
3672 return rb_gc_impl_size_allocatable_p(size);
3673}
3674
3675static enum rb_id_table_iterator_result
3676update_id_table(VALUE *value, void *data, int existing)
3677{
3678 void *objspace = (void *)data;
3679
3680 if (gc_object_moved_p_internal(objspace, (VALUE)*value)) {
3681 *value = gc_location_internal(objspace, (VALUE)*value);
3682 }
3683
3684 return ID_TABLE_CONTINUE;
3685}
3686
3687static void
3688update_m_tbl(void *objspace, struct rb_id_table *tbl)
3689{
3690 if (tbl) {
3691 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table, objspace);
3692 }
3693}
3694
3695static enum rb_id_table_iterator_result
3696update_const_tbl_i(VALUE value, void *objspace)
3697{
3698 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3699
3700 if (gc_object_moved_p_internal(objspace, ce->value)) {
3701 ce->value = gc_location_internal(objspace, ce->value);
3702 }
3703
3704 if (gc_object_moved_p_internal(objspace, ce->file)) {
3705 ce->file = gc_location_internal(objspace, ce->file);
3706 }
3707
3708 return ID_TABLE_CONTINUE;
3709}
3710
3711static void
3712update_const_tbl(void *objspace, struct rb_id_table *tbl)
3713{
3714 if (!tbl) return;
3715 rb_id_table_foreach_values(tbl, update_const_tbl_i, objspace);
3716}
3717
3718static void
3719update_subclasses(void *objspace, rb_classext_t *ext)
3720{
3721 rb_subclass_entry_t *entry = RCLASSEXT_SUBCLASSES(ext);
3722 if (!entry) return;
3723 while (entry) {
3724 if (entry->klass)
3725 UPDATE_IF_MOVED(objspace, entry->klass);
3726 entry = entry->next;
3727 }
3728}
3729
3730static void
3731update_superclasses(rb_objspace_t *objspace, rb_classext_t *ext)
3732{
3733 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
3734 size_t array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
3735 for (size_t i = 0; i < array_size; i++) {
3736 UPDATE_IF_MOVED(objspace, RCLASSEXT_SUPERCLASSES(ext)[i]);
3737 }
3738 }
3739}
3740
3741static void
3742update_classext_values(rb_objspace_t *objspace, rb_classext_t *ext, bool is_iclass)
3743{
3744 UPDATE_IF_MOVED(objspace, RCLASSEXT_ORIGIN(ext));
3745 UPDATE_IF_MOVED(objspace, RCLASSEXT_REFINED_CLASS(ext));
3746 UPDATE_IF_MOVED(objspace, RCLASSEXT_CLASSPATH(ext));
3747 if (is_iclass) {
3748 UPDATE_IF_MOVED(objspace, RCLASSEXT_INCLUDER(ext));
3749 }
3750}
3751
3752static void
3753update_classext(rb_classext_t *ext, bool is_prime, VALUE box_value, void *arg)
3754{
3755 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
3756 rb_objspace_t *objspace = args->objspace;
3757
3758 if (RCLASSEXT_SUPER(ext)) {
3759 UPDATE_IF_MOVED(objspace, RCLASSEXT_SUPER(ext));
3760 }
3761
3762 update_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3763
3764 UPDATE_IF_MOVED(objspace, ext->fields_obj);
3765 if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
3766 update_const_tbl(objspace, RCLASSEXT_CONST_TBL(ext));
3767 }
3768 UPDATE_IF_MOVED(objspace, RCLASSEXT_CC_TBL(ext));
3769 UPDATE_IF_MOVED(objspace, RCLASSEXT_CVC_TBL(ext));
3770 update_superclasses(objspace, ext);
3771 update_subclasses(objspace, ext);
3772
3773 update_classext_values(objspace, ext, false);
3774}
3775
3776static void
3777update_iclass_classext(rb_classext_t *ext, bool is_prime, VALUE box_value, void *arg)
3778{
3779 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
3780 rb_objspace_t *objspace = args->objspace;
3781
3782 if (RCLASSEXT_SUPER(ext)) {
3783 UPDATE_IF_MOVED(objspace, RCLASSEXT_SUPER(ext));
3784 }
3785 update_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3786 update_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3787 UPDATE_IF_MOVED(objspace, RCLASSEXT_CC_TBL(ext));
3788 UPDATE_IF_MOVED(objspace, RCLASSEXT_CVC_TBL(ext));
3789 update_subclasses(objspace, ext);
3790
3791 update_classext_values(objspace, ext, true);
3792}
3793
3795 vm_table_foreach_callback_func callback;
3796 vm_table_update_callback_func update_callback;
3797 void *data;
3798 bool weak_only;
3799};
3800
3801static int
3802vm_weak_table_foreach_weak_key(st_data_t key, st_data_t value, st_data_t data, int error)
3803{
3804 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3805
3806 int ret = iter_data->callback((VALUE)key, iter_data->data);
3807
3808 if (!iter_data->weak_only) {
3809 if (ret != ST_CONTINUE) return ret;
3810
3811 ret = iter_data->callback((VALUE)value, iter_data->data);
3812 }
3813
3814 return ret;
3815}
3816
3817static int
3818vm_weak_table_foreach_update_weak_key(st_data_t *key, st_data_t *value, st_data_t data, int existing)
3819{
3820 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3821
3822 int ret = iter_data->update_callback((VALUE *)key, iter_data->data);
3823
3824 if (!iter_data->weak_only) {
3825 if (ret != ST_CONTINUE) return ret;
3826
3827 ret = iter_data->update_callback((VALUE *)value, iter_data->data);
3828 }
3829
3830 return ret;
3831}
3832
3833static int
3834vm_weak_table_cc_refinement_foreach(st_data_t key, st_data_t data, int error)
3835{
3836 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3837
3838 return iter_data->callback((VALUE)key, iter_data->data);
3839}
3840
3841static int
3842vm_weak_table_cc_refinement_foreach_update_update(st_data_t *key, st_data_t data, int existing)
3843{
3844 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3845
3846 return iter_data->update_callback((VALUE *)key, iter_data->data);
3847}
3848
3849
3850static int
3851vm_weak_table_sym_set_foreach(VALUE *sym_ptr, void *data)
3852{
3853 VALUE sym = *sym_ptr;
3854 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3855
3856 if (RB_SPECIAL_CONST_P(sym)) return ST_CONTINUE;
3857
3858 int ret = iter_data->callback(sym, iter_data->data);
3859
3860 if (ret == ST_REPLACE) {
3861 ret = iter_data->update_callback(sym_ptr, iter_data->data);
3862 }
3863
3864 return ret;
3865}
3866
3867struct st_table *rb_generic_fields_tbl_get(void);
3868
3869static int
3870vm_weak_table_id2ref_foreach(st_data_t key, st_data_t value, st_data_t data, int error)
3871{
3872 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3873
3874 if (!iter_data->weak_only && !FIXNUM_P((VALUE)key)) {
3875 int ret = iter_data->callback((VALUE)key, iter_data->data);
3876 if (ret != ST_CONTINUE) return ret;
3877 }
3878
3879 return iter_data->callback((VALUE)value, iter_data->data);
3880}
3881
3882static int
3883vm_weak_table_id2ref_foreach_update(st_data_t *key, st_data_t *value, st_data_t data, int existing)
3884{
3885 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3886
3887 iter_data->update_callback((VALUE *)value, iter_data->data);
3888
3889 if (!iter_data->weak_only && !FIXNUM_P((VALUE)*key)) {
3890 iter_data->update_callback((VALUE *)key, iter_data->data);
3891 }
3892
3893 return ST_CONTINUE;
3894}
3895
3896static int
3897vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
3898{
3899 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3900
3901 int ret = iter_data->callback((VALUE)key, iter_data->data);
3902
3903 VALUE new_value = (VALUE)value;
3904 VALUE new_key = (VALUE)key;
3905
3906 switch (ret) {
3907 case ST_CONTINUE:
3908 break;
3909
3910 case ST_DELETE:
3911 RBASIC_SET_SHAPE_ID((VALUE)key, ROOT_SHAPE_ID);
3912 return ST_DELETE;
3913
3914 case ST_REPLACE: {
3915 ret = iter_data->update_callback(&new_key, iter_data->data);
3916 if (key != new_key) {
3917 ret = ST_DELETE;
3918 }
3919 break;
3920 }
3921
3922 default:
3923 rb_bug("vm_weak_table_gen_fields_foreach: return value %d not supported", ret);
3924 }
3925
3926 if (!iter_data->weak_only) {
3927 int ivar_ret = iter_data->callback(new_value, iter_data->data);
3928 switch (ivar_ret) {
3929 case ST_CONTINUE:
3930 break;
3931
3932 case ST_REPLACE:
3933 iter_data->update_callback(&new_value, iter_data->data);
3934 break;
3935
3936 default:
3937 rb_bug("vm_weak_table_gen_fields_foreach: return value %d not supported", ivar_ret);
3938 }
3939 }
3940
3941 if (key != new_key || value != new_value) {
3942 DURING_GC_COULD_MALLOC_REGION_START();
3943 {
3944 st_insert(rb_generic_fields_tbl_get(), (st_data_t)new_key, new_value);
3945 }
3946 DURING_GC_COULD_MALLOC_REGION_END();
3947 }
3948
3949 return ret;
3950}
3951
3952static int
3953vm_weak_table_frozen_strings_foreach(VALUE *str, void *data)
3954{
3955 // int retval = vm_weak_table_foreach_weak_key(key, value, data, error);
3956 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3957 int retval = iter_data->callback(*str, iter_data->data);
3958
3959 if (retval == ST_REPLACE) {
3960 retval = iter_data->update_callback(str, iter_data->data);
3961 }
3962
3963 if (retval == ST_DELETE) {
3964 FL_UNSET(*str, RSTRING_FSTR);
3965 }
3966
3967 return retval;
3968}
3969
3970void rb_fstring_foreach_with_replace(int (*callback)(VALUE *str, void *data), void *data);
3971void
3972rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
3973 vm_table_update_callback_func update_callback,
3974 void *data,
3975 bool weak_only,
3976 enum rb_gc_vm_weak_tables table)
3977{
3978 rb_vm_t *vm = GET_VM();
3979
3980 struct global_vm_table_foreach_data foreach_data = {
3981 .callback = callback,
3982 .update_callback = update_callback,
3983 .data = data,
3984 .weak_only = weak_only,
3985 };
3986
3987 switch (table) {
3988 case RB_GC_VM_CI_TABLE: {
3989 if (vm->ci_table) {
3990 st_foreach_with_replace(
3991 vm->ci_table,
3992 vm_weak_table_foreach_weak_key,
3993 vm_weak_table_foreach_update_weak_key,
3994 (st_data_t)&foreach_data
3995 );
3996 }
3997 break;
3998 }
3999 case RB_GC_VM_OVERLOADED_CME_TABLE: {
4000 if (vm->overloaded_cme_table) {
4001 st_foreach_with_replace(
4002 vm->overloaded_cme_table,
4003 vm_weak_table_foreach_weak_key,
4004 vm_weak_table_foreach_update_weak_key,
4005 (st_data_t)&foreach_data
4006 );
4007 }
4008 break;
4009 }
4010 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
4011 rb_sym_global_symbol_table_foreach_weak_reference(
4012 vm_weak_table_sym_set_foreach,
4013 &foreach_data
4014 );
4015 break;
4016 }
4017 case RB_GC_VM_ID2REF_TABLE: {
4018 if (id2ref_tbl) {
4019 st_foreach_with_replace(
4020 id2ref_tbl,
4021 vm_weak_table_id2ref_foreach,
4022 vm_weak_table_id2ref_foreach_update,
4023 (st_data_t)&foreach_data
4024 );
4025 }
4026 break;
4027 }
4028 case RB_GC_VM_GENERIC_FIELDS_TABLE: {
4029 st_table *generic_fields_tbl = rb_generic_fields_tbl_get();
4030 if (generic_fields_tbl) {
4031 st_foreach(
4032 generic_fields_tbl,
4033 vm_weak_table_gen_fields_foreach,
4034 (st_data_t)&foreach_data
4035 );
4036 }
4037 break;
4038 }
4039 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
4040 rb_fstring_foreach_with_replace(
4041 vm_weak_table_frozen_strings_foreach,
4042 &foreach_data
4043 );
4044 break;
4045 }
4046 case RB_GC_VM_CC_REFINEMENT_TABLE: {
4047 if (vm->cc_refinement_table) {
4048 set_foreach_with_replace(
4049 vm->cc_refinement_table,
4050 vm_weak_table_cc_refinement_foreach,
4051 vm_weak_table_cc_refinement_foreach_update_update,
4052 (st_data_t)&foreach_data
4053 );
4054 }
4055 break;
4056 }
4057 case RB_GC_VM_WEAK_TABLE_COUNT:
4058 rb_bug("Unreachable");
4059 default:
4060 rb_bug("rb_gc_vm_weak_table_foreach: unknown table %d", table);
4061 }
4062}
4063
4064void
4065rb_gc_update_vm_references(void *objspace)
4066{
4067 rb_execution_context_t *ec = GET_EC();
4068 rb_vm_t *vm = rb_ec_vm_ptr(ec);
4069
4070 rb_vm_update_references(vm);
4071 rb_gc_update_global_tbl();
4072 rb_sym_global_symbols_mark_and_move();
4073
4074#if USE_YJIT
4075 void rb_yjit_root_update_references(void); // in Rust
4076
4077 if (rb_yjit_enabled_p) {
4078 rb_yjit_root_update_references();
4079 }
4080#endif
4081
4082#if USE_ZJIT
4083 void rb_zjit_root_update_references(void); // in Rust
4084
4085 if (rb_zjit_enabled_p) {
4086 rb_zjit_root_update_references();
4087 }
4088#endif
4089}
4090
4091void
4092rb_gc_update_object_references(void *objspace, VALUE obj)
4093{
4094 struct classext_foreach_args args;
4095
4096 switch (BUILTIN_TYPE(obj)) {
4097 case T_CLASS:
4098 if (FL_TEST_RAW(obj, FL_SINGLETON)) {
4099 UPDATE_IF_MOVED(objspace, RCLASS_ATTACHED_OBJECT(obj));
4100 }
4101 // Continue to the shared T_CLASS/T_MODULE
4102 case T_MODULE:
4103 args.klass = obj;
4104 args.objspace = objspace;
4105 rb_class_classext_foreach(obj, update_classext, (void *)&args);
4106 break;
4107
4108 case T_ICLASS:
4109 args.objspace = objspace;
4110 rb_class_classext_foreach(obj, update_iclass_classext, (void *)&args);
4111 break;
4112
4113 case T_IMEMO:
4114 rb_imemo_mark_and_move(obj, true);
4115 return;
4116
4117 case T_NIL:
4118 case T_FIXNUM:
4119 case T_NODE:
4120 case T_MOVED:
4121 case T_NONE:
4122 /* These can't move */
4123 return;
4124
4125 case T_ARRAY:
4126 gc_ref_update_array(objspace, obj);
4127 break;
4128
4129 case T_HASH:
4130 gc_ref_update_hash(objspace, obj);
4131 UPDATE_IF_MOVED(objspace, RHASH(obj)->ifnone);
4132 break;
4133
4134 case T_STRING:
4135 {
4136 if (STR_SHARED_P(obj)) {
4137 UPDATE_IF_MOVED(objspace, RSTRING(obj)->as.heap.aux.shared);
4138 }
4139
4140 /* If, after move the string is not embedded, and can fit in the
4141 * slot it's been placed in, then re-embed it. */
4142 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
4143 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
4144 rb_str_make_embedded(obj);
4145 }
4146 }
4147
4148 break;
4149 }
4150 case T_DATA:
4151 /* Call the compaction callback, if it exists */
4152 {
4153 bool typed_data = RTYPEDDATA_P(obj);
4154 void *const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
4155
4156 if (typed_data) {
4157 UPDATE_IF_MOVED(objspace, RTYPEDDATA(obj)->fields_obj);
4158 }
4159
4160 if (ptr) {
4161 if (typed_data && gc_declarative_marking_p(RTYPEDDATA_TYPE(obj))) {
4162 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
4163
4164 for (size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
4165 VALUE *ref = (VALUE *)((char *)ptr + offset);
4166 *ref = gc_location_internal(objspace, *ref);
4167 }
4168 }
4169 else if (typed_data) {
4170 RUBY_DATA_FUNC compact_func = RTYPEDDATA_TYPE(obj)->function.dcompact;
4171 if (compact_func) (*compact_func)(ptr);
4172 }
4173 }
4174 }
4175 break;
4176
4177 case T_OBJECT:
4178 gc_ref_update_object(objspace, obj);
4179 break;
4180
4181 case T_FILE:
4182 if (RFILE(obj)->fptr) {
4183 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->self);
4184 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->pathv);
4185 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->tied_io_for_writing);
4186 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->writeconv_asciicompat);
4187 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->writeconv_pre_ecopts);
4188 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->encs.ecopts);
4189 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->write_lock);
4190 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->timeout);
4191 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->wakeup_mutex);
4192 }
4193 break;
4194 case T_REGEXP:
4195 UPDATE_IF_MOVED(objspace, RREGEXP(obj)->src);
4196 break;
4197
4198 case T_SYMBOL:
4199 UPDATE_IF_MOVED(objspace, RSYMBOL(obj)->fstr);
4200 break;
4201
4202 case T_FLOAT:
4203 case T_BIGNUM:
4204 break;
4205
4206 case T_MATCH:
4207 UPDATE_IF_MOVED(objspace, RMATCH(obj)->regexp);
4208
4209 if (RMATCH(obj)->str) {
4210 UPDATE_IF_MOVED(objspace, RMATCH(obj)->str);
4211 }
4212 break;
4213
4214 case T_RATIONAL:
4215 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->num);
4216 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->den);
4217 break;
4218
4219 case T_COMPLEX:
4220 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->real);
4221 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->imag);
4222
4223 break;
4224
4225 case T_STRUCT:
4226 {
4227 long i, len = RSTRUCT_LEN(obj);
4228 VALUE *ptr = (VALUE *)RSTRUCT_CONST_PTR(obj);
4229
4230 for (i = 0; i < len; i++) {
4231 UPDATE_IF_MOVED(objspace, ptr[i]);
4232 }
4233
4234 if (RSTRUCT_EMBED_LEN(obj)) {
4235 if (!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
4236 UPDATE_IF_MOVED(objspace, ptr[len]);
4237 }
4238 }
4239 else {
4240 UPDATE_IF_MOVED(objspace, RSTRUCT(obj)->as.heap.fields_obj);
4241 }
4242 }
4243 break;
4244 default:
4245 rb_bug("unreachable");
4246 break;
4247 }
4248
4249 UPDATE_IF_MOVED(objspace, RBASIC(obj)->klass);
4250}
4251
4252VALUE
4254{
4255 rb_gc();
4256 return Qnil;
4257}
4258
4259void
4261{
4262 unless_objspace(objspace) { return; }
4263
4264 rb_gc_impl_start(objspace, true, true, true, false);
4265}
4266
4267int
4269{
4270 unless_objspace(objspace) { return FALSE; }
4271
4272 return rb_gc_impl_during_gc_p(objspace);
4273}
4274
4275size_t
4277{
4278 return rb_gc_impl_gc_count(rb_gc_get_objspace());
4279}
4280
4281static VALUE
4282gc_count(rb_execution_context_t *ec, VALUE self)
4283{
4284 return SIZET2NUM(rb_gc_count());
4285}
4286
4287VALUE
4288rb_gc_latest_gc_info(VALUE key)
4289{
4290 if (!SYMBOL_P(key) && !RB_TYPE_P(key, T_HASH)) {
4291 rb_raise(rb_eTypeError, "non-hash or symbol given");
4292 }
4293
4294 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
4295
4296 if (val == Qundef) {
4297 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
4298 }
4299
4300 return val;
4301}
4302
4303static VALUE
4304gc_stat(rb_execution_context_t *ec, VALUE self, VALUE arg) // arg is (nil || hash || symbol)
4305{
4306 if (NIL_P(arg)) {
4307 arg = rb_hash_new();
4308 }
4309 else if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) {
4310 rb_raise(rb_eTypeError, "non-hash or symbol given");
4311 }
4312
4313 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4314
4315 if (ret == Qundef) {
4316 GC_ASSERT(SYMBOL_P(arg));
4317
4318 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
4319 }
4320
4321 return ret;
4322}
4323
4324size_t
4325rb_gc_stat(VALUE arg)
4326{
4327 if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) {
4328 rb_raise(rb_eTypeError, "non-hash or symbol given");
4329 }
4330
4331 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4332
4333 if (ret == Qundef) {
4334 GC_ASSERT(SYMBOL_P(arg));
4335
4336 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
4337 }
4338
4339 if (SYMBOL_P(arg)) {
4340 return NUM2SIZET(ret);
4341 }
4342 else {
4343 return 0;
4344 }
4345}
4346
4347static VALUE
4348gc_stat_heap(rb_execution_context_t *ec, VALUE self, VALUE heap_name, VALUE arg)
4349{
4350 if (NIL_P(arg)) {
4351 arg = rb_hash_new();
4352 }
4353
4354 if (NIL_P(heap_name)) {
4355 if (!RB_TYPE_P(arg, T_HASH)) {
4356 rb_raise(rb_eTypeError, "non-hash given");
4357 }
4358 }
4359 else if (FIXNUM_P(heap_name)) {
4360 if (!SYMBOL_P(arg) && !RB_TYPE_P(arg, T_HASH)) {
4361 rb_raise(rb_eTypeError, "non-hash or symbol given");
4362 }
4363 }
4364 else {
4365 rb_raise(rb_eTypeError, "heap_name must be nil or an Integer");
4366 }
4367
4368 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
4369
4370 if (ret == Qundef) {
4371 GC_ASSERT(SYMBOL_P(arg));
4372
4373 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
4374 }
4375
4376 return ret;
4377}
4378
4379static VALUE
4380gc_config_get(rb_execution_context_t *ec, VALUE self)
4381{
4382 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
4383 rb_hash_aset(cfg_hash, sym("implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
4384
4385 return cfg_hash;
4386}
4387
4388static VALUE
4389gc_config_set(rb_execution_context_t *ec, VALUE self, VALUE hash)
4390{
4391 void *objspace = rb_gc_get_objspace();
4392
4393 rb_gc_impl_config_set(objspace, hash);
4394
4395 return Qnil;
4396}
4397
4398static VALUE
4399gc_stress_get(rb_execution_context_t *ec, VALUE self)
4400{
4401 return rb_gc_impl_stress_get(rb_gc_get_objspace());
4402}
4403
4404static VALUE
4405gc_stress_set_m(rb_execution_context_t *ec, VALUE self, VALUE flag)
4406{
4407 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
4408
4409 return flag;
4410}
4411
4412void
4413rb_gc_initial_stress_set(VALUE flag)
4414{
4415 initial_stress = flag;
4416}
4417
4418size_t *
4419rb_gc_heap_sizes(void)
4420{
4421 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
4422}
4423
4424VALUE
4426{
4427 return rb_objspace_gc_enable(rb_gc_get_objspace());
4428}
4429
4430VALUE
4431rb_objspace_gc_enable(void *objspace)
4432{
4433 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
4434 rb_gc_impl_gc_enable(objspace);
4435 return RBOOL(disabled);
4436}
4437
4438static VALUE
4439gc_enable(rb_execution_context_t *ec, VALUE _)
4440{
4441 return rb_gc_enable();
4442}
4443
4444static VALUE
4445gc_disable_no_rest(void *objspace)
4446{
4447 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
4448 rb_gc_impl_gc_disable(objspace, false);
4449 return RBOOL(disabled);
4450}
4451
4452VALUE
4453rb_gc_disable_no_rest(void)
4454{
4455 return gc_disable_no_rest(rb_gc_get_objspace());
4456}
4457
4458VALUE
4460{
4461 return rb_objspace_gc_disable(rb_gc_get_objspace());
4462}
4463
4464VALUE
4465rb_objspace_gc_disable(void *objspace)
4466{
4467 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
4468 rb_gc_impl_gc_disable(objspace, true);
4469 return RBOOL(disabled);
4470}
4471
4472static VALUE
4473gc_disable(rb_execution_context_t *ec, VALUE _)
4474{
4475 return rb_gc_disable();
4476}
4477
4478// TODO: think about moving ruby_gc_set_params into Init_heap or Init_gc
4479void
4480ruby_gc_set_params(void)
4481{
4482 rb_gc_impl_set_params(rb_gc_get_objspace());
4483}
4484
4485void
4486rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data)
4487{
4488 RB_VM_LOCKING() {
4489 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug("rb_objspace_reachable_objects_from() is not supported while during GC");
4490
4491 if (!RB_SPECIAL_CONST_P(obj)) {
4492 rb_vm_t *vm = GET_VM();
4493 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4494 struct gc_mark_func_data_struct mfd = {
4495 .mark_func = func,
4496 .data = data,
4497 };
4498
4499 vm->gc.mark_func_data = &mfd;
4500 rb_gc_mark_children(rb_gc_get_objspace(), obj);
4501 vm->gc.mark_func_data = prev_mfd;
4502 }
4503 }
4504}
4505
4507 const char *category;
4508 void (*func)(const char *category, VALUE, void *);
4509 void *data;
4510};
4511
4512static void
4513root_objects_from(VALUE obj, void *ptr)
4514{
4515 const struct root_objects_data *data = (struct root_objects_data *)ptr;
4516 (*data->func)(data->category, obj, data->data);
4517}
4518
4519void
4520rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *passing_data)
4521{
4522 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");
4523
4524 rb_vm_t *vm = GET_VM();
4525
4526 struct root_objects_data data = {
4527 .func = func,
4528 .data = passing_data,
4529 };
4530
4531 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4532 struct gc_mark_func_data_struct mfd = {
4533 .mark_func = root_objects_from,
4534 .data = &data,
4535 };
4536
4537 vm->gc.mark_func_data = &mfd;
4538 rb_gc_save_machine_context();
4539 rb_gc_mark_roots(vm->gc.objspace, &data.category);
4540 vm->gc.mark_func_data = prev_mfd;
4541}
4542
4543/*
4544 ------------------------------ DEBUG ------------------------------
4545*/
4546
4547static const char *
4548type_name(int type, VALUE obj)
4549{
4550 switch (type) {
4551#define TYPE_NAME(t) case (t): return #t;
4552 TYPE_NAME(T_NONE);
4553 TYPE_NAME(T_OBJECT);
4554 TYPE_NAME(T_CLASS);
4555 TYPE_NAME(T_MODULE);
4556 TYPE_NAME(T_FLOAT);
4557 TYPE_NAME(T_STRING);
4558 TYPE_NAME(T_REGEXP);
4559 TYPE_NAME(T_ARRAY);
4560 TYPE_NAME(T_HASH);
4561 TYPE_NAME(T_STRUCT);
4562 TYPE_NAME(T_BIGNUM);
4563 TYPE_NAME(T_FILE);
4564 TYPE_NAME(T_MATCH);
4565 TYPE_NAME(T_COMPLEX);
4566 TYPE_NAME(T_RATIONAL);
4567 TYPE_NAME(T_NIL);
4568 TYPE_NAME(T_TRUE);
4569 TYPE_NAME(T_FALSE);
4570 TYPE_NAME(T_SYMBOL);
4571 TYPE_NAME(T_FIXNUM);
4572 TYPE_NAME(T_UNDEF);
4573 TYPE_NAME(T_IMEMO);
4574 TYPE_NAME(T_ICLASS);
4575 TYPE_NAME(T_MOVED);
4576 TYPE_NAME(T_ZOMBIE);
4577 case T_DATA:
4578 if (obj && rb_objspace_data_type_name(obj)) {
4579 return rb_objspace_data_type_name(obj);
4580 }
4581 return "T_DATA";
4582#undef TYPE_NAME
4583 }
4584 return "unknown";
4585}
4586
4587static const char *
4588obj_type_name(VALUE obj)
4589{
4590 return type_name(TYPE(obj), obj);
4591}
4592
4593const char *
4594rb_method_type_name(rb_method_type_t type)
4595{
4596 switch (type) {
4597 case VM_METHOD_TYPE_ISEQ: return "iseq";
4598 case VM_METHOD_TYPE_ATTRSET: return "attrest";
4599 case VM_METHOD_TYPE_IVAR: return "ivar";
4600 case VM_METHOD_TYPE_BMETHOD: return "bmethod";
4601 case VM_METHOD_TYPE_ALIAS: return "alias";
4602 case VM_METHOD_TYPE_REFINED: return "refined";
4603 case VM_METHOD_TYPE_CFUNC: return "cfunc";
4604 case VM_METHOD_TYPE_ZSUPER: return "zsuper";
4605 case VM_METHOD_TYPE_MISSING: return "missing";
4606 case VM_METHOD_TYPE_OPTIMIZED: return "optimized";
4607 case VM_METHOD_TYPE_UNDEF: return "undef";
4608 case VM_METHOD_TYPE_NOTIMPLEMENTED: return "notimplemented";
4609 }
4610 rb_bug("rb_method_type_name: unreachable (type: %d)", type);
4611}
4612
4613static void
4614rb_raw_iseq_info(char *const buff, const size_t buff_size, const rb_iseq_t *iseq)
4615{
4616 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj, T_MOVED)) {
4617 VALUE path = rb_iseq_path(iseq);
4618 int n = ISEQ_BODY(iseq)->location.first_lineno;
4619 snprintf(buff, buff_size, " %s@%s:%d",
4620 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4621 RSTRING_PTR(path), n);
4622 }
4623}
4624
4625static int
4626str_len_no_raise(VALUE str)
4627{
4628 long len = RSTRING_LEN(str);
4629 if (len < 0) return 0;
4630 if (len > INT_MAX) return INT_MAX;
4631 return (int)len;
4632}
4633
4634#define BUFF_ARGS buff + pos, buff_size - pos
4635#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4636#define APPEND_S(s) do { \
4637 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4638 goto end; \
4639 } \
4640 else { \
4641 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4642 } \
4643 } while (0)
4644#define C(c, s) ((c) != 0 ? (s) : " ")
4645
4646static size_t
4647rb_raw_obj_info_common(char *const buff, const size_t buff_size, const VALUE obj)
4648{
4649 size_t pos = 0;
4650
4651 if (SPECIAL_CONST_P(obj)) {
4652 APPEND_F("%s", obj_type_name(obj));
4653
4654 if (FIXNUM_P(obj)) {
4655 APPEND_F(" %ld", FIX2LONG(obj));
4656 }
4657 else if (SYMBOL_P(obj)) {
4658 APPEND_F(" %s", rb_id2name(SYM2ID(obj)));
4659 }
4660 }
4661 else {
4662 // const int age = RVALUE_AGE_GET(obj);
4663
4664 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj)) {
4665 APPEND_F("%p %s/", (void *)obj, obj_type_name(obj));
4666 // TODO: fixme
4667 // APPEND_F("%p [%d%s%s%s%s%s%s] %s ",
4668 // (void *)obj, age,
4669 // C(RVALUE_UNCOLLECTIBLE_BITMAP(obj), "L"),
4670 // C(RVALUE_MARK_BITMAP(obj), "M"),
4671 // C(RVALUE_PIN_BITMAP(obj), "P"),
4672 // C(RVALUE_MARKING_BITMAP(obj), "R"),
4673 // C(RVALUE_WB_UNPROTECTED_BITMAP(obj), "U"),
4674 // C(rb_objspace_garbage_object_p(obj), "G"),
4675 // obj_type_name(obj));
4676 }
4677 else {
4678 /* fake */
4679 // APPEND_F("%p [%dXXXX] %s",
4680 // (void *)obj, age,
4681 // obj_type_name(obj));
4682 }
4683
4684 if (internal_object_p(obj)) {
4685 /* ignore */
4686 }
4687 else if (RBASIC(obj)->klass == 0) {
4688 APPEND_S("(temporary internal)");
4689 }
4690 else if (RTEST(RBASIC(obj)->klass)) {
4691 VALUE class_path = rb_class_path_cached(RBASIC(obj)->klass);
4692 if (!NIL_P(class_path)) {
4693 APPEND_F("%s ", RSTRING_PTR(class_path));
4694 }
4695 }
4696 }
4697 end:
4698
4699 return pos;
4700}
4701
4702const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);
4703
4704static size_t
4705rb_raw_obj_info_buitin_type(char *const buff, const size_t buff_size, const VALUE obj, size_t pos)
4706{
4707 if (LIKELY(pos < buff_size) && !SPECIAL_CONST_P(obj)) {
4708 const enum ruby_value_type type = BUILTIN_TYPE(obj);
4709
4710 switch (type) {
4711 case T_NODE:
4712 UNEXPECTED_NODE(rb_raw_obj_info);
4713 break;
4714 case T_ARRAY:
4715 if (ARY_SHARED_P(obj)) {
4716 APPEND_S("shared -> ");
4717 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4718 }
4719 else {
4720 APPEND_F("[%s%s%s] ",
4721 C(ARY_EMBED_P(obj), "E"),
4722 C(ARY_SHARED_P(obj), "S"),
4723 C(ARY_SHARED_ROOT_P(obj), "R"));
4724
4725 if (ARY_EMBED_P(obj)) {
4726 APPEND_F("len: %ld (embed)",
4727 RARRAY_LEN(obj));
4728 }
4729 else {
4730 APPEND_F("len: %ld, capa:%ld ptr:%p",
4731 RARRAY_LEN(obj),
4732 RARRAY(obj)->as.heap.aux.capa,
4733 (void *)RARRAY_CONST_PTR(obj));
4734 }
4735 }
4736 break;
4737 case T_STRING: {
4738 if (STR_SHARED_P(obj)) {
4739 APPEND_F(" [shared] len: %ld", RSTRING_LEN(obj));
4740 }
4741 else {
4742 if (STR_EMBED_P(obj)) APPEND_S(" [embed]");
4743
4744 APPEND_F(" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj), rb_str_capacity(obj));
4745 }
4746 APPEND_F(" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
4747 break;
4748 }
4749 case T_SYMBOL: {
4750 VALUE fstr = RSYMBOL(obj)->fstr;
4751 ID id = RSYMBOL(obj)->id;
4752 if (RB_TYPE_P(fstr, T_STRING)) {
4753 APPEND_F(":%s id:%d", RSTRING_PTR(fstr), (unsigned int)id);
4754 }
4755 else {
4756 APPEND_F("(%p) id:%d", (void *)fstr, (unsigned int)id);
4757 }
4758 break;
4759 }
4760 case T_MOVED: {
4761 APPEND_F("-> %p", (void*)gc_location_internal(rb_gc_get_objspace(), obj));
4762 break;
4763 }
4764 case T_HASH: {
4765 APPEND_F("[%c] %"PRIdSIZE,
4766 RHASH_AR_TABLE_P(obj) ? 'A' : 'S',
4767 RHASH_SIZE(obj));
4768 break;
4769 }
4770 case T_CLASS:
4771 case T_MODULE:
4772 {
4773 VALUE class_path = rb_class_path_cached(obj);
4774 if (!NIL_P(class_path)) {
4775 APPEND_F("%s", RSTRING_PTR(class_path));
4776 }
4777 else {
4778 APPEND_S("(anon)");
4779 }
4780 break;
4781 }
4782 case T_ICLASS:
4783 {
4784 VALUE class_path = rb_class_path_cached(RBASIC_CLASS(obj));
4785 if (!NIL_P(class_path)) {
4786 APPEND_F("src:%s", RSTRING_PTR(class_path));
4787 }
4788 break;
4789 }
4790 case T_OBJECT:
4791 {
4792 if (FL_TEST_RAW(obj, ROBJECT_HEAP)) {
4793 if (rb_shape_obj_too_complex_p(obj)) {
4794 size_t hash_len = rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
4795 APPEND_F("(too_complex) len:%zu", hash_len);
4796 }
4797 else {
4798 APPEND_F("(embed) len:%d capa:%d", RSHAPE_LEN(RBASIC_SHAPE_ID(obj)), ROBJECT_FIELDS_CAPACITY(obj));
4799 }
4800 }
4801 else {
4802 APPEND_F("len:%d capa:%d ptr:%p", RSHAPE_LEN(RBASIC_SHAPE_ID(obj)), ROBJECT_FIELDS_CAPACITY(obj), (void *)ROBJECT_FIELDS(obj));
4803 }
4804 }
4805 break;
4806 case T_DATA: {
4807 const struct rb_block *block;
4808 const rb_iseq_t *iseq;
4809 if (rb_obj_is_proc(obj) &&
4810 (block = vm_proc_block(obj)) != NULL &&
4811 (vm_block_type(block) == block_type_iseq) &&
4812 (iseq = vm_block_iseq(block)) != NULL) {
4813 rb_raw_iseq_info(BUFF_ARGS, iseq);
4814 }
4815 else if (rb_ractor_p(obj)) {
4816 rb_ractor_t *r = (void *)DATA_PTR(obj);
4817 if (r) {
4818 APPEND_F("r:%d", r->pub.id);
4819 }
4820 }
4821 else {
4822 const char * const type_name = rb_objspace_data_type_name(obj);
4823 if (type_name) {
4824 APPEND_F("%s", type_name);
4825 }
4826 }
4827 break;
4828 }
4829 case T_IMEMO: {
4830 APPEND_F("<%s> ", rb_imemo_name(imemo_type(obj)));
4831
4832 switch (imemo_type(obj)) {
4833 case imemo_ment:
4834 {
4835 const rb_method_entry_t *me = (const rb_method_entry_t *)obj;
4836
4837 APPEND_F(":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
4838 rb_id2name(me->called_id),
4839 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ? "pub" :
4840 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ? "pri" : "pro",
4841 METHOD_ENTRY_COMPLEMENTED(me) ? ",cmp" : "",
4842 METHOD_ENTRY_CACHED(me) ? ",cc" : "",
4843 METHOD_ENTRY_INVALIDATED(me) ? ",inv" : "",
4844 me->def ? rb_method_type_name(me->def->type) : "NULL",
4845 me->def ? me->def->aliased : -1,
4846 (void *)me->owner, // obj_info(me->owner),
4847 (void *)me->defined_class); //obj_info(me->defined_class)));
4848
4849 if (me->def) {
4850 switch (me->def->type) {
4851 case VM_METHOD_TYPE_ISEQ:
4852 APPEND_S(" (iseq:");
4853 rb_raw_obj_info(BUFF_ARGS, (VALUE)me->def->body.iseq.iseqptr);
4854 APPEND_S(")");
4855 break;
4856 default:
4857 break;
4858 }
4859 }
4860
4861 break;
4862 }
4863 case imemo_iseq: {
4864 const rb_iseq_t *iseq = (const rb_iseq_t *)obj;
4865 rb_raw_iseq_info(BUFF_ARGS, iseq);
4866 break;
4867 }
4868 case imemo_callinfo:
4869 {
4870 const struct rb_callinfo *ci = (const struct rb_callinfo *)obj;
4871 APPEND_F("(mid:%s, flag:%x argc:%d, kwarg:%s)",
4872 rb_id2name(vm_ci_mid(ci)),
4873 vm_ci_flag(ci),
4874 vm_ci_argc(ci),
4875 vm_ci_kwarg(ci) ? "available" : "NULL");
4876 break;
4877 }
4878 case imemo_callcache:
4879 {
4880 const struct rb_callcache *cc = (const struct rb_callcache *)obj;
4881 VALUE class_path = vm_cc_valid(cc) ? rb_class_path_cached(cc->klass) : Qnil;
4882 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4883
4884 APPEND_F("(klass:%s cme:%s%s (%p) call:%p",
4885 NIL_P(class_path) ? (vm_cc_valid(cc) ? "??" : "<NULL>") : RSTRING_PTR(class_path),
4886 cme ? rb_id2name(cme->called_id) : "<NULL>",
4887 cme ? (METHOD_ENTRY_INVALIDATED(cme) ? " [inv]" : "") : "",
4888 (void *)cme,
4889 (void *)(uintptr_t)vm_cc_call(cc));
4890 break;
4891 }
4892 default:
4893 break;
4894 }
4895 }
4896 default:
4897 break;
4898 }
4899 }
4900 end:
4901
4902 return pos;
4903}
4904
4905#undef C
4906
4907#ifdef RUBY_ASAN_ENABLED
4908void
4909rb_asan_poison_object(VALUE obj)
4910{
4911 MAYBE_UNUSED(struct RVALUE *) ptr = (void *)obj;
4912 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
4913}
4914
4915void
4916rb_asan_unpoison_object(VALUE obj, bool newobj_p)
4917{
4918 MAYBE_UNUSED(struct RVALUE *) ptr = (void *)obj;
4919 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
4920}
4921
4922void *
4923rb_asan_poisoned_object_p(VALUE obj)
4924{
4925 MAYBE_UNUSED(struct RVALUE *) ptr = (void *)obj;
4926 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
4927}
4928#endif
4929
4930static void
4931raw_obj_info(char *const buff, const size_t buff_size, VALUE obj)
4932{
4933 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4934 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4935 if (pos >= buff_size) {} // truncated
4936}
4937
4938const char *
4939rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj)
4940{
4941 void *objspace = rb_gc_get_objspace();
4942
4943 if (SPECIAL_CONST_P(obj)) {
4944 raw_obj_info(buff, buff_size, obj);
4945 }
4946 else if (!rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj)) {
4947 snprintf(buff, buff_size, "out-of-heap:%p", (void *)obj);
4948 }
4949#if 0 // maybe no need to check it?
4950 else if (0 && rb_gc_impl_garbage_object_p(objspace, obj)) {
4951 snprintf(buff, buff_size, "garbage:%p", (void *)obj);
4952 }
4953#endif
4954 else {
4955 asan_unpoisoning_object(obj) {
4956 raw_obj_info(buff, buff_size, obj);
4957 }
4958 }
4959 return buff;
4960}
4961
4962#undef APPEND_S
4963#undef APPEND_F
4964#undef BUFF_ARGS
4965
4966/* Increments *var atomically and resets *var to 0 when maxval is
4967 * reached. Returns the wraparound old *var value (0...maxval). */
4968static rb_atomic_t
4969atomic_inc_wraparound(rb_atomic_t *var, const rb_atomic_t maxval)
4970{
4971 rb_atomic_t oldval = RUBY_ATOMIC_FETCH_ADD(*var, 1);
4972 if (RB_UNLIKELY(oldval >= maxval - 1)) { // wraparound *var
4973 const rb_atomic_t newval = oldval + 1;
4974 RUBY_ATOMIC_CAS(*var, newval, newval % maxval);
4975 oldval %= maxval;
4976 }
4977 return oldval;
4978}
4979
4980static const char *
4981obj_info(VALUE obj)
4982{
4983 if (RGENGC_OBJ_INFO) {
4984 static struct {
4985 rb_atomic_t index;
4986 char buffers[10][0x100];
4987 } info = {0};
4988
4989 rb_atomic_t index = atomic_inc_wraparound(&info.index, numberof(info.buffers));
4990 char *const buff = info.buffers[index];
4991 return rb_raw_obj_info(buff, sizeof(info.buffers[0]), obj);
4992 }
4993 return obj_type_name(obj);
4994}
4995
4996/*
4997 ------------------------ Extended allocator ------------------------
4998*/
4999
5001 VALUE exc;
5002 const char *fmt;
5003 va_list *ap;
5004};
5005
5006static void *
5007gc_vraise(void *ptr)
5008{
5009 struct gc_raise_tag *argv = ptr;
5010 rb_vraise(argv->exc, argv->fmt, *argv->ap);
5011 UNREACHABLE_RETURN(NULL);
5012}
5013
5014static void
5015gc_raise(VALUE exc, const char *fmt, ...)
5016{
5017 va_list ap;
5018 va_start(ap, fmt);
5019 struct gc_raise_tag argv = {
5020 exc, fmt, &ap,
5021 };
5022
5023 if (ruby_native_thread_p()) {
5024 rb_thread_call_with_gvl(gc_vraise, &argv);
5026 }
5027 else {
5028 /* Not in a ruby thread */
5029 fprintf(stderr, "%s", "[FATAL] ");
5030 vfprintf(stderr, fmt, ap);
5031 }
5032
5033 va_end(ap);
5034 abort();
5035}
5036
5037NORETURN(static void negative_size_allocation_error(const char *));
5038static void
5039negative_size_allocation_error(const char *msg)
5040{
5041 gc_raise(rb_eNoMemError, "%s", msg);
5042}
5043
5044static void *
5045ruby_memerror_body(void *dummy)
5046{
5047 rb_memerror();
5048 return 0;
5049}
5050
5051NORETURN(static void ruby_memerror(void));
5053static void
5054ruby_memerror(void)
5055{
5056 if (ruby_thread_has_gvl_p()) {
5057 rb_memerror();
5058 }
5059 else {
5060 if (ruby_native_thread_p()) {
5061 rb_thread_call_with_gvl(ruby_memerror_body, 0);
5062 }
5063 else {
5064 /* no ruby thread */
5065 fprintf(stderr, "[FATAL] failed to allocate memory\n");
5066 }
5067 }
5068
5069 /* We have discussions whether we should die here; */
5070 /* We might rethink about it later. */
5071 exit(EXIT_FAILURE);
5072}
5073
5074void
5076{
5077 /* the `GET_VM()->special_exceptions` below assumes that
5078 * the VM is reachable from the current thread. We should
5079 * definitely make sure of that. */
5081
5082 rb_execution_context_t *ec = GET_EC();
5083 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
5084
5085 if (!exc ||
5086 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
5087 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
5088 fprintf(stderr, "[FATAL] failed to allocate memory\n");
5089 exit(EXIT_FAILURE);
5090 }
5091 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
5092 rb_ec_raised_clear(ec);
5093 }
5094 else {
5095 rb_ec_raised_set(ec, RAISED_NOMEMORY);
5096 exc = ruby_vm_special_exception_copy(exc);
5097 }
5098 ec->errinfo = exc;
5099 EC_JUMP_TAG(ec, TAG_RAISE);
5100}
5101
5102bool
5103rb_memerror_reentered(void)
5104{
5105 rb_execution_context_t *ec = GET_EC();
5106 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
5107}
5108
5109static void *
5110handle_malloc_failure(void *ptr)
5111{
5112 if (LIKELY(ptr)) {
5113 return ptr;
5114 }
5115 else {
5116 ruby_memerror();
5117 UNREACHABLE_RETURN(ptr);
5118 }
5119}
5120
5121static void *ruby_xmalloc_body(size_t size);
5122
5123void *
5124ruby_xmalloc(size_t size)
5125{
5126 return handle_malloc_failure(ruby_xmalloc_body(size));
5127}
5128
5129static bool
5130malloc_gc_allowed(void)
5131{
5132 rb_ractor_t *r = rb_current_ractor_raw(false);
5133
5134 return r == NULL || !r->malloc_gc_disabled;
5135}
5136
5137static void *
5138ruby_xmalloc_body(size_t size)
5139{
5140 if ((ssize_t)size < 0) {
5141 negative_size_allocation_error("too large allocation size");
5142 }
5143
5144 return rb_gc_impl_malloc(rb_gc_get_objspace(), size, malloc_gc_allowed());
5145}
5146
5147void
5148ruby_malloc_size_overflow(size_t count, size_t elsize)
5149{
5150 rb_raise(rb_eArgError,
5151 "malloc: possible integer overflow (%"PRIuSIZE"*%"PRIuSIZE")",
5152 count, elsize);
5153}
5154
5155void
5156ruby_malloc_add_size_overflow(size_t x, size_t y)
5157{
5158 rb_raise(rb_eArgError,
5159 "malloc: possible integer overflow (%"PRIuSIZE"+%"PRIuSIZE")",
5160 x, y);
5161}
5162
5163static void *ruby_xmalloc2_body(size_t n, size_t size);
5164
5165void *
5166ruby_xmalloc2(size_t n, size_t size)
5167{
5168 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
5169}
5170
5171static void *
5172ruby_xmalloc2_body(size_t n, size_t size)
5173{
5174 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
5175}
5176
5177static void *ruby_xcalloc_body(size_t n, size_t size);
5178
5179void *
5180ruby_xcalloc(size_t n, size_t size)
5181{
5182 return handle_malloc_failure(ruby_xcalloc_body(n, size));
5183}
5184
5185static void *
5186ruby_xcalloc_body(size_t n, size_t size)
5187{
5188 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size), malloc_gc_allowed());
5189}
5190
5191static void *ruby_sized_xrealloc_body(void *ptr, size_t new_size, size_t old_size);
5192
5193#ifdef ruby_sized_xrealloc
5194#undef ruby_sized_xrealloc
5195#endif
5196void *
5197ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size)
5198{
5199 return handle_malloc_failure(ruby_sized_xrealloc_body(ptr, new_size, old_size));
5200}
5201
5202static void *
5203ruby_sized_xrealloc_body(void *ptr, size_t new_size, size_t old_size)
5204{
5205 if ((ssize_t)new_size < 0) {
5206 negative_size_allocation_error("too large allocation size");
5207 }
5208
5209 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size, malloc_gc_allowed());
5210}
5211
5212void *
5213ruby_xrealloc(void *ptr, size_t new_size)
5214{
5215 return ruby_sized_xrealloc(ptr, new_size, 0);
5216}
5217
5218static void *ruby_sized_xrealloc2_body(void *ptr, size_t n, size_t size, size_t old_n);
5219
5220#ifdef ruby_sized_xrealloc2
5221#undef ruby_sized_xrealloc2
5222#endif
5223void *
5224ruby_sized_xrealloc2(void *ptr, size_t n, size_t size, size_t old_n)
5225{
5226 return handle_malloc_failure(ruby_sized_xrealloc2_body(ptr, n, size, old_n));
5227}
5228
5229static void *
5230ruby_sized_xrealloc2_body(void *ptr, size_t n, size_t size, size_t old_n)
5231{
5232 size_t len = xmalloc2_size(n, size);
5233 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, len, old_n * size, malloc_gc_allowed());
5234}
5235
5236void *
5237ruby_xrealloc2(void *ptr, size_t n, size_t size)
5238{
5239 return ruby_sized_xrealloc2(ptr, n, size, 0);
5240}
5241
5242#ifdef ruby_sized_xfree
5243#undef ruby_sized_xfree
5244#endif
5245void
5246ruby_sized_xfree(void *x, size_t size)
5247{
5248 if (LIKELY(x)) {
5249 /* It's possible for a C extension's pthread destructor function set by pthread_key_create
5250 * to be called after ruby_vm_destruct and attempt to free memory. Fall back to mimfree in
5251 * that case. */
5252 if (LIKELY(GET_VM())) {
5253 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
5254 }
5255 else {
5256 ruby_mimfree(x);
5257 }
5258 }
5259}
5260
5261void
5262ruby_xfree(void *x)
5263{
5264 ruby_sized_xfree(x, 0);
5265}
5266
5267void *
5268rb_xmalloc_mul_add(size_t x, size_t y, size_t z) /* x * y + z */
5269{
5270 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5271 return ruby_xmalloc(w);
5272}
5273
5274void *
5275rb_xcalloc_mul_add(size_t x, size_t y, size_t z) /* x * y + z */
5276{
5277 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5278 return ruby_xcalloc(w, 1);
5279}
5280
5281void *
5282rb_xrealloc_mul_add(const void *p, size_t x, size_t y, size_t z) /* x * y + z */
5283{
5284 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5285 return ruby_xrealloc((void *)p, w);
5286}
5287
5288void *
5289rb_xmalloc_mul_add_mul(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
5290{
5291 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5292 return ruby_xmalloc(u);
5293}
5294
5295void *
5296rb_xcalloc_mul_add_mul(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
5297{
5298 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5299 return ruby_xcalloc(u, 1);
5300}
5301
5302/* Mimic ruby_xmalloc, but need not rb_objspace.
5303 * should return pointer suitable for ruby_xfree
5304 */
5305void *
5306ruby_mimmalloc(size_t size)
5307{
5308 void *mem;
5309#if CALC_EXACT_MALLOC_SIZE
5310 size += sizeof(struct malloc_obj_info);
5311#endif
5312 mem = malloc(size);
5313#if CALC_EXACT_MALLOC_SIZE
5314 if (!mem) {
5315 return NULL;
5316 }
5317 else
5318 /* set 0 for consistency of allocated_size/allocations */
5319 {
5320 struct malloc_obj_info *info = mem;
5321 info->size = 0;
5322 mem = info + 1;
5323 }
5324#endif
5325 return mem;
5326}
5327
5328void *
5329ruby_mimcalloc(size_t num, size_t size)
5330{
5331 void *mem;
5332#if CALC_EXACT_MALLOC_SIZE
5333 struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(num, size);
5334 if (UNLIKELY(t.overflowed)) {
5335 return NULL;
5336 }
5337 size = t.result + sizeof(struct malloc_obj_info);
5338 mem = calloc1(size);
5339 if (!mem) {
5340 return NULL;
5341 }
5342 else
5343 /* set 0 for consistency of allocated_size/allocations */
5344 {
5345 struct malloc_obj_info *info = mem;
5346 info->size = 0;
5347 mem = info + 1;
5348 }
5349#else
5350 mem = calloc(num, size);
5351#endif
5352 return mem;
5353}
5354
5355void
5356ruby_mimfree(void *ptr)
5357{
5358#if CALC_EXACT_MALLOC_SIZE
5359 struct malloc_obj_info *info = (struct malloc_obj_info *)ptr - 1;
5360 ptr = info;
5361#endif
5362 free(ptr);
5363}
5364
5365void
5366rb_gc_adjust_memory_usage(ssize_t diff)
5367{
5368 unless_objspace(objspace) { return; }
5369
5370 rb_gc_impl_adjust_memory_usage(objspace, diff);
5371}
5372
5373const char *
5374rb_obj_info(VALUE obj)
5375{
5376 return obj_info(obj);
5377}
5378
5379void
5380rb_obj_info_dump(VALUE obj)
5381{
5382 char buff[0x100];
5383 fprintf(stderr, "rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
5384}
5385
5386void
5387rb_obj_info_dump_loc(VALUE obj, const char *file, int line, const char *func)
5388{
5389 char buff[0x100];
5390 fprintf(stderr, "<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
5391}
5392
5393void
5394rb_gc_before_fork(void)
5395{
5396 rb_gc_impl_before_fork(rb_gc_get_objspace());
5397}
5398
5399void
5400rb_gc_after_fork(rb_pid_t pid)
5401{
5402 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
5403}
5404
5405bool
5406rb_gc_obj_shareable_p(VALUE obj)
5407{
5408 return RB_OBJ_SHAREABLE_P(obj);
5409}
5410
5411void
5412rb_gc_rp(VALUE obj)
5413{
5414 rp(obj);
5415}
5416
5418 VALUE parent;
5419 long err_count;
5420};
5421
5422static void
5423check_shareable_i(const VALUE child, void *ptr)
5424{
5425 struct check_shareable_data *data = (struct check_shareable_data *)ptr;
5426
5427 if (!rb_gc_obj_shareable_p(child)) {
5428 fprintf(stderr, "(a) ");
5429 rb_gc_rp(data->parent);
5430 fprintf(stderr, "(b) ");
5431 rb_gc_rp(child);
5432 fprintf(stderr, "check_shareable_i: shareable (a) -> unshareable (b)\n");
5433
5434 data->err_count++;
5435 rb_bug("!! violate shareable constraint !!");
5436 }
5437}
5438
5439static bool gc_checking_shareable = false;
5440
5441static void
5442gc_verify_shareable(void *objspace, VALUE obj, void *data)
5443{
5444 // while gc_checking_shareable is true,
5445 // other Ractors should not run the GC, until the flag is not local.
5446 // TODO: remove VM locking if the flag is Ractor local
5447
5448 unsigned int lev = RB_GC_VM_LOCK();
5449 {
5450 gc_checking_shareable = true;
5451 rb_objspace_reachable_objects_from(obj, check_shareable_i, (void *)data);
5452 gc_checking_shareable = false;
5453 }
5454 RB_GC_VM_UNLOCK(lev);
5455}
5456
5457// TODO: only one level (non-recursive)
5458void
5459rb_gc_verify_shareable(VALUE obj)
5460{
5461 rb_objspace_t *objspace = rb_gc_get_objspace();
5462 struct check_shareable_data data = {
5463 .parent = obj,
5464 .err_count = 0,
5465 };
5466 gc_verify_shareable(objspace, obj, &data);
5467
5468 if (data.err_count > 0) {
5469 rb_bug("rb_gc_verify_shareable");
5470 }
5471}
5472
5473bool
5474rb_gc_checking_shareable(void)
5475{
5476 return gc_checking_shareable;
5477}
5478
5479/*
5480 * Document-module: ObjectSpace
5481 *
5482 * The ObjectSpace module contains a number of routines
5483 * that interact with the garbage collection facility and allow you to
5484 * traverse all living objects with an iterator.
5485 *
5486 * ObjectSpace also provides support for object finalizers, procs that will be
5487 * called after a specific object was destroyed by garbage collection. See
5488 * the documentation for +ObjectSpace.define_finalizer+ for important
5489 * information on how to use this method correctly.
5490 *
5491 * a = "A"
5492 * b = "B"
5493 *
5494 * ObjectSpace.define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
5495 * ObjectSpace.define_finalizer(b, proc {|id| puts "Finalizer two on #{id}" })
5496 *
5497 * a = nil
5498 * b = nil
5499 *
5500 * _produces:_
5501 *
5502 * Finalizer two on 537763470
5503 * Finalizer one on 537763480
5504 */
5505
5506/* Document-class: GC::Profiler
5507 *
5508 * The GC profiler provides access to information on GC runs including time,
5509 * length and object space size.
5510 *
5511 * Example:
5512 *
5513 * GC::Profiler.enable
5514 *
5515 * require 'rdoc/rdoc'
5516 *
5517 * GC::Profiler.report
5518 *
5519 * GC::Profiler.disable
5520 *
5521 * See also GC.count, GC.malloc_allocated_size and GC.malloc_allocations
5522 */
5523
5524#include "gc.rbinc"
5525
5526void
5527Init_GC(void)
5528{
5529#undef rb_intern
5530 rb_gc_register_address(&id2ref_value);
5531
5532 malloc_offset = gc_compute_malloc_offset();
5533
5534 rb_mGC = rb_define_module("GC");
5535
5536 VALUE rb_mObjSpace = rb_define_module("ObjectSpace");
5537
5538 rb_define_module_function(rb_mObjSpace, "each_object", os_each_obj, -1);
5539
5540 rb_define_module_function(rb_mObjSpace, "define_finalizer", define_final, -1);
5541 rb_define_module_function(rb_mObjSpace, "undefine_finalizer", undefine_final, 1);
5542
5543 rb_define_module_function(rb_mObjSpace, "_id2ref", os_id2ref, 1);
5544
5545 rb_vm_register_special_exception(ruby_error_nomemory, rb_eNoMemError, "failed to allocate memory");
5546
5547 rb_define_method(rb_cBasicObject, "__id__", rb_obj_id, 0);
5548 rb_define_method(rb_mKernel, "object_id", rb_obj_id, 0);
5549
5550 rb_define_module_function(rb_mObjSpace, "count_objects", count_objects, -1);
5551
5552 rb_gc_impl_init();
5553}
5554
5555// Set a name for the anonymous virtual memory area. `addr` is the starting
5556// address of the area and `size` is its length in bytes. `name` is a
5557// NUL-terminated human-readable string.
5558//
5559// This function is usually called after calling `mmap()`. The human-readable
5560// annotation helps developers identify the call site of `mmap()` that created
5561// the memory mapping.
5562//
5563// This function currently only works on Linux 5.17 or higher. After calling
5564// this function, we can see annotations in the form of "[anon:...]" in
5565// `/proc/self/maps`, where `...` is the content of `name`. This function has
5566// no effect when called on other platforms.
5567void
5568ruby_annotate_mmap(const void *addr, unsigned long size, const char *name)
5569{
5570#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
5571 // The name length cannot exceed 80 (including the '\0').
5572 RUBY_ASSERT(strlen(name) < 80);
5573 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name);
5574 // We ignore errors in prctl. prctl may set errno to EINVAL for several
5575 // reasons.
5576 // 1. The attr (PR_SET_VMA_ANON_NAME) is not a valid attribute.
5577 // 2. addr is an invalid address.
5578 // 3. The string pointed by name is too long.
5579 // The first error indicates PR_SET_VMA_ANON_NAME is not available, and may
5580 // happen if we run the compiled binary on an old kernel. In theory, all
5581 // other errors should result in a failure. But since EINVAL cannot tell
5582 // the first error from others, and this function is mainly used for
5583 // debugging, we silently ignore the error.
5584 errno = 0;
5585#endif
5586}
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define RUBY_ATOMIC_VALUE_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
Definition atomic.h:406
#define RUBY_ATOMIC_SIZE_FETCH_ADD(var, val)
Identical to RUBY_ATOMIC_FETCH_ADD, except it expects its arguments to be size_t.
Definition atomic.h:235
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
Definition atomic.h:165
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#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...
Definition atomic.h:118
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
Definition event.h:93
@ RUBY_FL_WB_PROTECTED
Definition fl_type.h:198
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1591
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.
Definition class.c:3133
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define T_FILE
Old name of RUBY_T_FILE.
Definition value_type.h:62
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:133
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define T_MASK
Old name of RUBY_T_MASK.
Definition value_type.h:68
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define LL2NUM
Old name of RB_LL2NUM.
Definition long_long.h:30
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define T_NODE
Old name of RUBY_T_NODE.
Definition value_type.h:73
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
Definition fl_type.h:61
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define FL_ABLE
Old name of RB_FL_ABLE.
Definition fl_type.h:121
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#define T_UNDEF
Old name of RUBY_T_UNDEF.
Definition value_type.h:82
#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.
Definition value_type.h:83
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define NUM2ULL
Old name of RB_NUM2ULL.
Definition long_long.h:35
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define T_MATCH
Old name of RUBY_T_MATCH.
Definition value_type.h:69
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define T_MOVED
Old name of RUBY_T_MOVED.
Definition value_type.h:71
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
#define FL_UNSET
Old name of RB_FL_UNSET.
Definition fl_type.h:132
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
Definition size_t.h:61
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
Definition gc.c:2539
int ruby_stack_check(void)
Checks for stack overflow.
Definition gc.c:2579
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:476
VALUE rb_eNoMemError
NoMemoryError exception.
Definition error.c:1442
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1435
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:475
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1431
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
VALUE rb_mKernel
Kernel module.
Definition object.c:60
VALUE rb_mGC
GC module.
Definition gc.c:424
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:264
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:59
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.
Definition object.c:923
size_t rb_obj_embedded_size(uint32_t fields_count)
Internal header for Object.
Definition object.c:94
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3306
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
Definition fixnum.h:43
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:947
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
void rb_memerror(void)
Triggers out-of-memory error.
Definition gc.c:5075
VALUE rb_gc_disable(void)
Disables GC.
Definition gc.c:4459
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
Definition gc.c:4253
VALUE rb_gc_enable(void)
(Re-) enables GC.
Definition gc.c:4425
int rb_during_gc(void)
Queries if the GC is busy.
Definition gc.c:4268
void rb_gc(void)
Triggers a GC process.
Definition gc.c:4260
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
Definition gc.c:4276
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
Definition enumerator.h:242
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1464
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:983
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:120
void rb_str_free(VALUE str)
Destroys the given string for no reason.
Definition string.c:1752
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:1001
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
Definition variable.c:389
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
Definition variable.c:1308
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1664
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.
Definition vm_eval.c:686
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition vm_method.c:1670
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition vm_method.c:3400
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:993
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition io.h:2
int rb_io_fptr_finalize(rb_io_t *fptr)
Destroys the given IO.
Definition io.c:5693
int len
Length of the buffer.
Definition io.h:8
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:235
int ruby_thread_has_gvl_p(void)
Whether the current thread is holding the GVL.
Definition thread.c:2103
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
Definition thread.c:2062
bool ruby_free_at_exit_p(void)
Returns whether the Ruby VM will free all memory at shutdown.
Definition vm.c:4807
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1372
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]].
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
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.
Definition rarray.h:51
#define RARRAY(obj)
Convenient casting macro.
Definition rarray.h:44
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RCLASS(obj)
Convenient casting macro.
Definition rclass.h:38
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.
Definition gc.c:1066
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
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...
Definition gc.c:1081
#define RDATA(obj)
Convenient casting macro.
Definition rdata.h:59
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
Definition rdata.h:78
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
Definition rdata.h:104
#define RFILE(obj)
Convenient casting macro.
Definition rfile.h:50
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
#define RMATCH(obj)
Convenient casting macro.
Definition rmatch.h:37
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
Definition robject.h:128
#define RREGEXP(obj)
Convenient casting macro.
Definition rregexp.h:37
#define RREGEXP_PTR(obj)
Convenient accessor macro.
Definition rregexp.h:45
#define RSTRING(obj)
Convenient casting macro.
Definition rstring.h:41
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:575
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.
Definition gc.c:1105
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...
Definition gc.c:1115
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:461
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition rtypeddata.h:95
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:598
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:515
void rb_p(VALUE obj)
Inspects an object.
Definition io.c:9056
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
Definition thread.c:5806
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.
Defines old _.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
Ruby's array.
Definition rarray.h:128
Ruby object's base components.
Definition rbasic.h:69
Definition rdata.h:120
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rdata.h:143
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rdata.h:134
void * data
Pointer to the actual C level struct that you want to wrap.
Definition rdata.h:149
Definition hash.h:53
Ruby's ordinal objects.
Definition robject.h:85
Ruby's String.
Definition rstring.h:196
"Typed" user data.
Definition rtypeddata.h:358
void * data
Pointer to the actual C level struct that you want to wrap.
Definition rtypeddata.h:378
VALUE fields_obj
Direct reference to the slots that holds instance variables, if any.
Definition rtypeddata.h:364
struct rb_data_type_struct::@006053276242002167166045007236206132153037024024 function
Function pointers.
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rtypeddata.h:238
RUBY_DATA_FUNC dcompact
This function is called when the object is relocated.
Definition rtypeddata.h:259
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:215
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rtypeddata.h:229
VALUE flags
Type-specific behavioural characteristics.
Definition rtypeddata.h:317
Definition gc_impl.h:15
Ruby's IO, metadata and buffers.
Definition io.h:295
struct rmatch_offset * char_offset
Capture group offsets, in C array.
Definition rmatch.h:79
int char_offset_num_allocated
Number of rmatch_offset that rmatch::char_offset holds.
Definition rmatch.h:82
struct re_registers regs
"Registers" of a match.
Definition rmatch.h:76
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:143
Represents the region of a capture group.
Definition rmatch.h:65
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
ruby_value_type
C-level type of an object.
Definition value_type.h:113