Ruby 3.5.0dev (2025-02-22 revision b17f984e4e903d3ece3013c1488279d1947dfc39)
gc_impl.h
1#ifndef GC_GC_IMPL_H
2#define GC_GC_IMPL_H
11#include "ruby/ruby.h"
12
13#ifndef RB_GC_OBJECT_METADATA_ENTRY_DEFINED
14# define RB_GC_OBJECT_METADATA_ENTRY_DEFINED
16 ID name;
17 VALUE val;
18};
19#endif
20
21#ifdef BUILDING_MODULAR_GC
22# define GC_IMPL_FN
23#else
24// `GC_IMPL_FN` is an implementation detail of `!USE_MODULAR_GC` builds
25// to have the default GC in the same translation unit as gc.c for
26// the sake of optimizer visibility. It expands to nothing unless
27// you're the default GC.
28//
29// For the default GC, do not copy-paste this when implementing
30// these functions. This takes advantage of internal linkage winning
31// when appearing first. See C99 6.2.2p4.
32# define GC_IMPL_FN static
33#endif
34
35// Bootup
36GC_IMPL_FN void *rb_gc_impl_objspace_alloc(void);
37GC_IMPL_FN void rb_gc_impl_objspace_init(void *objspace_ptr);
38GC_IMPL_FN void rb_gc_impl_objspace_free(void *objspace_ptr);
39GC_IMPL_FN void *rb_gc_impl_ractor_cache_alloc(void *objspace_ptr, void *ractor);
40GC_IMPL_FN void rb_gc_impl_ractor_cache_free(void *objspace_ptr, void *cache);
41GC_IMPL_FN void rb_gc_impl_set_params(void *objspace_ptr);
42GC_IMPL_FN void rb_gc_impl_init(void);
43GC_IMPL_FN size_t *rb_gc_impl_heap_sizes(void *objspace_ptr);
44// Shutdown
45GC_IMPL_FN void rb_gc_impl_shutdown_free_objects(void *objspace_ptr);
46// GC
47GC_IMPL_FN void rb_gc_impl_start(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact);
48GC_IMPL_FN bool rb_gc_impl_during_gc_p(void *objspace_ptr);
49GC_IMPL_FN void rb_gc_impl_prepare_heap(void *objspace_ptr);
50GC_IMPL_FN void rb_gc_impl_gc_enable(void *objspace_ptr);
51GC_IMPL_FN void rb_gc_impl_gc_disable(void *objspace_ptr, bool finish_current_gc);
52GC_IMPL_FN bool rb_gc_impl_gc_enabled_p(void *objspace_ptr);
53GC_IMPL_FN void rb_gc_impl_stress_set(void *objspace_ptr, VALUE flag);
54GC_IMPL_FN VALUE rb_gc_impl_stress_get(void *objspace_ptr);
55GC_IMPL_FN VALUE rb_gc_impl_config_get(void *objspace_ptr);
56GC_IMPL_FN void rb_gc_impl_config_set(void *objspace_ptr, VALUE hash);
57// Object allocation
58GC_IMPL_FN VALUE rb_gc_impl_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);
59GC_IMPL_FN size_t rb_gc_impl_obj_slot_size(VALUE obj);
60GC_IMPL_FN size_t rb_gc_impl_heap_id_for_size(void *objspace_ptr, size_t size);
61GC_IMPL_FN bool rb_gc_impl_size_allocatable_p(size_t size);
62// Malloc
63/*
64 * BEWARE: These functions may or may not run under GVL.
65 *
66 * You might want to make them thread-safe.
67 * Garbage collecting inside is possible if and only if you
68 * already have GVL. Also raising exceptions without one is a
69 * total disaster.
70 *
71 * When you absolutely cannot allocate the requested amount of
72 * memory just return NULL (with appropriate errno set).
73 * The caller side takes care of that situation.
74 */
75GC_IMPL_FN void *rb_gc_impl_malloc(void *objspace_ptr, size_t size);
76GC_IMPL_FN void *rb_gc_impl_calloc(void *objspace_ptr, size_t size);
77GC_IMPL_FN void *rb_gc_impl_realloc(void *objspace_ptr, void *ptr, size_t new_size, size_t old_size);
78GC_IMPL_FN void rb_gc_impl_free(void *objspace_ptr, void *ptr, size_t old_size);
79GC_IMPL_FN void rb_gc_impl_adjust_memory_usage(void *objspace_ptr, ssize_t diff);
80// Marking
81GC_IMPL_FN void rb_gc_impl_mark(void *objspace_ptr, VALUE obj);
82GC_IMPL_FN void rb_gc_impl_mark_and_move(void *objspace_ptr, VALUE *ptr);
83GC_IMPL_FN void rb_gc_impl_mark_and_pin(void *objspace_ptr, VALUE obj);
84GC_IMPL_FN void rb_gc_impl_mark_maybe(void *objspace_ptr, VALUE obj);
85GC_IMPL_FN void rb_gc_impl_mark_weak(void *objspace_ptr, VALUE *ptr);
86GC_IMPL_FN void rb_gc_impl_remove_weak(void *objspace_ptr, VALUE parent_obj, VALUE *ptr);
87// Compaction
88GC_IMPL_FN bool rb_gc_impl_object_moved_p(void *objspace_ptr, VALUE obj);
89GC_IMPL_FN VALUE rb_gc_impl_location(void *objspace_ptr, VALUE value);
90// Write barriers
91GC_IMPL_FN void rb_gc_impl_writebarrier(void *objspace_ptr, VALUE a, VALUE b);
92GC_IMPL_FN void rb_gc_impl_writebarrier_unprotect(void *objspace_ptr, VALUE obj);
93GC_IMPL_FN void rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj);
94// Heap walking
95GC_IMPL_FN void rb_gc_impl_each_objects(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data);
96GC_IMPL_FN void rb_gc_impl_each_object(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data);
97// Finalizers
98GC_IMPL_FN void rb_gc_impl_make_zombie(void *objspace_ptr, VALUE obj, void (*dfree)(void *), void *data);
99GC_IMPL_FN VALUE rb_gc_impl_define_finalizer(void *objspace_ptr, VALUE obj, VALUE block);
100GC_IMPL_FN void rb_gc_impl_undefine_finalizer(void *objspace_ptr, VALUE obj);
101GC_IMPL_FN void rb_gc_impl_copy_finalizer(void *objspace_ptr, VALUE dest, VALUE obj);
102GC_IMPL_FN void rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr);
103// Object ID
104GC_IMPL_FN VALUE rb_gc_impl_object_id(void *objspace_ptr, VALUE obj);
105GC_IMPL_FN VALUE rb_gc_impl_object_id_to_ref(void *objspace_ptr, VALUE object_id);
106// Forking
107GC_IMPL_FN void rb_gc_impl_before_fork(void *objspace_ptr);
108GC_IMPL_FN void rb_gc_impl_after_fork(void *objspace_ptr, rb_pid_t pid);
109// Statistics
110GC_IMPL_FN void rb_gc_impl_set_measure_total_time(void *objspace_ptr, VALUE flag);
111GC_IMPL_FN bool rb_gc_impl_get_measure_total_time(void *objspace_ptr);
112GC_IMPL_FN unsigned long long rb_gc_impl_get_total_time(void *objspace_ptr);
113GC_IMPL_FN size_t rb_gc_impl_gc_count(void *objspace_ptr);
114GC_IMPL_FN VALUE rb_gc_impl_latest_gc_info(void *objspace_ptr, VALUE key);
115GC_IMPL_FN VALUE rb_gc_impl_stat(void *objspace_ptr, VALUE hash_or_sym);
116GC_IMPL_FN VALUE rb_gc_impl_stat_heap(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym);
117GC_IMPL_FN const char *rb_gc_impl_active_gc_name(void);
118// Miscellaneous
119GC_IMPL_FN struct rb_gc_object_metadata_entry *rb_gc_impl_object_metadata(void *objspace_ptr, VALUE obj);
120GC_IMPL_FN bool rb_gc_impl_pointer_to_heap_p(void *objspace_ptr, const void *ptr);
121GC_IMPL_FN bool rb_gc_impl_garbage_object_p(void *objspace_ptr, VALUE obj);
122GC_IMPL_FN void rb_gc_impl_set_event_hook(void *objspace_ptr, const rb_event_flag_t event);
123GC_IMPL_FN void rb_gc_impl_copy_attributes(void *objspace_ptr, VALUE dest, VALUE obj);
124
125#undef GC_IMPL_FN
126
127#endif
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
Definition gc_impl.h:15
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