Ruby: gc.c Source File

Ruby 3.4.9p82 (2026-03-11 revision 76cca827ab52ab1d346a728f068d5b8da3e2952b)

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