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