Ruby: internal/array.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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 */
26 VALUE rb_ary_last(int, const VALUE *, VALUE);
27void rb_ary_set_len(VALUE, long);
28void rb_ary_delete_same(VALUE, VALUE);
29 VALUE rb_ary_hidden_new_fill(long capa);
30 VALUE rb_ary_at(VALUE, VALUE);
31size_t rb_ary_memsize(VALUE);
32 VALUE rb_to_array_type(VALUE obj);
33 VALUE 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);
38 VALUE 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
46 VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
47 VALUE rb_check_to_array(VALUE ary);
48 VALUE rb_ary_behead(VALUE, long);
49 VALUE rb_ary_aref1(VALUE ary, VALUE i);
50
51struct rb_execution_context_struct;
52 VALUE 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
78 RBIMPL_ATTR_MAYBE_UNUSED()
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
153 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
154 RBIMPL_ATTR_ARTIFICIAL()
155static inline VALUE
156 RARRAY_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 */
RBIMPL_ATTR_ARTIFICIAL
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
FL_UNSET_RAW
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:142
FL_TEST_RAW
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:140
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
T_ARRAY
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
FL_ALL_RAW
#define FL_ALL_RAW
Old name of RB_FL_ALL_RAW.
Definition fl_type.h:132
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:138
RBIMPL_ATTR_MAYBE_UNUSED
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
Definition maybe_unused.h:35
RBIMPL_ATTR_PURE_UNLESS_DEBUG
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition pure.h:38
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:68
RARRAY_CONST_PTR_TRANSIENT
#define RARRAY_CONST_PTR_TRANSIENT
Just another name of rb_array_const_ptr_transient.
Definition rarray.h:70
RARRAY
#define RARRAY(obj)
Convenient casting macro.
Definition rarray.h:56
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:583
ruby.h
stdbool.h
C99 shim for <stdbool.h>
rb_execution_context_struct
Definition vm_core.h:899
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
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_ARRAY
@ RUBY_T_ARRAY
Definition value_type.h:121