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