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