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
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
49};
50
51/* CREF (Class REFerence) is defined in method.h */
52
54struct vm_svar {
60};
61
69};
70
71#define THROW_DATA_CONSUMED IMEMO_FL_USER0
72
73/* IFUNC (Internal FUNCtion) */
74
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
85struct vm_ifunc {
89 const void *data;
91};
92
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
105struct MEMO {
108 const VALUE v1;
109 const VALUE v2;
110 union {
111 long cnt;
112 long state;
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
132VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0);
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
149VALUE 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
152VALUE 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
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. */
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{
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
221 /* create tmpbuf to keep the pointer before xmalloc */
222 imemo = rb_imemo_tmpbuf_auto_free_pointer();
223 tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
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 */
enum @11::@13::@14 mask
union @11::@13 imemo
struct RIMemo * ptr
Definition: debug.c:88
uint8_t len
Definition: escape.c:17
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
#define memcpy(d, s, n)
Definition: ffi_common.h:55
#define LIKELY(x)
Definition: ffi_common.h:125
#define FL_USHIFT
Definition: fl_type.h:61
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
Definition: gc.c:12795
rb_imemo_tmpbuf_t * rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt)
Definition: gc.c:2432
void rb_strterm_mark(VALUE obj)
Definition: ripper.c:828
#define IMEMO_MASK
Definition: imemo.h:23
const char * rb_imemo_name(enum imemo_type type)
Definition: gc.c:2385
imemo_type
Definition: imemo.h:34
@ imemo_ment
Definition: imemo.h:41
@ imemo_ifunc
iterator function
Definition: imemo.h:39
@ imemo_callinfo
Definition: imemo.h:46
@ imemo_parser_strterm
Definition: imemo.h:45
@ imemo_env
Definition: imemo.h:35
@ imemo_iseq
Definition: imemo.h:42
@ imemo_callcache
Definition: imemo.h:47
@ imemo_tmpbuf
Definition: imemo.h:43
@ imemo_cref
class reference
Definition: imemo.h:36
@ imemo_svar
special variable
Definition: imemo.h:37
@ imemo_memo
Definition: imemo.h:40
@ imemo_throw_data
Definition: imemo.h:38
@ imemo_constcache
Definition: imemo.h:48
@ imemo_ast
Definition: imemo.h:44
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
VALUE rb_imemo_new(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0)
Definition: gc.c:2412
Thin wrapper to ruby/config.h.
#define UNLIMITED_ARGUMENTS
Definition: error.h:29
Internal header for Array.
voidpf void * buf
Definition: ioapi.h:138
rb_block_call_func * rb_block_call_func_t
Definition: iterator.h:34
#define CHAR_BIT
Definition: limits.h:44
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
#define RBASIC(obj)
Definition: rbasic.h:34
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
#define SafeStringValue(v)
Definition: rstring.h:53
rb_atomic_t cnt[RUBY_NSIG]
Definition: signal.c:508
C99 shim for <stdbool.h>
MEMO.
Definition: imemo.h:105
const VALUE v2
Definition: imemo.h:109
VALUE flags
Definition: imemo.h:106
void(* func)(void)
Definition: imemo.h:114
VALUE reserved
Definition: imemo.h:107
const VALUE v1
Definition: imemo.h:108
long cnt
Definition: imemo.h:111
union MEMO::@112 u3
const VALUE value
Definition: imemo.h:113
long state
Definition: imemo.h:112
Definition: gzappend.c:170
struct rb_imemo_tmpbuf_struct * next
Definition: imemo.h:97
int max
Definition: imemo.h:80
int min
Definition: imemo.h:80
IFUNC (Internal FUNCtion)
Definition: imemo.h:85
VALUE flags
Definition: imemo.h:86
const void * data
Definition: imemo.h:89
struct vm_ifunc_argc argc
Definition: imemo.h:90
VALUE reserved
Definition: imemo.h:87
rb_block_call_func_t func
Definition: imemo.h:88
SVAR (Special VARiable)
Definition: imemo.h:54
const VALUE lastline
Definition: imemo.h:57
const VALUE backref
Definition: imemo.h:58
const VALUE others
Definition: imemo.h:59
VALUE flags
Definition: imemo.h:55
const VALUE cref_or_me
class reference or rb_method_entry_t
Definition: imemo.h:56
THROW_DATA.
Definition: imemo.h:63
const struct rb_control_frame_struct * catch_frame
Definition: imemo.h:67
VALUE reserved
Definition: imemo.h:65
const VALUE throw_obj
Definition: imemo.h:66
int throw_state
Definition: imemo.h:68
VALUE flags
Definition: imemo.h:64
unsigned long VALUE
Definition: value.h:38
#define SIZEOF_VALUE
Definition: value.h:41
#define T_IMEMO
Definition: value_type.h:66
@ RUBY_T_MASK
Definition: value_type.h:142