Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
array.h
1#ifndef INTERNAL_ARRAY_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_ARRAY_H
11#include "ruby/internal/config.h"
12#include <stddef.h> /* for size_t */
13#include "internal/static_assert.h" /* for STATIC_ASSERT */
14#include "ruby/internal/stdbool.h" /* for bool */
15#include "ruby/ruby.h" /* for RARRAY_LEN */
16
17#ifndef ARRAY_DEBUG
18# define ARRAY_DEBUG (0+RUBY_DEBUG)
19#endif
20
21#define RARRAY_SHARED_FLAG ELTS_SHARED
22#define RARRAY_SHARED_ROOT_FLAG FL_USER12
23#define RARRAY_PTR_IN_USE_FLAG FL_USER14
24
25/* array.c */
26VALUE rb_ary_last(int, const VALUE *, VALUE);
27void rb_ary_set_len(VALUE, long);
28void rb_ary_delete_same(VALUE, VALUE);
29VALUE rb_ary_hidden_new_fill(long capa);
30VALUE rb_ary_at(VALUE, VALUE);
31size_t rb_ary_memsize(VALUE);
32VALUE rb_to_array_type(VALUE obj);
33VALUE rb_to_array(VALUE obj);
34void rb_ary_cancel_sharing(VALUE ary);
35size_t rb_ary_size_as_embedded(VALUE ary);
36void rb_ary_make_embedded(VALUE ary);
37bool rb_ary_embeddable_p(VALUE ary);
38VALUE rb_ary_diff(VALUE ary1, VALUE ary2);
39
40static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
41static inline bool ARY_PTR_USING_P(VALUE ary);
42static inline void RARY_TRANSIENT_SET(VALUE ary);
43static inline void RARY_TRANSIENT_UNSET(VALUE ary);
44
45MJIT_SYMBOL_EXPORT_BEGIN
46VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
47VALUE rb_check_to_array(VALUE ary);
48VALUE rb_ary_behead(VALUE, long);
49VALUE rb_ary_aref1(VALUE ary, VALUE i);
50
52VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts);
53MJIT_SYMBOL_EXPORT_END
54
55// YJIT needs this function to never allocate and never raise
56static inline VALUE
57rb_ary_entry_internal(VALUE ary, long offset)
58{
59 long len = RARRAY_LEN(ary);
60 const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
61 if (len == 0) return Qnil;
62 if (offset < 0) {
63 offset += len;
64 if (offset < 0) return Qnil;
65 }
66 else if (len <= offset) {
67 return Qnil;
68 }
69 return ptr[offset];
70}
71
72static inline bool
73ARY_PTR_USING_P(VALUE ary)
74{
75 return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
76}
77
79static inline int
80ary_should_not_be_shared_and_embedded(VALUE ary)
81{
82 return !FL_ALL_RAW(ary, RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG);
83}
84
85static inline bool
86ARY_SHARED_P(VALUE ary)
87{
88 assert(RB_TYPE_P(ary, T_ARRAY));
89 assert(ary_should_not_be_shared_and_embedded(ary));
90 return FL_TEST_RAW(ary, RARRAY_SHARED_FLAG);
91}
92
93static inline bool
94ARY_EMBED_P(VALUE ary)
95{
96 assert(RB_TYPE_P(ary, T_ARRAY));
97 assert(ary_should_not_be_shared_and_embedded(ary));
98 return FL_TEST_RAW(ary, RARRAY_EMBED_FLAG);
99}
100
101static inline VALUE
102ARY_SHARED_ROOT(VALUE ary)
103{
104 assert(ARY_SHARED_P(ary));
105 return RARRAY(ary)->as.heap.aux.shared_root;
106}
107
108static inline bool
109ARY_SHARED_ROOT_P(VALUE ary)
110{
111 assert(RB_TYPE_P(ary, T_ARRAY));
112 return FL_TEST_RAW(ary, RARRAY_SHARED_ROOT_FLAG);
113}
114
115static inline long
116ARY_SHARED_ROOT_REFCNT(VALUE ary)
117{
118 assert(ARY_SHARED_ROOT_P(ary));
119 return RARRAY(ary)->as.heap.aux.capa;
120}
121
122static inline void
123RARY_TRANSIENT_SET(VALUE ary)
124{
125#if USE_TRANSIENT_HEAP
126 FL_SET_RAW(ary, RARRAY_TRANSIENT_FLAG);
127#endif
128}
129
130static inline void
131RARY_TRANSIENT_UNSET(VALUE ary)
132{
133#if USE_TRANSIENT_HEAP
134 FL_UNSET_RAW(ary, RARRAY_TRANSIENT_FLAG);
135#endif
136}
137
138#undef rb_ary_new_from_args
139#if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
140# /* Skip it; clang -pedantic doesn't like the following */
141#elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
142#define rb_ary_new_from_args(n, ...) \
143 __extension__ ({ \
144 const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
145 if (__builtin_constant_p(n)) { \
146 STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
147 } \
148 rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
149 })
150#endif
151
152#undef RARRAY_AREF
155static inline VALUE
156RARRAY_AREF(VALUE ary, long i)
157{
158 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
159
160 return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
161}
162
163#endif /* INTERNAL_ARRAY_H */
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition: artificial.h:43
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition: fl_type.h:142
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition: fl_type.h:140
#define Qnil
Old name of RUBY_Qnil.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition: value_type.h:56
#define FL_ALL_RAW
Old name of RB_FL_ALL_RAW.
Definition: fl_type.h:132
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition: fl_type.h:138
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
Definition: maybe_unused.h:35
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition: pure.h:38
#define RARRAY_LEN
Just another name of rb_array_len.
Definition: rarray.h:68
#define RARRAY_CONST_PTR_TRANSIENT
Just another name of rb_array_const_ptr_transient.
Definition: rarray.h:70
#define RARRAY(obj)
Convenient casting macro.
Definition: rarray.h:56
#define RARRAY_AREF(a, i)
Definition: rarray.h:583
C99 shim for <stdbool.h>
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
@ RUBY_T_ARRAY
Definition: value_type.h:121