1 #ifndef INTERNAL_ARRAY_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_ARRAY_H
4 * @author Ruby developers <ruby-core@ruby-lang.org>
5 * @copyright This file is a part of the programming language Ruby.
6 * Permission is hereby granted, to either redistribute and/or
7 * modify this file, provided that the conditions mentioned in the
8 * file COPYING are met. Consult the file for details.
9 * @brief Internal header for Array.
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 */
18 # define ARRAY_DEBUG (0+RUBY_DEBUG)
21 #define RARRAY_SHARED_FLAG ELTS_SHARED
22 #define RARRAY_SHARED_ROOT_FLAG FL_USER12
23 #define RARRAY_PTR_IN_USE_FLAG FL_USER14
26 VALUE
rb_ary_hash_values(long len
, const VALUE
*elements
);
27 VALUE
rb_ary_last(int, const VALUE
*, VALUE
);
28 void rb_ary_set_len(VALUE
, long);
29 void rb_ary_delete_same(VALUE
, VALUE
);
30 VALUE
rb_ary_hidden_new_fill(long capa
);
31 VALUE
rb_ary_at(VALUE
, VALUE
);
32 size_t rb_ary_memsize(VALUE
);
33 VALUE
rb_to_array_type(VALUE obj
);
34 VALUE
rb_to_array(VALUE obj
);
35 void rb_ary_cancel_sharing(VALUE ary
);
36 size_t rb_ary_size_as_embedded(VALUE ary
);
37 void rb_ary_make_embedded(VALUE ary
);
38 bool rb_ary_embeddable_p(VALUE ary
);
39 VALUE
rb_ary_diff(VALUE ary1
, VALUE ary2
);
41 static inline VALUE
rb_ary_entry_internal(VALUE ary
, long offset
);
42 static inline bool ARY_PTR_USING_P(VALUE ary
);
44 VALUE
rb_ary_tmp_new_from_values(VALUE
, long, const VALUE
*);
45 VALUE
rb_check_to_array(VALUE ary
);
46 VALUE
rb_ary_behead(VALUE
, long);
47 VALUE
rb_ary_aref1(VALUE ary
, VALUE i
);
49 struct rb_execution_context_struct
;
50 VALUE
rb_ec_ary_new_from_values(struct rb_execution_context_struct
*ec
, long n
, const VALUE
*elts
);
52 // YJIT needs this function to never allocate and never raise
54 rb_ary_entry_internal(VALUE ary
, long offset
)
56 long len
= RARRAY_LEN(ary
);
57 const VALUE
*ptr
= RARRAY_CONST_PTR(ary
);
58 if (len
== 0) return Qnil
;
61 if (offset
< 0) return Qnil
;
63 else if (len
<= offset
) {
70 ARY_PTR_USING_P(VALUE ary
)
72 return FL_TEST_RAW(ary
, RARRAY_PTR_IN_USE_FLAG
);
75 RBIMPL_ATTR_MAYBE_UNUSED()
77 ary_should_not_be_shared_and_embedded(VALUE ary
)
79 return !FL_ALL_RAW(ary
, RARRAY_SHARED_FLAG
|RARRAY_EMBED_FLAG
);
83 ARY_SHARED_P(VALUE ary
)
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
);
91 ARY_EMBED_P(VALUE ary
)
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
);
99 ARY_SHARED_ROOT(VALUE ary
)
101 assert(ARY_SHARED_P(ary
));
102 return RARRAY(ary
)->as
.heap
.aux
.shared_root
;
106 ARY_SHARED_ROOT_P(VALUE ary
)
108 assert(RB_TYPE_P(ary
, T_ARRAY
));
109 return FL_TEST_RAW(ary
, RARRAY_SHARED_ROOT_FLAG
);
113 ARY_SHARED_ROOT_REFCNT(VALUE ary
)
115 assert(ARY_SHARED_ROOT_P(ary
));
116 return RARRAY(ary
)->as
.heap
.aux
.capa
;
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, ...) \
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)); \
129 rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
134 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
135 RBIMPL_ATTR_ARTIFICIAL()
137 RARRAY_AREF(VALUE ary
, long i
)
140 RBIMPL_ASSERT_TYPE(ary
, RUBY_T_ARRAY
);
142 RBIMPL_WARNING_PUSH();
143 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 13
144 RBIMPL_WARNING_IGNORED(-Warray
-bounds
);
146 val
= RARRAY_CONST_PTR(ary
)[i
];
147 RBIMPL_WARNING_POP();
151 #endif /* INTERNAL_ARRAY_H */