Debian Codegraph @

include/ruby/internal/core/rarray.h

raw
1 #ifndef RBIMPL_RARRAY_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_RARRAY_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Defines struct ::RArray.
22 */
23 #include "ruby/internal/arithmetic/long.h"
24 #include "ruby/internal/attr/artificial.h"
25 #include "ruby/internal/attr/constexpr.h"
26 #include "ruby/internal/attr/maybe_unused.h"
27 #include "ruby/internal/attr/pure.h"
28 #include "ruby/internal/cast.h"
29 #include "ruby/internal/core/rbasic.h"
30 #include "ruby/internal/dllexport.h"
31 #include "ruby/internal/fl_type.h"
32 #include "ruby/internal/gc.h"
33 #include "ruby/internal/stdbool.h"
34 #include "ruby/internal/value.h"
35 #include "ruby/internal/value_type.h"
36 #include "ruby/assert.h"
37
38 /**
39 * Convenient casting macro.
40 *
41 * @param obj An object, which is in fact an ::RArray.
42 * @return The passed object casted to ::RArray.
43 */
44 #define RARRAY(obj) RBIMPL_CAST((struct RArray *)(obj))
45 /** @cond INTERNAL_MACRO */
46 #define RARRAY_EMBED_FLAG RARRAY_EMBED_FLAG
47 #define RARRAY_EMBED_LEN_MASK RARRAY_EMBED_LEN_MASK
48 #define RARRAY_EMBED_LEN_MAX RARRAY_EMBED_LEN_MAX
49 #define RARRAY_EMBED_LEN_SHIFT RARRAY_EMBED_LEN_SHIFT
50 /** @endcond */
51 #define RARRAY_LEN rb_array_len /**< @alias{rb_array_len} */
52 #define RARRAY_CONST_PTR rb_array_const_ptr /**< @alias{rb_array_const_ptr} */
53
54 /** @cond INTERNAL_MACRO */
55 #if defined(__fcc__) || defined(__fcc_version) || \
56 defined(__FCC__) || defined(__FCC_VERSION)
57 /* workaround for old version of Fujitsu C Compiler (fcc) */
58 # define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
59 #else
60 # define FIX_CONST_VALUE_PTR(x) (x)
61 #endif
62
63 #define RARRAY_EMBED_LEN RARRAY_EMBED_LEN
64 #define RARRAY_LENINT RARRAY_LENINT
65 #define RARRAY_ASET RARRAY_ASET
66 #define RARRAY_PTR RARRAY_PTR
67 /** @endcond */
68
69 /**
70 * @private
71 *
72 * Bits that you can set to ::RBasic::flags.
73 *
74 * @warning These enums are not the only bits we use for arrays.
75 *
76 * @internal
77 *
78 * Unlike strings, flag usages for arrays are scattered across the entire
79 * source codes. @shyouhei doesn't know the complete list. But what is listed
80 * here is at least incomplete.
81 */
82 enum ruby_rarray_flags {
83 /**
84 * This flag has something to do with memory footprint. If the array is
85 * "small" enough, ruby tries to be creative to abuse padding bits of
86 * struct ::RArray for storing its contents. This flag denotes that
87 * situation.
88 *
89 * @warning This bit has to be considered read-only. Setting/clearing
90 * this bit without corresponding fix up must cause immediate
91 * SEGV. Also, internal structures of an array change
92 * dynamically and transparently throughout of its lifetime.
93 * Don't assume it being persistent.
94 *
95 * @internal
96 *
97 * 3rd parties must not be aware that there even is more than one way to
98 * store array elements. It was a bad idea to expose this to them.
99 */
100 RARRAY_EMBED_FLAG = RUBY_FL_USER1,
101
102 /* RUBY_FL_USER2 is for ELTS_SHARED */
103
104 /**
105 * When an array employs embedded strategy (see ::RARRAY_EMBED_FLAG), these
106 * bits are used to store the number of elements actually filled into
107 * ::RArray::ary.
108 *
109 * @internal
110 *
111 * 3rd parties must not be aware that there even is more than one way to
112 * store array elements. It was a bad idea to expose this to them.
113 */
114 RARRAY_EMBED_LEN_MASK = RUBY_FL_USER9 | RUBY_FL_USER8 | RUBY_FL_USER7 | RUBY_FL_USER6 |
115 RUBY_FL_USER5 | RUBY_FL_USER4 | RUBY_FL_USER3
116 };
117
118 /**
119 * This is an enum because GDB wants it (rather than a macro). People need not
120 * bother.
121 */
122 enum ruby_rarray_consts {
123 /** Where ::RARRAY_EMBED_LEN_MASK resides. */
124 RARRAY_EMBED_LEN_SHIFT = RUBY_FL_USHIFT + 3
125 };
126
127 /** Ruby's array. */
128 struct RArray {
129
130 /** Basic part, including flags and class. */
131 struct RBasic basic;
132
133 /** Array's specific fields. */
134 union {
135
136 /**
137 * Arrays that use separated memory region for elements use this
138 * pattern.
139 */
140 struct {
141
142 /** Number of elements of the array. */
143 long len;
144
145 /** Auxiliary info. */
146 union {
147
148 /**
149 * Capacity of `*ptr`. A continuous memory region of at least
150 * `capa` elements is expected to exist at `*ptr`. This can be
151 * bigger than `len`.
152 */
153 long capa;
154
155 /**
156 * Parent of the array. Nowadays arrays can share their
157 * backend memory regions each other, constructing gigantic
158 * nest of objects. This situation is called "shared", and
159 * this is the field to control such properties.
160 */
161 #if defined(__clang__) /* <- clang++ is sane */ || \
162 !defined(__cplusplus) /* <- C99 is sane */ || \
163 (__cplusplus > 199711L) /* <- C++11 is sane */
164 const
165 #endif
166 VALUE shared_root;
167 } aux;
168
169 /**
170 * Pointer to the C array that holds the elements of the array. In
171 * the old days each array had dedicated memory regions. That is
172 * no longer true today, but there still are arrays of such
173 * properties. This field could be used to point such things.
174 */
175 const VALUE *ptr;
176 } heap;
177
178 /**
179 * Embedded elements. When an array is short enough, it uses this area
180 * to store its elements. In this case the length is encoded into the
181 * flags.
182 */
183 /* This is a length 1 array because:
184 * 1. GCC has a bug that does not optimize C flexible array members
185 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
186 * 2. Zero length arrays are not supported by all compilers
187 */
188 const VALUE ary[1];
189 } as;
190 };
191
192 RBIMPL_SYMBOL_EXPORT_BEGIN()
193 /**
194 * @private
195 *
196 * Declares a section of code where raw pointers are used. This is an
197 * implementation detail of #RARRAY_PTR_USE. People don't use it directly.
198 *
199 * @param[in] ary An object of ::RArray.
200 * @return `ary`'s backend C array.
201 */
202 VALUE *rb_ary_ptr_use_start(VALUE ary);
203
204 /**
205 * @private
206 *
207 * Declares an end of a section formerly started by rb_ary_ptr_use_start().
208 * This is an implementation detail of #RARRAY_PTR_USE. People don't use it
209 * directly.
210 *
211 * @param[in] a An object of ::RArray.
212 */
213 void rb_ary_ptr_use_end(VALUE a);
214
215 RBIMPL_SYMBOL_EXPORT_END()
216
217 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
218 RBIMPL_ATTR_ARTIFICIAL()
219 /**
220 * Queries the length of the array.
221 *
222 * @param[in] ary Array in question.
223 * @return Its number of elements.
224 * @pre `ary` must be an instance of ::RArray, and must has its
225 * ::RARRAY_EMBED_FLAG flag set.
226 *
227 * @internal
228 *
229 * This was a macro before. It was inevitable to be public, since macros are
230 * global constructs. But should it be forever? Now that it is a function,
231 * @shyouhei thinks it could just be eliminated, hidden into implementation
232 * details.
233 */
234 static inline long
235 RARRAY_EMBED_LEN(VALUE ary)
236 {
237 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
238 RBIMPL_ASSERT_OR_ASSUME(RB_FL_ANY_RAW(ary, RARRAY_EMBED_FLAG));
239
240 VALUE f = RBASIC(ary)->flags;
241 f &= RARRAY_EMBED_LEN_MASK;
242 f >>= RARRAY_EMBED_LEN_SHIFT;
243 return RBIMPL_CAST((long)f);
244 }
245
246 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
247 /**
248 * Queries the length of the array.
249 *
250 * @param[in] a Array in question.
251 * @return Its number of elements.
252 * @pre `a` must be an instance of ::RArray.
253 */
254 static inline long
255 rb_array_len(VALUE a)
256 {
257 RBIMPL_ASSERT_TYPE(a, RUBY_T_ARRAY);
258
259 if (RB_FL_ANY_RAW(a, RARRAY_EMBED_FLAG)) {
260 return RARRAY_EMBED_LEN(a);
261 }
262 else {
263 return RARRAY(a)->as.heap.len;
264 }
265 }
266
267 RBIMPL_ATTR_ARTIFICIAL()
268 /**
269 * Identical to rb_array_len(), except it differs for the return type.
270 *
271 * @param[in] ary Array in question.
272 * @exception rb_eRangeError Too long.
273 * @return Its number of elements.
274 * @pre `ary` must be an instance of ::RArray.
275 *
276 * @internal
277 *
278 * This API seems redundant but has actual usages.
279 */
280 static inline int
281 RARRAY_LENINT(VALUE ary)
282 {
283 return rb_long2int(RARRAY_LEN(ary));
284 }
285
286 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
287 /**
288 * @private
289 *
290 * This is an implementation detail of RARRAY_PTR(). People do not use it
291 * directly.
292 *
293 * @param[in] a An object of ::RArray.
294 * @return Its backend storage.
295 */
296 static inline const VALUE *
297 rb_array_const_ptr(VALUE a)
298 {
299 RBIMPL_ASSERT_TYPE(a, RUBY_T_ARRAY);
300
301 if (RB_FL_ANY_RAW(a, RARRAY_EMBED_FLAG)) {
302 return FIX_CONST_VALUE_PTR(RARRAY(a)->as.ary);
303 }
304 else {
305 return FIX_CONST_VALUE_PTR(RARRAY(a)->as.heap.ptr);
306 }
307 }
308
309 /**
310 * @private
311 *
312 * This is an implementation detail of #RARRAY_PTR_USE. People do not use it
313 * directly.
314 */
315 #define RBIMPL_RARRAY_STMT(ary, var, expr) do { \
316 RBIMPL_ASSERT_TYPE((ary), RUBY_T_ARRAY); \
317 const VALUE rbimpl_ary = (ary); \
318 VALUE *var = rb_ary_ptr_use_start(rbimpl_ary); \
319 expr; \
320 rb_ary_ptr_use_end(rbimpl_ary); \
321 } while (0)
322
323 /**
324 * Declares a section of code where raw pointers are used. In case you need to
325 * touch the raw C array instead of polite CAPIs, then that operation shall be
326 * wrapped using this macro.
327 *
328 * ```CXX
329 * const auto ary = rb_eval_string("[...]");
330 * const auto len = RARRAY_LENINT(ary);
331 * const auto symwrite = rb_intern("write");
332 *
333 * RARRAY_PTR_USE(ary, ptr, {
334 * rb_funcallv(rb_stdout, symwrite, len, ptr);
335 * });
336 * ```
337 *
338 * @param ary An object of ::RArray.
339 * @param ptr_name A variable name which points the C array in `expr`.
340 * @param expr The expression that touches `ptr_name`.
341 *
342 * @internal
343 *
344 * For historical reasons use of this macro is not enforced. There are
345 * extension libraries in the wild which call RARRAY_PTR() without it. We want
346 * them use it... Maybe some transition path can be implemented later.
347 */
348 #define RARRAY_PTR_USE(ary, ptr_name, expr) \
349 RBIMPL_RARRAY_STMT(ary, ptr_name, expr)
350
351 /**
352 * Wild use of a C pointer. This function accesses the backend storage
353 * directly. This is slower than #RARRAY_PTR_USE. It exercises
354 * extra manoeuvres to protect our generational GC. Use of this function is
355 * considered archaic. Use a modern way instead.
356 *
357 * @param[in] ary An object of ::RArray.
358 * @return The backend C array.
359 *
360 * @internal
361 *
362 * That said... there are extension libraries in the wild who uses it. We
363 * cannot but continue supporting.
364 */
365 static inline VALUE *
366 RARRAY_PTR(VALUE ary)
367 {
368 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
369
370 VALUE tmp = RB_OBJ_WB_UNPROTECT_FOR(ARRAY, ary);
371 return RBIMPL_CAST((VALUE *)RARRAY_CONST_PTR(tmp));
372 }
373
374 /**
375 * Assigns an object in an array.
376 *
377 * @param[out] ary Destination array object.
378 * @param[in] i Index of `ary`.
379 * @param[in] v Arbitrary ruby object.
380 * @pre `ary` must be an instance of ::RArray.
381 * @pre `ary`'s length must be longer than or equal to `i`.
382 * @pre `i` must be greater than or equal to zero.
383 * @post `ary`'s `i`th element is set to `v`.
384 */
385 static inline void
386 RARRAY_ASET(VALUE ary, long i, VALUE v)
387 {
388 RARRAY_PTR_USE(ary, ptr,
389 RB_OBJ_WRITE(ary, &ptr[i], v));
390 }
391
392 /**
393 * @deprecated
394 *
395 * :FIXME: we want to convert RARRAY_AREF into an inline function (to add rooms
396 * for more sanity checks). However there were situations where the address of
397 * this macro is taken i.e. &RARRAY_AREF(...). They cannot be possible if this
398 * is not a macro. Such usages are abuse, and we eliminated them internally.
399 * However we are afraid of similar things to remain in the wild. This macro
400 * remains as it is due to that. If we could warn such usages we can set a
401 * transition path, but currently no way is found to do so.
402 */
403 #define RARRAY_AREF(a, i) RARRAY_CONST_PTR(a)[i]
404
405 #endif /* RBIMPL_RARRAY_H */