Ruby 4.1.0dev (2026-10-08 revision dade419372bc39b569ccb4cfbabb78cfbfd285ee)
gc.h
1#ifndef INTERNAL_GC_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_GC_H
11#include "ruby/internal/config.h"
12
13#include <stddef.h> /* for size_t */
14
15#include "internal/compilers.h" /* for __has_attribute */
16#include "ruby/ruby.h" /* for rb_event_flag_t */
17#include "ruby_atomic.h" /* for RUBY_ATOMIC_VALUE_SET */
18
20
21#ifndef USE_MODULAR_GC
22# define USE_MODULAR_GC 0
23#endif
24
25#if defined(__x86_64__) && !defined(_ILP32) && defined(__GNUC__)
26#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movq\t%%rsp, %0" : "=r" (*(p)))
27#elif defined(__i386) && defined(__GNUC__)
28#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movl\t%%esp, %0" : "=r" (*(p)))
29#elif (defined(__powerpc__) || defined(__powerpc64__)) && defined(__GNUC__) && !defined(_AIX)
30#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mr\t%0, %%r1" : "=r" (*(p)))
31#elif (defined(__powerpc__) || defined(__powerpc64__)) && defined(__GNUC__) && defined(_AIX)
32#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mr %0,1" : "=r" (*(p)))
33#elif defined(__aarch64__) && defined(__GNUC__)
34#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mov\t%0, sp" : "=r" (*(p)))
35#else
36NOINLINE(void rb_gc_set_stack_end(VALUE **stack_end_p));
37#define SET_MACHINE_STACK_END(p) rb_gc_set_stack_end(p)
38#define USE_CONSERVATIVE_STACK_END
39#endif
40
41/* for GC debug */
42
43#ifndef RUBY_MARK_FREE_DEBUG
44#define RUBY_MARK_FREE_DEBUG 0
45#endif
46
47#if RUBY_MARK_FREE_DEBUG
48extern int ruby_gc_debug_indent;
49
50static inline void
51rb_gc_debug_indent(void)
52{
53 ruby_debug_printf("%*s", ruby_gc_debug_indent, "");
54}
55
56static inline void
57rb_gc_debug_body(const char *mode, const char *msg, int st, void *ptr)
58{
59 if (st == 0) {
60 ruby_gc_debug_indent--;
61 }
62 rb_gc_debug_indent();
63 ruby_debug_printf("%s: %s %s (%p)\n", mode, st ? "->" : "<-", msg, ptr);
64
65 if (st) {
66 ruby_gc_debug_indent++;
67 }
68
69 fflush(stdout);
70}
71
72#define RUBY_MARK_ENTER(msg) rb_gc_debug_body("mark", (msg), 1, ptr)
73#define RUBY_MARK_LEAVE(msg) rb_gc_debug_body("mark", (msg), 0, ptr)
74#define RUBY_FREE_ENTER(msg) rb_gc_debug_body("free", (msg), 1, ptr)
75#define RUBY_FREE_LEAVE(msg) rb_gc_debug_body("free", (msg), 0, ptr)
76#define RUBY_GC_INFO rb_gc_debug_indent(), ruby_debug_printf
77
78#else
79#define RUBY_MARK_ENTER(msg)
80#define RUBY_MARK_LEAVE(msg)
81#define RUBY_FREE_ENTER(msg)
82#define RUBY_FREE_LEAVE(msg)
83#define RUBY_GC_INFO if(0)printf
84#endif
85
86#if STACK_GROW_DIRECTION > 0
87# define STACK_UPPER(x, a, b) (a)
88#elif STACK_GROW_DIRECTION < 0
89# define STACK_UPPER(x, a, b) (b)
90#else
91RUBY_EXTERN int ruby_stack_grow_direction;
92int ruby_get_stack_grow_direction(volatile VALUE *addr);
93# define stack_growup_p(x) ( \
94 (ruby_stack_grow_direction ? \
95 ruby_stack_grow_direction : \
96 ruby_get_stack_grow_direction(x)) > 0)
97# define STACK_UPPER(x, a, b) (stack_growup_p(x) ? (a) : (b))
98#endif
99
100/*
101 STACK_GROW_DIR_DETECTION is used with STACK_DIR_UPPER.
102
103 On most normal systems, stacks grow from high address to lower address. In
104 this case, STACK_DIR_UPPER(a, b) will return (b), but on exotic systems where
105 the stack grows UP (from low address to high address), it will return (a).
106*/
107
108#if STACK_GROW_DIRECTION
109#define STACK_GROW_DIR_DETECTION
110#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, (a), (b))
111#else
112#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection
113#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, (a), (b))
114#endif
115#define IS_STACK_DIR_UPPER() STACK_DIR_UPPER(1,0)
116
117const char *rb_obj_info(VALUE obj);
118const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);
119
120struct rb_execution_context_struct; /* in vm_core.h */
121struct rb_objspace; /* in vm_core.h */
122struct rb_ractor_struct; /* in vm_core.h */
123
124#define EC_NEWOBJ_OF(var, T, c, f, s, ec) \
125 T *(var) = (T *)rb_ec_newobj_of((ec), (c), (f), s)
126#define NEWOBJ_OF(var, T, c, f, s) EC_NEWOBJ_OF(var, T, c, f, s, GET_EC())
127#define UNPROTECTED_NEWOBJ_OF(var, T, c, f, s) \
128 T *(var) = (T *)rb_newobj((GET_EC()), (c), (f), ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER, false, s)
129
130#ifndef RB_GC_OBJECT_METADATA_ENTRY_DEFINED
131# define RB_GC_OBJECT_METADATA_ENTRY_DEFINED
133 ID name;
134 VALUE val;
135};
136#endif
137
138#ifndef USE_UNALIGNED_MEMBER_ACCESS
139# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
140#elif ! USE_UNALIGNED_MEMBER_ACCESS
141# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
142#elif ! (__has_warning("-Waddress-of-packed-member") || GCC_VERSION_SINCE(9, 0, 0))
143# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
144#else
145# include "internal/warnings.h"
146# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
147 COMPILER_WARNING_PUSH; \
148 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
149 __typeof__(expr) unaligned_member_access_result = (expr); \
150 COMPILER_WARNING_POP; \
151 unaligned_member_access_result; \
152})
153
154# define UNALIGNED_MEMBER_PTR(ptr, mem) __extension__({ \
155 COMPILER_WARNING_PUSH; \
156 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
157 const volatile void *unaligned_member_ptr_result = &(ptr)->mem; \
158 COMPILER_WARNING_POP; \
159 (__typeof__((ptr)->mem) *)unaligned_member_ptr_result; \
160})
161#endif
162
163#ifndef UNALIGNED_MEMBER_PTR
164# define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
165#endif
166
167#define RB_OBJ_WRITE_UNALIGNED(old, slot, young) do { \
168 VALUE *_slot = UNALIGNED_MEMBER_ACCESS(slot); \
169 RB_OBJ_WRITE(old, _slot, young); \
170} while (0)
171
172/* Used in places that could malloc during, which can cause the GC to run. We
173 * need to temporarily disable the GC to allow the malloc to happen.
174 * Allocating memory during GC is a bad idea, so use this only when absolutely
175 * necessary. */
176/* Only re-entrant GC of the current objspace needs suppressing (the malloc happens inside this
177 * Ractor), so use the local disable. The during-GC malloc guard reads the per-objspace dont_gc
178 * flag rather than a process-wide one. */
179#define DURING_GC_COULD_MALLOC_REGION_START() \
180 assert(rb_during_gc()); \
181 VALUE _already_disabled = rb_gc_local_disable_no_rest()
182
183#define DURING_GC_COULD_MALLOC_REGION_END() \
184 if (_already_disabled == Qfalse) rb_gc_local_enable()
185
186/* gc.c */
187RUBY_ATTR_MALLOC void *ruby_mimmalloc(size_t size);
188RUBY_ATTR_MALLOC void *ruby_mimcalloc(size_t num, size_t size);
189void ruby_mimfree(void *ptr);
190void rb_gc_prepare_heap(void);
191void rb_objspace_set_event_hook(const rb_event_flag_t event);
192VALUE rb_objspace_gc_enable(void *objspace);
193VALUE rb_objspace_gc_disable(void *objspace);
194void ruby_gc_set_params(void);
195void rb_gc_copy_attributes(VALUE dest, VALUE obj);
196size_t rb_size_mul_or_raise(size_t, size_t, VALUE); /* used in compile.c */
197size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE); /* used in iseq.h */
198size_t rb_malloc_grow_capa(size_t current_capacity, size_t type_size);
199RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add(size_t, size_t, size_t);
200RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add(size_t, size_t, size_t);
201void *rb_xrealloc_mul_add(const void *, size_t, size_t, size_t);
202RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t);
203RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
204void rb_gc_register_pinning_obj(VALUE obj);
205struct rb_execution_context_struct *rb_gc_get_ec(void);
206VALUE rb_newobj_of_unprotected(VALUE klass, VALUE flags, size_t size);
207
208void *rb_gc_ractor_cache_alloc(struct rb_ractor_struct *ractor);
209void rb_gc_ractor_cache_free(void *cache);
210bool rb_gc_zjit_new_obj_fastpath(size_t alloc_size, VALUE flags, VALUE klass, struct rb_gc_zjit_fastpath *fastpath);
211
212bool rb_gc_size_allocatable_p(size_t size);
213size_t rb_gc_size_slot_size(size_t size);
214size_t rb_gc_max_allocation_size(void);
215
216void rb_gc_mark_and_move(VALUE *ptr);
217
218/* The objspace a marking walk marks into, plus the per-Ractor traversal redirect slot
219 * (vm_core.h's gc_mark_func_data_struct) and the shareable-check flag cached from it.
220 * Resolving the current Ractor costs an out-of-line rb_current_ec() call on arm64, so a
221 * walk resolves it once into a ctx and threads that down instead of repeating it per
222 * reference. Only gc.c builds one; everyone else just passes the pointer along. */
224
226 void *objspace;
227 struct gc_mark_func_data_struct **mfdp;
228 bool checking_shareable;
229};
230
231void rb_gc_mark_and_pin_ctx(const struct rb_gc_mark_ctx *ctx, VALUE obj);
232void rb_gc_mark_movable_ctx(const struct rb_gc_mark_ctx *ctx, VALUE obj);
233void rb_gc_mark_maybe_ctx(const struct rb_gc_mark_ctx *ctx, VALUE obj);
234void rb_gc_mark_and_move_ctx(const struct rb_gc_mark_ctx *ctx, VALUE *ptr);
235void rb_gc_mark_locations_ctx(const struct rb_gc_mark_ctx *ctx, const VALUE *start, const VALUE *end);
236void rb_mark_tbl_no_pin_ctx(const struct rb_gc_mark_ctx *ctx, st_table *tbl);
237void rb_gc_mark_set_no_pin_ctx(const struct rb_gc_mark_ctx *ctx, st_table *tbl);
238VALUE rb_gc_location_ctx(const struct rb_gc_mark_ctx *ctx, VALUE value);
239void rb_gc_update_moved_ctx(const struct rb_gc_mark_ctx *ctx, VALUE *ptr);
240
241static inline bool
242rb_gc_checking_shareable_ctx(const struct rb_gc_mark_ctx *ctx)
243{
244 return ctx->checking_shareable;
245}
246
247void rb_gc_ref_update_table_values_only(st_table *tbl);
248
249void rb_gc_initial_stress_set(VALUE flag);
250
251void rb_gc_before_fork(void);
252void rb_gc_after_fork(rb_pid_t pid);
253
254#define rb_gc_mark_and_move_ptr(ptr) do { \
255 VALUE _obj = (VALUE)*(ptr); \
256 rb_gc_mark_and_move(&_obj); \
257 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
258} while (0)
259
260#define rb_gc_update_moved_ptr(ptr) do { \
261 VALUE _obj = (VALUE)*(ptr); \
262 rb_gc_update_moved(&_obj); \
263 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
264} while (0)
265
266#define rb_gc_mark_and_move_ptr_ctx(ctx, ptr) do { \
267 VALUE _obj = (VALUE)*(ptr); \
268 rb_gc_mark_and_move_ctx((ctx), &_obj); \
269 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
270} while (0)
271
272#define rb_gc_update_moved_ptr_ctx(ctx, ptr) do { \
273 VALUE _obj = (VALUE)*(ptr); \
274 rb_gc_update_moved_ctx((ctx), &_obj); \
275 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
276} while (0)
277
278void rb_objspace_reachable_objects_from_local(VALUE obj, void (func)(VALUE, void *), void *data);
279
280#if USE_MODULAR_GC
281RUBY_SYMBOL_EXPORT_BEGIN
282#endif
283/* Called only by the default GC's verifier, which lives in the GC's shared object in a
284 * modular build, so it needs exporting only there. */
285void rb_objspace_reachable_objects_from_unlocked(VALUE obj, void (func)(VALUE, void *), void *data);
286#if USE_MODULAR_GC
287RUBY_SYMBOL_EXPORT_END
288#endif
289
290RUBY_SYMBOL_EXPORT_BEGIN
291/* exports for objspace module */
292void rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data);
293void rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *data);
294int rb_objspace_internal_object_p(VALUE obj);
295int rb_objspace_garbage_object_p(VALUE obj);
296int rb_objspace_foreign_object_p(VALUE obj);
297int rb_objspace_live_object_p(VALUE obj);
298void rb_gc_declare_weak_references(VALUE obj);
299bool rb_gc_handle_weak_references_alive_p(VALUE obj);
300
301void rb_objspace_each_objects(
302 int (*callback)(void *start, void *end, size_t stride, void *data),
303 void *data);
304
305
306size_t rb_gc_obj_slot_size(VALUE obj);
307
308VALUE rb_gc_disable_no_rest(void);
309/* Local GC disable/enable covering only the current Ractor's objspace. Unlike rb_gc_disable*,
310 * which became process-wide, this suppresses GC in one's own objspace only. Exported because
311 * DURING_GC_COULD_MALLOC_REGION above expands in bundled extensions. */
312VALUE rb_gc_local_enable(void);
313VALUE rb_gc_local_disable_no_rest(void);
314
315#define RB_GC_MAX_NAME_LEN 20
316
317/* gc.c (export) */
318const char *rb_objspace_data_type_name(VALUE obj);
319VALUE rb_newobj(struct rb_execution_context_struct *, VALUE, VALUE, uint32_t /* shape_id_t */, bool, size_t);
320VALUE rb_newobj_of(VALUE, VALUE, size_t);
321VALUE rb_ec_newobj_of(struct rb_execution_context_struct *, VALUE, VALUE, size_t);
322size_t rb_obj_memsize_of(VALUE);
323struct rb_gc_object_metadata_entry *rb_gc_object_metadata(VALUE obj);
324void rb_gc_mark_values(long n, const VALUE *values);
325void rb_gc_mark_vm_stack_values(long n, const VALUE *values);
326struct rb_vm_struct;
327void rb_gc_mark_registered_addrs(struct rb_ractor_struct *r, bool need_lock);
328void rb_gc_registered_addrs_enroll_without_gc(struct rb_vm_struct *vm, struct rb_ractor_struct *r);
329void rb_gc_registered_addrs_unenroll_without_gc(struct rb_vm_struct *vm, struct rb_ractor_struct *r);
330void rb_gc_update_values(long n, VALUE *values);
331void rb_gc_mark_set_no_pin(st_table *);
332/* Exercised by the bundled -test-/gc/writebarrier extension, so it must be visible
333 * outside the ruby binary. */
334void rb_gc_writebarrier_remember(VALUE obj);
335void rb_gc_update_set_refs(st_table *);
336
337#if USE_MODULAR_GC
338const char *rb_gc_active_gc_name(void);
339int rb_gc_modular_gc_loaded_p(void);
340#endif
341
342RUBY_SYMBOL_EXPORT_END
343
344static inline VALUE
345rb_obj_atomic_write(
346 VALUE a, VALUE *slot, VALUE b,
348 const char *filename,
350 int line)
351{
352#ifdef RGENGC_LOGGING_WRITE
353 RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
354#endif
355
356 RUBY_ATOMIC_VALUE_SET(*slot, b);
357
358 rb_obj_written(a, RUBY_Qundef /* ignore `oldv' now */, b, filename, line);
359 return a;
360}
361#define RB_OBJ_ATOMIC_WRITE(old, slot, young) \
362 RBIMPL_CAST(rb_obj_atomic_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
363
364int rb_ec_stack_check(struct rb_execution_context_struct *ec);
365void rb_gc_obj_became_shareable(VALUE obj);
366bool rb_gc_multi_objspace_p(void);
367bool rb_gc_obj_foreign_p(VALUE obj);
368void *rb_gc_objspace_alloc(void);
369void rb_gc_objspace_retire_gc(void);
370/* Build an object, or suppress GC, in a named objspace rather than the current
371 * Ractor's. Only create_ractor_alloc_thread() needs these. */
372VALUE rb_data_typed_object_wrap_in_objspace(void *objspace, VALUE klass, void *datap, const rb_data_type_t *type);
373VALUE rb_data_typed_object_zalloc_in_objspace(void *objspace, VALUE klass, size_t size, const rb_data_type_t *type);
374VALUE rb_gc_objspace_disable_no_rest(void *objspace);
375VALUE rb_gc_objspace_enable(void *objspace);
376
377void rb_gc_objspace_retire(void **objspace_slot);
378void rb_gc_objspace_postmortem_self(void);
379void rb_gc_objspace_absorb_into_current(void **objspace_slot);
380void rb_gc_objspace_absorb_all_zombies(void);
381void rb_gc_objspace_disown(void *objspace);
382void rb_gc_zombie_objspaces_atfork(void);
383void rb_gc_disable_holders_atfork(void);
384void rb_gc_atfork_global_locks(void);
385void rb_gc_stash_cleanup_objspace(void);
386void rb_gc_rest(void);
387bool rb_gc_during_global_gc_p(void);
388bool rb_gc_single_objspace_p(void);
389const char *rb_obj_info(VALUE obj);
390void ruby_annotate_mmap(const void *addr, unsigned long size, const char *name);
391
392# define SIZED_REALLOC_N(v, T, m, n) \
393 ((v) = (T *)ruby_xrealloc2_sized((void *)(v), (m), sizeof(T), (n)))
394
395# define SIZED_FREE(v) ruby_xfree_sized((void *)(v), sizeof(*(v)))
396# define SIZED_FREE_N(v, n) ruby_xfree_sized((void *)(v), sizeof(*(v)) * (n))
397
398static inline void *
399ruby_sized_realloc_n(void *ptr, size_t new_count, size_t element_size, size_t old_count)
400{
401 return ruby_xrealloc2_sized(ptr, new_count, element_size, old_count);
402}
403
404void rb_gc_verify_shareable(VALUE);
405bool rb_gc_checking_shareable(void);
406
407#endif /* INTERNAL_GC_H */
#define RUBY_ATOMIC_VALUE_SET(var, val)
Identical to RUBY_ATOMIC_SET, except it expects its arguments are VALUE.
Definition atomic.h:378
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
VALUE type(ANYARGS)
ANYARGS-ed function type.
@ RUBY_Qundef
Represents so-called undef.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:242
Definition gc_impl.h:34
Definition st.h:79
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