Ruby: internal/gc.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

gc.h
Go to the documentation of this file.
1#ifndef INTERNAL_GC_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_GC_H
12#include "ruby/internal/config.h"
13
14#include <stddef.h> /* for size_t */
15
16#include "internal/compilers.h" /* for __has_attribute */
17#include "ruby/ruby.h" /* for rb_event_flag_t */
18
19struct rb_execution_context_struct; /* in vm_core.h */
20struct rb_objspace; /* in vm_core.h */
21
22#ifdef NEWOBJ_OF
23# undef NEWOBJ_OF
24# undef RB_NEWOBJ_OF
25# undef RB_OBJ_WRITE
26#endif
27
28/* optimized version of NEWOBJ() */
29#define RB_NEWOBJ_OF(var, T, c, f) \
30 T *(var) = (T *)(((f) & FL_WB_PROTECTED) ? \
31 rb_wb_protected_newobj_of((c), (f) & ~FL_WB_PROTECTED) : \
32 rb_wb_unprotected_newobj_of((c), (f)))
33
34#define RB_EC_NEWOBJ_OF(ec, var, T, c, f) \
35 T *(var) = (T *)(((f) & FL_WB_PROTECTED) ? \
36 rb_ec_wb_protected_newobj_of((ec), (c), (f) & ~FL_WB_PROTECTED) : \
37 rb_wb_unprotected_newobj_of((c), (f)))
38
39#define NEWOBJ_OF(var, T, c, f) RB_NEWOBJ_OF((var), T, (c), (f))
40#define RB_OBJ_GC_FLAGS_MAX 6 /* used in ext/objspace */
41
42#ifndef USE_UNALIGNED_MEMBER_ACCESS
43# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
44#elif ! USE_UNALIGNED_MEMBER_ACCESS
45# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
46#elif ! (__has_warning("-Waddress-of-packed-member") || GCC_VERSION_SINCE(9, 0, 0))
47# define UNALIGNED_MEMBER_ACCESS(expr) (expr)
48#else
49# include "internal/warnings.h"
50# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
51 COMPILER_WARNING_PUSH; \
52 COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
53 __typeof__(expr) unaligned_member_access_result = (expr); \
54 COMPILER_WARNING_POP; \
55 unaligned_member_access_result; \
56})
57#endif
58
59#define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)
60#define RB_OBJ_WRITE(a, slot, b) \
61 rb_obj_write((VALUE)(a), UNALIGNED_MEMBER_ACCESS((VALUE *)(slot)), \
62 (VALUE)(b), __FILE__, __LINE__)
63
64 typedef struct ractor_newobj_cache {
65 struct RVALUE *freelist;
66 struct heap_page *using_page;
67} rb_ractor_newobj_cache_t;
68
69/* gc.c */
70extern VALUE *ruby_initial_gc_stress_ptr;
71extern int ruby_disable_gc;
72 RUBY_ATTR_MALLOC void *ruby_mimmalloc(size_t size);
73void ruby_mimfree(void *ptr);
74void rb_objspace_set_event_hook(const rb_event_flag_t event);
75 VALUE rb_objspace_gc_enable(struct rb_objspace *);
76 VALUE rb_objspace_gc_disable(struct rb_objspace *);
77void ruby_gc_set_params(void);
78void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj);
79#if __has_attribute(alloc_align)
80 __attribute__((__alloc_align__(1)))
81#endif
82 RUBY_ATTR_MALLOC void *rb_aligned_malloc(size_t, size_t) RUBY_ATTR_ALLOC_SIZE((2));
83size_t rb_size_mul_or_raise(size_t, size_t, VALUE); /* used in compile.c */
84size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE); /* used in iseq.h */
85 RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add(size_t, size_t, size_t);
86void *rb_xrealloc_mul_add(const void *, size_t, size_t, size_t);
87 RUBY_ATTR_MALLOC void *rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t);
88 RUBY_ATTR_MALLOC void *rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t);
89static 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));
90static 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));
91static inline void ruby_sized_xfree_inlined(void *ptr, size_t size);
92 VALUE rb_class_allocate_instance(VALUE klass);
93
94RUBY_SYMBOL_EXPORT_BEGIN
95/* gc.c (export) */
96const char *rb_objspace_data_type_name(VALUE obj);
97 VALUE rb_wb_protected_newobj_of(VALUE, VALUE);
98 VALUE rb_wb_unprotected_newobj_of(VALUE, VALUE);
99 VALUE rb_ec_wb_protected_newobj_of(struct rb_execution_context_struct *ec, VALUE klass, VALUE flags);
100size_t rb_obj_memsize_of(VALUE);
101void rb_gc_verify_internal_consistency(void);
102 size_t rb_obj_gc_flags(VALUE, ID[], size_t);
103void rb_gc_mark_values(long n, const VALUE *values);
104void rb_gc_mark_vm_stack_values(long n, const VALUE *values);
105void *ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size) RUBY_ATTR_RETURNS_NONNULL RUBY_ATTR_ALLOC_SIZE((2));
106 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));
107void ruby_sized_xfree(void *x, size_t size);
108void rb_gc_ractor_newobj_cache_clear(rb_ractor_newobj_cache_t *newobj_cache);
109RUBY_SYMBOL_EXPORT_END
110
111 MJIT_SYMBOL_EXPORT_BEGIN
112int rb_ec_stack_check(struct rb_execution_context_struct *ec);
113void rb_gc_writebarrier_remember(VALUE obj);
114const char *rb_obj_info(VALUE obj);
115 MJIT_SYMBOL_EXPORT_END
116
117#if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE) || defined(_WIN32)
118
119static inline void *
120ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
121{
122 return ruby_xrealloc(ptr, new_size);
123}
124
125static inline void *
126ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
127{
128 return ruby_xrealloc2(ptr, new_count, elemsiz);
129}
130
131static inline void
132ruby_sized_xfree_inlined(void *ptr, size_t size)
133{
134 ruby_xfree(ptr);
135}
136
137# define SIZED_REALLOC_N(x, y, z, w) REALLOC_N(x, y, z)
138
139#else
140
141static inline void *
142ruby_sized_xrealloc_inlined(void *ptr, size_t new_size, size_t old_size)
143{
144 return ruby_sized_xrealloc(ptr, new_size, old_size);
145}
146
147static inline void *
148ruby_sized_xrealloc2_inlined(void *ptr, size_t new_count, size_t elemsiz, size_t old_count)
149{
150 return ruby_sized_xrealloc2(ptr, new_count, elemsiz, old_count);
151}
152
153static inline void
154ruby_sized_xfree_inlined(void *ptr, size_t size)
155{
156 ruby_sized_xfree(ptr, size);
157}
158
159# define SIZED_REALLOC_N(v, T, m, n) \
160 ((v) = (T *)ruby_sized_xrealloc2((void *)(v), (m), sizeof(T), (n)))
161
162#endif /* HAVE_MALLOC_USABLE_SIZE */
163
164#define ruby_sized_xrealloc ruby_sized_xrealloc_inlined
165#define ruby_sized_xrealloc2 ruby_sized_xrealloc2_inlined
166#define ruby_sized_xfree ruby_sized_xfree_inlined
167#endif /* INTERNAL_GC_H */
RUBY_ATTR_MALLOC
#define RUBY_ATTR_MALLOC
Definition: attributes.h:120
RUBY_ATTR_ALLOC_SIZE
#define RUBY_ATTR_ALLOC_SIZE
Definition: attributes.h:117
RUBY_ATTR_RETURNS_NONNULL
#define RUBY_ATTR_RETURNS_NONNULL
Definition: attributes.h:123
compilers.h
Internal header absorbing C compipler differences.
ptr
struct RIMemo * ptr
Definition: debug.c:88
MJIT_SYMBOL_EXPORT_END
#define MJIT_SYMBOL_EXPORT_END
Definition: dllexport.h:63
MJIT_SYMBOL_EXPORT_BEGIN
#define MJIT_SYMBOL_EXPORT_BEGIN
Definition: dllexport.h:62
rb_event_flag_t
uint32_t rb_event_flag_t
Definition: event.h:66
ruby_xfree
void ruby_xfree(void *x)
Deallocates a storage instance.
Definition: gc.c:10914
ruby_xrealloc2
void * ruby_xrealloc2(void *ptr, size_t n, size_t new_size)
Identical to ruby_xrealloc(), except it resizes the given storage instance to newelems * newsiz bytes...
Definition: gc.c:12835
rb_aligned_malloc
void * rb_aligned_malloc(size_t alignment, size_t size)
Definition: gc.c:10344
ruby_xrealloc
void * ruby_xrealloc(void *ptr, size_t new_size)
Resize the storage instance.
Definition: gc.c:12825
__attribute__
void *PTR64 __attribute__((mode(DI)))
Definition: ffi.c:41
config.h
Thin wrapper to ruby/config.h.
rb_wb_unprotected_newobj_of
VALUE rb_wb_unprotected_newobj_of(VALUE, VALUE)
Definition: gc.c:2342
ruby_initial_gc_stress_ptr
VALUE * ruby_initial_gc_stress_ptr
Definition: gc.c:879
ruby_sized_xrealloc
#define ruby_sized_xrealloc
Definition: gc.h:164
rb_ractor_newobj_cache_t
struct ractor_newobj_cache rb_ractor_newobj_cache_t
ruby_sized_xrealloc2
#define ruby_sized_xrealloc2
Definition: gc.h:165
rb_obj_memsize_of
size_t rb_obj_memsize_of(VALUE)
Definition: gc.c:4296
rb_copy_wb_protected_attribute
void rb_copy_wb_protected_attribute(VALUE dest, VALUE obj)
Definition: gc.c:7876
ruby_mimfree
void ruby_mimfree(void *ptr)
Definition: gc.c:10979
rb_ec_stack_check
int rb_ec_stack_check(struct rb_execution_context_struct *ec)
Definition: gc.c:5546
rb_xrealloc_mul_add
void * rb_xrealloc_mul_add(const void *, size_t, size_t, size_t)
Definition: gc.c:10927
rb_size_mul_or_raise
size_t rb_size_mul_or_raise(size_t, size_t, VALUE)
Definition: gc.c:188
rb_obj_gc_flags
size_t rb_obj_gc_flags(VALUE, ID[], size_t)
rb_class_allocate_instance
VALUE rb_class_allocate_instance(VALUE klass)
Definition: gc.c:2482
rb_objspace_gc_disable
VALUE rb_objspace_gc_disable(struct rb_objspace *)
Definition: gc.c:9932
rb_objspace_data_type_name
const char * rb_objspace_data_type_name(VALUE obj)
Definition: gc.c:2534
rb_gc_ractor_newobj_cache_clear
void rb_gc_ractor_newobj_cache_clear(rb_ractor_newobj_cache_t *newobj_cache)
Definition: gc.c:7937
ruby_gc_set_params
void ruby_gc_set_params(void)
Definition: gc.c:10122
rb_objspace_set_event_hook
void rb_objspace_set_event_hook(const rb_event_flag_t event)
Definition: gc.c:2052
rb_objspace_gc_enable
VALUE rb_objspace_gc_enable(struct rb_objspace *)
Definition: gc.c:9895
rb_gc_verify_internal_consistency
void rb_gc_verify_internal_consistency(void)
Definition: gc.c:7137
rb_gc_mark_values
void rb_gc_mark_values(long n, const VALUE *values)
Definition: gc.c:5596
rb_xmalloc_mul_add_mul
RUBY_ATTR_MALLOC void * rb_xmalloc_mul_add_mul(size_t, size_t, size_t, size_t)
Definition: gc.c:10934
rb_obj_info
const char * rb_obj_info(VALUE obj)
Definition: gc.c:12499
ruby_disable_gc
int ruby_disable_gc
Definition: gc.c:982
rb_xcalloc_mul_add_mul
RUBY_ATTR_MALLOC void * rb_xcalloc_mul_add_mul(size_t, size_t, size_t, size_t)
Definition: gc.c:10941
rb_xmalloc_mul_add
RUBY_ATTR_MALLOC void * rb_xmalloc_mul_add(size_t, size_t, size_t)
Definition: gc.c:10920
rb_size_mul_add_or_raise
size_t rb_size_mul_add_or_raise(size_t, size_t, size_t, VALUE)
Definition: gc.c:215
rb_gc_writebarrier_remember
void rb_gc_writebarrier_remember(VALUE obj)
Definition: gc.c:7814
rb_ec_wb_protected_newobj_of
VALUE rb_ec_wb_protected_newobj_of(struct rb_execution_context_struct *ec, VALUE klass, VALUE flags)
Definition: gc.c:2356
rb_gc_mark_vm_stack_values
void rb_gc_mark_vm_stack_values(long n, const VALUE *values)
Definition: gc.c:5619
rb_wb_protected_newobj_of
VALUE rb_wb_protected_newobj_of(VALUE, VALUE)
Definition: gc.c:2349
ruby_sized_xfree
#define ruby_sized_xfree
Definition: gc.h:166
ruby_mimmalloc
RUBY_ATTR_MALLOC void * ruby_mimmalloc(size_t size)
Definition: gc.c:10951
size
voidpf void uLong size
Definition: ioapi.h:138
ruby.h
RVALUE
Definition: gc.c:557
heap_page
Definition: gc.c:825
heap_page::flags
struct heap_page::@93 flags
ractor_newobj_cache
Definition: gc.h:64
ractor_newobj_cache::using_page
struct heap_page * using_page
Definition: gc.h:66
ractor_newobj_cache::freelist
struct RVALUE * freelist
Definition: gc.h:65
rb_execution_context_struct
Definition: vm_core.h:854
rb_objspace
Definition: gc.c:666
VALUE
unsigned long VALUE
Definition: value.h:38
ID
unsigned long ID
Definition: value.h:39
warnings.h
Internal header to suppres / mandate warnings.