2020-04-10 14:11:40 +09:00
|
|
|
#ifndef INTERNAL_IMEMO_H /*-*-C-*-vi:se ft=c:*/
|
|
|
|
#define INTERNAL_IMEMO_H
|
|
|
|
/**
|
2020-04-08 13:28:13 +09:00
|
|
|
* @author Ruby developers <ruby-core@ruby-lang.org>
|
2019-11-29 15:18:34 +09:00
|
|
|
* @copyright This file is a part of the programming language Ruby.
|
|
|
|
* Permission is hereby granted, to either redistribute and/or
|
|
|
|
* modify this file, provided that the conditions mentioned in the
|
|
|
|
* file COPYING are met. Consult the file for details.
|
2020-04-08 13:28:13 +09:00
|
|
|
* @brief IMEMO: Internal memo object.
|
2019-11-29 15:18:34 +09:00
|
|
|
*/
|
2020-05-08 18:31:09 +09:00
|
|
|
#include "ruby/internal/config.h"
|
2019-12-03 14:42:53 +09:00
|
|
|
#include <stddef.h> /* for size_t */
|
2025-04-30 13:48:02 +09:00
|
|
|
#include "id_table.h"
|
2022-07-25 10:40:45 -04:00
|
|
|
#include "internal/array.h" /* for rb_ary_hidden_new_fill */
|
2020-05-08 18:31:09 +09:00
|
|
|
#include "ruby/internal/stdbool.h" /* for bool */
|
2019-12-03 14:42:53 +09:00
|
|
|
#include "ruby/ruby.h" /* for rb_block_call_func_t */
|
2019-11-29 15:18:34 +09:00
|
|
|
|
2019-12-03 14:42:53 +09:00
|
|
|
#define IMEMO_MASK 0x0f
|
|
|
|
|
|
|
|
/* FL_USER0 to FL_USER3 is for type */
|
|
|
|
#define IMEMO_FL_USHIFT (FL_USHIFT + 4)
|
|
|
|
#define IMEMO_FL_USER0 FL_USER4
|
|
|
|
#define IMEMO_FL_USER1 FL_USER5
|
|
|
|
#define IMEMO_FL_USER2 FL_USER6
|
|
|
|
#define IMEMO_FL_USER3 FL_USER7
|
|
|
|
#define IMEMO_FL_USER4 FL_USER8
|
2020-01-08 16:14:01 +09:00
|
|
|
#define IMEMO_FL_USER5 FL_USER9
|
2025-04-30 13:48:02 +09:00
|
|
|
#define IMEMO_FL_USER6 FL_USER10
|
2019-11-29 15:18:34 +09:00
|
|
|
|
|
|
|
enum imemo_type {
|
|
|
|
imemo_env = 0,
|
|
|
|
imemo_cref = 1, /*!< class reference */
|
|
|
|
imemo_svar = 2, /*!< special variable */
|
|
|
|
imemo_throw_data = 3,
|
|
|
|
imemo_ifunc = 4, /*!< iterator function */
|
|
|
|
imemo_memo = 5,
|
|
|
|
imemo_ment = 6,
|
|
|
|
imemo_iseq = 7,
|
|
|
|
imemo_tmpbuf = 8,
|
[Universal parser] Decouple IMEMO from rb_ast_t
This patch removes the `VALUE flags` member from the `rb_ast_t` structure making `rb_ast_t` no longer an IMEMO object.
## Background
We are trying to make the Ruby parser generated from parse.y a universal parser that can be used by other implementations such as mruby.
To achieve this, it is necessary to exclude VALUE and IMEMO from parse.y, AST, and NODE.
## Summary (file by file)
- `rubyparser.h`
- Remove the `VALUE flags` member from `rb_ast_t`
- `ruby_parser.c` and `internal/ruby_parser.h`
- Use TypedData_Make_Struct VALUE which wraps `rb_ast_t` `in ast_alloc()` so that GC can manage it
- You can retrieve `rb_ast_t` from the VALUE by `rb_ruby_ast_data_get()`
- Change the return type of `rb_parser_compile_XXXX()` functions from `rb_ast_t *` to `VALUE`
- rb_ruby_ast_new() which internally `calls ast_alloc()` is to create VALUE vast outside ruby_parser.c
- `iseq.c` and `vm_core.h`
- Amend the first parameter of `rb_iseq_new_XXXX()` functions from `rb_ast_body_t *` to `VALUE`
- This keeps the VALUE of AST on the machine stack to prevent being removed by GC
- `ast.c`
- Almost all change is replacement `rb_ast_t *ast` with `VALUE vast` (sorry for the big diff)
- Fix `node_memsize()`
- Now it includes `rb_ast_local_table_link`, `tokens` and script_lines
- `compile.c`, `load.c`, `node.c`, `parse.y`, `proc.c`, `ruby.c`, `template/prelude.c.tmpl`, `vm.c` and `vm_eval.c`
- Follow-up due to the above changes
- `imemo.{c|h}`
- If an object with `imemo_ast` appears, considers it a bug
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2024-04-16 18:42:42 +09:00
|
|
|
imemo_ast = 9, // Obsolete due to the universal parser
|
VALUE size packed callinfo (ci).
Now, rb_call_info contains how to call the method with tuple of
(mid, orig_argc, flags, kwarg). Most of cases, kwarg == NULL and
mid+argc+flags only requires 64bits. So this patch packed
rb_call_info to VALUE (1 word) on such cases. If we can not
represent it in VALUE, then use imemo_callinfo which contains
conventional callinfo (rb_callinfo, renamed from rb_call_info).
iseq->body->ci_kw_size is removed because all of callinfo is VALUE
size (packed ci or a pointer to imemo_callinfo).
To access ci information, we need to use these functions:
vm_ci_mid(ci), _flag(ci), _argc(ci), _kwarg(ci).
struct rb_call_info_kw_arg is renamed to rb_callinfo_kwarg.
rb_funcallv_with_cc() and rb_method_basic_definition_p_with_cc()
is temporary removed because cd->ci should be marked.
2020-01-08 08:20:36 +09:00
|
|
|
imemo_parser_strterm = 10,
|
|
|
|
imemo_callinfo = 11,
|
2020-01-08 16:14:01 +09:00
|
|
|
imemo_callcache = 12,
|
2021-01-04 18:08:25 +09:00
|
|
|
imemo_constcache = 13,
|
2025-05-22 14:01:46 +02:00
|
|
|
imemo_class_fields = 14,
|
2019-11-29 15:18:34 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/* CREF (Class REFerence) is defined in method.h */
|
|
|
|
|
|
|
|
/*! SVAR (Special VARiable) */
|
|
|
|
struct vm_svar {
|
|
|
|
VALUE flags;
|
|
|
|
const VALUE cref_or_me; /*!< class reference or rb_method_entry_t */
|
|
|
|
const VALUE lastline;
|
|
|
|
const VALUE backref;
|
|
|
|
const VALUE others;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! THROW_DATA */
|
|
|
|
struct vm_throw_data {
|
|
|
|
VALUE flags;
|
|
|
|
VALUE reserved;
|
|
|
|
const VALUE throw_obj;
|
|
|
|
const struct rb_control_frame_struct *catch_frame;
|
|
|
|
int throw_state;
|
|
|
|
};
|
|
|
|
|
2019-12-03 14:42:53 +09:00
|
|
|
#define THROW_DATA_CONSUMED IMEMO_FL_USER0
|
2019-11-29 15:18:34 +09:00
|
|
|
|
|
|
|
/* IFUNC (Internal FUNCtion) */
|
|
|
|
|
|
|
|
struct vm_ifunc_argc {
|
|
|
|
#if SIZEOF_INT * 2 > SIZEOF_VALUE
|
|
|
|
signed int min: (SIZEOF_VALUE * CHAR_BIT) / 2;
|
|
|
|
signed int max: (SIZEOF_VALUE * CHAR_BIT) / 2;
|
|
|
|
#else
|
|
|
|
int min, max;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2024-10-11 10:22:44 -04:00
|
|
|
/*! IFUNC (Internal FUNCtion)
|
|
|
|
*
|
|
|
|
* Bookkeeping for converting a C function and some closed-over data into a
|
|
|
|
* block passable to methods. Like Ruby Proc, but not directly accessible at
|
|
|
|
* Ruby level since this is an imemo. See rb_block_call() and friends.
|
|
|
|
*/
|
2019-11-29 15:18:34 +09:00
|
|
|
struct vm_ifunc {
|
|
|
|
VALUE flags;
|
2023-02-10 16:02:20 +09:00
|
|
|
VALUE *svar_lep;
|
2019-11-29 15:18:34 +09:00
|
|
|
rb_block_call_func_t func;
|
|
|
|
const void *data;
|
|
|
|
struct vm_ifunc_argc argc;
|
|
|
|
};
|
2024-07-09 18:27:02 +09:00
|
|
|
#define IFUNC_YIELD_OPTIMIZABLE IMEMO_FL_USER0
|
2019-11-29 15:18:34 +09:00
|
|
|
|
2019-12-03 14:42:53 +09:00
|
|
|
struct rb_imemo_tmpbuf_struct {
|
2019-11-29 15:18:34 +09:00
|
|
|
VALUE flags;
|
|
|
|
VALUE reserved;
|
|
|
|
VALUE *ptr; /* malloc'ed buffer */
|
|
|
|
struct rb_imemo_tmpbuf_struct *next; /* next imemo */
|
|
|
|
size_t cnt; /* buffer size in VALUE */
|
2019-12-03 14:42:53 +09:00
|
|
|
};
|
2019-11-29 15:18:34 +09:00
|
|
|
|
|
|
|
/*! MEMO
|
|
|
|
*
|
|
|
|
* @see imemo_type
|
|
|
|
* */
|
|
|
|
struct MEMO {
|
|
|
|
VALUE flags;
|
|
|
|
VALUE reserved;
|
|
|
|
const VALUE v1;
|
|
|
|
const VALUE v2;
|
|
|
|
union {
|
|
|
|
long cnt;
|
|
|
|
long state;
|
|
|
|
const VALUE value;
|
|
|
|
void (*func)(void);
|
|
|
|
} u3;
|
|
|
|
};
|
|
|
|
|
2025-01-07 11:00:16 -05:00
|
|
|
#define IMEMO_NEW(T, type, v0) ((T *)rb_imemo_new((type), (v0), sizeof(T)))
|
2024-02-20 15:58:10 -05:00
|
|
|
|
2019-12-03 14:42:53 +09:00
|
|
|
/* ment is in method.h */
|
2019-11-29 15:18:34 +09:00
|
|
|
|
2019-12-03 14:42:53 +09:00
|
|
|
#define THROW_DATA_P(err) imemo_throw_data_p((VALUE)err)
|
2020-07-22 14:44:25 +09:00
|
|
|
#define MEMO_CAST(m) ((struct MEMO *)(m))
|
2019-11-29 15:18:34 +09:00
|
|
|
#define MEMO_FOR(type, value) ((type *)RARRAY_PTR(value))
|
|
|
|
#define NEW_MEMO_FOR(type, value) \
|
2022-07-25 10:40:45 -04:00
|
|
|
((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
|
2019-11-29 15:18:34 +09:00
|
|
|
#define NEW_PARTIAL_MEMO_FOR(type, value, member) \
|
2022-07-25 10:40:45 -04:00
|
|
|
((value) = rb_ary_hidden_new_fill(type_roomof(type, VALUE)), \
|
2019-11-29 15:18:34 +09:00
|
|
|
rb_ary_set_len((value), offsetof(type, member) / sizeof(VALUE)), \
|
|
|
|
MEMO_FOR(type, value))
|
|
|
|
|
[Feature #20257] Rearchitect Ripper
Introduce another semantic value stack for Ripper so that
Ripper can manage both Node and Ruby Object separately.
This rearchitectutre of Ripper solves these issues.
Therefore adding test cases for them.
* [Bug 10436] https://bugs.ruby-lang.org/issues/10436
* [Bug 18988] https://bugs.ruby-lang.org/issues/18988
* [Bug 20055] https://bugs.ruby-lang.org/issues/20055
Checked the differences of `Ripper.sexp` for files under `/test/ruby`
are only on test_pattern_matching.rb.
The differences comes from the differences between
`new_hash_pattern_tail` functions between parser and Ripper.
Ripper `new_hash_pattern_tail` didn’t call `assignable` then
`kw_rest_arg` wasn’t marked as local variable.
This is also fixed by this commit.
```
--- a/./tmp/before/test_pattern_matching.rb
+++ b/./tmp/after/test_pattern_matching.rb
@@ -3607,7 +3607,7 @@
[:in,
[:hshptn, nil, [], [:var_field, [:@ident, “a”, [984, 13]]]],
[[:binary,
- [:vcall, [:@ident, “a”, [985, 10]]],
+ [:var_ref, [:@ident, “a”, [985, 10]]],
:==,
[:hash, nil]]],
nil]]],
@@ -3662,7 +3662,7 @@
[:in,
[:hshptn, nil, [], [:var_field, [:@ident, “a”, [993, 13]]]],
[[:binary,
- [:vcall, [:@ident, “a”, [994, 10]]],
+ [:var_ref, [:@ident, “a”, [994, 10]]],
:==,
[:hash,
[:assoclist_from_args,
@@ -3813,7 +3813,7 @@
[:command,
[:@ident, “raise”, [1022, 10]],
[:args_add_block,
- [[:vcall, [:@ident, “b”, [1022, 16]]]],
+ [[:var_ref, [:@ident, “b”, [1022, 16]]]],
false]]],
[:else, [[:var_ref, [:@kw, “true”, [1024, 10]]]]]]]],
nil,
@@ -3876,7 +3876,7 @@
[:@int, “0”, [1033, 15]]],
:“&&“,
[:binary,
- [:vcall, [:@ident, “b”, [1033, 20]]],
+ [:var_ref, [:@ident, “b”, [1033, 20]]],
:==,
[:hash, nil]]]],
nil]]],
@@ -3946,7 +3946,7 @@
[:@int, “0”, [1042, 15]]],
:“&&“,
[:binary,
- [:vcall, [:@ident, “b”, [1042, 20]]],
+ [:var_ref, [:@ident, “b”, [1042, 20]]],
:==,
[:hash,
[:assoclist_from_args,
@@ -5206,7 +5206,7 @@
[[:assoc_new,
[:@label, “c:“, [1352, 22]],
[:@int, “0”, [1352, 25]]]]]],
- [:vcall, [:@ident, “r”, [1352, 29]]]],
+ [:var_ref, [:@ident, “r”, [1352, 29]]]],
false]]],
[:binary,
[:call,
@@ -5299,7 +5299,7 @@
[:assoc_new,
[:@label, “c:“, [1367, 34]],
[:@int, “0”, [1367, 37]]]]]],
- [:vcall, [:@ident, “r”, [1367, 41]]]],
+ [:var_ref, [:@ident, “r”, [1367, 41]]]],
false]]],
[:binary,
[:call,
@@ -5931,7 +5931,7 @@
[:in,
[:hshptn, nil, [], [:var_field, [:@ident, “r”, [1533, 11]]]],
[[:binary,
- [:vcall, [:@ident, “r”, [1534, 8]]],
+ [:var_ref, [:@ident, “r”, [1534, 8]]],
:==,
[:hash,
[:assoclist_from_args,
```
2024-01-12 10:46:17 +09:00
|
|
|
#ifndef RUBY_RUBYPARSER_H
|
2019-12-03 14:42:53 +09:00
|
|
|
typedef struct rb_imemo_tmpbuf_struct rb_imemo_tmpbuf_t;
|
[Feature #20257] Rearchitect Ripper
Introduce another semantic value stack for Ripper so that
Ripper can manage both Node and Ruby Object separately.
This rearchitectutre of Ripper solves these issues.
Therefore adding test cases for them.
* [Bug 10436] https://bugs.ruby-lang.org/issues/10436
* [Bug 18988] https://bugs.ruby-lang.org/issues/18988
* [Bug 20055] https://bugs.ruby-lang.org/issues/20055
Checked the differences of `Ripper.sexp` for files under `/test/ruby`
are only on test_pattern_matching.rb.
The differences comes from the differences between
`new_hash_pattern_tail` functions between parser and Ripper.
Ripper `new_hash_pattern_tail` didn’t call `assignable` then
`kw_rest_arg` wasn’t marked as local variable.
This is also fixed by this commit.
```
--- a/./tmp/before/test_pattern_matching.rb
+++ b/./tmp/after/test_pattern_matching.rb
@@ -3607,7 +3607,7 @@
[:in,
[:hshptn, nil, [], [:var_field, [:@ident, “a”, [984, 13]]]],
[[:binary,
- [:vcall, [:@ident, “a”, [985, 10]]],
+ [:var_ref, [:@ident, “a”, [985, 10]]],
:==,
[:hash, nil]]],
nil]]],
@@ -3662,7 +3662,7 @@
[:in,
[:hshptn, nil, [], [:var_field, [:@ident, “a”, [993, 13]]]],
[[:binary,
- [:vcall, [:@ident, “a”, [994, 10]]],
+ [:var_ref, [:@ident, “a”, [994, 10]]],
:==,
[:hash,
[:assoclist_from_args,
@@ -3813,7 +3813,7 @@
[:command,
[:@ident, “raise”, [1022, 10]],
[:args_add_block,
- [[:vcall, [:@ident, “b”, [1022, 16]]]],
+ [[:var_ref, [:@ident, “b”, [1022, 16]]]],
false]]],
[:else, [[:var_ref, [:@kw, “true”, [1024, 10]]]]]]]],
nil,
@@ -3876,7 +3876,7 @@
[:@int, “0”, [1033, 15]]],
:“&&“,
[:binary,
- [:vcall, [:@ident, “b”, [1033, 20]]],
+ [:var_ref, [:@ident, “b”, [1033, 20]]],
:==,
[:hash, nil]]]],
nil]]],
@@ -3946,7 +3946,7 @@
[:@int, “0”, [1042, 15]]],
:“&&“,
[:binary,
- [:vcall, [:@ident, “b”, [1042, 20]]],
+ [:var_ref, [:@ident, “b”, [1042, 20]]],
:==,
[:hash,
[:assoclist_from_args,
@@ -5206,7 +5206,7 @@
[[:assoc_new,
[:@label, “c:“, [1352, 22]],
[:@int, “0”, [1352, 25]]]]]],
- [:vcall, [:@ident, “r”, [1352, 29]]]],
+ [:var_ref, [:@ident, “r”, [1352, 29]]]],
false]]],
[:binary,
[:call,
@@ -5299,7 +5299,7 @@
[:assoc_new,
[:@label, “c:“, [1367, 34]],
[:@int, “0”, [1367, 37]]]]]],
- [:vcall, [:@ident, “r”, [1367, 41]]]],
+ [:var_ref, [:@ident, “r”, [1367, 41]]]],
false]]],
[:binary,
[:call,
@@ -5931,7 +5931,7 @@
[:in,
[:hshptn, nil, [], [:var_field, [:@ident, “r”, [1533, 11]]]],
[[:binary,
- [:vcall, [:@ident, “r”, [1534, 8]]],
+ [:var_ref, [:@ident, “r”, [1534, 8]]],
:==,
[:hash,
[:assoclist_from_args,
```
2024-01-12 10:46:17 +09:00
|
|
|
#endif
|
2019-12-03 14:42:53 +09:00
|
|
|
rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
|
|
|
|
struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
|
|
|
|
static inline enum imemo_type imemo_type(VALUE imemo);
|
|
|
|
static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
|
|
|
|
static inline bool imemo_throw_data_p(VALUE imemo);
|
|
|
|
static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
|
|
|
|
static inline VALUE rb_imemo_tmpbuf_auto_free_pointer(void);
|
|
|
|
static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
|
|
|
|
static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
|
|
|
|
static inline VALUE rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str);
|
|
|
|
static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
|
|
|
|
static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
|
2019-11-29 15:18:34 +09:00
|
|
|
|
2024-02-21 15:48:40 -05:00
|
|
|
size_t rb_imemo_memsize(VALUE obj);
|
|
|
|
void rb_cc_table_mark(VALUE klass);
|
|
|
|
void rb_imemo_mark_and_move(VALUE obj, bool reference_updating);
|
|
|
|
void rb_cc_table_free(VALUE klass);
|
2025-04-30 13:48:02 +09:00
|
|
|
void rb_cc_tbl_free(struct rb_id_table *cc_tbl, VALUE klass);
|
2024-02-21 15:48:40 -05:00
|
|
|
void rb_imemo_free(VALUE obj);
|
|
|
|
|
2019-11-29 15:18:34 +09:00
|
|
|
RUBY_SYMBOL_EXPORT_BEGIN
|
2025-01-07 11:00:16 -05:00
|
|
|
VALUE rb_imemo_new(enum imemo_type type, VALUE v0, size_t size);
|
2020-07-09 18:37:03 -04:00
|
|
|
const char *rb_imemo_name(enum imemo_type type);
|
2019-11-29 15:18:34 +09:00
|
|
|
RUBY_SYMBOL_EXPORT_END
|
|
|
|
|
2024-02-20 15:58:10 -05:00
|
|
|
static inline struct MEMO *
|
|
|
|
MEMO_NEW(VALUE a, VALUE b, VALUE c)
|
|
|
|
{
|
|
|
|
struct MEMO *memo = IMEMO_NEW(struct MEMO, imemo_memo, 0);
|
|
|
|
*((VALUE *)&memo->v1) = a;
|
|
|
|
*((VALUE *)&memo->v2) = b;
|
|
|
|
*((VALUE *)&memo->u3.value) = c;
|
|
|
|
|
|
|
|
return memo;
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:42:53 +09:00
|
|
|
static inline enum imemo_type
|
|
|
|
imemo_type(VALUE imemo)
|
|
|
|
{
|
|
|
|
return (RBASIC(imemo)->flags >> FL_USHIFT) & IMEMO_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
imemo_type_p(VALUE imemo, enum imemo_type imemo_type)
|
|
|
|
{
|
|
|
|
if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) {
|
|
|
|
/* fixed at compile time if imemo_type is given. */
|
|
|
|
const VALUE mask = (IMEMO_MASK << FL_USHIFT) | RUBY_T_MASK;
|
|
|
|
const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO;
|
|
|
|
/* fixed at runtime. */
|
|
|
|
return expected_type == (RBASIC(imemo)->flags & mask);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:30:32 +09:00
|
|
|
#define IMEMO_TYPE_P(v, t) imemo_type_p((VALUE)(v), t)
|
2020-01-08 16:14:01 +09:00
|
|
|
|
2019-12-03 14:42:53 +09:00
|
|
|
static inline bool
|
|
|
|
imemo_throw_data_p(VALUE imemo)
|
|
|
|
{
|
|
|
|
return RB_TYPE_P(imemo, T_IMEMO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct vm_ifunc *
|
|
|
|
rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
|
|
|
|
{
|
|
|
|
return rb_vm_ifunc_new(func, data, 0, UNLIMITED_ARGUMENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
rb_imemo_tmpbuf_auto_free_pointer(void)
|
|
|
|
{
|
2025-01-07 11:00:16 -05:00
|
|
|
return rb_imemo_new(imemo_tmpbuf, 0, sizeof(rb_imemo_tmpbuf_t));
|
2019-12-03 14:42:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *
|
|
|
|
RB_IMEMO_TMPBUF_PTR(VALUE v)
|
|
|
|
{
|
|
|
|
const struct rb_imemo_tmpbuf_struct *p = (const void *)v;
|
|
|
|
return p->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *
|
|
|
|
rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr)
|
|
|
|
{
|
|
|
|
return ((rb_imemo_tmpbuf_t *)v)->ptr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str)
|
|
|
|
{
|
|
|
|
const void *src;
|
|
|
|
VALUE imemo;
|
|
|
|
rb_imemo_tmpbuf_t *tmpbuf;
|
|
|
|
void *dst;
|
|
|
|
size_t len;
|
|
|
|
|
2024-01-09 20:04:20 +01:00
|
|
|
StringValue(str);
|
2019-12-03 14:42:53 +09:00
|
|
|
/* create tmpbuf to keep the pointer before xmalloc */
|
|
|
|
imemo = rb_imemo_tmpbuf_auto_free_pointer();
|
|
|
|
tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
|
|
|
|
len = RSTRING_LEN(str);
|
|
|
|
src = RSTRING_PTR(str);
|
|
|
|
dst = ruby_xmalloc(len);
|
|
|
|
memcpy(dst, src, len);
|
|
|
|
tmpbuf->ptr = dst;
|
|
|
|
return imemo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
MEMO_V1_SET(struct MEMO *m, VALUE v)
|
|
|
|
{
|
|
|
|
RB_OBJ_WRITE(m, &m->v1, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
MEMO_V2_SET(struct MEMO *m, VALUE v)
|
|
|
|
{
|
|
|
|
RB_OBJ_WRITE(m, &m->v2, v);
|
|
|
|
}
|
|
|
|
|
2025-05-22 14:01:46 +02:00
|
|
|
struct rb_class_fields {
|
|
|
|
struct RBasic basic;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
VALUE fields[1];
|
|
|
|
} embed;
|
|
|
|
struct {
|
|
|
|
VALUE *ptr;
|
|
|
|
} external;
|
|
|
|
struct {
|
|
|
|
// Note: the st_table could be embedded, but complex T_CLASS should be rare to
|
|
|
|
// non-existent, so not really worth the trouble.
|
|
|
|
st_table *table;
|
|
|
|
} complex;
|
|
|
|
} as;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OBJ_FIELD_EXTERNAL IMEMO_FL_USER0
|
|
|
|
#define IMEMO_OBJ_FIELDS(fields) ((struct rb_class_fields *)fields)
|
|
|
|
|
|
|
|
VALUE rb_imemo_class_fields_new(VALUE klass, size_t capa);
|
|
|
|
VALUE rb_imemo_class_fields_new_complex(VALUE klass, size_t capa);
|
|
|
|
VALUE rb_imemo_class_fields_clone(VALUE fields_obj);
|
|
|
|
|
|
|
|
static inline VALUE *
|
|
|
|
rb_imemo_class_fields_ptr(VALUE obj_fields)
|
|
|
|
{
|
|
|
|
if (!obj_fields) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RUBY_ASSERT(IMEMO_TYPE_P(obj_fields, imemo_class_fields));
|
|
|
|
|
|
|
|
if (RB_UNLIKELY(FL_TEST_RAW(obj_fields, OBJ_FIELD_EXTERNAL))) {
|
|
|
|
return IMEMO_OBJ_FIELDS(obj_fields)->as.external.ptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IMEMO_OBJ_FIELDS(obj_fields)->as.embed.fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline st_table *
|
|
|
|
rb_imemo_class_fields_complex_tbl(VALUE obj_fields)
|
|
|
|
{
|
|
|
|
if (!obj_fields) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RUBY_ASSERT(IMEMO_TYPE_P(obj_fields, imemo_class_fields));
|
|
|
|
|
|
|
|
return IMEMO_OBJ_FIELDS(obj_fields)->as.complex.table;
|
|
|
|
}
|
|
|
|
|
2019-11-29 15:18:34 +09:00
|
|
|
#endif /* INTERNAL_IMEMO_H */
|