Ruby 4.1.0dev (2026-09-21 revision 61de3dd727146cfcf24e8051595bb2fb842f37aa)
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) && !defined(__APPLE__) // Not Apple is NEEDED to unbreak ppc64 build on Darwin. Don't ask.
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(__POWERPC__) && defined(__APPLE__) // Darwin ppc and ppc64
34#define SET_MACHINE_STACK_END(p) __asm__ volatile("mr %0, r1" : "=r" (*(p)))
35#elif defined(__aarch64__) && defined(__GNUC__)
36#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mov\t%0, sp" : "=r" (*(p)))
37#else
38NOINLINE(void rb_gc_set_stack_end(VALUE **stack_end_p));
39#define SET_MACHINE_STACK_END(p) rb_gc_set_stack_end(p)
40#define USE_CONSERVATIVE_STACK_END
41#endif
42
43/* for GC debug */
44
45#ifndef RUBY_MARK_FREE_DEBUG
46#define RUBY_MARK_FREE_DEBUG 0
47#endif
48
49#if RUBY_MARK_FREE_DEBUG
50extern int ruby_gc_debug_indent;
51
52static inline void
53rb_gc_debug_indent(void)
54{
55 ruby_debug_printf("%*s", ruby_gc_debug_indent, "");
56}
57
58static inline void
59rb_gc_debug_body(const char *mode, const char *msg, int st, void *ptr)
60{
61 if (st == 0) {
62 ruby_gc_debug_indent--;
63 }
64 rb_gc_debug_indent();
65 ruby_debug_printf("%s: %s %s (%p)\n", mode, st ? "->" : "<-", msg, ptr);
66
67 if (st) {
68 ruby_gc_debug_indent++;
69 }
70
71 fflush(stdout);
72}
73
74#define RUBY_MARK_ENTER(msg) rb_gc_debug_body("mark", (msg), 1, ptr)
75#define RUBY_MARK_LEAVE(msg) rb_gc_debug_body("mark", (msg), 0, ptr)
76#define RUBY_FREE_ENTER(msg) rb_gc_debug_body("free", (msg), 1, ptr)
77#define RUBY_FREE_LEAVE(msg) rb_gc_debug_body("free", (msg), 0, ptr)
78#define RUBY_GC_INFO rb_gc_debug_indent(), ruby_debug_printf
79
80#else
81#define RUBY_MARK_ENTER(msg)
82#define RUBY_MARK_LEAVE(msg)
83#define RUBY_FREE_ENTER(msg)
84#define RUBY_FREE_LEAVE(msg)
85#define RUBY_GC_INFO if(0)printf
86#endif
87
88#if STACK_GROW_DIRECTION > 0
89# define STACK_UPPER(x, a, b) (a)
90#elif STACK_GROW_DIRECTION < 0
91# define STACK_UPPER(x, a, b) (b)
92#else
93RUBY_EXTERN int ruby_stack_grow_direction;
94int ruby_get_stack_grow_direction(volatile VALUE *addr);
95# define stack_growup_p(x) ( \
96 (ruby_stack_grow_direction ? \
97 ruby_stack_grow_direction : \
98 ruby_get_stack_grow_direction(x)) > 0)
99# define STACK_UPPER(x, a, b) (stack_growup_p(x) ? (a) : (b))
100#endif
101
102/*
103 STACK_GROW_DIR_DETECTION is used with STACK_DIR_UPPER.
104
105 On most normal systems, stacks grow from high address to lower address. In
106 this case, STACK_DIR_UPPER(a, b) will return (b), but on exotic systems where
107 the stack grows UP (from low address to high address), it will return (a).
108*/
109
110#if STACK_GROW_DIRECTION
111#define STACK_GROW_DIR_DETECTION
112#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, (a), (b))
113#else
114#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection
115#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, (a), (b))
116#endif
117#define IS_STACK_DIR_UPPER() STACK_DIR_UPPER(1,0)
118
119const char *rb_obj_info(VALUE obj);
120const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);
121
122struct rb_execution_context_struct; /* in vm_core.h */
123struct rb_objspace; /* in vm_core.h */
124struct rb_ractor_struct; /* in vm_core.h */
125
126#define EC_NEWOBJ_OF(var, T, c, f, s, ec) \
127 T *(var) = (T *)rb_ec_newobj_of((ec), (c), (f), s)
128#define NEWOBJ_OF(var, T, c, f, s) EC_NEWOBJ_OF(var, T, c, f, s, GET_EC())
129#define UNPROTECTED_NEWOBJ_OF(var, T, c, f, s) \
130 T *(var) = (T *)rb_newobj((GET_EC()), (c), (f), ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER, false, s)
131
132#ifndef RB_GC_OBJECT_METADATA_ENTRY_DEFINED
133# define RB_GC_OBJECT_METADATA_ENTRY_DEFINED
135 ID name;
136 VALUE val;
137};
138#endif
139
140#ifndef USE_UNALIGNED_MEMBER_ACCESS
141# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
142#elif ! USE_UNALIGNED_MEMBER_ACCESS
143# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
144#elif ! (__has_warning("-Waddress-of-packed-member") || GCC_VERSION_SINCE(9, 0, 0))
145# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
146#else
147# include "internal/warnings.h"
148# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
149 COMPILER_WARNING_PUSH; \
150 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
151 __typeof__(expr) unaligned_member_access_result = (expr); \
152 COMPILER_WARNING_POP; \
153 unaligned_member_access_result; \
154})
155
156# define UNALIGNED_MEMBER_PTR(ptr, mem) __extension__({ \
157 COMPILER_WARNING_PUSH; \
158 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
159 const volatile void *unaligned_member_ptr_result = &(ptr)->mem; \
160 COMPILER_WARNING_POP; \
161 (__typeof__((ptr)->mem) *)unaligned_member_ptr_result; \
162})
163#endif
164
165#ifndef UNALIGNED_MEMBER_PTR
166# define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
167#endif
168
169#define RB_OBJ_WRITE_UNALIGNED(old, slot, young) do { \
170 VALUE *_slot = UNALIGNED_MEMBER_ACCESS(slot); \
171 RB_OBJ_WRITE(old, _slot, young); \
172} while (0)
173
174/* Used in places that could malloc during, which can cause the GC to run. We
175 * need to temporarily disable the GC to allow the malloc to happen.
176 * Allocating memory during GC is a bad idea, so use this only when absolutely
177 * necessary. */
178/* Only re-entrant GC of the current objspace needs suppressing (the malloc happens inside this
179 * Ractor), so use the local disable. The during-GC malloc guard reads the per-objspace dont_gc
180 * flag rather than a process-wide one. */
181#define DURING_GC_COULD_MALLOC_REGION_START() \
182 assert(rb_during_gc()); \
183 VALUE _already_disabled = rb_gc_local_disable_no_rest()
184
185#define DURING_GC_COULD_MALLOC_REGION_END() \
186 if (_already_disabled == Qfalse) rb_gc_local_enable()
187
188/* gc.c */
189RUBY_ATTR_MALLOC void *ruby_mimmalloc(size_t size);
190RUBY_ATTR_MALLOC void *ruby_mimcalloc(size_t num, size_t size);
191void ruby_mimfree(void *ptr);
192void rb_gc_prepare_heap(void);
193void rb_objspace_set_event_hook(const rb_event_flag_t event);
194VALUE rb_objspace_gc_enable(void *objspace);
195VALUE rb_objspace_gc_disable(void *objspace);
196void ruby_gc_set_params(void);
197void rb_gc_copy_attributes(VALUE dest, VALUE obj);
198size_t rb_size_mul_or_raise(size_t, size_t, VALUE); /* used in compile.c */
199size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE); /* used in iseq.h */
200size_t rb_malloc_grow_capa(size_t current_capacity, size_t type_size);
201RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add(size_t, size_t, size_t);
202RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add(size_t, size_t, size_t);
203void *rb_xrealloc_mul_add(const void *, size_t, size_t, size_t);
204RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t);
205RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
206void rb_gc_register_pinning_obj(VALUE obj);
207struct rb_execution_context_struct *rb_gc_get_ec(void);
208VALUE rb_newobj_of_unprotected(VALUE klass, VALUE flags, size_t size);
209
210void *rb_gc_ractor_cache_alloc(struct rb_ractor_struct *ractor);
211void rb_gc_ractor_cache_free(void *cache);
212bool rb_gc_zjit_new_obj_fastpath(size_t alloc_size, VALUE flags, VALUE klass, struct rb_gc_zjit_fastpath *fastpath);
213
214bool rb_gc_size_allocatable_p(size_t size);
215size_t rb_gc_size_slot_size(size_t size);
216size_t rb_gc_max_allocation_size(void);
217
218void rb_gc_mark_and_move(VALUE *ptr);
219
220void rb_gc_ref_update_table_values_only(st_table *tbl);
221
222void rb_gc_initial_stress_set(VALUE flag);
223
224void rb_gc_before_fork(void);
225void rb_gc_after_fork(rb_pid_t pid);
226
227#define rb_gc_mark_and_move_ptr(ptr) do { \
228 VALUE _obj = (VALUE)*(ptr); \
229 rb_gc_mark_and_move(&_obj); \
230 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
231} while (0)
232
233#define rb_gc_update_moved_ptr(ptr) do { \
234 VALUE _obj = (VALUE)*(ptr); \
235 rb_gc_update_moved(&_obj); \
236 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
237} while (0)
238
239RUBY_SYMBOL_EXPORT_BEGIN
240/* exports for objspace module */
241void rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data);
242void rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *data);
243int rb_objspace_internal_object_p(VALUE obj);
244int rb_objspace_garbage_object_p(VALUE obj);
245int rb_objspace_foreign_object_p(VALUE obj);
246void rb_gc_declare_weak_references(VALUE obj);
247bool rb_gc_handle_weak_references_alive_p(VALUE obj);
248
249void rb_objspace_each_objects(
250 int (*callback)(void *start, void *end, size_t stride, void *data),
251 void *data);
252
253
254size_t rb_gc_obj_slot_size(VALUE obj);
255
256VALUE rb_gc_disable_no_rest(void);
257/* Local GC disable/enable covering only the current Ractor's objspace. Unlike rb_gc_disable*,
258 * which became process-wide, this suppresses GC in one's own objspace only. Exported because
259 * DURING_GC_COULD_MALLOC_REGION above expands in bundled extensions. */
260VALUE rb_gc_local_enable(void);
261VALUE rb_gc_local_disable_no_rest(void);
262
263#define RB_GC_MAX_NAME_LEN 20
264
265/* gc.c (export) */
266const char *rb_objspace_data_type_name(VALUE obj);
267VALUE rb_newobj(struct rb_execution_context_struct *, VALUE, VALUE, uint32_t /* shape_id_t */, bool, size_t);
268VALUE rb_newobj_of(VALUE, VALUE, size_t);
269VALUE rb_ec_newobj_of(struct rb_execution_context_struct *, VALUE, VALUE, size_t);
270size_t rb_obj_memsize_of(VALUE);
271struct rb_gc_object_metadata_entry *rb_gc_object_metadata(VALUE obj);
272void rb_gc_mark_values(long n, const VALUE *values);
273void rb_gc_mark_vm_stack_values(long n, const VALUE *values);
274void rb_gc_update_values(long n, VALUE *values);
275void rb_gc_mark_set_no_pin(st_table *);
276void rb_gc_update_set_refs(st_table *);
277
278#if USE_MODULAR_GC
279const char *rb_gc_active_gc_name(void);
280int rb_gc_modular_gc_loaded_p(void);
281#endif
282
283RUBY_SYMBOL_EXPORT_END
284
285static inline VALUE
286rb_obj_atomic_write(
287 VALUE a, VALUE *slot, VALUE b,
289 const char *filename,
291 int line)
292{
293#ifdef RGENGC_LOGGING_WRITE
294 RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
295#endif
296
297 RUBY_ATOMIC_VALUE_SET(*slot, b);
298
299 rb_obj_written(a, RUBY_Qundef /* ignore `oldv' now */, b, filename, line);
300 return a;
301}
302#define RB_OBJ_ATOMIC_WRITE(old, slot, young) \
303 RBIMPL_CAST(rb_obj_atomic_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
304
305int rb_ec_stack_check(struct rb_execution_context_struct *ec);
306void rb_gc_writebarrier_remember(VALUE obj);
307void rb_gc_obj_became_shareable(VALUE obj);
308bool rb_gc_multi_objspace_p(void);
309bool rb_gc_obj_foreign_p(VALUE obj);
310void *rb_gc_objspace_alloc(void);
311void rb_gc_objspace_retire_gc(void);
312/* Build an object, or suppress GC, in a named objspace rather than the current
313 * Ractor's. Only create_ractor_alloc_thread() needs these. */
314VALUE rb_data_typed_object_wrap_in_objspace(void *objspace, VALUE klass, void *datap, const rb_data_type_t *type);
315VALUE rb_data_typed_object_zalloc_in_objspace(void *objspace, VALUE klass, size_t size, const rb_data_type_t *type);
316VALUE rb_gc_objspace_disable_no_rest(void *objspace);
317VALUE rb_gc_objspace_enable(void *objspace);
318
319void rb_gc_objspace_retire(void **objspace_slot);
320void rb_gc_objspace_postmortem_self(void);
321void rb_gc_objspace_absorb_into_current(void **objspace_slot);
322void rb_gc_objspace_absorb_all_zombies(void);
323void rb_gc_objspace_disown(void *objspace);
324void rb_gc_zombie_objspaces_atfork(void);
325void rb_gc_disable_holders_atfork(void);
326void rb_gc_atfork_global_locks(void);
327void rb_gc_stash_cleanup_objspace(void);
328void rb_gc_rest(void);
329bool rb_gc_during_global_gc_p(void);
330bool rb_gc_single_objspace_p(void);
331const char *rb_obj_info(VALUE obj);
332void ruby_annotate_mmap(const void *addr, unsigned long size, const char *name);
333
334# define SIZED_REALLOC_N(v, T, m, n) \
335 ((v) = (T *)ruby_xrealloc2_sized((void *)(v), (m), sizeof(T), (n)))
336
337# define SIZED_FREE(v) ruby_xfree_sized((void *)(v), sizeof(*(v)))
338# define SIZED_FREE_N(v, n) ruby_xfree_sized((void *)(v), sizeof(*(v)) * (n))
339
340static inline void *
341ruby_sized_realloc_n(void *ptr, size_t new_count, size_t element_size, size_t old_count)
342{
343 return ruby_xrealloc2_sized(ptr, new_count, element_size, old_count);
344}
345
346void rb_gc_verify_shareable(VALUE);
347bool rb_gc_checking_shareable(void);
348
349#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