Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
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#if USE_RVARGC
71# define SIZE_POOL_COUNT 4
72#else
73# define SIZE_POOL_COUNT 1
74#endif
75
76typedef struct ractor_newobj_size_pool_cache {
77 struct RVALUE *freelist;
78 struct heap_page *using_page;
80
81typedef struct ractor_newobj_cache {
82 rb_ractor_newobj_size_pool_cache_t size_pool_caches[SIZE_POOL_COUNT];
84
85/* gc.c */
86extern VALUE *ruby_initial_gc_stress_ptr;
87extern int ruby_disable_gc;
88RUBY_ATTR_MALLOC void *ruby_mimmalloc(size_t size);
89void ruby_mimfree(void *ptr);
90void rb_objspace_set_event_hook(const rb_event_flag_t event);
91VALUE rb_objspace_gc_enable(struct rb_objspace *);
92VALUE rb_objspace_gc_disable(struct rb_objspace *);
93void ruby_gc_set_params(void);
94void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj);
95#if __has_attribute(alloc_align)
96__attribute__((__alloc_align__(1)))
97#endif
98RUBY_ATTR_MALLOC void *rb_aligned_malloc(size_t, size_t) RUBY_ATTR_ALLOC_SIZE((2));
99size_t rb_size_mul_or_raise(size_t, size_t, VALUE); /* used in compile.c */
100size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE); /* used in iseq.h */
101RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add(size_t, size_t, size_t);
102void *rb_xrealloc_mul_add(const void *, size_t, size_t, size_t);
103RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t);
104RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
105static 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));
106static 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));
107static inline void ruby_sized_xfree_inlined(void *ptr, size_t size);
108VALUE rb_class_allocate_instance(VALUE klass);
109void rb_gc_ractor_newobj_cache_clear(rb_ractor_newobj_cache_t *newobj_cache);
110size_t rb_gc_obj_slot_size(VALUE obj);
111bool rb_gc_size_allocatable_p(size_t size);
112int rb_objspace_garbage_object_p(VALUE obj);
113
114RUBY_SYMBOL_EXPORT_BEGIN
115/* gc.c (export) */
116const char *rb_objspace_data_type_name(VALUE obj);
117VALUE rb_wb_protected_newobj_of(VALUE, VALUE, size_t);
118VALUE rb_wb_unprotected_newobj_of(VALUE, VALUE, size_t);
119VALUE rb_ec_wb_protected_newobj_of(struct rb_execution_context_struct *ec, VALUE klass, VALUE flags, size_t);
120size_t rb_obj_memsize_of(VALUE);
121void rb_gc_verify_internal_consistency(void);
122size_t rb_obj_gc_flags(VALUE, ID[], size_t);
123void rb_gc_mark_values(long n, const VALUE *values);
124void rb_gc_mark_vm_stack_values(long n, const VALUE *values);
125void *ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
126void *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));
127void ruby_sized_xfree(void *x, size_t size);
128RUBY_SYMBOL_EXPORT_END
129
130MJIT_SYMBOL_EXPORT_BEGIN
131int rb_ec_stack_check(struct rb_execution_context_struct *ec);
132void rb_gc_writebarrier_remember(VALUE obj);
133const char *rb_obj_info(VALUE obj);
134MJIT_SYMBOL_EXPORT_END
135
136#if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE) || defined(_WIN32)
137
138static inline void *
139ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
140{
141 return ruby_xrealloc(ptr, new_size);
142}
143
144static inline void *
145ruby_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
150static inline void
151ruby_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
160static inline void *
161ruby_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
166static inline void *
167ruby_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
172static inline void
173ruby_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 */
Definition: gc.c:572
Definition: gc.c:885
void * ruby_xrealloc(void *ptr, size_t newsiz)
Resize the storage instance.
Definition: gc.c:13695
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:13705
void ruby_xfree(void *ptr)
Deallocates a storage instance.
Definition: gc.c:11772