Remove reference counting for all frozen arrays
[ruby.git] / internal / array.h
blob0de82d9f3026b081a9c15c094db25c5621f6516d
1 #ifndef INTERNAL_ARRAY_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_ARRAY_H
3 /**
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 */
17 #ifndef ARRAY_DEBUG
18 # define ARRAY_DEBUG (0+RUBY_DEBUG)
19 #endif
21 #define RARRAY_SHARED_FLAG ELTS_SHARED
22 #define RARRAY_SHARED_ROOT_FLAG FL_USER12
23 #define RARRAY_PTR_IN_USE_FLAG FL_USER14
25 /* array.c */
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_tmp_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);
39 static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
40 static inline bool ARY_PTR_USING_P(VALUE ary);
41 static inline void RARY_TRANSIENT_SET(VALUE ary);
42 static inline void RARY_TRANSIENT_UNSET(VALUE ary);
44 MJIT_SYMBOL_EXPORT_BEGIN
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);
52 MJIT_SYMBOL_EXPORT_END
54 // YJIT needs this function to never allocate and never raise
55 static inline VALUE
56 rb_ary_entry_internal(VALUE ary, long offset)
58 long len = RARRAY_LEN(ary);
59 const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
60 if (len == 0) return Qnil;
61 if (offset < 0) {
62 offset += len;
63 if (offset < 0) return Qnil;
65 else if (len <= offset) {
66 return Qnil;
68 return ptr[offset];
71 static inline bool
72 ARY_PTR_USING_P(VALUE ary)
74 return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
77 RBIMPL_ATTR_MAYBE_UNUSED()
78 static inline int
79 ary_should_not_be_shared_and_embedded(VALUE ary)
81 return !FL_ALL_RAW(ary, RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG);
84 static inline bool
85 ARY_SHARED_P(VALUE ary)
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);
92 static inline bool
93 ARY_EMBED_P(VALUE ary)
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);
100 static inline VALUE
101 ARY_SHARED_ROOT(VALUE ary)
103 assert(ARY_SHARED_P(ary));
104 return RARRAY(ary)->as.heap.aux.shared_root;
107 static inline bool
108 ARY_SHARED_ROOT_P(VALUE ary)
110 assert(RB_TYPE_P(ary, T_ARRAY));
111 return FL_TEST_RAW(ary, RARRAY_SHARED_ROOT_FLAG);
114 static inline long
115 ARY_SHARED_ROOT_REFCNT(VALUE ary)
117 assert(ARY_SHARED_ROOT_P(ary));
118 return RARRAY(ary)->as.heap.aux.capa;
121 static inline void
122 RARY_TRANSIENT_SET(VALUE ary)
124 #if USE_TRANSIENT_HEAP
125 FL_SET_RAW(ary, RARRAY_TRANSIENT_FLAG);
126 #endif
129 static inline void
130 RARY_TRANSIENT_UNSET(VALUE ary)
132 #if USE_TRANSIENT_HEAP
133 FL_UNSET_RAW(ary, RARRAY_TRANSIENT_FLAG);
134 #endif
137 #undef rb_ary_new_from_args
138 #if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
139 # /* Skip it; clang -pedantic doesn't like the following */
140 #elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
141 #define rb_ary_new_from_args(n, ...) \
142 __extension__ ({ \
143 const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
144 if (__builtin_constant_p(n)) { \
145 STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
147 rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
149 #endif
151 #undef RARRAY_AREF
152 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
153 RBIMPL_ATTR_ARTIFICIAL()
154 static inline VALUE
155 RARRAY_AREF(VALUE ary, long i)
157 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
159 return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
162 #endif /* INTERNAL_ARRAY_H */