Ruby: internal/imemo.h Source File

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

imemo.h
Go to the documentation of this file.
1#ifndef INTERNAL_IMEMO_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_IMEMO_H
12#include "ruby/internal/config.h"
13#include <stddef.h> /* for size_t */
14#include "internal/array.h" /* for rb_ary_tmp_new_fill */
15#include "internal/gc.h" /* for RB_OBJ_WRITE */
16#include "ruby/internal/stdbool.h" /* for bool */
17#include "ruby/ruby.h" /* for rb_block_call_func_t */
18
19#ifndef IMEMO_DEBUG
20# define IMEMO_DEBUG 0
21#endif
22
23#define IMEMO_MASK 0x0f
24
25/* FL_USER0 to FL_USER3 is for type */
26#define IMEMO_FL_USHIFT (FL_USHIFT + 4)
27#define IMEMO_FL_USER0 FL_USER4
28#define IMEMO_FL_USER1 FL_USER5
29#define IMEMO_FL_USER2 FL_USER6
30#define IMEMO_FL_USER3 FL_USER7
31#define IMEMO_FL_USER4 FL_USER8
32#define IMEMO_FL_USER5 FL_USER9
33
34 enum imemo_type {
35 imemo_env = 0,
36 imemo_cref = 1,
37 imemo_svar = 2,
38 imemo_throw_data = 3,
39 imemo_ifunc = 4,
40 imemo_memo = 5,
41 imemo_ment = 6,
42 imemo_iseq = 7,
43 imemo_tmpbuf = 8,
44 imemo_ast = 9,
45 imemo_parser_strterm = 10,
46 imemo_callinfo = 11,
47 imemo_callcache = 12,
48 imemo_constcache = 13,
49};
50
51/* CREF (Class REFerence) is defined in method.h */
52
54 struct vm_svar {
55 VALUE flags;
56 const VALUE cref_or_me;
57 const VALUE lastline;
58 const VALUE backref;
59 const VALUE others;
60};
61
63 struct vm_throw_data {
64 VALUE flags;
65 VALUE reserved;
66 const VALUE throw_obj;
67 const struct rb_control_frame_struct *catch_frame;
68 int throw_state;
69};
70
71#define THROW_DATA_CONSUMED IMEMO_FL_USER0
72
73/* IFUNC (Internal FUNCtion) */
74
75 struct vm_ifunc_argc {
76#if SIZEOF_INT * 2 > SIZEOF_VALUE
77 signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
78 signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
79#else
80 int min, max;
81#endif
82};
83
85 struct vm_ifunc {
86 VALUE flags;
87 VALUE reserved;
88 rb_block_call_func_t func;
89 const void *data;
90 struct vm_ifunc_argc argc;
91};
92
93 struct rb_imemo_tmpbuf_struct {
94 VALUE flags;
95 VALUE reserved;
96 VALUE *ptr; /* malloc'ed buffer */
97 struct rb_imemo_tmpbuf_struct *next; /* next imemo */
98 size_t cnt; /* buffer size in VALUE */
99};
100
105 struct MEMO {
106 VALUE flags;
107 VALUE reserved;
108 const VALUE v1;
109 const VALUE v2;
110 union {
111 long cnt;
112 long state;
113 const VALUE value;
114 void (*func)(void);
115 } u3;
116};
117
118/* ment is in method.h */
119
120#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
121#define MEMO_CAST(m) ((struct MEMO *)(m))
122#define MEMO_NEW(a, b, c) ((struct MEMO *)rb_imemo_new(imemo_memo, (VALUE)(a), (VALUE)(b), (VALUE)(c), 0))
123#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
124#define NEW_MEMO_FOR(type, value) \
125 ((value) = rb_ary_tmp_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
126#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
127 ((value) = rb_ary_tmp_new_fill(type_roomof(type, VALUE)), \
128 rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
129 MEMO_FOR(type, value))
130
131 typedef struct rb_imemo_tmpbuf_struct rb_imemo_tmpbuf_t;
132 VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0);
133 rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
134struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
135void rb_strterm_mark(VALUE obj);
136static inline enum imemo_type imemo_type(VALUE imemo);
137static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
138static inline bool imemo_throw_data_p(VALUE imemo);
139static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
140static inline VALUE rb_imemo_tmpbuf_auto_free_pointer(void);
141static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
142static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
143static inline VALUE rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str);
144static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
145static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
146
147RUBY_SYMBOL_EXPORT_BEGIN
148#if IMEMO_DEBUG
149 VALUE rb_imemo_new_debug(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0, const char *file, int line);
150#define rb_imemo_new(type, v1, v2, v3, v0) rb_imemo_new_debug(type, v1, v2, v3, v0, __FILE__, __LINE__)
151#else
152 VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0);
153#endif
154const char *rb_imemo_name(enum imemo_type type);
155RUBY_SYMBOL_EXPORT_END
156
157static inline enum imemo_type
158 imemo_type(VALUE imemo)
159{
160 return (RBASIC(imemo)->flags >> FL_USHIFT) & IMEMO_MASK;
161}
162
163static inline int
164imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
165{
166 if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
167 /* fixed at compile time if imemo_type is given. */
168 const VALUE mask = (IMEMO_MASK << FL_USHIFT) | RUBY_T_MASK;
169 const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
170 /* fixed at runtime. */
171 return expected_type == (RBASIC(imemo)->flags & mask);
172 }
173 else {
174 return 0;
175 }
176}
177
178#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)v, t)
179
180static inline bool
181imemo_throw_data_p(VALUE imemo)
182{
183 return RB_TYPE_P(imemo, T_IMEMO);
184}
185
186static inline struct vm_ifunc *
187rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
188{
189 return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
190}
191
192static inline VALUE
193rb_imemo_tmpbuf_auto_free_pointer(void)
194{
195 return rb_imemo_new(imemo_tmpbuf, 0, 0, 0, 0);
196}
197
198static inline void *
199RB_IMEMO_TMPBUF_PTR(VALUE v)
200{
201 const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
202 return p->ptr;
203}
204
205static inline void *
206rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr)
207{
208 return ((rb_imemo_tmpbuf_t *)v)->ptr = ptr;
209}
210
211static inline VALUE
212rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str)
213{
214 const void *src;
215 VALUE imemo;
216 rb_imemo_tmpbuf_t *tmpbuf;
217 void *dst;
218 size_t len;
219
220 SafeStringValue(str);
221 /* create tmpbuf to keep the pointer before xmalloc */
222 imemo = rb_imemo_tmpbuf_auto_free_pointer();
223 tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
224 len = RSTRING_LEN(str);
225 src = RSTRING_PTR(str);
226 dst = ruby_xmalloc(len);
227 memcpy(dst, src, len);
228 tmpbuf->ptr = dst;
229 return imemo;
230}
231
232static inline void
233MEMO_V1_SET(struct MEMO *m, VALUE v)
234{
235 RB_OBJ_WRITE(m, &m->v1, v);
236}
237
238static inline void
239MEMO_V2_SET(struct MEMO *m, VALUE v)
240{
241 RB_OBJ_WRITE(m, &m->v2, v);
242}
243
244#endif /* INTERNAL_IMEMO_H */
mask
enum @11::@13::@14 mask
imemo
union @11::@13 imemo
ptr
struct RIMemo * ptr
Definition: debug.c:88
len
uint8_t len
Definition: escape.c:17
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
memcpy
#define memcpy(d, s, n)
Definition: ffi_common.h:55
LIKELY
#define LIKELY(x)
Definition: ffi_common.h:125
FL_USHIFT
#define FL_USHIFT
Definition: fl_type.h:61
ruby_xmalloc
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
Definition: gc.c:12795
rb_imemo_tmpbuf_parser_heap
rb_imemo_tmpbuf_t * rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt)
Definition: gc.c:2432
rb_strterm_mark
void rb_strterm_mark(VALUE obj)
Definition: ripper.c:828
IMEMO_MASK
#define IMEMO_MASK
Definition: imemo.h:23
rb_imemo_name
const char * rb_imemo_name(enum imemo_type type)
Definition: gc.c:2385
imemo_type
imemo_type
Definition: imemo.h:34
imemo_ment
@ imemo_ment
Definition: imemo.h:41
imemo_ifunc
@ imemo_ifunc
iterator function
Definition: imemo.h:39
imemo_callinfo
@ imemo_callinfo
Definition: imemo.h:46
imemo_parser_strterm
@ imemo_parser_strterm
Definition: imemo.h:45
imemo_env
@ imemo_env
Definition: imemo.h:35
imemo_iseq
@ imemo_iseq
Definition: imemo.h:42
imemo_callcache
@ imemo_callcache
Definition: imemo.h:47
imemo_tmpbuf
@ imemo_tmpbuf
Definition: imemo.h:43
imemo_cref
@ imemo_cref
class reference
Definition: imemo.h:36
imemo_svar
@ imemo_svar
special variable
Definition: imemo.h:37
imemo_memo
@ imemo_memo
Definition: imemo.h:40
imemo_throw_data
@ imemo_throw_data
Definition: imemo.h:38
imemo_constcache
@ imemo_constcache
Definition: imemo.h:48
imemo_ast
@ imemo_ast
Definition: imemo.h:44
rb_vm_ifunc_new
struct vm_ifunc * rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc)
Definition: proc.c:705
rb_imemo_new
VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
Definition: gc.c:2412
config.h
Thin wrapper to ruby/config.h.
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
Definition: error.h:29
array.h
Internal header for Array.
buf
voidpf void * buf
Definition: ioapi.h:138
rb_block_call_func_t
rb_block_call_func * rb_block_call_func_t
Definition: iterator.h:34
CHAR_BIT
#define CHAR_BIT
Definition: limits.h:44
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
RBASIC
#define RBASIC(obj)
Definition: rbasic.h:34
RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
SafeStringValue
#define SafeStringValue(v)
Definition: rstring.h:53
ruby.h
cnt
rb_atomic_t cnt[RUBY_NSIG]
Definition: signal.c:508
stdbool.h
C99 shim for <stdbool.h>
MEMO
MEMO.
Definition: imemo.h:105
MEMO::v2
const VALUE v2
Definition: imemo.h:109
MEMO::flags
VALUE flags
Definition: imemo.h:106
MEMO::func
void(* func)(void)
Definition: imemo.h:114
MEMO::reserved
VALUE reserved
Definition: imemo.h:107
MEMO::v1
const VALUE v1
Definition: imemo.h:108
MEMO::cnt
long cnt
Definition: imemo.h:111
MEMO::u3
union MEMO::@112 u3
MEMO::value
const VALUE value
Definition: imemo.h:113
MEMO::state
long state
Definition: imemo.h:112
file
Definition: gzappend.c:170
rb_control_frame_struct
Definition: vm_core.h:769
rb_imemo_tmpbuf_struct
Definition: imemo.h:93
rb_imemo_tmpbuf_struct::cnt
size_t cnt
Definition: imemo.h:98
rb_imemo_tmpbuf_struct::next
struct rb_imemo_tmpbuf_struct * next
Definition: imemo.h:97
rb_imemo_tmpbuf_struct::reserved
VALUE reserved
Definition: imemo.h:95
rb_imemo_tmpbuf_struct::flags
VALUE flags
Definition: imemo.h:94
rb_imemo_tmpbuf_struct::ptr
VALUE * ptr
Definition: imemo.h:96
vm_ifunc_argc
Definition: imemo.h:75
vm_ifunc_argc::max
int max
Definition: imemo.h:80
vm_ifunc_argc::min
int min
Definition: imemo.h:80
vm_ifunc
IFUNC (Internal FUNCtion)
Definition: imemo.h:85
vm_ifunc::flags
VALUE flags
Definition: imemo.h:86
vm_ifunc::data
const void * data
Definition: imemo.h:89
vm_ifunc::argc
struct vm_ifunc_argc argc
Definition: imemo.h:90
vm_ifunc::reserved
VALUE reserved
Definition: imemo.h:87
vm_ifunc::func
rb_block_call_func_t func
Definition: imemo.h:88
vm_svar
SVAR (Special VARiable)
Definition: imemo.h:54
vm_svar::lastline
const VALUE lastline
Definition: imemo.h:57
vm_svar::backref
const VALUE backref
Definition: imemo.h:58
vm_svar::others
const VALUE others
Definition: imemo.h:59
vm_svar::flags
VALUE flags
Definition: imemo.h:55
vm_svar::cref_or_me
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition: imemo.h:56
vm_throw_data
THROW_DATA.
Definition: imemo.h:63
vm_throw_data::catch_frame
const struct rb_control_frame_struct * catch_frame
Definition: imemo.h:67
vm_throw_data::reserved
VALUE reserved
Definition: imemo.h:65
vm_throw_data::throw_obj
const VALUE throw_obj
Definition: imemo.h:66
vm_throw_data::throw_state
int throw_state
Definition: imemo.h:68
vm_throw_data::flags
VALUE flags
Definition: imemo.h:64
VALUE
unsigned long VALUE
Definition: value.h:38
SIZEOF_VALUE
#define SIZEOF_VALUE
Definition: value.h:41
T_IMEMO
#define T_IMEMO
Definition: value_type.h:66
RUBY_T_MASK
@ RUBY_T_MASK
Definition: value_type.h:142