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