11#include "ruby/internal/config.h"
15#include "internal/compilers.h"
17#include "ruby_atomic.h"
22# define USE_MODULAR_GC 0
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__)
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__)
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)))
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
45#ifndef RUBY_MARK_FREE_DEBUG
46#define RUBY_MARK_FREE_DEBUG 0
49#if RUBY_MARK_FREE_DEBUG
50extern int ruby_gc_debug_indent;
53rb_gc_debug_indent(
void)
55 ruby_debug_printf(
"%*s", ruby_gc_debug_indent,
"");
59rb_gc_debug_body(
const char *mode,
const char *msg,
int st,
void *ptr)
62 ruby_gc_debug_indent--;
65 ruby_debug_printf(
"%s: %s %s (%p)\n", mode, st ?
"->" :
"<-", msg, ptr);
68 ruby_gc_debug_indent++;
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
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
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)
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))
110#if STACK_GROW_DIRECTION
111#define STACK_GROW_DIR_DETECTION
112#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, (a), (b))
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))
117#define IS_STACK_DIR_UPPER() STACK_DIR_UPPER(1,0)
119const char *rb_obj_info(
VALUE obj);
120const char *rb_raw_obj_info(
char *
const buff,
const size_t buff_size,
VALUE obj);
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)
132#ifndef RB_GC_OBJECT_METADATA_ENTRY_DEFINED
133# define RB_GC_OBJECT_METADATA_ENTRY_DEFINED
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)
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; \
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; \
165#ifndef UNALIGNED_MEMBER_PTR
166# define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
169#define RB_OBJ_WRITE_UNALIGNED(old, slot, young) do { \
170 VALUE *_slot = UNALIGNED_MEMBER_ACCESS(slot); \
171 RB_OBJ_WRITE(old, _slot, young); \
181#define DURING_GC_COULD_MALLOC_REGION_START() \
182 assert(rb_during_gc()); \
183 VALUE _already_disabled = rb_gc_local_disable_no_rest()
185#define DURING_GC_COULD_MALLOC_REGION_END() \
186 if (_already_disabled == Qfalse) rb_gc_local_enable()
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);
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);
199size_t rb_size_mul_add_or_raise(
size_t,
size_t,
size_t,
VALUE);
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);
211void rb_gc_ractor_cache_free(
void *cache);
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);
218void rb_gc_mark_and_move(
VALUE *ptr);
220void rb_gc_ref_update_table_values_only(
st_table *tbl);
222void rb_gc_initial_stress_set(
VALUE flag);
224void rb_gc_before_fork(
void);
225void rb_gc_after_fork(rb_pid_t pid);
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; \
233#define rb_gc_move_ptr(ptr) do { \
234 VALUE _obj = rb_gc_location((VALUE)*(ptr)); \
235 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
238RUBY_SYMBOL_EXPORT_BEGIN
240void rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data);
241void rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *data);
242int rb_objspace_internal_object_p(
VALUE obj);
243int rb_objspace_garbage_object_p(
VALUE obj);
244int rb_objspace_foreign_object_p(
VALUE obj);
245void rb_gc_declare_weak_references(
VALUE obj);
246bool rb_gc_handle_weak_references_alive_p(
VALUE obj);
248void rb_objspace_each_objects(
249 int (*callback)(
void *start,
void *end,
size_t stride,
void *data),
253size_t rb_gc_obj_slot_size(
VALUE obj);
255VALUE rb_gc_disable_no_rest(
void);
259VALUE rb_gc_local_enable(
void);
260VALUE rb_gc_local_disable_no_rest(
void);
262#define RB_GC_MAX_NAME_LEN 20
265const char *rb_objspace_data_type_name(
VALUE obj);
269size_t rb_obj_memsize_of(
VALUE);
271void rb_gc_mark_values(
long n,
const VALUE *values);
272void rb_gc_mark_vm_stack_values(
long n,
const VALUE *values);
273void rb_gc_update_values(
long n,
VALUE *values);
274void rb_gc_mark_set_no_pin(
st_table *);
275void rb_gc_update_set_refs(
st_table *);
278const char *rb_gc_active_gc_name(
void);
279int rb_gc_modular_gc_loaded_p(
void);
282RUBY_SYMBOL_EXPORT_END
288 const char *filename,
292#ifdef RGENGC_LOGGING_WRITE
293 RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
298 rb_obj_written(a,
RUBY_Qundef , b, filename, line);
301#define RB_OBJ_ATOMIC_WRITE(old, slot, young) \
302 RBIMPL_CAST(rb_obj_atomic_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
305void rb_gc_writebarrier_remember(
VALUE obj);
306void rb_gc_obj_became_shareable(
VALUE obj);
307bool rb_gc_multi_objspace_p(
void);
308bool rb_gc_obj_foreign_p(
VALUE obj);
309void *rb_gc_objspace_alloc(
void);
310void rb_gc_objspace_retire_gc(
void);
311void rb_gc_objspace_retire(
void **objspace_slot);
312void rb_gc_objspace_postmortem_self(
void);
313void rb_gc_objspace_absorb_into_current(
void **objspace_slot);
314void rb_gc_objspace_absorb_all_zombies(
void);
315void rb_gc_objspace_disown(
void *
objspace);
316void rb_gc_zombie_objspaces_atfork(
void);
317void rb_gc_disable_holders_atfork(
void);
318void rb_gc_atfork_global_locks(
void);
319void rb_gc_stash_cleanup_objspace(
void);
320void rb_gc_rest(
void);
321bool rb_gc_during_global_gc_p(
void);
322bool rb_gc_single_objspace_p(
void);
323const char *rb_obj_info(
VALUE obj);
324void ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name);
326# define SIZED_REALLOC_N(v, T, m, n) \
327 ((v) = (T *)ruby_xrealloc2_sized((void *)(v), (m), sizeof(T), (n)))
329# define SIZED_FREE(v) ruby_xfree_sized((void *)(v), sizeof(*(v)))
330# define SIZED_FREE_N(v, n) ruby_xfree_sized((void *)(v), sizeof(*(v)) * (n))
333ruby_sized_realloc_n(
void *ptr,
size_t new_count,
size_t element_size,
size_t old_count)
335 return ruby_xrealloc2_sized(ptr, new_count, element_size, old_count);
338void rb_gc_verify_shareable(
VALUE);
339bool rb_gc_checking_shareable(
void);
#define RUBY_ATOMIC_VALUE_SET(var, val)
Identical to RUBY_ATOMIC_SET, except it expects its arguments are VALUE.
#define RUBY_EXTERN
Declaration of externally visible global variables.
uint32_t rb_event_flag_t
Represents event(s).
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
@ RUBY_Qundef
Represents so-called undef.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.