Ruby: internal/gc.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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
18struct rb_execution_context_struct; /* in vm_core.h */
19struct rb_objspace; /* in vm_core.h */
20
21#ifdef NEWOBJ_OF
22# undef NEWOBJ_OF
23# undef RB_NEWOBJ_OF
24# undef RB_OBJ_WRITE
25#endif
26
27#define RVALUE_SIZE (sizeof(struct RBasic) + sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX]))
28
29#define RB_RVARGC_NEWOBJ_OF(var, T, c, f, s) \
30 T *(var) = (T *)(((f) & FL_WB_PROTECTED) ? \
31 rb_wb_protected_newobj_of((c), (f) & ~FL_WB_PROTECTED, s) : \
32 rb_wb_unprotected_newobj_of((c), (f), s))
33
34#define RB_RVARGC_EC_NEWOBJ_OF(ec, var, T, c, f, s) \
35 T *(var) = (T *)(((f) & FL_WB_PROTECTED) ? \
36 rb_ec_wb_protected_newobj_of((ec), (c), (f) & ~FL_WB_PROTECTED, s) : \
37 rb_wb_unprotected_newobj_of((c), (f), s))
38
39/* optimized version of NEWOBJ() */
40#define RB_NEWOBJ_OF(var, T, c, f) RB_RVARGC_NEWOBJ_OF(var, T, c, f, RVALUE_SIZE)
41
42#define RB_EC_NEWOBJ_OF(ec, var, T, c, f) RB_RVARGC_EC_NEWOBJ_OF(ec, var, T, c, f, RVALUE_SIZE)
43
44#define NEWOBJ_OF(var, T, c, f) RB_NEWOBJ_OF((var), T, (c), (f))
45#define RVARGC_NEWOBJ_OF(var, T, c, f, s) RB_RVARGC_NEWOBJ_OF((var), T, (c), (f), (s))
46#define RB_OBJ_GC_FLAGS_MAX 6 /* used in ext/objspace */
47
48#ifndef USE_UNALIGNED_MEMBER_ACCESS
49# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
50#elif ! USE_UNALIGNED_MEMBER_ACCESS
51# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
52#elif ! (__has_warning("-Waddress-of-packed-member") || GCC_VERSION_SINCE(9, 0, 0))
53# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
54#else
55# include "internal/warnings.h"
56# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
57 COMPILER_WARNING_PUSH; \
58 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
59 __typeof__(expr) unaligned_member_access_result = (expr); \
60 COMPILER_WARNING_POP; \
61 unaligned_member_access_result; \
62})
63#endif
64
65#define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
66#define RB_OBJ_WRITE(a, slot, b) \
67 rb_obj_write((VALUE)(a), UNALIGNED_MEMBER_ACCESS((VALUE *)(slot)), \
68 (VALUE)(b), __FILE__, __LINE__)
69
70// We use SIZE_POOL_COUNT number of shape IDs for transitions out of different size pools
71// The next available shapd ID will be the SPECIAL_CONST_SHAPE_ID
72#if USE_RVARGC && (SIZEOF_UINT64_T == SIZEOF_VALUE)
73# define SIZE_POOL_COUNT 5
74#else
75# define SIZE_POOL_COUNT 1
76#endif
77
78#define RCLASS_EXT_EMBEDDED (SIZE_POOL_COUNT > 1)
79
80 typedef struct ractor_newobj_size_pool_cache {
81 struct RVALUE *freelist;
82 struct heap_page *using_page;
83} rb_ractor_newobj_size_pool_cache_t;
84
85 typedef struct ractor_newobj_cache {
86 size_t incremental_mark_step_allocated_slots;
87 rb_ractor_newobj_size_pool_cache_t size_pool_caches[SIZE_POOL_COUNT];
88} rb_ractor_newobj_cache_t;
89
90/* gc.c */
91extern VALUE *ruby_initial_gc_stress_ptr;
92extern int ruby_disable_gc;
93RUBY_ATTR_MALLOC void *ruby_mimmalloc(size_t size);
94void ruby_mimfree(void *ptr);
95void rb_objspace_set_event_hook(const rb_event_flag_t event);
96 VALUE rb_objspace_gc_enable(struct rb_objspace *);
97 VALUE rb_objspace_gc_disable(struct rb_objspace *);
98void ruby_gc_set_params(void);
99void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj);
100#if __has_attribute(alloc_align)
101 __attribute__((__alloc_align__(1)))
102#endif
103RUBY_ATTR_MALLOC void *rb_aligned_malloc(size_t, size_t) RUBY_ATTR_ALLOC_SIZE((2));
104size_t rb_size_mul_or_raise(size_t, size_t, VALUE); /* used in compile.c */
105size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE); /* used in iseq.h */
106RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add(size_t, size_t, size_t);
107RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add(size_t, size_t, size_t);
108void *rb_xrealloc_mul_add(const void *, size_t, size_t, size_t);
109RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t);
110RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
111static 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));
112static 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));
113static inline void ruby_sized_xfree_inlined(void *ptr, size_t size);
114 VALUE rb_class_allocate_instance(VALUE klass);
115void rb_gc_ractor_newobj_cache_clear(rb_ractor_newobj_cache_t *newobj_cache);
116size_t rb_gc_obj_slot_size(VALUE obj);
117bool rb_gc_size_allocatable_p(size_t size);
118int rb_objspace_garbage_object_p(VALUE obj);
119
120RUBY_SYMBOL_EXPORT_BEGIN
121/* gc.c (export) */
122const char *rb_objspace_data_type_name(VALUE obj);
123 VALUE rb_wb_protected_newobj_of(VALUE, VALUE, size_t);
124 VALUE rb_wb_unprotected_newobj_of(VALUE, VALUE, size_t);
125 VALUE rb_ec_wb_protected_newobj_of(struct rb_execution_context_struct *ec, VALUE klass, VALUE flags, size_t);
126size_t rb_obj_memsize_of(VALUE);
127void rb_gc_verify_internal_consistency(void);
128size_t rb_obj_gc_flags(VALUE, ID[], size_t);
129void rb_gc_mark_values(long n, const VALUE *values);
130void rb_gc_mark_vm_stack_values(long n, const VALUE *values);
131void *ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
132void *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));
133void ruby_sized_xfree(void *x, size_t size);
134RUBY_SYMBOL_EXPORT_END
135
136MJIT_SYMBOL_EXPORT_BEGIN
137int rb_ec_stack_check(struct rb_execution_context_struct *ec);
138void rb_gc_writebarrier_remember(VALUE obj);
139const char *rb_obj_info(VALUE obj);
140MJIT_SYMBOL_EXPORT_END
141
142#if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE) || defined(_WIN32)
143
144static inline void *
145ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
146{
147 return ruby_xrealloc(ptr, new_size);
148}
149
150static inline void *
151ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
152{
153 return ruby_xrealloc2(ptr, new_count, elemsiz);
154}
155
156static inline void
157ruby_sized_xfree_inlined(void *ptr, size_t size)
158{
159 ruby_xfree(ptr);
160}
161
162# define SIZED_REALLOC_N(x, y, z, w) REALLOC_N(x, y, z)
163
164#else
165
166static inline void *
167ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
168{
169 return ruby_sized_xrealloc(ptr, new_size, old_size);
170}
171
172static inline void *
173ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
174{
175 return ruby_sized_xrealloc2(ptr, new_count, elemsiz, old_count);
176}
177
178static inline void
179ruby_sized_xfree_inlined(void *ptr, size_t size)
180{
181 ruby_sized_xfree(ptr, size);
182}
183
184# define SIZED_REALLOC_N(v, T, m, n) \
185 ((v) = (T *)ruby_sized_xrealloc2((void *)(v), (m), sizeof(T), (n)))
186
187#endif /* HAVE_MALLOC_USABLE_SIZE */
188
189#define ruby_sized_xrealloc ruby_sized_xrealloc_inlined
190#define ruby_sized_xrealloc2 ruby_sized_xrealloc2_inlined
191#define ruby_sized_xfree ruby_sized_xfree_inlined
192#endif /* INTERNAL_GC_H */
rb_event_flag_t
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:103
ruby.h
RVALUE
Definition gc.c:582
heap_page
Definition gc.c:945
ractor_newobj_cache
Definition gc.h:85
ractor_newobj_size_pool_cache
Definition gc.h:80
rb_execution_context_struct
Definition vm_core.h:899
rb_objspace
Definition gc.c:731
__attribute__
Definition vm_insnhelper.c:2198
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40