Ruby 3.5.0dev (2025-01-10 revision 5fab31b15e32622c4b71d1d347a41937e9f9c212)
gc.h
1#ifndef GC_GC_H
2#define GC_GC_H
12#include "ruby/ruby.h"
13
14#if USE_MODULAR_GC
15#include "ruby/thread_native.h"
16
17struct rb_gc_vm_context {
18 rb_nativethread_lock_t lock;
19
21};
22
23typedef int (*vm_table_foreach_callback_func)(VALUE value, void *data);
24typedef int (*vm_table_update_callback_func)(VALUE *value, void *data);
25
26
27enum rb_gc_vm_weak_tables {
28 RB_GC_VM_CI_TABLE,
29 RB_GC_VM_OVERLOADED_CME_TABLE,
30 RB_GC_VM_GLOBAL_SYMBOLS_TABLE,
31 RB_GC_VM_GENERIC_IV_TABLE,
32 RB_GC_VM_FROZEN_STRINGS_TABLE,
33 RB_GC_VM_WEAK_TABLE_COUNT
34};
35#endif
36
37RUBY_SYMBOL_EXPORT_BEGIN
38unsigned int rb_gc_vm_lock(void);
39void rb_gc_vm_unlock(unsigned int lev);
40unsigned int rb_gc_cr_lock(void);
41void rb_gc_cr_unlock(unsigned int lev);
42unsigned int rb_gc_vm_lock_no_barrier(void);
43void rb_gc_vm_unlock_no_barrier(unsigned int lev);
44void rb_gc_vm_barrier(void);
45size_t rb_gc_obj_optimal_size(VALUE obj);
46void rb_gc_mark_children(void *objspace, VALUE obj);
47void rb_gc_update_object_references(void *objspace, VALUE obj);
48void rb_gc_update_vm_references(void *objspace);
49void rb_gc_event_hook(VALUE obj, rb_event_flag_t event);
50void *rb_gc_get_objspace(void);
51size_t rb_size_mul_or_raise(size_t x, size_t y, VALUE exc);
52void rb_gc_run_obj_finalizer(VALUE objid, long count, VALUE (*callback)(long i, void *data), void *data);
53void rb_gc_set_pending_interrupt(void);
54void rb_gc_unset_pending_interrupt(void);
55void rb_gc_obj_free_vm_weak_references(VALUE obj);
56bool rb_gc_obj_free(void *objspace, VALUE obj);
57void rb_gc_save_machine_context(void);
58void rb_gc_mark_roots(void *objspace, const char **categoryp);
59void rb_gc_ractor_newobj_cache_foreach(void (*func)(void *cache, void *data), void *data);
60bool rb_gc_multi_ractor_p(void);
61void rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *passing_data);
62void rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data);
63void rb_obj_info_dump(VALUE obj);
64const char *rb_obj_info(VALUE obj);
65bool rb_gc_shutdown_call_finalizer_p(VALUE obj);
66uint32_t rb_gc_get_shape(VALUE obj);
67void rb_gc_set_shape(VALUE obj, uint32_t shape_id);
68uint32_t rb_gc_rebuild_shape(VALUE obj, size_t heap_id);
69size_t rb_obj_memsize_of(VALUE obj);
70void rb_gc_prepare_heap_process_object(VALUE obj);
71bool ruby_free_at_exit_p(void);
72bool rb_memerror_reentered(void);
73
74#if USE_MODULAR_GC
75bool rb_gc_event_hook_required_p(rb_event_flag_t event);
76void *rb_gc_get_ractor_newobj_cache(void);
77void rb_gc_initialize_vm_context(struct rb_gc_vm_context *context);
78void rb_gc_worker_thread_set_vm_context(struct rb_gc_vm_context *context);
79void rb_gc_worker_thread_unset_vm_context(struct rb_gc_vm_context *context);
80void rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback, vm_table_update_callback_func update_callback, void *data, enum rb_gc_vm_weak_tables table);
81#endif
82RUBY_SYMBOL_EXPORT_END
83
84void rb_ractor_finish_marking(void);
85
86// -------------------Private section begin------------------------
87// Functions in this section are private to the default GC and gc.c
88
89#ifdef BUILDING_MODULAR_GC
91RBIMPL_WARNING_IGNORED(-Wunused-function)
92#endif
93
94/* RGENGC_CHECK_MODE
95 * 0: disable all assertions
96 * 1: enable assertions (to debug RGenGC)
97 * 2: enable internal consistency check at each GC (for debugging)
98 * 3: enable internal consistency check at each GC steps (for debugging)
99 * 4: enable liveness check
100 * 5: show all references
101 */
102#ifndef RGENGC_CHECK_MODE
103# define RGENGC_CHECK_MODE 0
104#endif
105
106#ifndef GC_ASSERT
107# define GC_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(RGENGC_CHECK_MODE > 0, expr, #expr)
108#endif
109
110static int
111hash_foreach_replace_value(st_data_t key, st_data_t value, st_data_t argp, int error)
112{
113 if (rb_gc_location((VALUE)value) != (VALUE)value) {
114 return ST_REPLACE;
115 }
116 return ST_CONTINUE;
117}
118
119static int
120hash_replace_ref_value(st_data_t *key, st_data_t *value, st_data_t argp, int existing)
121{
122 *value = rb_gc_location((VALUE)*value);
123
124 return ST_CONTINUE;
125}
126
127static void
128gc_ref_update_table_values_only(st_table *tbl)
129{
130 if (!tbl || tbl->num_entries == 0) return;
131
132 if (st_foreach_with_replace(tbl, hash_foreach_replace_value, hash_replace_ref_value, 0)) {
133 rb_raise(rb_eRuntimeError, "hash modified during iteration");
134 }
135}
136
137static int
138gc_mark_tbl_no_pin_i(st_data_t key, st_data_t value, st_data_t data)
139{
140 rb_gc_mark_movable((VALUE)value);
141
142 return ST_CONTINUE;
143}
144
145static int
146hash_foreach_replace(st_data_t key, st_data_t value, st_data_t argp, int error)
147{
148 if (rb_gc_location((VALUE)key) != (VALUE)key) {
149 return ST_REPLACE;
150 }
151
152 if (rb_gc_location((VALUE)value) != (VALUE)value) {
153 return ST_REPLACE;
154 }
155
156 return ST_CONTINUE;
157}
158
159static int
160hash_replace_ref(st_data_t *key, st_data_t *value, st_data_t argp, int existing)
161{
162 if (rb_gc_location((VALUE)*key) != (VALUE)*key) {
163 *key = rb_gc_location((VALUE)*key);
164 }
165
166 if (rb_gc_location((VALUE)*value) != (VALUE)*value) {
167 *value = rb_gc_location((VALUE)*value);
168 }
169
170 return ST_CONTINUE;
171}
172
173static void
174gc_update_table_refs(st_table *tbl)
175{
176 if (!tbl || tbl->num_entries == 0) return;
177
178 if (st_foreach_with_replace(tbl, hash_foreach_replace, hash_replace_ref, 0)) {
179 rb_raise(rb_eRuntimeError, "hash modified during iteration");
180 }
181}
182
183static inline size_t
184xmalloc2_size(const size_t count, const size_t elsize)
185{
186 return rb_size_mul_or_raise(count, elsize, rb_eArgError);
187}
188
189static VALUE
190type_sym(size_t type)
191{
192 switch (type) {
193#define COUNT_TYPE(t) case (t): return ID2SYM(rb_intern(#t)); break;
194 COUNT_TYPE(T_NONE);
195 COUNT_TYPE(T_OBJECT);
196 COUNT_TYPE(T_CLASS);
197 COUNT_TYPE(T_MODULE);
198 COUNT_TYPE(T_FLOAT);
199 COUNT_TYPE(T_STRING);
200 COUNT_TYPE(T_REGEXP);
201 COUNT_TYPE(T_ARRAY);
202 COUNT_TYPE(T_HASH);
203 COUNT_TYPE(T_STRUCT);
204 COUNT_TYPE(T_BIGNUM);
205 COUNT_TYPE(T_FILE);
206 COUNT_TYPE(T_DATA);
207 COUNT_TYPE(T_MATCH);
208 COUNT_TYPE(T_COMPLEX);
209 COUNT_TYPE(T_RATIONAL);
210 COUNT_TYPE(T_NIL);
211 COUNT_TYPE(T_TRUE);
212 COUNT_TYPE(T_FALSE);
213 COUNT_TYPE(T_SYMBOL);
214 COUNT_TYPE(T_FIXNUM);
215 COUNT_TYPE(T_IMEMO);
216 COUNT_TYPE(T_UNDEF);
217 COUNT_TYPE(T_NODE);
218 COUNT_TYPE(T_ICLASS);
219 COUNT_TYPE(T_ZOMBIE);
220 COUNT_TYPE(T_MOVED);
221#undef COUNT_TYPE
222 default: return SIZET2NUM(type); break;
223 }
224}
225
226#ifdef BUILDING_MODULAR_GC
228#endif
229// -------------------Private section end------------------------
230
231#endif
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define T_FILE
Old name of RUBY_T_FILE.
Definition value_type.h:62
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#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 T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#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 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 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 T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
Definition value_type.h:83
#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 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 T_MOVED
Old name of RUBY_T_MOVED.
Definition value_type.h:71
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1428
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
#define RBIMPL_WARNING_POP()
Pops compiler warning state.