Ruby: gc.h Source File

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

gc.h
1#ifndef RUBY_GC_H
2#define RUBY_GC_H 1
3#include "ruby/ruby.h"
4
5#if defined(__x86_64__) && !defined(_ILP32) && defined(__GNUC__)
6#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movq\t%%rsp, %0" : "=r" (*(p)))
7#elif defined(__i386) && defined(__GNUC__)
8#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("movl\t%%esp, %0" : "=r" (*(p)))
9#elif (defined(__powerpc__) || defined(__powerpc64__)) && defined(__GNUC__) && !defined(_AIX) && !defined(__APPLE__) // Not Apple is NEEDED to unbreak ppc64 build on Darwin. Don't ask.
10#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mr\t%0, %%r1" : "=r" (*(p)))
11#elif (defined(__powerpc__) || defined(__powerpc64__)) && defined(__GNUC__) && defined(_AIX)
12#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mr %0,1" : "=r" (*(p)))
13#elif defined(__POWERPC__) && defined(__APPLE__) // Darwin ppc and ppc64
14#define SET_MACHINE_STACK_END(p) __asm__ volatile("mr %0, r1" : "=r" (*(p)))
15#elif defined(__aarch64__) && defined(__GNUC__)
16#define SET_MACHINE_STACK_END(p) __asm__ __volatile__ ("mov\t%0, sp" : "=r" (*(p)))
17#else
18NOINLINE(void rb_gc_set_stack_end(VALUE **stack_end_p));
19#define SET_MACHINE_STACK_END(p) rb_gc_set_stack_end(p)
20#define USE_CONSERVATIVE_STACK_END
21#endif
22
23#define RB_GC_SAVE_MACHINE_CONTEXT(th) \
24 do { \
25 FLUSH_REGISTER_WINDOWS; \
26 setjmp((th)->ec->machine.regs); \
27 SET_MACHINE_STACK_END(&(th)->ec->machine.stack_end); \
28 } while (0)
29
30/* for GC debug */
31
32#ifndef RUBY_MARK_FREE_DEBUG
33#define RUBY_MARK_FREE_DEBUG 0
34#endif
35
36#if RUBY_MARK_FREE_DEBUG
37extern int ruby_gc_debug_indent;
38
39static inline void
40rb_gc_debug_indent(void)
41{
42 ruby_debug_printf("%*s", ruby_gc_debug_indent, "");
43}
44
45static inline void
46rb_gc_debug_body(const char *mode, const char *msg, int st, void *ptr)
47{
48 if (st == 0) {
49 ruby_gc_debug_indent--;
50 }
51 rb_gc_debug_indent();
52 ruby_debug_printf("%s: %s %s (%p)\n", mode, st ? "->" : "<-", msg, ptr);
53
54 if (st) {
55 ruby_gc_debug_indent++;
56 }
57
58 fflush(stdout);
59}
60
61#define RUBY_MARK_ENTER(msg) rb_gc_debug_body("mark", (msg), 1, ptr)
62#define RUBY_MARK_LEAVE(msg) rb_gc_debug_body("mark", (msg), 0, ptr)
63#define RUBY_FREE_ENTER(msg) rb_gc_debug_body("free", (msg), 1, ptr)
64#define RUBY_FREE_LEAVE(msg) rb_gc_debug_body("free", (msg), 0, ptr)
65#define RUBY_GC_INFO rb_gc_debug_indent(), ruby_debug_printf
66
67#else
68#define RUBY_MARK_ENTER(msg)
69#define RUBY_MARK_LEAVE(msg)
70#define RUBY_FREE_ENTER(msg)
71#define RUBY_FREE_LEAVE(msg)
72#define RUBY_GC_INFO if(0)printf
73#endif
74
75#define RUBY_MARK_MOVABLE_UNLESS_NULL(ptr) do { \
76 VALUE markobj = (ptr); \
77 if (RTEST(markobj)) {rb_gc_mark_movable(markobj);} \
78} while (0)
79#define RUBY_MARK_UNLESS_NULL(ptr) do { \
80 VALUE markobj = (ptr); \
81 if (RTEST(markobj)) {rb_gc_mark(markobj);} \
82} while (0)
83#define RUBY_FREE_UNLESS_NULL(ptr) if(ptr){ruby_xfree(ptr);(ptr)=NULL;}
84
85#if STACK_GROW_DIRECTION > 0
86# define STACK_UPPER(x, a, b) (a)
87#elif STACK_GROW_DIRECTION < 0
88# define STACK_UPPER(x, a, b) (b)
89#else
90 RUBY_EXTERN int ruby_stack_grow_direction;
91int ruby_get_stack_grow_direction(volatile VALUE *addr);
92# define stack_growup_p(x) ( \
93 (ruby_stack_grow_direction ? \
94 ruby_stack_grow_direction : \
95 ruby_get_stack_grow_direction(x)) > 0)
96# define STACK_UPPER(x, a, b) (stack_growup_p(x) ? (a) : (b))
97#endif
98
99/*
100 STACK_GROW_DIR_DETECTION is used with STACK_DIR_UPPER.
101
102 On most normal systems, stacks grow from high address to lower address. In
103 this case, STACK_DIR_UPPER(a, b) will return (b), but on exotic systems where
104 the stack grows UP (from low address to high address), it will return (a).
105*/
106
107#if STACK_GROW_DIRECTION
108#define STACK_GROW_DIR_DETECTION
109#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, (a), (b))
110#else
111#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection
112#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, (a), (b))
113#endif
114#define IS_STACK_DIR_UPPER() STACK_DIR_UPPER(1,0)
115
116const char *rb_obj_info(VALUE obj);
117const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);
118
119struct rb_thread_struct;
120
121size_t rb_size_pool_slot_size(unsigned char pool_id);
122
123RUBY_SYMBOL_EXPORT_BEGIN
124
125/* exports for objspace module */
126size_t rb_objspace_data_type_memsize(VALUE obj);
127void rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data);
128void rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *data);
129int rb_objspace_markable_object_p(VALUE obj);
130int rb_objspace_internal_object_p(VALUE obj);
131int rb_objspace_marked_object_p(VALUE obj);
132
133void rb_objspace_each_objects(
134 int (*callback)(void *start, void *end, size_t stride, void *data),
135 void *data);
136
137void rb_objspace_each_objects_without_setup(
138 int (*callback)(void *, void *, size_t, void *),
139 void *data);
140
141size_t rb_gc_obj_slot_size(VALUE obj);
142
143 VALUE rb_gc_disable_no_rest(void);
144
145RUBY_SYMBOL_EXPORT_END
146
147#endif /* RUBY_GC_H */
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:47
ruby.h
rb_thread_struct
Definition vm_core.h:984
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40