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