Ruby: internal/array.h Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

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