Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)
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_hash_values(long len, const VALUE *elements);
27VALUE rb_ary_last(int, const VALUE *, VALUE);
28void rb_ary_set_len(VALUE, long);
29void rb_ary_delete_same(VALUE, VALUE);
30VALUE rb_ary_hidden_new_fill(long capa);
31VALUE rb_ary_at(VALUE, VALUE);
32size_t rb_ary_memsize(VALUE);
33VALUE rb_to_array_type(VALUE obj);
34VALUE rb_to_array(VALUE obj);
35void rb_ary_cancel_sharing(VALUE ary);
36size_t rb_ary_size_as_embedded(VALUE ary);
37void rb_ary_make_embedded(VALUE ary);
38bool rb_ary_embeddable_p(VALUE ary);
39VALUE rb_ary_diff(VALUE ary1, VALUE ary2);
40
41static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
42static inline bool ARY_PTR_USING_P(VALUE ary);
43
44VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
45VALUE rb_check_to_array(VALUE ary);
46VALUE rb_ary_behead(VALUE, long);
47VALUE rb_ary_aref1(VALUE ary, VALUE i);
48
50VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts);
51
52// YJIT needs this function to never allocate and never raise
53static inline VALUE
54rb_ary_entry_internal(VALUE ary, long offset)
55{
56 long len = RARRAY_LEN(ary);
57 const VALUE *ptr = RARRAY_CONST_PTR(ary);
58 if (len == 0) return Qnil;
59 if (offset < 0) {
60 offset += len;
61 if (offset < 0) return Qnil;
62 }
63 else if (len <= offset) {
64 return Qnil;
65 }
66 return ptr[offset];
67}
68
69static inline bool
70ARY_PTR_USING_P(VALUE ary)
71{
72 return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
73}
74
76static inline int
77ary_should_not_be_shared_and_embedded(VALUE ary)
78{
79 return !FL_ALL_RAW(ary, RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG);
80}
81
82static inline bool
83ARY_SHARED_P(VALUE ary)
84{
85 assert(RB_TYPE_P(ary, T_ARRAY));
86 assert(ary_should_not_be_shared_and_embedded(ary));
87 return FL_TEST_RAW(ary, RARRAY_SHARED_FLAG);
88}
89
90static inline bool
91ARY_EMBED_P(VALUE ary)
92{
93 assert(RB_TYPE_P(ary, T_ARRAY));
94 assert(ary_should_not_be_shared_and_embedded(ary));
95 return FL_TEST_RAW(ary, RARRAY_EMBED_FLAG);
96}
97
98static inline VALUE
99ARY_SHARED_ROOT(VALUE ary)
100{
101 assert(ARY_SHARED_P(ary));
102 return RARRAY(ary)->as.heap.aux.shared_root;
103}
104
105static inline bool
106ARY_SHARED_ROOT_P(VALUE ary)
107{
108 assert(RB_TYPE_P(ary, T_ARRAY));
109 return FL_TEST_RAW(ary, RARRAY_SHARED_ROOT_FLAG);
110}
111
112static inline long
113ARY_SHARED_ROOT_REFCNT(VALUE ary)
114{
115 assert(ARY_SHARED_ROOT_P(ary));
116 return RARRAY(ary)->as.heap.aux.capa;
117}
118
119#undef rb_ary_new_from_args
120#if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
121# /* Skip it; clang -pedantic doesn't like the following */
122#elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
123#define rb_ary_new_from_args(n, ...) \
124 __extension__ ({ \
125 const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
126 if (__builtin_constant_p(n)) { \
127 STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
128 } \
129 rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
130 })
131#endif
132
133#undef RARRAY_AREF
136static inline VALUE
137RARRAY_AREF(VALUE ary, long i)
138{
139 VALUE val;
140 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
141
143#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 13
144 RBIMPL_WARNING_IGNORED(-Warray-bounds);
145#endif
146 val = RARRAY_CONST_PTR(ary)[i];
148 return val;
149}
150
151#endif /* INTERNAL_ARRAY_H */
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:132
#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:124
int capa
Designed capacity of the buffer.
Definition io.h:11
int len
Length of the buffer.
Definition io.h:8
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:66
#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:51
#define RARRAY(obj)
Convenient casting macro.
Definition rarray.h:44
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
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
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
#define RBIMPL_WARNING_POP()
Pops compiler warning state.