Ruby 4.0.0dev (2025-12-13 revision 71dd272506a31f28d216f26b0568028ed600f6ed)
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 "vm_core.h" /* for GET_EC() */
18
19#ifndef USE_MODULAR_GC
20# define USE_MODULAR_GC 0
21#endif
22
23#if defined(__x86_64__) && !defined(_ILP32) && defined(__GNUC__)
24#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movq\t%%rsp, %0" : "=r" (*(p)))
25#elif defined(__i386) && defined(__GNUC__)
26#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movl\t%%esp, %0" : "=r" (*(p)))
27#elif (defined(__powerpc__) || defined(__powerpc64__)) && defined(__GNUC__) && !defined(_AIX) && !defined(__APPLE__) // Not Apple is NEEDED to unbreak ppc64 build on Darwin. Don't ask.
28#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mr\t%0, %%r1" : "=r" (*(p)))
29#elif (defined(__powerpc__) || defined(__powerpc64__)) && defined(__GNUC__) && defined(_AIX)
30#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mr %0,1" : "=r" (*(p)))
31#elif defined(__POWERPC__) && defined(__APPLE__) // Darwin ppc and ppc64
32#define SET_MACHINE_STACK_END(p) __asm__ volatile("mr %0, r1" : "=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#define RUBY_FREE_UNLESS_NULL(ptr) if(ptr){ruby_xfree(ptr);(ptr)=NULL;}
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 */
124
125#define NEWOBJ_OF_WITH_SHAPE(var, T, c, f, shape_id, s, ec) \
126 T *(var) = (T *)(((f) & FL_WB_PROTECTED) ? \
127 rb_wb_protected_newobj_of((ec ? ec : GET_EC()), (c), (f) & ~FL_WB_PROTECTED, shape_id, s) : \
128 rb_wb_unprotected_newobj_of((c), (f), shape_id, s))
129
130#define NEWOBJ_OF(var, T, c, f, s, ec) NEWOBJ_OF_WITH_SHAPE(var, T, c, f, 0 /* ROOT_SHAPE_ID */, s, ec)
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#define DURING_GC_COULD_MALLOC_REGION_START() \
179 assert(rb_during_gc()); \
180 VALUE _already_disabled = rb_gc_disable_no_rest()
181
182#define DURING_GC_COULD_MALLOC_REGION_END() \
183 if (_already_disabled == Qfalse) rb_gc_enable()
184
185/* gc.c */
186RUBY_ATTR_MALLOC void *ruby_mimmalloc(size_t size);
187RUBY_ATTR_MALLOC void *ruby_mimcalloc(size_t num, size_t size);
188void ruby_mimfree(void *ptr);
189void rb_gc_prepare_heap(void);
190void rb_objspace_set_event_hook(const rb_event_flag_t event);
191VALUE rb_objspace_gc_enable(void *objspace);
192VALUE rb_objspace_gc_disable(void *objspace);
193void ruby_gc_set_params(void);
194void rb_gc_copy_attributes(VALUE dest, VALUE obj);
195size_t rb_size_mul_or_raise(size_t, size_t, VALUE); /* used in compile.c */
196size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE); /* used in iseq.h */
197size_t rb_malloc_grow_capa(size_t current_capacity, size_t type_size);
198RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add(size_t, size_t, size_t);
199RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add(size_t, size_t, size_t);
200void *rb_xrealloc_mul_add(const void *, size_t, size_t, size_t);
201RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t);
202RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
203static inline void *ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
204static inline void *ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2, 3));
205static inline void ruby_sized_xfree_inlined(void *ptr, size_t size);
206void rb_gc_obj_id_moved(VALUE obj);
207
208void *rb_gc_ractor_cache_alloc(rb_ractor_t *ractor);
209void rb_gc_ractor_cache_free(void *cache);
210
211bool rb_gc_size_allocatable_p(size_t size);
212size_t *rb_gc_heap_sizes(void);
213size_t rb_gc_heap_id_for_size(size_t size);
214
215void rb_gc_mark_and_move(VALUE *ptr);
216
217void rb_gc_mark_weak(VALUE *ptr);
218void rb_gc_remove_weak(VALUE parent_obj, 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
233RUBY_SYMBOL_EXPORT_BEGIN
234/* exports for objspace module */
235void rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data);
236void rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *data);
237int rb_objspace_internal_object_p(VALUE obj);
238int rb_objspace_garbage_object_p(VALUE obj);
239bool rb_gc_pointer_to_heap_p(VALUE obj);
240
241void rb_objspace_each_objects(
242 int (*callback)(void *start, void *end, size_t stride, void *data),
243 void *data);
244
245size_t rb_gc_obj_slot_size(VALUE obj);
246
247VALUE rb_gc_disable_no_rest(void);
248
249#define RB_GC_MAX_NAME_LEN 20
250
251/* gc.c (export) */
252const char *rb_objspace_data_type_name(VALUE obj);
253VALUE rb_wb_protected_newobj_of(struct rb_execution_context_struct *, VALUE, VALUE, uint32_t /* shape_id_t */, size_t);
254VALUE rb_wb_unprotected_newobj_of(VALUE, VALUE, uint32_t /* shape_id_t */, size_t);
255size_t rb_obj_memsize_of(VALUE);
256struct rb_gc_object_metadata_entry *rb_gc_object_metadata(VALUE obj);
257void rb_gc_mark_values(long n, const VALUE *values);
258void rb_gc_mark_vm_stack_values(long n, const VALUE *values);
259void rb_gc_update_values(long n, VALUE *values);
260void *ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
261void *ruby_sized_xrealloc2(void *ptr, size_t new_count, size_t element_size, size_t old_count) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2, 3));
262void ruby_sized_xfree(void *x, size_t size);
263
264const char *rb_gc_active_gc_name(void);
265int rb_gc_modular_gc_loaded_p(void);
266
267RUBY_SYMBOL_EXPORT_END
268
269static inline VALUE
270rb_obj_atomic_write(
271 VALUE a, VALUE *slot, VALUE b,
273 const char *filename,
275 int line)
276{
277#ifdef RGENGC_LOGGING_WRITE
278 RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
279#endif
280
281 RUBY_ATOMIC_VALUE_SET(*slot, b);
282
283 rb_obj_written(a, RUBY_Qundef /* ignore `oldv' now */, b, filename, line);
284 return a;
285}
286#define RB_OBJ_ATOMIC_WRITE(old, slot, young) \
287 RBIMPL_CAST(rb_obj_atomic_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
288
289int rb_ec_stack_check(struct rb_execution_context_struct *ec);
290void rb_gc_writebarrier_remember(VALUE obj);
291const char *rb_obj_info(VALUE obj);
292void ruby_annotate_mmap(const void *addr, unsigned long size, const char *name);
293
294#if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE) || defined(_WIN32)
295
296static inline void *
297ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
298{
299 return ruby_xrealloc(ptr, new_size);
300}
301
302static inline void *
303ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
304{
305 return ruby_xrealloc2(ptr, new_count, elemsiz);
306}
307
308static inline void
309ruby_sized_xfree_inlined(void *ptr, size_t size)
310{
311 ruby_xfree(ptr);
312}
313
314# define SIZED_REALLOC_N(x, y, z, w) REALLOC_N(x, y, z)
315
316static inline void *
317ruby_sized_realloc_n(void *ptr, size_t new_count, size_t element_size, size_t old_count)
318{
319 return ruby_xrealloc2(ptr, new_count, element_size);
320}
321
322#else
323
324static inline void *
325ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
326{
327 return ruby_sized_xrealloc(ptr, new_size, old_size);
328}
329
330static inline void *
331ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
332{
333 return ruby_sized_xrealloc2(ptr, new_count, elemsiz, old_count);
334}
335
336static inline void
337ruby_sized_xfree_inlined(void *ptr, size_t size)
338{
339 ruby_sized_xfree(ptr, size);
340}
341
342# define SIZED_REALLOC_N(v, T, m, n) \
343 ((v) = (T *)ruby_sized_xrealloc2((void *)(v), (m), sizeof(T), (n)))
344
345static inline void *
346ruby_sized_realloc_n(void *ptr, size_t new_count, size_t element_size, size_t old_count)
347{
348 return ruby_sized_xrealloc2(ptr, new_count, element_size, old_count);
349}
350
351#endif /* HAVE_MALLOC_USABLE_SIZE */
352
353#define ruby_sized_xrealloc ruby_sized_xrealloc_inlined
354#define ruby_sized_xrealloc2 ruby_sized_xrealloc2_inlined
355#define ruby_sized_xfree ruby_sized_xfree_inlined
356
357void rb_gc_verify_shareable(VALUE);
358bool rb_gc_checking_shareable(void);
359
360#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]]
@ RUBY_Qundef
Represents so-called undef.
Definition gc_impl.h:15
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