Ruby  3.1.4p223 (2023-03-30 revision HEAD)
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 
18 struct rb_execution_context_struct; /* in vm_core.h */
19 struct 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 #if USE_RVARGC
71 # define SIZE_POOL_COUNT 4
72 #else
73 # define SIZE_POOL_COUNT 1
74 #endif
75 
77  struct RVALUE *freelist;
78  struct heap_page *using_page;
80 
81 typedef struct ractor_newobj_cache {
82  rb_ractor_newobj_size_pool_cache_t size_pool_caches[SIZE_POOL_COUNT];
84 
85 /* gc.c */
86 extern VALUE *ruby_initial_gc_stress_ptr;
87 extern int ruby_disable_gc;
88 RUBY_ATTR_MALLOC void *ruby_mimmalloc(size_t size);
89 void ruby_mimfree(void *ptr);
90 void rb_objspace_set_event_hook(const rb_event_flag_t event);
91 VALUE rb_objspace_gc_enable(struct rb_objspace *);
92 VALUE rb_objspace_gc_disable(struct rb_objspace *);
93 void ruby_gc_set_params(void);
94 void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj);
95 #if __has_attribute(alloc_align)
96 __attribute__((__alloc_align__(1)))
97 #endif
98 RUBY_ATTR_MALLOC void *rb_aligned_malloc(size_t, size_t) RUBY_ATTR_ALLOC_SIZE((2));
99 size_t rb_size_mul_or_raise(size_t, size_t, VALUE); /* used in compile.c */
100 size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE); /* used in iseq.h */
101 RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add(size_t, size_t, size_t);
102 void *rb_xrealloc_mul_add(const void *, size_t, size_t, size_t);
103 RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t);
104 RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
105 static 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));
106 static 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));
107 static inline void ruby_sized_xfree_inlined(void *ptr, size_t size);
108 VALUE rb_class_allocate_instance(VALUE klass);
109 void rb_gc_ractor_newobj_cache_clear(rb_ractor_newobj_cache_t *newobj_cache);
110 size_t rb_gc_obj_slot_size(VALUE obj);
111 bool rb_gc_size_allocatable_p(size_t size);
112 int rb_objspace_garbage_object_p(VALUE obj);
113 
114 RUBY_SYMBOL_EXPORT_BEGIN
115 /* gc.c (export) */
116 const char *rb_objspace_data_type_name(VALUE obj);
117 VALUE rb_wb_protected_newobj_of(VALUE, VALUE, size_t);
118 VALUE rb_wb_unprotected_newobj_of(VALUE, VALUE, size_t);
119 VALUE rb_ec_wb_protected_newobj_of(struct rb_execution_context_struct *ec, VALUE klass, VALUE flags, size_t);
120 size_t rb_obj_memsize_of(VALUE);
121 void rb_gc_verify_internal_consistency(void);
122 size_t rb_obj_gc_flags(VALUE, ID[], size_t);
123 void rb_gc_mark_values(long n, const VALUE *values);
124 void rb_gc_mark_vm_stack_values(long n, const VALUE *values);
125 void *ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
126 void *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));
127 void ruby_sized_xfree(void *x, size_t size);
128 RUBY_SYMBOL_EXPORT_END
129 
130 MJIT_SYMBOL_EXPORT_BEGIN
131 int rb_ec_stack_check(struct rb_execution_context_struct *ec);
132 void rb_gc_writebarrier_remember(VALUE obj);
133 const char *rb_obj_info(VALUE obj);
134 MJIT_SYMBOL_EXPORT_END
135 
136 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE) || defined(_WIN32)
137 
138 static inline void *
139 ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
140 {
141  return ruby_xrealloc(ptr, new_size);
142 }
143 
144 static inline void *
145 ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
146 {
147  return ruby_xrealloc2(ptr, new_count, elemsiz);
148 }
149 
150 static inline void
151 ruby_sized_xfree_inlined(void *ptr, size_t size)
152 {
153  ruby_xfree(ptr);
154 }
155 
156 # define SIZED_REALLOC_N(x, y, z, w) REALLOC_N(x, y, z)
157 
158 #else
159 
160 static inline void *
161 ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
162 {
163  return ruby_sized_xrealloc(ptr, new_size, old_size);
164 }
165 
166 static inline void *
167 ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
168 {
169  return ruby_sized_xrealloc2(ptr, new_count, elemsiz, old_count);
170 }
171 
172 static inline void
173 ruby_sized_xfree_inlined(void *ptr, size_t size)
174 {
175  ruby_sized_xfree(ptr, size);
176 }
177 
178 # define SIZED_REALLOC_N(v, T, m, n) \
179  ((v) = (T *)ruby_sized_xrealloc2((void *)(v), (m), sizeof(T), (n)))
180 
181 #endif /* HAVE_MALLOC_USABLE_SIZE */
182 
183 #define ruby_sized_xrealloc ruby_sized_xrealloc_inlined
184 #define ruby_sized_xrealloc2 ruby_sized_xrealloc2_inlined
185 #define ruby_sized_xfree ruby_sized_xfree_inlined
186 #endif /* INTERNAL_GC_H */
uint32_t rb_event_flag_t
Represents event(s).
Definition: event.h:103
Definition: gc.c:572
Definition: gc.c:885
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
void ruby_xfree(void *ptr)
Deallocates a storage instance.
Definition: gc.c:11775
void * ruby_xrealloc(void *ptr, size_t newsiz)
Resize the storage instance.
Definition: gc.c:13734
void * ruby_xrealloc2(void *ptr, size_t newelems, size_t newsiz)
Identical to ruby_xrealloc(), except it resizes the given storage instance to newelems * newsiz bytes...
Definition: gc.c:13744