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_last(int, const VALUE
*, VALUE
);
27 void rb_ary_set_len(VALUE
, long);
28 void rb_ary_delete_same(VALUE
, VALUE
);
29 VALUE
rb_ary_hidden_new_fill(long capa
);
30 VALUE
rb_ary_at(VALUE
, VALUE
);
31 size_t rb_ary_memsize(VALUE
);
32 VALUE
rb_to_array_type(VALUE obj
);
33 VALUE
rb_to_array(VALUE obj
);
34 void rb_ary_cancel_sharing(VALUE ary
);
35 size_t rb_ary_size_as_embedded(VALUE ary
);
36 void rb_ary_make_embedded(VALUE ary
);
37 bool rb_ary_embeddable_p(VALUE ary
);
38 VALUE
rb_ary_diff(VALUE ary1
, VALUE ary2
);
40 static inline VALUE
rb_ary_entry_internal(VALUE ary
, long offset
);
41 static inline bool ARY_PTR_USING_P(VALUE ary
);
42 static inline void RARY_TRANSIENT_SET(VALUE ary
);
43 static inline void RARY_TRANSIENT_UNSET(VALUE ary
);
45 VALUE
rb_ary_tmp_new_from_values(VALUE
, long, const VALUE
*);
46 VALUE
rb_check_to_array(VALUE ary
);
47 VALUE
rb_ary_behead(VALUE
, long);
48 VALUE
rb_ary_aref1(VALUE ary
, VALUE i
);
50 struct rb_execution_context_struct
;
51 VALUE
rb_ec_ary_new_from_values(struct rb_execution_context_struct
*ec
, long n
, const VALUE
*elts
);
53 // YJIT needs this function to never allocate and never raise
55 rb_ary_entry_internal(VALUE ary
, long offset
)
57 long len
= RARRAY_LEN(ary
);
58 const VALUE
*ptr
= RARRAY_CONST_PTR_TRANSIENT(ary
);
59 if (len
== 0) return Qnil
;
62 if (offset
< 0) return Qnil
;
64 else if (len
<= offset
) {
71 ARY_PTR_USING_P(VALUE ary
)
73 return FL_TEST_RAW(ary
, RARRAY_PTR_IN_USE_FLAG
);
76 RBIMPL_ATTR_MAYBE_UNUSED()
78 ary_should_not_be_shared_and_embedded(VALUE ary
)
80 return !FL_ALL_RAW(ary
, RARRAY_SHARED_FLAG
|RARRAY_EMBED_FLAG
);
84 ARY_SHARED_P(VALUE ary
)
86 assert(RB_TYPE_P(ary
, T_ARRAY
));
87 assert(ary_should_not_be_shared_and_embedded(ary
));
88 return FL_TEST_RAW(ary
, RARRAY_SHARED_FLAG
);
92 ARY_EMBED_P(VALUE ary
)
94 assert(RB_TYPE_P(ary
, T_ARRAY
));
95 assert(ary_should_not_be_shared_and_embedded(ary
));
96 return FL_TEST_RAW(ary
, RARRAY_EMBED_FLAG
);
100 ARY_SHARED_ROOT(VALUE ary
)
102 assert(ARY_SHARED_P(ary
));
103 return RARRAY(ary
)->as
.heap
.aux
.shared_root
;
107 ARY_SHARED_ROOT_P(VALUE ary
)
109 assert(RB_TYPE_P(ary
, T_ARRAY
));
110 return FL_TEST_RAW(ary
, RARRAY_SHARED_ROOT_FLAG
);
114 ARY_SHARED_ROOT_REFCNT(VALUE ary
)
116 assert(ARY_SHARED_ROOT_P(ary
));
117 return RARRAY(ary
)->as
.heap
.aux
.capa
;
121 RARY_TRANSIENT_SET(VALUE ary
)
123 #if USE_TRANSIENT_HEAP
124 FL_SET_RAW(ary
, RARRAY_TRANSIENT_FLAG
);
129 RARY_TRANSIENT_UNSET(VALUE ary
)
131 #if USE_TRANSIENT_HEAP
132 FL_UNSET_RAW(ary
, RARRAY_TRANSIENT_FLAG
);
136 #undef rb_ary_new_from_args
137 #if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
138 # /* Skip it; clang -pedantic doesn't like the following */
139 #elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
140 #define rb_ary_new_from_args(n, ...) \
142 const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
143 if (__builtin_constant_p(n)) { \
144 STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
146 rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
151 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
152 RBIMPL_ATTR_ARTIFICIAL()
154 RARRAY_AREF(VALUE ary
, long i
)
156 RBIMPL_ASSERT_TYPE(ary
, RUBY_T_ARRAY
);
158 return RARRAY_CONST_PTR_TRANSIENT(ary
)[i
];
161 #endif /* INTERNAL_ARRAY_H */