11#include "ruby/internal/config.h"
15#include "internal/compilers.h"
20# define USE_MODULAR_GC 0
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__)
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__)
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)))
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
43#ifndef RUBY_MARK_FREE_DEBUG
44#define RUBY_MARK_FREE_DEBUG 0
47#if RUBY_MARK_FREE_DEBUG
48extern int ruby_gc_debug_indent;
51rb_gc_debug_indent(
void)
53 ruby_debug_printf(
"%*s", ruby_gc_debug_indent,
"");
57rb_gc_debug_body(
const char *mode,
const char *msg,
int st,
void *ptr)
60 ruby_gc_debug_indent--;
63 ruby_debug_printf(
"%s: %s %s (%p)\n", mode, st ?
"->" :
"<-", msg, ptr);
66 ruby_gc_debug_indent++;
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
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
86#define RUBY_FREE_UNLESS_NULL(ptr) if(ptr){ruby_xfree(ptr);(ptr)=NULL;}
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);
125#define NEWOBJ_OF(var, T, c, f, 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, s) : \
128 rb_wb_unprotected_newobj_of((c), (f), s))
130#define RB_OBJ_GC_FLAGS_MAX 6
132#ifndef USE_UNALIGNED_MEMBER_ACCESS
133# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
134#elif ! USE_UNALIGNED_MEMBER_ACCESS
135# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
136#elif ! (__has_warning("-Waddress-of-packed-member") || GCC_VERSION_SINCE(9, 0, 0))
137# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
139# include "internal/warnings.h"
140# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
141 COMPILER_WARNING_PUSH; \
142 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
143 __typeof__(expr) unaligned_member_access_result = (expr); \
144 COMPILER_WARNING_POP; \
145 unaligned_member_access_result; \
148# define UNALIGNED_MEMBER_PTR(ptr, mem) __extension__({ \
149 COMPILER_WARNING_PUSH; \
150 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
151 const volatile void *unaligned_member_ptr_result = &(ptr)->mem; \
152 COMPILER_WARNING_POP; \
153 (__typeof__((ptr)->mem) *)unaligned_member_ptr_result; \
157#ifndef UNALIGNED_MEMBER_PTR
158# define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
161#define RB_OBJ_WRITE_UNALIGNED(old, slot, young) do { \
162 VALUE *_slot = UNALIGNED_MEMBER_ACCESS(slot); \
163 RB_OBJ_WRITE(old, _slot, young); \
170#define DURING_GC_COULD_MALLOC_REGION_START() \
171 assert(rb_during_gc()); \
172 VALUE _already_disabled = rb_gc_disable_no_rest()
174#define DURING_GC_COULD_MALLOC_REGION_END() \
175 if (_already_disabled == Qfalse) rb_gc_enable()
178RUBY_ATTR_MALLOC
void *ruby_mimmalloc(
size_t size);
179RUBY_ATTR_MALLOC
void *ruby_mimcalloc(
size_t num,
size_t size);
180void ruby_mimfree(
void *ptr);
181void rb_gc_prepare_heap(
void);
185void ruby_gc_set_params(
void);
186void rb_gc_copy_attributes(
VALUE dest,
VALUE obj);
187size_t rb_size_mul_or_raise(
size_t,
size_t,
VALUE);
188size_t rb_size_mul_add_or_raise(
size_t,
size_t,
size_t,
VALUE);
189size_t rb_malloc_grow_capa(
size_t current_capacity,
size_t type_size);
190RUBY_ATTR_MALLOC
void *rb_xmalloc_mul_add(
size_t,
size_t,
size_t);
191RUBY_ATTR_MALLOC
void *rb_xcalloc_mul_add(
size_t,
size_t,
size_t);
192void *rb_xrealloc_mul_add(
const void *,
size_t,
size_t,
size_t);
193RUBY_ATTR_MALLOC
void *rb_xmalloc_mul_add_mul(
size_t,
size_t,
size_t,
size_t);
194RUBY_ATTR_MALLOC
void *rb_xcalloc_mul_add_mul(
size_t,
size_t,
size_t,
size_t);
195static 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));
196static 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));
197static inline void ruby_sized_xfree_inlined(
void *ptr,
size_t size);
199void *rb_gc_ractor_cache_alloc(
rb_ractor_t *ractor);
200void rb_gc_ractor_cache_free(
void *cache);
202bool rb_gc_size_allocatable_p(
size_t size);
203size_t *rb_gc_heap_sizes(
void);
204size_t rb_gc_heap_id_for_size(
size_t size);
206void rb_gc_mark_and_move(
VALUE *ptr);
208void rb_gc_mark_weak(
VALUE *ptr);
209void rb_gc_remove_weak(
VALUE parent_obj,
VALUE *ptr);
211void rb_gc_ref_update_table_values_only(
st_table *tbl);
213void rb_gc_initial_stress_set(
VALUE flag);
215void rb_gc_before_fork(
void);
216void rb_gc_after_fork(rb_pid_t pid);
218#define rb_gc_mark_and_move_ptr(ptr) do { \
219 VALUE _obj = (VALUE)*(ptr); \
220 rb_gc_mark_and_move(&_obj); \
221 if (_obj != (VALUE)*(ptr)) *(ptr) = (void *)_obj; \
224RUBY_SYMBOL_EXPORT_BEGIN
226void rb_objspace_reachable_objects_from(
VALUE obj,
void (func)(
VALUE,
void *),
void *data);
227void rb_objspace_reachable_objects_from_root(
void (func)(
const char *category,
VALUE,
void *),
void *data);
228int rb_objspace_internal_object_p(
VALUE obj);
229int rb_objspace_garbage_object_p(
VALUE obj);
230bool rb_gc_pointer_to_heap_p(
VALUE obj);
232void rb_objspace_each_objects(
233 int (*callback)(
void *start,
void *end,
size_t stride,
void *data),
236size_t rb_gc_obj_slot_size(
VALUE obj);
238VALUE rb_gc_disable_no_rest(
void);
240#define RB_GC_MAX_NAME_LEN 20
243const char *rb_objspace_data_type_name(
VALUE obj);
246size_t rb_obj_memsize_of(
VALUE);
247size_t rb_obj_gc_flags(
VALUE,
ID[],
size_t);
248void rb_gc_mark_values(
long n,
const VALUE *values);
249void rb_gc_mark_vm_stack_values(
long n,
const VALUE *values);
250void rb_gc_update_values(
long n,
VALUE *values);
251void *ruby_sized_xrealloc(
void *ptr,
size_t new_size,
size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
252void *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));
253void ruby_sized_xfree(
void *x,
size_t size);
255const char *rb_gc_active_gc_name(
void);
256int rb_gc_modular_gc_loaded_p(
void);
259void ruby_load_modular_gc_from_argv(
int argc,
char **argv);
261RUBY_SYMBOL_EXPORT_END
264void rb_gc_writebarrier_remember(
VALUE obj);
265const char *rb_obj_info(
VALUE obj);
266void ruby_annotate_mmap(
const void *addr,
unsigned long size,
const char *name);
268#if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE) || defined(_WIN32)
271ruby_sized_xrealloc_inlined(
void *ptr,
size_t new_size,
size_t old_size)
273 return ruby_xrealloc(ptr, new_size);
277ruby_sized_xrealloc2_inlined(
void *ptr,
size_t new_count,
size_t elemsiz,
size_t old_count)
279 return ruby_xrealloc2(ptr, new_count, elemsiz);
283ruby_sized_xfree_inlined(
void *ptr,
size_t size)
288# define SIZED_REALLOC_N(x, y, z, w) REALLOC_N(x, y, z)
291ruby_sized_realloc_n(
void *ptr,
size_t new_count,
size_t element_size,
size_t old_count)
293 return ruby_xrealloc2(ptr, new_count, element_size);
299ruby_sized_xrealloc_inlined(
void *ptr,
size_t new_size,
size_t old_size)
301 return ruby_sized_xrealloc(ptr, new_size, old_size);
305ruby_sized_xrealloc2_inlined(
void *ptr,
size_t new_count,
size_t elemsiz,
size_t old_count)
307 return ruby_sized_xrealloc2(ptr, new_count, elemsiz, old_count);
311ruby_sized_xfree_inlined(
void *ptr,
size_t size)
313 ruby_sized_xfree(ptr, size);
316# define SIZED_REALLOC_N(v, T, m, n) \
317 ((v) = (T *)ruby_sized_xrealloc2((void *)(v), (m), sizeof(T), (n)))
320ruby_sized_realloc_n(
void *ptr,
size_t new_count,
size_t element_size,
size_t old_count)
322 return ruby_sized_xrealloc2(ptr, new_count, element_size, old_count);
327#define ruby_sized_xrealloc ruby_sized_xrealloc_inlined
328#define ruby_sized_xrealloc2 ruby_sized_xrealloc2_inlined
329#define ruby_sized_xfree ruby_sized_xfree_inlined
#define RUBY_EXTERN
Declaration of externally visible global variables.
uint32_t rb_event_flag_t
Represents event(s).
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.