Ruby: gc.h Source File

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

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