Ruby 4.1.0dev (2026-08-15 revision cfb2ed7c723c5435f630fd70897273db032b0bcc)
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#define RARRAY_FAKEARY FL_USER19
25
26/* array.c */
27VALUE rb_ary_hash_values(long len, const VALUE *elements);
28VALUE rb_ary_last(int, const VALUE *, VALUE);
29void rb_ary_set_len(VALUE, long);
30void rb_ary_delete_same(VALUE, VALUE);
31VALUE rb_ary_hidden_new_fill(long capa);
32VALUE rb_ary_at(VALUE, VALUE);
33size_t rb_ary_memsize(VALUE);
34VALUE rb_to_array_type(VALUE obj);
35VALUE rb_to_array(VALUE obj);
36void rb_ary_cancel_sharing(VALUE ary);
37size_t rb_ary_size_as_embedded(VALUE ary);
38void rb_ary_make_embedded(VALUE ary);
39bool rb_ary_embeddable_p(VALUE ary);
40bool rb_ary_embedded_shared_root_p(VALUE ary);
41VALUE rb_ary_diff(VALUE ary1, VALUE ary2);
42VALUE rb_ary_compact_bang(VALUE ary);
43VALUE rb_ary_modify_expand(VALUE ary, long expand);
44RUBY_EXTERN VALUE rb_cArray_empty_frozen;
45
46static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
47static inline bool ARY_PTR_USING_P(VALUE ary);
48
49VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
50VALUE rb_check_to_array(VALUE ary);
51VALUE rb_ary_behead(VALUE, long);
52VALUE rb_ary_aref1(VALUE ary, VALUE i);
53
55VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts);
56
57// YJIT needs this function to never allocate and never raise
58static inline VALUE
59rb_ary_entry_internal(VALUE ary, long offset)
60{
61 long len = RARRAY_LEN(ary);
62 const VALUE *ptr = RARRAY_CONST_PTR(ary);
63 if (len == 0) return Qnil;
64 if (offset < 0) {
65 offset += len;
66 if (offset < 0) return Qnil;
67 }
68 else if (len <= offset) {
69 return Qnil;
70 }
71 return ptr[offset];
72}
73
74static inline bool
75ARY_PTR_USING_P(VALUE ary)
76{
77 return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
78}
79
81static inline int
82ary_should_not_be_shared_and_embedded(VALUE ary)
83{
84 return !FL_ALL_RAW(ary, RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG);
85}
86
87static inline bool
88ARY_SHARED_P(VALUE ary)
89{
90 assert(RB_TYPE_P(ary, T_ARRAY));
91 assert(ary_should_not_be_shared_and_embedded(ary));
92 return FL_TEST_RAW(ary, RARRAY_SHARED_FLAG);
93}
94
95static inline bool
96ARY_EMBED_P(VALUE ary)
97{
98 assert(RB_TYPE_P(ary, T_ARRAY));
99 assert(ary_should_not_be_shared_and_embedded(ary));
100 return FL_TEST_RAW(ary, RARRAY_EMBED_FLAG);
101}
102
103static inline VALUE
104ARY_SHARED_ROOT(VALUE ary)
105{
106 assert(ARY_SHARED_P(ary));
107 return RARRAY(ary)->as.heap.aux.shared_root;
108}
109
110static inline bool
111ARY_SHARED_ROOT_P(VALUE ary)
112{
113 assert(RB_TYPE_P(ary, T_ARRAY));
114 return FL_TEST_RAW(ary, RARRAY_SHARED_ROOT_FLAG);
115}
116
117static inline long
118ARY_SHARED_ROOT_REFCNT(VALUE ary)
119{
120 assert(ARY_SHARED_ROOT_P(ary));
121 return RARRAY(ary)->as.heap.aux.capa;
122}
123
124#undef rb_ary_new_from_args
125#if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
126# /* Skip it; clang -pedantic doesn't like the following */
127#elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
128#define rb_ary_new_from_args(n, ...) \
129 __extension__ ({ \
130 const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
131 if (__builtin_constant_p(n)) { \
132 STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
133 } \
134 rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
135 })
136#endif
137
138#undef RARRAY_AREF
141static inline VALUE
142RARRAY_AREF(VALUE ary, long i)
143{
144 VALUE val;
145 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
146
147 RUBY_ASSERT(i < RARRAY_LEN(ary));
148
150#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 13
151 RBIMPL_WARNING_IGNORED(-Warray-bounds);
152#endif
153 val = RARRAY_CONST_PTR(ary)[i];
155 return val;
156}
157
158#endif /* INTERNAL_ARRAY_H */
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#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:120
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 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:50
#define RARRAY(obj)
Convenient casting macro.
Definition rarray.h:44
#define RARRAY_AREF(a, i)
Definition rarray.h:402
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
C99 shim for <stdbool.h>
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
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:376
@ RUBY_T_ARRAY
Definition value_type.h:122
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
#define RBIMPL_WARNING_POP()
Pops compiler warning state.