Ruby 4.1.0dev (2026-11-17 revision 3a19e2efcf133ee35daaeffc36633cd99a9f2c47)
mmtk.c
1#include <pthread.h>
2#include <stdbool.h>
3#include <string.h>
4
5#include "ruby/assert.h"
6#include "ruby/atomic.h"
7#include "ruby/debug.h"
8
9#include "gc/gc.h"
10#include "gc/gc_impl.h"
11#include "gc/mmtk/mmtk.h"
12
13#if USE_ZJIT
14# include "gc/mmtk/zjit_fastpath.h"
15#endif
16
17#include "ccan/list/list.h"
18#include "darray.h"
19
20#ifdef __APPLE__
21#include <sys/sysctl.h>
22#endif
23
24struct objspace {
25 bool measure_gc_time;
26 bool gc_stress;
27
28 size_t gc_count;
29 size_t moving_gc_count;
30 size_t total_gc_time;
31 size_t total_allocated_objects;
32
33 st_table *finalizer_table;
34 struct MMTk_final_job *finalizer_jobs;
35 rb_postponed_job_handle_t finalizer_postponed_job;
36
37 struct ccan_list_head ractor_caches;
38 unsigned long live_ractor_cache_count;
39
40 pthread_mutex_t mutex;
41 rb_atomic_t mutator_blocking_count;
42 bool world_stopped;
43 pthread_cond_t cond_world_stopped;
44 pthread_cond_t cond_world_started;
45 size_t start_the_world_count;
46
47 pthread_mutex_t event_hook_mutex;
48
49 struct {
50 bool gc_thread_crashed;
51 char crash_msg[256];
52 } crash_context;
53
54 struct rb_gc_vm_context vm_context;
55
56 unsigned int fork_hook_vm_lock_lev;
57
58 uintptr_t vo_bit_log_region_size;
59 uintptr_t vo_bit_base_addr;
60 size_t max_non_los_default_alloc_bytes;
61 bool write_barrier_required;
62};
63
64#define OBJ_FREE_BUF_CAPACITY 128
65
67 struct ccan_list_node list_node;
68
69 MMTk_Mutator *mutator;
70 bool gc_mutator_p;
71
72 MMTk_BumpPointer *bump_pointer;
73
74 MMTk_ObjectReference obj_free_parallel_buf[OBJ_FREE_BUF_CAPACITY];
75 size_t obj_free_parallel_count;
76 MMTk_ObjectReference obj_free_non_parallel_buf[OBJ_FREE_BUF_CAPACITY];
77 size_t obj_free_non_parallel_count;
78};
79
81 struct MMTk_final_job *next;
82 enum {
83 MMTK_FINAL_JOB_DFREE,
84 MMTK_FINAL_JOB_FINALIZE,
85 } kind;
86 union {
87 struct {
88 void (*func)(void *);
89 void *data;
90 } dfree;
91 struct {
92 /* HACK: we store the object ID on the 0th element of this array. */
93 VALUE finalizer_array;
94 } finalize;
95 } as;
96};
97
98#ifdef RB_THREAD_LOCAL_SPECIFIER
99RB_THREAD_LOCAL_SPECIFIER struct MMTk_GCThreadTLS *rb_mmtk_gc_thread_tls;
100
101RB_THREAD_LOCAL_SPECIFIER VALUE marking_parent_object;
102#else
103# error We currently need language-supported TLS
104#endif
105
106#ifdef MMTK_DEBUG
107# define MMTK_ASSERT(expr, ...) RUBY_ASSERT_ALWAYS(expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
108#else
109# define MMTK_ASSERT(expr, ...) ((void)0)
110#endif
111
112#include <pthread.h>
113
114#define MMTK_ALLOCATION_SEMANTICS_DEFAULT 0
115#define MMTK_ALLOCATION_SEMANTICS_LOS 2
116
117/* Functions called from the Rust binding (upcalls). They are not static so
118 * that the linker can resolve them from libmmtk_ruby.a, but they are hidden
119 * so that calls do not go through the PLT and so that LTO can inline them
120 * into their Rust callers. */
121#ifdef __GNUC__
122# define RB_MMTK_UPCALL __attribute__((visibility("hidden")))
123#else
124# define RB_MMTK_UPCALL
125#endif
126
127static inline VALUE rb_mmtk_call_object_closure(VALUE obj, bool pin);
128
129RB_MMTK_UPCALL void
130rb_mmtk_init_gc_worker_thread(MMTk_VMWorkerThread gc_thread_tls)
131{
132 rb_mmtk_gc_thread_tls = gc_thread_tls;
133}
134
135RB_MMTK_UPCALL bool
136rb_mmtk_is_mutator(void)
137{
138 return ruby_native_thread_p();
139}
140
141RB_MMTK_UPCALL void
142rb_mmtk_stop_the_world(void)
143{
144 struct objspace *objspace = rb_gc_get_objspace();
145
146 int err;
147 if ((err = pthread_mutex_lock(&objspace->mutex)) != 0) {
148 rb_bug("ERROR: cannot lock objspace->mutex: %s", strerror(err));
149 }
150
151 while (!objspace->world_stopped) {
152 pthread_cond_wait(&objspace->cond_world_stopped, &objspace->mutex);
153 }
154
155 if ((err = pthread_mutex_unlock(&objspace->mutex)) != 0) {
156 rb_bug("ERROR: cannot release objspace->mutex: %s", strerror(err));
157 }
158}
159
160RB_MMTK_UPCALL void
161rb_mmtk_resume_mutators(bool current_gc_may_move)
162{
163 struct objspace *objspace = rb_gc_get_objspace();
164
165 int err;
166 if ((err = pthread_mutex_lock(&objspace->mutex)) != 0) {
167 rb_bug("ERROR: cannot lock objspace->mutex: %s", strerror(err));
168 }
169
170 objspace->world_stopped = false;
171 objspace->gc_count++;
172 if (current_gc_may_move) objspace->moving_gc_count++;
173 pthread_cond_broadcast(&objspace->cond_world_started);
174
175 if ((err = pthread_mutex_unlock(&objspace->mutex)) != 0) {
176 rb_bug("ERROR: cannot release objspace->mutex: %s", strerror(err));
177 }
178}
179
180static void mmtk_flush_obj_free_buffer(struct MMTk_ractor_cache *cache);
181
182RB_MMTK_UPCALL void
183rb_mmtk_block_for_gc(MMTk_VMMutatorThread mutator)
184{
185 struct objspace *objspace = rb_gc_get_objspace();
186
187 size_t starting_gc_count = objspace->gc_count;
188 RUBY_ATOMIC_INC(objspace->mutator_blocking_count);
189 int lock_lev = RB_GC_VM_LOCK();
190 RUBY_ATOMIC_DEC(objspace->mutator_blocking_count);
191 int err;
192 if ((err = pthread_mutex_lock(&objspace->mutex)) != 0) {
193 rb_bug("ERROR: cannot lock objspace->mutex: %s", strerror(err));
194 }
195
196 if (objspace->gc_count == starting_gc_count) {
197 rb_gc_event_hook(0, RUBY_INTERNAL_EVENT_GC_START);
198
199 rb_gc_initialize_vm_context(&objspace->vm_context);
200
201 mutator->gc_mutator_p = true;
202
203 struct timespec gc_start_time;
204 if (objspace->measure_gc_time) {
205 clock_gettime(CLOCK_MONOTONIC, &gc_start_time);
206 }
207
208 rb_gc_save_machine_context();
209
210 rb_gc_vm_barrier();
211
212 struct MMTk_ractor_cache *rc;
213 ccan_list_for_each(&objspace->ractor_caches, rc, list_node) {
214 mmtk_flush_obj_free_buffer(rc);
215 }
216
217 objspace->world_stopped = true;
218
219 pthread_cond_broadcast(&objspace->cond_world_stopped);
220
221 // Wait for GC end
222 while (objspace->world_stopped) {
223 pthread_cond_wait(&objspace->cond_world_started, &objspace->mutex);
224 }
225
226 if (RB_UNLIKELY(objspace->crash_context.gc_thread_crashed)) {
227 rb_bug("%s", objspace->crash_context.crash_msg);
228 }
229
230 if (objspace->measure_gc_time) {
231 struct timespec gc_end_time;
232 clock_gettime(CLOCK_MONOTONIC, &gc_end_time);
233
234 objspace->total_gc_time +=
235 (gc_end_time.tv_sec - gc_start_time.tv_sec) * (1000 * 1000 * 1000) +
236 (gc_end_time.tv_nsec - gc_start_time.tv_nsec);
237 }
238 }
239
240 if ((err = pthread_mutex_unlock(&objspace->mutex)) != 0) {
241 rb_bug("ERROR: cannot release objspace->mutex: %s", strerror(err));
242 }
243 RB_GC_VM_UNLOCK(lock_lev);
244}
245
246RB_MMTK_UPCALL void
247rb_mmtk_before_updating_jit_code(void)
248{
249 rb_gc_before_updating_jit_code();
250}
251
252RB_MMTK_UPCALL void
253rb_mmtk_after_updating_jit_code(void)
254{
255 rb_gc_after_updating_jit_code();
256}
257
258RB_MMTK_UPCALL size_t
259rb_mmtk_number_of_mutators(void)
260{
261 struct objspace *objspace = rb_gc_get_objspace();
262 return objspace->live_ractor_cache_count;
263}
264
265RB_MMTK_UPCALL void
266rb_mmtk_get_mutators(void (*visit_mutator)(MMTk_Mutator *mutator, void *data), void *data)
267{
268 struct objspace *objspace = rb_gc_get_objspace();
269 struct MMTk_ractor_cache *ractor_cache;
270
271 ccan_list_for_each(&objspace->ractor_caches, ractor_cache, list_node) {
272 visit_mutator(ractor_cache->mutator, data);
273 }
274}
275
276RB_MMTK_UPCALL void
277rb_mmtk_scan_gc_roots(void)
278{
279 struct objspace *objspace = rb_gc_get_objspace();
280
281 rb_gc_mark_roots(objspace, NULL);
282}
283
284static int
285pin_value(st_data_t key, st_data_t value, st_data_t data)
286{
287 rb_gc_impl_mark_and_pin((void *)data, (VALUE)value);
288
289 return ST_CONTINUE;
290}
291
292RB_MMTK_UPCALL void
293rb_mmtk_scan_objspace(void)
294{
295 struct objspace *objspace = rb_gc_get_objspace();
296
297 if (objspace->finalizer_table != NULL) {
298 st_foreach(objspace->finalizer_table, pin_value, (st_data_t)objspace);
299 }
300
301 struct MMTk_final_job *job = objspace->finalizer_jobs;
302 while (job != NULL) {
303 switch (job->kind) {
304 case MMTK_FINAL_JOB_DFREE:
305 break;
306 case MMTK_FINAL_JOB_FINALIZE:
307 rb_gc_impl_mark(objspace, job->as.finalize.finalizer_array);
308 break;
309 default:
310 rb_bug("rb_mmtk_scan_objspace: unknown final job type %d", job->kind);
311 }
312
313 job = job->next;
314 }
315}
316
317RB_MMTK_UPCALL void
318rb_mmtk_move_obj_during_marking(MMTk_ObjectReference from, MMTk_ObjectReference to)
319{
320 rb_gc_move_obj_during_marking((VALUE)from, (VALUE)to);
321
322 size_t from_slot_size = rb_gc_impl_obj_slot_size((VALUE)from);
323 size_t to_slot_size = rb_gc_impl_obj_slot_size((VALUE)to);
324 if (from_slot_size != to_slot_size) {
325 /* The object was copied to a differently sized slot; transition the
326 * shape so its capacity matches the new slot size. The re-embedding
327 * of contents (e.g. arrays, strings, object fields) happens later
328 * during reference updating. */
329 rb_gc_obj_changed_slot_size((VALUE)to, to_slot_size);
330 }
331}
332
333RB_MMTK_UPCALL size_t
334rb_mmtk_obj_optimal_size(MMTk_ObjectReference obj)
335{
336 return rb_gc_obj_optimal_size((VALUE)obj);
337}
338
339RB_MMTK_UPCALL void
340rb_mmtk_update_object_references(MMTk_ObjectReference mmtk_object)
341{
342 VALUE object = (VALUE)mmtk_object;
343
344 if (!RB_FL_TEST(object, RUBY_FL_WEAK_REFERENCE)) {
345 marking_parent_object = object;
346 rb_gc_update_object_references(rb_gc_get_objspace(), object);
347 marking_parent_object = 0;
348 }
349}
350
351RB_MMTK_UPCALL void
352rb_mmtk_call_gc_mark_children(MMTk_ObjectReference object)
353{
354 marking_parent_object = (VALUE)object;
355 rb_gc_mark_children(rb_gc_get_objspace(), (VALUE)object);
356 marking_parent_object = 0;
357}
358
359RB_MMTK_UPCALL void
360rb_mmtk_handle_weak_references(MMTk_ObjectReference mmtk_object, bool moving)
361{
362 VALUE object = (VALUE)mmtk_object;
363
364 marking_parent_object = object;
365
366 rb_gc_handle_weak_references(rb_gc_get_objspace(), object);
367
368 if (moving) {
369 rb_gc_update_object_references(rb_gc_get_objspace(), object);
370 }
371
372 marking_parent_object = 0;
373}
374
375RB_MMTK_UPCALL void
376rb_mmtk_call_obj_free(MMTk_ObjectReference object)
377{
378 VALUE obj = (VALUE)object;
379 struct objspace *objspace = rb_gc_get_objspace();
380
381 if (RB_UNLIKELY(rb_gc_event_hook_required_p(RUBY_INTERNAL_EVENT_FREEOBJ))) {
382 pthread_mutex_lock(&objspace->event_hook_mutex);
383 rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_FREEOBJ);
384 pthread_mutex_unlock(&objspace->event_hook_mutex);
385 }
386
387 if (RB_UNLIKELY(rb_gc_obj_needs_cleanup_p(obj))) {
388 rb_gc_obj_free(objspace, obj);
389 }
390
391#ifdef MMTK_DEBUG
392 memset((void *)obj, 0, rb_gc_impl_obj_slot_size(obj));
393#endif
394}
395
396RB_MMTK_UPCALL size_t
397rb_mmtk_vm_live_bytes(void)
398{
399 return 0;
400}
401
402static void
403make_final_job(struct objspace *objspace, VALUE obj, VALUE table)
404{
405 MMTK_ASSERT(RB_BUILTIN_TYPE(table) == T_ARRAY);
406
407 struct MMTk_final_job *job = xmalloc(sizeof(struct MMTk_final_job));
408 job->next = objspace->finalizer_jobs;
409 job->kind = MMTK_FINAL_JOB_FINALIZE;
410 job->as.finalize.finalizer_array = table;
411
412 objspace->finalizer_jobs = job;
413}
414
415static int
416rb_mmtk_update_finalizer_table_i(st_data_t key, st_data_t value, st_data_t data, int error)
417{
418 MMTK_ASSERT(mmtk_is_reachable((MMTk_ObjectReference)value));
419 MMTK_ASSERT(RB_BUILTIN_TYPE(value) == T_ARRAY);
420
421 struct objspace *objspace = (struct objspace *)data;
422
423 if (mmtk_is_reachable((MMTk_ObjectReference)key)) {
424 VALUE new_key_location = rb_mmtk_call_object_closure((VALUE)key, false);
425
426 MMTK_ASSERT(RB_FL_TEST(new_key_location, RUBY_FL_FINALIZE));
427
428 if (new_key_location != key) {
429 return ST_REPLACE;
430 }
431 }
432 else {
433 make_final_job(objspace, (VALUE)key, (VALUE)value);
434
435 rb_postponed_job_trigger(objspace->finalizer_postponed_job);
436
437 return ST_DELETE;
438 }
439
440 return ST_CONTINUE;
441}
442
443static int
444rb_mmtk_update_finalizer_table_replace_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
445{
446 *key = rb_mmtk_call_object_closure((VALUE)*key, false);
447
448 return ST_CONTINUE;
449}
450
451RB_MMTK_UPCALL void
452rb_mmtk_update_finalizer_table(void)
453{
454 struct objspace *objspace = rb_gc_get_objspace();
455
456 st_foreach_with_replace(
457 objspace->finalizer_table,
458 rb_mmtk_update_finalizer_table_i,
459 rb_mmtk_update_finalizer_table_replace_i,
460 (st_data_t)objspace
461 );
462}
463
464RB_MMTK_UPCALL int
465rb_mmtk_global_tables_count(void)
466{
467 return RB_GC_VM_WEAK_TABLE_COUNT;
468}
469
470static inline VALUE rb_mmtk_call_object_closure(VALUE obj, bool pin);
471
472static int
473rb_mmtk_update_global_tables_i(VALUE val, void *data)
474{
475 if (!mmtk_is_reachable((MMTk_ObjectReference)val)) {
476 return ST_DELETE;
477 }
478
479 // TODO: check only if in moving GC
480 if (rb_mmtk_call_object_closure(val, false) != val) {
481 return ST_REPLACE;
482 }
483
484 return ST_CONTINUE;
485}
486
487static int
488rb_mmtk_update_global_tables_replace_i(VALUE *ptr, void *data)
489{
490 // TODO: cache the new location so we don't call rb_mmtk_call_object_closure twice
491 *ptr = rb_mmtk_call_object_closure(*ptr, false);
492
493 return ST_CONTINUE;
494}
495
496RB_MMTK_UPCALL void
497rb_mmtk_update_global_tables(int table, bool moving)
498{
499 MMTK_ASSERT(table < RB_GC_VM_WEAK_TABLE_COUNT);
500
501 rb_gc_vm_weak_table_foreach(
502 rb_mmtk_update_global_tables_i,
503 rb_mmtk_update_global_tables_replace_i,
504 NULL,
505 !moving,
506 (enum rb_gc_vm_weak_tables)table
507 );
508}
509
510RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2)
512static void
513rb_mmtk_gc_thread_bug(const char *msg, ...)
514{
515 struct objspace *objspace = rb_gc_get_objspace();
516
517 objspace->crash_context.gc_thread_crashed = true;
518
519 va_list args;
520 va_start(args, msg);
521 vsnprintf(objspace->crash_context.crash_msg, sizeof(objspace->crash_context.crash_msg), msg, args);
522 va_end(args);
523
524 fprintf(stderr, "-- GC thread backtrace "
525 "-------------------------------------------\n");
526 rb_gc_print_backtrace();
527 fprintf(stderr, "\n");
528
529 rb_mmtk_resume_mutators(false);
530
531 sleep(5);
532
533 rb_bug("rb_mmtk_gc_thread_bug");
534}
535
537RB_MMTK_UPCALL void
538rb_mmtk_gc_thread_panic_handler(void)
539{
540 rb_mmtk_gc_thread_bug("MMTk GC thread panicked");
541}
542
544RB_MMTK_UPCALL void
545rb_mmtk_mutator_thread_panic_handler(void)
546{
547 rb_bug("Ruby mutator thread panicked");
548}
549
550// Bootup
551
552// Use max 80% of the available memory by default for MMTk
553#define RB_MMTK_HEAP_LIMIT_PERC 80
554#define RB_MMTK_DEFAULT_HEAP_MIN (1024 * 1024)
555#define RB_MMTK_DEFAULT_HEAP_MAX (rb_mmtk_system_physical_memory() / 100 * RB_MMTK_HEAP_LIMIT_PERC)
556
557enum mmtk_heap_mode {
558 RB_MMTK_DYNAMIC_HEAP,
559 RB_MMTK_FIXED_HEAP
560};
561
562MMTk_Builder *
563rb_mmtk_builder_init(void)
564{
565 MMTk_Builder *builder = mmtk_builder_default();
566 return builder;
567}
568
569
570void *
571rb_gc_impl_objspace_alloc(void)
572{
573 /* One heap, binding and objspace per process (multi_objspace_p=false). The VM
574 * shares the same objspace across all Ractors, so a re-entry returns the same
575 * instance. */
576 static struct objspace *the_objspace = NULL;
577 if (the_objspace == NULL) {
578 MMTk_Builder *builder = rb_mmtk_builder_init();
579 MMTk_RubyBindingOptions binding_options = {
580 .suffix_size = 0,
581 };
582 mmtk_init_binding(builder, &binding_options);
583 the_objspace = calloc(1, sizeof(struct objspace));
584 }
585
586 return the_objspace;
587}
588
589bool
590rb_gc_impl_multi_objspace_p(void)
591{
592 return false;
593}
594
595static void gc_run_finalizers(void *data);
596
597void
598rb_gc_impl_objspace_init(void *objspace_ptr)
599{
600 struct objspace *objspace = objspace_ptr;
601
602 /* The objspace is a singleton (see rb_gc_impl_objspace_alloc). A re-init must
603 * not clobber finalizer_table or ractor_caches. */
604 if (objspace->finalizer_table != NULL) return;
605
606 objspace->measure_gc_time = true;
607
608 objspace->finalizer_table = st_init_numtable();
609 objspace->finalizer_postponed_job = rb_postponed_job_preregister(0, gc_run_finalizers, objspace);
610
611 ccan_list_head_init(&objspace->ractor_caches);
612
613 objspace->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
614 objspace->cond_world_stopped = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
615 objspace->cond_world_started = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
616
617 objspace->event_hook_mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
618
619 objspace->vo_bit_log_region_size = mmtk_get_vo_bit_log_region_size();
620 objspace->vo_bit_base_addr = mmtk_get_vo_bit_base_addr();
621 objspace->max_non_los_default_alloc_bytes = mmtk_max_non_los_default_alloc_bytes();
622 objspace->write_barrier_required = mmtk_plan_has_write_barrier();
623}
624
625void
626rb_gc_impl_objspace_free(void *objspace_ptr)
627{
628 /* The objspace is a process-lifetime singleton. */
629}
630
631void *
632rb_gc_impl_ractor_cache_alloc(void *objspace_ptr, void *ractor)
633{
634 struct objspace *objspace = objspace_ptr;
635 if (objspace->live_ractor_cache_count == 0) {
636 mmtk_initialize_collection(ractor);
637 }
638 objspace->live_ractor_cache_count++;
639
640 struct MMTk_ractor_cache *cache = calloc(1, sizeof(struct MMTk_ractor_cache));
641 ccan_list_add(&objspace->ractor_caches, &cache->list_node);
642
643 cache->mutator = mmtk_bind_mutator(cache);
644 cache->bump_pointer = mmtk_get_bump_pointer_allocator(cache->mutator);
645
646 return cache;
647}
648
649void
650rb_gc_impl_objspace_retire_gc(void *objspace_ptr)
651{
652 /* A single objspace needs no per-Ractor GC at teardown. */
653}
654
655void
656rb_gc_impl_ractor_cache_free(void *objspace_ptr, void *cache_ptr)
657{
658 struct objspace *objspace = objspace_ptr;
659 struct MMTk_ractor_cache *cache = cache_ptr;
660
661 ccan_list_del(&cache->list_node);
662
663 mmtk_flush_obj_free_buffer(cache);
664
665 if (ruby_free_at_exit_p()) {
666 MMTK_ASSERT(objspace->live_ractor_cache_count > 0);
667 }
668 else {
669 MMTK_ASSERT(objspace->live_ractor_cache_count > 1);
670 }
671
672 objspace->live_ractor_cache_count--;
673
674 mmtk_destroy_mutator(cache->mutator);
675
676 free(cache);
677}
678
679static bool
680zjit_mmtk_gc_stress_p(void *objspace_ptr)
681{
682 return ((struct objspace *)objspace_ptr)->gc_stress;
683}
684
685static bool
686zjit_mmtk_newobj_tracing_p(void)
687{
688 return rb_gc_event_hook_required_p(RUBY_INTERNAL_EVENT_NEWOBJ);
689}
690
691void rb_gc_impl_set_params(void *objspace_ptr) { }
692
693static VALUE gc_verify_internal_consistency(VALUE self) { return Qnil; }
694
695static inline size_t
696rb_mmtk_align_obj_size(size_t object_size)
697{
698 return (object_size + MMTk_MIN_OBJ_ALIGN - 1) & ~((size_t)MMTk_MIN_OBJ_ALIGN - 1);
699}
700
701bool
702rb_gc_impl_zjit_new_obj_fastpath(void *objspace_ptr, size_t alloc_size, VALUE flags, VALUE klass,
703 struct rb_gc_zjit_fastpath *fastpath)
704{
705#if USE_ZJIT
706 struct objspace *objspace = objspace_ptr;
707
708 size_t total_size = rb_mmtk_align_obj_size(alloc_size + sizeof(VALUE));
709 size_t object_size = total_size - sizeof(VALUE);
710 size_t value_size_shift = sizeof(VALUE) == 8 ? 3 : 2;
711
712 if (total_size > objspace->max_non_los_default_alloc_bytes) return false;
713
714 struct rb_gc_zjit_mmtk_new_obj_fastpath mmtk_fastpath = {
715 objspace,
716 offsetof(struct objspace, total_allocated_objects),
717 offsetof(struct MMTk_ractor_cache, mutator),
718 offsetof(struct MMTk_ractor_cache, bump_pointer),
719 offsetof(struct MMTk_ractor_cache, obj_free_parallel_buf),
720 offsetof(struct MMTk_ractor_cache, obj_free_parallel_count),
721 offsetof(MMTk_BumpPointer, cursor),
722 offsetof(MMTk_BumpPointer, limit),
723 MMTk_MIN_OBJ_ALIGN,
724 object_size,
725 total_size,
726 MMTK_ALLOCATION_SEMANTICS_DEFAULT,
727 (uintptr_t)zjit_mmtk_gc_stress_p,
728 (uintptr_t)zjit_mmtk_newobj_tracing_p,
729 (uintptr_t)mmtk_post_alloc,
730 OBJ_FREE_BUF_CAPACITY - 1,
731 value_size_shift,
732 flags,
733 klass
734 };
735
736 memset(fastpath, 0, sizeof(*fastpath));
737 fastpath->kind = RB_GC_ZJIT_FASTPATH_MMTK;
738 memcpy(fastpath->data.words, &mmtk_fastpath, sizeof(mmtk_fastpath));
739
740 return true;
741#else
742 return false;
743#endif
744}
745
746int32_t
747rb_gc_impl_zjit_writebarrier_required_offset(void)
748{
749 /* MMTk's write barrier is not a no-op for young, unshareable receivers,
750 * so ZJIT must always call rb_gc_writebarrier(). */
751 return -1;
752}
753
754/*
755 * call-seq:
756 * GC::MMTk.harness_begin
757 *
758 * A hook to be called before a benchmark begins.
759 *
760 * MMTk will do necessary preparations (such as triggering a full-heap GC)
761 * and start collecting statistic data, such as the number of GC triggered,
762 * time spent in GC, time spent in mutator, etc.
763 */
764static VALUE
765rb_mmtk_harness_begin(VALUE _)
766{
767 mmtk_harness_begin(rb_gc_get_ractor_newobj_cache());
768
769 return Qnil;
770}
771
772/*
773 * call-seq:
774 * GC::MMTk.harness_end
775 *
776 * A hook to be called after a benchmark ends.
777 *
778 * When this method is called, MMTk will stop collecting statistic data and
779 * print out the data already collected.
780 */
781VALUE
782rb_mmtk_harness_end(VALUE _)
783{
784 mmtk_harness_end();
785
786 return Qnil;
787}
788
789void
790rb_gc_impl_init(void)
791{
792 VALUE gc_constants = rb_hash_new();
793 rb_hash_aset(gc_constants, ID2SYM(rb_intern("RVALUE_SIZE")), SIZET2NUM(sizeof(struct RBasic) + sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX])));
794 rb_hash_aset(gc_constants, ID2SYM(rb_intern("RBASIC_SIZE")), SIZET2NUM(sizeof(struct RBasic)));
795 rb_hash_aset(gc_constants, ID2SYM(rb_intern("RVALUE_OVERHEAD")), INT2NUM(0));
796 rb_hash_aset(gc_constants, ID2SYM(rb_intern("RVALUE_OLD_AGE")),
797 INT2FIX(strcmp((const char *)mmtk_plan(), "StickyImmix") == 0 ? 1 : 0));
798 OBJ_FREEZE(gc_constants);
799 rb_define_const(rb_mGC, "INTERNAL_CONSTANTS", gc_constants);
800
801 // no-ops for compatibility
802 rb_define_singleton_method(rb_mGC, "verify_internal_consistency", gc_verify_internal_consistency, 0);
803
807 rb_define_singleton_method(rb_mGC, "latest_compact_info", rb_f_notimplement, 0);
808 rb_define_singleton_method(rb_mGC, "verify_compaction_references", rb_f_notimplement, -1);
809
810 VALUE rb_mMMTk = rb_define_module_under(rb_mGC, "MMTk");
811 rb_define_singleton_method(rb_mMMTk, "harness_begin", rb_mmtk_harness_begin, 0);
812 rb_define_singleton_method(rb_mMMTk, "harness_end", rb_mmtk_harness_end, 0);
813}
814
815int
816rb_mmtk_obj_free_iter_wrapper(VALUE obj, void *data)
817{
818 struct objspace *objspace = data;
819
820 if (!RB_TYPE_P(obj, T_NONE)) {
821 rb_gc_obj_free_vm_weak_references(obj);
822 rb_gc_obj_free(objspace, obj);
823 }
824
825 return 0;
826}
827
828// Shutdown
829static void each_object(struct objspace *objspace, int (*func)(VALUE, void *), void *data);
830
831void
832rb_gc_impl_shutdown_free_objects(void *objspace_ptr)
833{
834 mmtk_set_gc_enabled(false);
835 each_object(objspace_ptr, rb_mmtk_obj_free_iter_wrapper, objspace_ptr);
836 mmtk_set_gc_enabled(true);
837}
838
839// GC
840void
841rb_gc_impl_start(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact, bool global)
842{
843 mmtk_handle_user_collection_request(rb_gc_get_ractor_newobj_cache(), true, full_mark);
844}
845
846bool
847rb_gc_impl_during_gc_p(void *objspace_ptr)
848{
849 struct objspace *objspace = objspace_ptr;
850 return objspace->world_stopped;
851}
852
853static void
854rb_gc_impl_prepare_heap_i(MMTk_ObjectReference obj, void *d)
855{
856 rb_gc_prepare_heap_process_object((VALUE)obj);
857}
858
859void
860rb_gc_impl_prepare_heap(void *objspace_ptr)
861{
862 mmtk_enumerate_objects(rb_gc_impl_prepare_heap_i, NULL);
863}
864
865void
866rb_gc_impl_gc_enable(void *objspace_ptr)
867{
868 mmtk_set_gc_enabled(true);
869}
870
871void
872rb_gc_impl_gc_disable(void *objspace_ptr, bool finish_current_gc)
873{
874 mmtk_set_gc_enabled(false);
875}
876
877bool
878rb_gc_impl_user_gc_disabled_set(void *objspace_ptr, bool disable)
879{
880 return mmtk_set_user_gc_disabled(disable);
881}
882
883bool
884rb_gc_impl_user_gc_disabled_p(void *objspace_ptr)
885{
886 return mmtk_user_gc_disabled_p();
887}
888
889bool
890rb_gc_impl_gc_enabled_p(void *objspace_ptr)
891{
892 return mmtk_gc_enabled_p();
893}
894
895void
896rb_gc_impl_stress_set(void *objspace_ptr, VALUE flag)
897{
898 struct objspace *objspace = objspace_ptr;
899
900 objspace->gc_stress = RTEST(flag);
901}
902
903VALUE
904rb_gc_impl_stress_get(void *objspace_ptr)
905{
906 struct objspace *objspace = objspace_ptr;
907
908 return objspace->gc_stress ? Qtrue : Qfalse;
909}
910
911VALUE
912rb_gc_impl_config_get(void *objspace_ptr)
913{
914 VALUE hash = rb_hash_new();
915
916 rb_hash_aset(hash, ID2SYM(rb_intern_const("mmtk_worker_count")), RB_ULONG2NUM(mmtk_worker_count()));
917 rb_hash_aset(hash, ID2SYM(rb_intern_const("mmtk_plan")), rb_str_new_cstr((const char *)mmtk_plan()));
918 rb_hash_aset(hash, ID2SYM(rb_intern_const("mmtk_heap_mode")), rb_str_new_cstr((const char *)mmtk_heap_mode()));
919 size_t heap_min = mmtk_heap_min();
920 if (heap_min > 0) rb_hash_aset(hash, ID2SYM(rb_intern_const("mmtk_heap_min")), RB_ULONG2NUM(heap_min));
921 rb_hash_aset(hash, ID2SYM(rb_intern_const("mmtk_heap_max")), RB_ULONG2NUM(mmtk_heap_max()));
922
923 return hash;
924}
925
926void
927rb_gc_impl_config_set(void *objspace_ptr, VALUE hash)
928{
929 // TODO
930}
931
932struct rb_gc_vm_context *
933rb_gc_impl_get_vm_context(void *objspace_ptr)
934{
935 struct objspace *objspace = objspace_ptr;
936
937 return &objspace->vm_context;
938}
939
940// Object allocation
941
942static VALUE
943rb_mmtk_alloc_fast_path(struct objspace *objspace, struct MMTk_ractor_cache *ractor_cache, size_t size, size_t align)
944{
945 MMTk_BumpPointer *bump_pointer = ractor_cache->bump_pointer;
946 if (bump_pointer == NULL) return 0;
947
948 uintptr_t cursor = bump_pointer->cursor;
949
950 // Ensure cursor is aligned
951 size_t mask = align - 1;
952 cursor = (cursor + mask) & ~mask;
953
954 cursor += size;
955
956 if (cursor > bump_pointer->limit) {
957 return 0;
958 }
959 else {
960 VALUE obj = cursor - size;
961 bump_pointer->cursor = cursor;
962 return obj;
963 }
964}
965
966static bool
967obj_can_parallel_free_p(VALUE obj)
968{
969 switch (RB_BUILTIN_TYPE(obj)) {
970 case T_ARRAY:
971 case T_BIGNUM:
972 case T_COMPLEX:
973 case T_FLOAT:
974 case T_HASH:
975 case T_OBJECT:
976 case T_RATIONAL:
977 case T_REGEXP:
978 case T_STRING:
979 case T_STRUCT:
980 case T_SYMBOL:
981 return true;
982 default:
983 return false;
984 }
985}
986
987static bool
988obj_need_obj_free_p(VALUE obj)
989{
990 switch (RB_BUILTIN_TYPE(obj)) {
991 case T_FLOAT:
992 case T_RATIONAL:
993 case T_COMPLEX:
994 case T_OBJECT:
995 return false;
996 default:
997 return true;
998 }
999}
1000
1001static void
1002mmtk_flush_obj_free_buffer(struct MMTk_ractor_cache *cache)
1003{
1004 if (cache->obj_free_parallel_count > 0) {
1005 mmtk_add_obj_free_candidates(cache->obj_free_parallel_buf,
1006 cache->obj_free_parallel_count, true);
1007 cache->obj_free_parallel_count = 0;
1008 }
1009 if (cache->obj_free_non_parallel_count > 0) {
1010 mmtk_add_obj_free_candidates(cache->obj_free_non_parallel_buf,
1011 cache->obj_free_non_parallel_count, false);
1012 cache->obj_free_non_parallel_count = 0;
1013 }
1014}
1015
1016static inline void
1017mmtk_buffer_obj_free_candidate(struct MMTk_ractor_cache *cache, VALUE obj)
1018{
1019 if (obj_can_parallel_free_p(obj)) {
1020 cache->obj_free_parallel_buf[cache->obj_free_parallel_count++] = (MMTk_ObjectReference)obj;
1021 if (cache->obj_free_parallel_count >= OBJ_FREE_BUF_CAPACITY) {
1022 mmtk_add_obj_free_candidates(cache->obj_free_parallel_buf,
1023 cache->obj_free_parallel_count, true);
1024 cache->obj_free_parallel_count = 0;
1025 }
1026 }
1027 else {
1028 cache->obj_free_non_parallel_buf[cache->obj_free_non_parallel_count++] = (MMTk_ObjectReference)obj;
1029 if (cache->obj_free_non_parallel_count >= OBJ_FREE_BUF_CAPACITY) {
1030 mmtk_add_obj_free_candidates(cache->obj_free_non_parallel_buf,
1031 cache->obj_free_non_parallel_count, false);
1032 cache->obj_free_non_parallel_count = 0;
1033 }
1034 }
1035}
1036
1037static void
1038mmtk_post_alloc_fast_immix(struct objspace *objspace, struct MMTk_ractor_cache *ractor_cache, uintptr_t obj)
1039{
1040 uintptr_t region_offset = obj >> objspace->vo_bit_log_region_size;
1041 uintptr_t byte_offset = region_offset / 8;
1042 uintptr_t bit_offset = region_offset % 8;
1043 uintptr_t meta_byte_address = objspace->vo_bit_base_addr + byte_offset;
1044 uint8_t byte = 1 << bit_offset;
1045 uint8_t *meta_byte_ptr = (uint8_t*)meta_byte_address;
1046 *meta_byte_ptr |= byte;
1047}
1048
1049VALUE
1050rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags, bool wb_protected, size_t alloc_size, size_t *actual_alloc_size)
1051{
1052 struct objspace *objspace = objspace_ptr;
1053 struct MMTk_ractor_cache *ractor_cache = cache_ptr;
1054
1055 if (alloc_size == 0) {
1056 rb_bug("rb_gc_impl_new_obj: allocation size out of range (size=%"PRIuSIZE")", alloc_size);
1057 }
1058
1059 // Layout: [hidden size header (sizeof(VALUE))][payload (alloc_size)]
1060 size_t total_size = rb_mmtk_align_obj_size(alloc_size + sizeof(VALUE));
1061 size_t object_size = total_size - sizeof(VALUE);
1062 MMTk_AllocationSemantics semantics = total_size > objspace->max_non_los_default_alloc_bytes
1063 ? MMTK_ALLOCATION_SEMANTICS_LOS
1064 : MMTK_ALLOCATION_SEMANTICS_DEFAULT;
1065 *actual_alloc_size = object_size;
1066
1067 if (objspace->gc_stress) {
1068 mmtk_handle_user_collection_request(ractor_cache, false, false);
1069 }
1070
1071 VALUE *alloc_obj = semantics == MMTK_ALLOCATION_SEMANTICS_DEFAULT
1072 ? (VALUE *)rb_mmtk_alloc_fast_path(objspace, ractor_cache, total_size, MMTk_MIN_OBJ_ALIGN)
1073 : NULL;
1074 if (!alloc_obj) {
1075 alloc_obj = mmtk_alloc(ractor_cache->mutator, total_size, MMTk_MIN_OBJ_ALIGN, 0, semantics);
1076
1077 // On heap exhaustion raise NoMemoryError.
1078 if (RB_UNLIKELY(alloc_obj == NULL)) {
1079 rb_memerror();
1080 }
1081 }
1082
1083 alloc_obj++;
1084 alloc_obj[-1] = object_size;
1085 alloc_obj[0] = flags;
1086 alloc_obj[1] = klass;
1087
1088 if (RB_UNLIKELY(!wb_protected)) {
1089 mmtk_register_wb_unprotected_object((MMTk_ObjectReference)alloc_obj);
1090 }
1091
1092 if (semantics == MMTK_ALLOCATION_SEMANTICS_LOS || ractor_cache->bump_pointer == NULL) {
1093 mmtk_post_alloc(ractor_cache->mutator, (void*)alloc_obj, total_size, semantics);
1094 }
1095 else {
1096 // We can use the post alloc fast path if we're using Immix bump pointer allocator
1097 mmtk_post_alloc_fast_immix(objspace, ractor_cache, (uintptr_t)alloc_obj);
1098 }
1099
1100 if (obj_need_obj_free_p((VALUE)alloc_obj)) {
1101 mmtk_buffer_obj_free_candidate(ractor_cache, (VALUE)alloc_obj);
1102 }
1103
1104 objspace->total_allocated_objects++;
1105
1106 return (VALUE)alloc_obj;
1107}
1108
1109size_t
1110rb_gc_impl_obj_slot_size(VALUE obj)
1111{
1112 return ((VALUE *)obj)[-1];
1113}
1114
1115size_t
1116rb_gc_impl_size_slot_size(void *objspace_ptr, size_t size)
1117{
1118 if (size == 0) {
1119 rb_bug("rb_gc_impl_size_slot_size: size too large (size=%"PRIuSIZE")", size);
1120 }
1121
1122 return rb_mmtk_align_obj_size(size + sizeof(VALUE)) - sizeof(VALUE);
1123}
1124
1125bool
1126rb_gc_impl_size_allocatable_p(size_t size)
1127{
1128 return true;
1129}
1130
1131size_t
1132rb_gc_impl_max_allocation_size(void)
1133{
1134 return SIZE_MAX;
1135}
1136
1137// Malloc
1138void *
1139rb_gc_impl_malloc(void *objspace_ptr, size_t size, bool gc_allowed)
1140{
1141 // TODO: don't use system malloc
1142 return malloc(size);
1143}
1144
1145void *
1146rb_gc_impl_calloc(void *objspace_ptr, size_t size, bool gc_allowed)
1147{
1148 // TODO: don't use system calloc
1149 return calloc(1, size);
1150}
1151
1152void *
1153rb_gc_impl_realloc(void *objspace_ptr, void *ptr, size_t new_size, size_t old_size, bool gc_allowed)
1154{
1155 // TODO: don't use system realloc
1156 return realloc(ptr, new_size);
1157}
1158
1159void
1160rb_gc_impl_free(void *objspace_ptr, void *ptr, size_t old_size)
1161{
1162 // TODO: don't use system free
1163 free(ptr);
1164}
1165
1166void rb_gc_impl_adjust_memory_usage(void *objspace_ptr, ssize_t diff) { }
1167
1168// Marking
1169static inline VALUE
1170rb_mmtk_call_object_closure(VALUE obj, bool pin)
1171{
1172 if (RB_UNLIKELY(RB_BUILTIN_TYPE(obj) == T_NONE)) {
1173 enum { info_size = 256 };
1174 char obj_info_buf[info_size];
1175 rb_raw_obj_info(obj_info_buf, info_size, obj);
1176
1177 char parent_obj_info_buf[info_size];
1178 rb_raw_obj_info(parent_obj_info_buf, info_size, marking_parent_object);
1179
1180 rb_mmtk_gc_thread_bug("try to mark T_NONE object (obj: %s, parent: %s)", obj_info_buf, parent_obj_info_buf);
1181 }
1182
1183 return (VALUE)rb_mmtk_gc_thread_tls->object_closure.c_function(
1184 rb_mmtk_gc_thread_tls->object_closure.rust_closure,
1185 rb_mmtk_gc_thread_tls->gc_context,
1186 (MMTk_ObjectReference)obj,
1187 pin
1188 );
1189}
1190
1191void
1192rb_gc_impl_mark(void *objspace_ptr, VALUE obj)
1193{
1194 if (RB_SPECIAL_CONST_P(obj)) return;
1195
1196 rb_mmtk_call_object_closure(obj, false);
1197}
1198
1199void
1200rb_gc_impl_mark_and_move(void *objspace_ptr, VALUE *ptr)
1201{
1202 if (RB_SPECIAL_CONST_P(*ptr)) return;
1203
1204 VALUE new_obj = rb_mmtk_call_object_closure(*ptr, false);
1205 if (new_obj != *ptr) {
1206 *ptr = new_obj;
1207 }
1208}
1209
1210void
1211rb_gc_impl_mark_and_pin(void *objspace_ptr, VALUE obj)
1212{
1213 if (RB_SPECIAL_CONST_P(obj)) return;
1214
1215 rb_mmtk_call_object_closure(obj, true);
1216}
1217
1218void
1219rb_gc_impl_mark_maybe(void *objspace_ptr, VALUE obj)
1220{
1221 if (rb_gc_impl_live_object_p(objspace_ptr, (const void *)obj)) {
1222 rb_gc_impl_mark_and_pin(objspace_ptr, obj);
1223 }
1224}
1225
1226void
1227rb_gc_impl_declare_weak_references(void *objspace_ptr, VALUE obj)
1228{
1230 mmtk_declare_weak_references((MMTk_ObjectReference)obj);
1231}
1232
1233bool
1234rb_gc_impl_handle_weak_references_alive_p(void *objspace_ptr, VALUE obj)
1235{
1236 return mmtk_weak_references_alive_p((MMTk_ObjectReference)obj);
1237}
1238
1239// Compaction
1240void
1241rb_gc_impl_register_pinning_obj(void *objspace_ptr, VALUE obj)
1242{
1243 mmtk_register_pinning_obj((MMTk_ObjectReference)obj);
1244}
1245
1246bool
1247rb_gc_impl_object_moved_p(void *objspace_ptr, VALUE obj)
1248{
1249 return rb_mmtk_call_object_closure(obj, false) != obj;
1250}
1251
1252bool
1253rb_gc_impl_pinned_p(void *objspace_ptr, VALUE obj)
1254{
1255 return mmtk_is_pinned((MMTk_ObjectReference)obj);
1256}
1257
1258VALUE
1259rb_gc_impl_location(void *objspace_ptr, VALUE obj)
1260{
1261 return rb_mmtk_call_object_closure(obj, false);
1262}
1263
1264// Write barriers
1265void
1266rb_gc_impl_writebarrier(void *objspace_ptr, VALUE a, VALUE b)
1267{
1268 struct objspace *objspace = objspace_ptr;
1269
1270 if (SPECIAL_CONST_P(b)) return;
1271
1272#ifdef MMTK_DEBUG
1273 if (!rb_gc_impl_live_object_p(objspace_ptr, (void *)a)) {
1274 char buff[256];
1275 rb_bug("a: %s is not an object", rb_raw_obj_info(buff, 256, a));
1276 }
1277
1278 if (!rb_gc_impl_live_object_p(objspace_ptr, (void *)b)) {
1279 char buff[256];
1280 rb_bug("b: %s is not an object", rb_raw_obj_info(buff, 256, b));
1281 }
1282#endif
1283
1284 MMTK_ASSERT(BUILTIN_TYPE(a) != T_NONE);
1285 MMTK_ASSERT(BUILTIN_TYPE(b) != T_NONE);
1286
1287 if (!objspace->write_barrier_required) return;
1288
1289 struct MMTk_ractor_cache *cache = rb_gc_get_ractor_newobj_cache();
1290 mmtk_object_reference_write_post(cache->mutator, (MMTk_ObjectReference)a);
1291}
1292
1293void
1294rb_gc_impl_writebarrier_unprotect(void *objspace_ptr, VALUE obj)
1295{
1296 mmtk_register_wb_unprotected_object((MMTk_ObjectReference)obj);
1297}
1298
1299void
1300rb_gc_impl_obj_became_shareable(void *objspace_ptr, VALUE obj)
1301{
1302 /* MMTk has no per-page shareable bits. */
1303}
1304
1305void
1306rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj)
1307{
1308 struct objspace *objspace = objspace_ptr;
1309
1310 if (!objspace->write_barrier_required) return;
1311
1312 if (!ruby_native_thread_p()) {
1313 /* Called from an MMTk GC worker thread (via gc_ref_update_object's
1314 * re-embedding path during reference updating). There is no mutator
1315 * whose modbuf we could log the object into. This is only reachable
1316 * during a moving GC, and moving GCs are always full-heap (major)
1317 * GCs, so the object and its children are all marked by the current
1318 * GC and no remembering is needed. */
1319 return;
1320 }
1321
1322 struct MMTk_ractor_cache *cache = rb_gc_get_ractor_newobj_cache();
1323
1324 mmtk_object_reference_write_post(cache->mutator, (MMTk_ObjectReference)obj);
1325}
1326
1327// Heap walking
1328static void
1329each_objects_i(MMTk_ObjectReference obj, void *d)
1330{
1331 rb_darray(VALUE) *objs = d;
1332
1333 rb_darray_append(objs, (VALUE)obj);
1334}
1335
1336static void
1337each_object(struct objspace *objspace, int (*func)(VALUE, void *), void *data)
1338{
1339 rb_darray(VALUE) objs;
1340 rb_darray_make(&objs, 0);
1341
1342 mmtk_enumerate_objects(each_objects_i, &objs);
1343
1344 VALUE *obj_ptr;
1345 rb_darray_foreach(objs, i, obj_ptr) {
1346 if (!mmtk_is_mmtk_object((MMTk_ObjectReference)*obj_ptr)) continue;
1347
1348 if (func(*obj_ptr, data) != 0) {
1349 break;
1350 }
1351 }
1352
1353 rb_darray_free(objs);
1354}
1355
1357 int (*func)(void *, void *, size_t, void *);
1358 void *data;
1359};
1360
1361static int
1362rb_gc_impl_each_objects_i(VALUE obj, void *d)
1363{
1364 struct rb_gc_impl_each_objects_data *data = d;
1365
1366 size_t slot_size = rb_gc_impl_obj_slot_size(obj);
1367
1368 return data->func((void *)obj, (void *)(obj + slot_size), slot_size, data->data);
1369}
1370
1371void
1372rb_gc_impl_each_objects(void *objspace_ptr, int (*func)(void *, void *, size_t, void *), void *data)
1373{
1374 struct rb_gc_impl_each_objects_data each_objects_data = {
1375 .func = func,
1376 .data = data
1377 };
1378
1379 each_object(objspace_ptr, rb_gc_impl_each_objects_i, &each_objects_data);
1380}
1381
1383 void (*func)(VALUE, void *);
1384 void *data;
1385};
1386
1387static int
1388rb_gc_impl_each_object_i(VALUE obj, void *d)
1389{
1390 struct rb_gc_impl_each_object_data *data = d;
1391
1392 data->func(obj, data->data);
1393
1394 return 0;
1395}
1396
1397void
1398rb_gc_impl_each_object(void *objspace_ptr, void (*func)(VALUE, void *), void *data)
1399{
1400 struct rb_gc_impl_each_object_data each_object_data = {
1401 .func = func,
1402 .data = data
1403 };
1404
1405 each_object(objspace_ptr, rb_gc_impl_each_object_i, &each_object_data);
1406}
1407
1408// Finalizers
1409static VALUE
1410gc_run_finalizers_get_final(rb_len_t i, void *data)
1411{
1412 VALUE table = (VALUE)data;
1413
1414 return RARRAY_AREF(table, i + 1);
1415}
1416
1417static void
1418gc_run_finalizers(void *data)
1419{
1420 struct objspace *objspace = data;
1421
1422 rb_gc_set_pending_interrupt();
1423
1424 while (objspace->finalizer_jobs != NULL) {
1425 struct MMTk_final_job *job = objspace->finalizer_jobs;
1426 objspace->finalizer_jobs = job->next;
1427
1428 switch (job->kind) {
1429 case MMTK_FINAL_JOB_DFREE:
1430 job->as.dfree.func(job->as.dfree.data);
1431 break;
1432 case MMTK_FINAL_JOB_FINALIZE: {
1433 VALUE finalizer_array = job->as.finalize.finalizer_array;
1434
1435 rb_gc_run_obj_finalizer(
1436 RARRAY_AREF(finalizer_array, 0),
1437 RARRAY_LEN(finalizer_array) - 1,
1438 gc_run_finalizers_get_final,
1439 (void *)finalizer_array
1440 );
1441
1442 RB_GC_GUARD(finalizer_array);
1443 break;
1444 }
1445 }
1446
1447 xfree(job);
1448 }
1449
1450 rb_gc_unset_pending_interrupt();
1451}
1452
1453void
1454rb_gc_impl_make_zombie(void *objspace_ptr, VALUE obj, void (*dfree)(void *), void *data)
1455{
1456 if (dfree == NULL) return;
1457
1458 struct objspace *objspace = objspace_ptr;
1459
1460 struct MMTk_final_job *job = xmalloc(sizeof(struct MMTk_final_job));
1461 job->kind = MMTK_FINAL_JOB_DFREE;
1462 job->as.dfree.func = dfree;
1463 job->as.dfree.data = data;
1464
1465 struct MMTk_final_job *prev;
1466 do {
1467 job->next = objspace->finalizer_jobs;
1468 prev = RUBY_ATOMIC_PTR_CAS(objspace->finalizer_jobs, job->next, job);
1469 } while (prev != job->next);
1470
1471 if (!ruby_free_at_exit_p()) {
1472 rb_postponed_job_trigger(objspace->finalizer_postponed_job);
1473 }
1474}
1475
1476VALUE
1477rb_gc_impl_define_finalizer(void *objspace_ptr, VALUE obj, VALUE block)
1478{
1479 struct objspace *objspace = objspace_ptr;
1480 VALUE table;
1481 st_data_t data;
1482
1483 RBASIC(obj)->flags |= FL_FINALIZE;
1484
1485 int lev = RB_GC_VM_LOCK();
1486
1487 if (st_lookup(objspace->finalizer_table, obj, &data)) {
1488 table = (VALUE)data;
1489
1490 /* avoid duplicate block, table is usually small */
1491 {
1492 rb_len_t len = RARRAY_LEN(table);
1493 rb_len_t i;
1494
1495 for (i = 0; i < len; i++) {
1496 VALUE recv = RARRAY_AREF(table, i);
1497 if (rb_equal(recv, block)) {
1498 RB_GC_VM_UNLOCK(lev);
1499 return recv;
1500 }
1501 }
1502 }
1503
1504 rb_ary_push(table, block);
1505 }
1506 else {
1507 table = rb_ary_new3(2, rb_obj_id(obj), block);
1508 rb_obj_hide(table);
1509 st_add_direct(objspace->finalizer_table, obj, table);
1510 }
1511
1512 RB_GC_VM_UNLOCK(lev);
1513
1514 return block;
1515}
1516
1517void
1518rb_gc_impl_undefine_finalizer(void *objspace_ptr, VALUE obj)
1519{
1520 struct objspace *objspace = objspace_ptr;
1521
1522 st_data_t data = obj;
1523
1524 int lev = RB_GC_VM_LOCK();
1525 st_delete(objspace->finalizer_table, &data, 0);
1526 RB_GC_VM_UNLOCK(lev);
1527
1528 FL_UNSET(obj, FL_FINALIZE);
1529}
1530
1531void
1532rb_gc_impl_copy_finalizer(void *objspace_ptr, VALUE dest, VALUE obj)
1533{
1534 struct objspace *objspace = objspace_ptr;
1535 VALUE table;
1536 st_data_t data;
1537
1538 if (!FL_TEST(obj, FL_FINALIZE)) return;
1539
1540 int lev = RB_GC_VM_LOCK();
1541 if (RB_LIKELY(st_lookup(objspace->finalizer_table, obj, &data))) {
1542 table = rb_ary_dup((VALUE)data);
1543 RARRAY_ASET(table, 0, rb_obj_id(dest));
1544 st_insert(objspace->finalizer_table, dest, table);
1545 FL_SET(dest, FL_FINALIZE);
1546 }
1547 else {
1548 rb_bug("rb_gc_copy_finalizer: FL_FINALIZE set but not found in finalizer_table: %s", rb_obj_info(obj));
1549 }
1550 RB_GC_VM_UNLOCK(lev);
1551}
1552
1553static int
1554move_finalizer_from_table_i(st_data_t key, st_data_t val, st_data_t arg)
1555{
1556 struct objspace *objspace = (struct objspace *)arg;
1557
1558 make_final_job(objspace, (VALUE)key, (VALUE)val);
1559
1560 return ST_DELETE;
1561}
1562
1563void
1564rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr)
1565{
1566 struct objspace *objspace = objspace_ptr;
1567
1568 while (objspace->finalizer_table->num_entries) {
1569 st_foreach(objspace->finalizer_table, move_finalizer_from_table_i, (st_data_t)objspace);
1570
1571 gc_run_finalizers(objspace);
1572 }
1573
1574 unsigned int lev = RB_GC_VM_LOCK();
1575 {
1576 struct MMTk_ractor_cache *rc;
1577 ccan_list_for_each(&objspace->ractor_caches, rc, list_node) {
1578 mmtk_flush_obj_free_buffer(rc);
1579 }
1580
1581 struct MMTk_RawVecOfObjRef registered_candidates = mmtk_get_all_obj_free_candidates();
1582 for (size_t i = 0; i < registered_candidates.len; i++) {
1583 VALUE obj = (VALUE)registered_candidates.ptr[i];
1584
1585 if (rb_gc_shutdown_call_finalizer_p(obj)) {
1586 rb_gc_obj_free(objspace_ptr, obj);
1587 RBASIC(obj)->flags = 0;
1588 }
1589 }
1590 mmtk_free_raw_vec_of_obj_ref(registered_candidates);
1591 }
1592 RB_GC_VM_UNLOCK(lev);
1593
1594 gc_run_finalizers(objspace);
1595}
1596
1597// Forking
1598
1599void
1600rb_gc_impl_before_fork(void *objspace_ptr)
1601{
1602 struct objspace *objspace = objspace_ptr;
1603
1604 retry:
1605 objspace->fork_hook_vm_lock_lev = RB_GC_VM_LOCK();
1606 rb_gc_vm_barrier();
1607
1608 /* At this point, we know that all the Ractors are paused because of the
1609 * rb_gc_vm_barrier above. Since rb_mmtk_block_for_gc is a barrier point,
1610 * one or more Ractors could be paused there. However, mmtk_before_fork is
1611 * not compatible with that because it assumes that the MMTk workers are idle,
1612 * but the workers are not idle because they are busy working on a GC.
1613 *
1614 * This essentially implements a trylock. It will optimistically lock but will
1615 * release the lock if it detects that any other Ractors are waiting in
1616 * rb_mmtk_block_for_gc.
1617 */
1618 rb_atomic_t mutator_blocking_count = RUBY_ATOMIC_LOAD(objspace->mutator_blocking_count);
1619 if (mutator_blocking_count != 0) {
1620 RB_GC_VM_UNLOCK(objspace->fork_hook_vm_lock_lev);
1621 goto retry;
1622 }
1623
1624 mmtk_before_fork();
1625}
1626
1627void
1628rb_gc_impl_after_fork(void *objspace_ptr, rb_pid_t pid)
1629{
1630 struct objspace *objspace = objspace_ptr;
1631
1632 mmtk_after_fork(rb_gc_get_ractor_newobj_cache());
1633
1634 RB_GC_VM_UNLOCK(objspace->fork_hook_vm_lock_lev);
1635}
1636
1637// Statistics
1638
1639void
1640rb_gc_impl_set_measure_total_time(void *objspace_ptr, VALUE flag)
1641{
1642 struct objspace *objspace = objspace_ptr;
1643
1644 objspace->measure_gc_time = RTEST(flag);
1645}
1646
1647bool
1648rb_gc_impl_get_measure_total_time(void *objspace_ptr)
1649{
1650 struct objspace *objspace = objspace_ptr;
1651
1652 return objspace->measure_gc_time;
1653}
1654
1655unsigned long long
1656rb_gc_impl_get_total_time(void *objspace_ptr)
1657{
1658 struct objspace *objspace = objspace_ptr;
1659
1660 return objspace->total_gc_time;
1661}
1662
1663size_t
1664rb_gc_impl_gc_count(void *objspace_ptr)
1665{
1666 struct objspace *objspace = objspace_ptr;
1667
1668 return objspace->gc_count;
1669}
1670
1671VALUE
1672rb_gc_impl_latest_gc_info(void *objspace_ptr, VALUE hash_or_key)
1673{
1674 VALUE hash = Qnil, key = Qnil;
1675
1676 if (SYMBOL_P(hash_or_key)) {
1677 key = hash_or_key;
1678 }
1679 else if (RB_TYPE_P(hash_or_key, T_HASH)) {
1680 hash = hash_or_key;
1681 }
1682 else {
1683 rb_bug("gc_info_decode: non-hash or symbol given");
1684 }
1685
1686#define SET(name, attr) \
1687 if (key == ID2SYM(rb_intern_const(#name))) \
1688 return (attr); \
1689 else if (hash != Qnil) \
1690 rb_hash_aset(hash, ID2SYM(rb_intern_const(#name)), (attr));
1691
1692 /* Hack to get StackProf working because it calls rb_gc_latest_gc_info with
1693 * the :state key and expects a result. This always returns the :none state. */
1694 SET(state, ID2SYM(rb_intern_const("none")));
1695#undef SET
1696
1697 if (!NIL_P(key)) {
1698 // Matched key should return above
1699 return Qundef;
1700 }
1701
1702 return hash;
1703}
1704
1705enum gc_stat_sym {
1706 gc_stat_sym_count,
1707 gc_stat_sym_moving_gc_count,
1708 gc_stat_sym_time,
1709 gc_stat_sym_total_allocated_objects,
1710 gc_stat_sym_total_bytes,
1711 gc_stat_sym_used_bytes,
1712 gc_stat_sym_free_bytes,
1713 gc_stat_sym_starting_heap_address,
1714 gc_stat_sym_last_heap_address,
1715 gc_stat_sym_weak_references_count,
1716 gc_stat_sym_last
1717};
1718
1719static VALUE gc_stat_symbols[gc_stat_sym_last];
1720
1721static void
1722setup_gc_stat_symbols(void)
1723{
1724 if (gc_stat_symbols[0] == 0) {
1725#define S(s) gc_stat_symbols[gc_stat_sym_##s] = ID2SYM(rb_intern_const(#s))
1726 S(count);
1727 S(moving_gc_count);
1728 S(time);
1729 S(total_allocated_objects);
1730 S(total_bytes);
1731 S(used_bytes);
1732 S(free_bytes);
1733 S(starting_heap_address);
1734 S(last_heap_address);
1735 S(weak_references_count);
1736 }
1737}
1738
1739VALUE
1740rb_gc_impl_stat(void *objspace_ptr, VALUE hash_or_sym)
1741{
1742 if (objspace_ptr == NULL) {
1744 }
1745
1746 struct objspace *objspace = objspace_ptr;
1747 VALUE hash = Qnil, key = Qnil;
1748
1749 setup_gc_stat_symbols();
1750
1751 if (RB_TYPE_P(hash_or_sym, T_HASH)) {
1752 hash = hash_or_sym;
1753 }
1754 else if (SYMBOL_P(hash_or_sym)) {
1755 key = hash_or_sym;
1756 }
1757 else {
1758 rb_bug("non-hash or symbol given");
1759 }
1760
1761#define SET(name, attr) \
1762 if (key == gc_stat_symbols[gc_stat_sym_##name]) \
1763 return SIZET2NUM(attr); \
1764 else if (hash != Qnil) \
1765 rb_hash_aset(hash, gc_stat_symbols[gc_stat_sym_##name], SIZET2NUM(attr));
1766
1767 SET(count, objspace->gc_count);
1768 SET(moving_gc_count, objspace->moving_gc_count);
1769 SET(time, objspace->total_gc_time / (1000 * 1000));
1770 SET(total_allocated_objects, objspace->total_allocated_objects);
1771 SET(total_bytes, mmtk_total_bytes());
1772 SET(used_bytes, mmtk_used_bytes());
1773 SET(free_bytes, mmtk_free_bytes());
1774 SET(starting_heap_address, (size_t)mmtk_starting_heap_address());
1775 SET(last_heap_address, (size_t)mmtk_last_heap_address());
1776 SET(weak_references_count, mmtk_weak_references_count());
1777#undef SET
1778
1779 if (!NIL_P(key)) {
1780 // Matched key should return above
1781 return Qundef;
1782 }
1783
1784 return hash;
1785}
1786
1787VALUE
1788rb_gc_impl_stat_heap(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym)
1789{
1790 if (NIL_P(heap_name) && RB_TYPE_P(hash_or_sym, T_HASH)) {
1791 return hash_or_sym;
1792 }
1793
1794 return Qundef;
1795}
1796
1797// Miscellaneous
1798
1799#define RB_GC_OBJECT_METADATA_ENTRY_COUNT 2
1800static struct rb_gc_object_metadata_entry object_metadata_entries[RB_GC_OBJECT_METADATA_ENTRY_COUNT + 1];
1801
1803rb_gc_impl_object_metadata(void *objspace_ptr, VALUE obj)
1804{
1805 static ID ID_wb_protected;
1806 static ID ID_object_id;
1807
1808 if (!ID_object_id) {
1809#define I(s) ID_##s = rb_intern(#s);
1810 I(wb_protected);
1811 I(object_id);
1812#undef I
1813 }
1814
1815 size_t n = 0;
1816
1817#define SET_ENTRY(na, v) do { \
1818 MMTK_ASSERT(n <= RB_GC_OBJECT_METADATA_ENTRY_COUNT); \
1819 object_metadata_entries[n].name = ID_##na; \
1820 object_metadata_entries[n].val = v; \
1821 n++; \
1822} while (0)
1823
1824 if (!mmtk_object_wb_unprotected_p((MMTk_ObjectReference)obj)) SET_ENTRY(wb_protected, Qtrue);
1825 if (rb_obj_id_p(obj)) SET_ENTRY(object_id, rb_obj_id(obj));
1826
1827 object_metadata_entries[n].name = 0;
1828 object_metadata_entries[n].val = 0;
1829
1830 return object_metadata_entries;
1831}
1832
1833bool
1834rb_gc_impl_live_object_p(void *objspace_ptr, const void *ptr)
1835{
1836 if (ptr == NULL) return false;
1837 if ((uintptr_t)ptr % sizeof(void*) != 0) return false;
1838 return mmtk_is_mmtk_object((MMTk_Address)ptr);
1839}
1840
1841bool
1842rb_gc_impl_garbage_object_p(void *objspace_ptr, VALUE obj)
1843{
1844 return false;
1845}
1846
1847void rb_gc_impl_set_event_hook(void *objspace_ptr, const rb_event_flag_t event) { }
1848
1849void
1850rb_gc_impl_copy_attributes(void *objspace_ptr, VALUE dest, VALUE obj)
1851{
1852 if (mmtk_object_wb_unprotected_p((MMTk_ObjectReference)obj)) {
1853 rb_gc_impl_writebarrier_unprotect(objspace_ptr, dest);
1854 }
1855
1856 rb_gc_impl_copy_finalizer(objspace_ptr, dest, obj);
1857}
1858
1859// GC Identification
1860
1861const char *
1862rb_gc_impl_active_gc_name(void)
1863{
1864 return "mmtk";
1865}
1866
1867bool
1868rb_gc_impl_during_global_gc_p(void *objspace_ptr)
1869{
1870 /* An mmtk GC is always a stop-the-world global GC. Helpers used during marking
1871 * (ractor_sync_mark and friends) read this to tell whether every mutator has
1872 * stopped. */
1873 struct objspace *objspace = objspace_ptr;
1874 return objspace->world_stopped;
1875}
1876
1877bool
1878rb_gc_impl_during_postmortem_p(void *objspace_ptr)
1879{
1880 /* mmtk has a single objspace and no per-Ractor retire collection. */
1881 return false;
1882}
1883
1884bool
1885rb_gc_impl_obj_foreign_p(void *objspace_ptr, VALUE obj)
1886{
1887 /* With a single objspace every object is our own. */
1888 return false;
1889}
1890
1891void
1892rb_gc_impl_objspace_set_ractor(void *objspace_ptr, void *ractor)
1893{
1894}
1895
1896void *
1897rb_gc_impl_obj_ractor(void *objspace_ptr, VALUE obj)
1898{
1899 return NULL; // nobody to ask: every object is the caller's own
1900}
1901
1902bool
1903rb_gc_impl_shref_marked_p(void *objspace_ptr, VALUE obj)
1904{
1905 /* With a single objspace there is no cross-objspace pinning to track. */
1906 return true;
1907}
1908
1909void
1910rb_gc_impl_objspace_zombify(void *objspace_ptr)
1911{
1912 /* With a single objspace zombie_objspaces is always empty. */
1913}
1914
1915void
1916rb_gc_impl_objspace_absorb(void *dst_ptr, void *src_ptr)
1917{
1918 /* A single objspace. */
1919}
1920
1921void
1922rb_gc_impl_gc_rest(void *objspace_ptr)
1923{
1924 /* An mmtk GC completes stop-the-world: there is no in-progress incremental
1925 * mark or lazy sweep state. */
1926}
1927
1929 int (*func)(void *, void *, size_t, void *);
1930 void *data;
1931};
1932
1933static int
1934each_objects_shareable_i(void *start, void *end, size_t stride, void *d)
1935{
1936 struct each_objects_shareable_data *data = d;
1937 for (VALUE obj = (VALUE)start; obj < (VALUE)end; obj += stride) {
1939 int ret = data->func((void *)obj, (void *)(obj + stride), stride, data->data);
1940 if (ret) return ret;
1941 }
1942 }
1943 return 0;
1944}
1945
1946void
1947rb_gc_impl_each_objects_shareable(void *objspace_ptr, int (*func)(void *, void *, size_t, void *), void *data)
1948{
1949 struct each_objects_shareable_data d = { func, data };
1950 rb_gc_impl_each_objects(objspace_ptr, each_objects_shareable_i, &d);
1951}
1952
1953void
1954rb_gc_impl_each_objects_foreign(void *objspace_ptr, int (*func)(void *, void *, size_t, void *), void *data)
1955{
1956 /* With a single objspace no object lives in a foreign objspace. */
1957}
Atomic operations.
#define RUBY_ATOMIC_INC(var)
Atomically increments the value pointed by var.
Definition atomic.h:214
#define RUBY_ATOMIC_PTR_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are void*.
Definition atomic.h:375
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_DEC(var)
Atomically decrements the value pointed by var.
Definition atomic.h:223
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
Definition atomic.h:175
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
unsigned int rb_postponed_job_handle_t
The type of a handle returned from rb_postponed_job_preregister and passed to rb_postponed_job_trigge...
Definition debug.h:703
void rb_postponed_job_trigger(rb_postponed_job_handle_t h)
Triggers a pre-registered job registered with rb_postponed_job_preregister, scheduling it for executi...
Definition vm_trace.c:1899
rb_postponed_job_handle_t rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
Pre-registers a func in Ruby's postponed job preregistration table, returning an opaque handle which ...
Definition vm_trace.c:1865
#define RUBY_INTERNAL_EVENT_FREEOBJ
Object swept.
Definition event.h:94
#define RUBY_INTERNAL_EVENT_GC_START
GC started.
Definition event.h:95
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(VALUE obj, VALUE flags)
Tests if the given flag(s) are set or not.
Definition fl_type.h:433
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
Definition fl_type.h:407
static void RB_FL_SET(VALUE obj, VALUE flags)
Sets the given flag(s).
Definition fl_type.h:564
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:253
@ RUBY_FL_FINALIZE
This flag has something to do with finalisers.
Definition fl_type.h:226
@ RUBY_FL_WEAK_REFERENCE
This object weakly refers to other objects.
Definition fl_type.h:260
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#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 Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#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:131
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
Definition fl_type.h:61
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:125
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#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 T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:127
#define FL_UNSET
Old name of RB_FL_UNSET.
Definition fl_type.h:129
#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
#define rb_notimplement()
Definition flock.c:3
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:94
VALUE rb_mGC
GC module.
Definition gc.c:477
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format)).
Definition format.h:33
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]].
Definition noreturn.h:38
void rb_memerror(void)
Triggers out-of-memory error.
Definition gc.c:6674
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1646
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1539
VALUE rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
Raises rb_eNotImpError.
Definition vm_method.c:913
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
int len
Length of the buffer.
Definition io.h:8
bool ruby_free_at_exit_p(void)
Returns whether the Ruby VM will free all memory at shutdown.
Definition vm.c:5287
#define RB_ULONG2NUM
Just another name of rb_ulong2num_inline.
Definition long.h:59
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:422
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
Definition rarray.h:386
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
Definition thread.c:6183
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.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
C99 shim for <stdbool.h>.
void * rust_closure
The pointer to the Rust-level closure object.
Definition mmtk.h:55
MMTk_ObjectClosureFunction c_function
The function to be called from C.
Definition mmtk.h:51
Ruby object's base components.
Definition rbasic.h:69
Definition gc_impl.h:34
Definition st.h:79
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376