Ruby 4.1.0dev (2026-08-15 revision d1c079751b80352d347452c8d134ffb177838adb)
rarray.h
Go to the documentation of this file.
1#ifndef RBIMPL_RARRAY_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_RARRAY_H
28#include "ruby/internal/cast.h"
32#include "ruby/internal/gc.h"
34#include "ruby/internal/value.h"
36#include "ruby/assert.h"
37
44#define RARRAY(obj) RBIMPL_CAST((struct RArray *)(obj))
46#define RARRAY_EMBED_FLAG RARRAY_EMBED_FLAG
47#define RARRAY_EMBED_LEN_MASK RARRAY_EMBED_LEN_MASK
48#define RARRAY_EMBED_LEN_SHIFT RARRAY_EMBED_LEN_SHIFT
50#define RARRAY_LEN rb_array_len
51#define RARRAY_CONST_PTR rb_array_const_ptr
54#if defined(__fcc__) || defined(__fcc_version) || \
55 defined(__FCC__) || defined(__FCC_VERSION)
56/* workaround for old version of Fujitsu C Compiler (fcc) */
57# define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
58#else
59# define FIX_CONST_VALUE_PTR(x) (x)
60#endif
61
62#define RARRAY_EMBED_LEN RARRAY_EMBED_LEN
63#define RARRAY_LENINT RARRAY_LENINT
64#define RARRAY_ASET RARRAY_ASET
65#define RARRAY_PTR RARRAY_PTR
81enum ruby_rarray_flags {
82 /* RUBY_FL_USER0 is for ELTS_SHARED */
83
101 RARRAY_EMBED_FLAG = RUBY_FL_USER1,
102
113 RARRAY_EMBED_LEN_MASK = RUBY_FL_USER9 | RUBY_FL_USER8 | RUBY_FL_USER7 | RUBY_FL_USER6 |
115};
116
125
127struct RArray {
128
130 struct RBasic basic;
131
133 union {
134
139 struct {
140
142 long len;
143
145 union {
146
152 long capa;
153
160#if defined(__clang__) /* <- clang++ is sane */ || \
161 !defined(__cplusplus) /* <- C99 is sane */ || \
162 (__cplusplus > 199711L) /* <- C++11 is sane */
163 const
164#endif
166 } aux;
167
174 const VALUE *ptr;
175 } heap;
176
182 /* This is a length 1 array because:
183 * 1. GCC has a bug that does not optimize C flexible array members
184 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
185 * 2. Zero length arrays are not supported by all compilers
186 */
187 const VALUE ary[1];
188 } as;
189};
190
201VALUE *rb_ary_ptr_use_start(VALUE ary);
202
212void rb_ary_ptr_use_end(VALUE a);
213
215
233static inline long
235{
236 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
237 RBIMPL_ASSERT_OR_ASSUME(RB_FL_ANY_RAW(ary, RARRAY_EMBED_FLAG));
238
239 VALUE f = RBASIC(ary)->flags;
240 f &= RARRAY_EMBED_LEN_MASK;
242 return RBIMPL_CAST((long)f);
243}
244
253static inline long
255{
256 RBIMPL_ASSERT_TYPE(a, RUBY_T_ARRAY);
257
258 if (RB_FL_ANY_RAW(a, RARRAY_EMBED_FLAG)) {
259 return RARRAY_EMBED_LEN(a);
260 }
261 else {
262 return RARRAY(a)->as.heap.len;
263 }
264}
265
279static inline int
281{
282 return rb_long2int(RARRAY_LEN(ary));
283}
284
295static inline const VALUE *
296rb_array_const_ptr(VALUE a)
297{
298 RBIMPL_ASSERT_TYPE(a, RUBY_T_ARRAY);
299
300 if (RB_FL_ANY_RAW(a, RARRAY_EMBED_FLAG)) {
301 return FIX_CONST_VALUE_PTR(RARRAY(a)->as.ary);
302 }
303 else {
304 return FIX_CONST_VALUE_PTR(RARRAY(a)->as.heap.ptr);
305 }
306}
307
314#define RBIMPL_RARRAY_STMT(ary, var, expr) do { \
315 RBIMPL_ASSERT_TYPE((ary), RUBY_T_ARRAY); \
316 const VALUE rbimpl_ary = (ary); \
317 VALUE *var = rb_ary_ptr_use_start(rbimpl_ary); \
318 expr; \
319 rb_ary_ptr_use_end(rbimpl_ary); \
320} while (0)
321
347#define RARRAY_PTR_USE(ary, ptr_name, expr) \
348 RBIMPL_RARRAY_STMT(ary, ptr_name, expr)
349
364static inline VALUE *
366{
367 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
368
369 VALUE tmp = RB_OBJ_WB_UNPROTECT(ary);
370 return RBIMPL_CAST((VALUE *)RARRAY_CONST_PTR(tmp));
371}
372
384static inline void
385RARRAY_ASET(VALUE ary, long i, VALUE v)
386{
387 RARRAY_PTR_USE(ary, ptr,
388 RB_OBJ_WRITE(ary, &ptr[i], v));
389}
390
402#define RARRAY_AREF(a, i) RARRAY_CONST_PTR(a)[i]
403
404#endif /* RBIMPL_RARRAY_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:311
RBIMPL_ATTR_CONSTEXPR.
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
Defines enum ruby_fl_type.
@ RUBY_FL_USHIFT
Number of bits in ruby_fl_type that are not open to users.
Definition fl_type.h:146
static bool RB_FL_ANY_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_ANY().
Definition fl_type.h:453
@ RUBY_FL_USER9
User-defined flag.
Definition fl_type.h:291
@ RUBY_FL_USER8
User-defined flag.
Definition fl_type.h:290
@ RUBY_FL_USER5
User-defined flag.
Definition fl_type.h:287
@ RUBY_FL_USER3
User-defined flag.
Definition fl_type.h:285
@ RUBY_FL_USER7
User-defined flag.
Definition fl_type.h:289
@ RUBY_FL_USER6
User-defined flag.
Definition fl_type.h:288
@ RUBY_FL_USER4
User-defined flag.
Definition fl_type.h:286
@ RUBY_FL_USER1
User-defined flag.
Definition fl_type.h:283
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:456
Registering values to the GC.
#define RB_OBJ_WB_UNPROTECT(x)
Marks the object as not protected by write barriers.
Definition gc.h:483
Arithmetic conversion between C's long and Ruby's.
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
Defines RBIMPL_ATTR_MAYBE_UNUSED.
Defines RBIMPL_ATTR_PURE.
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition pure.h:38
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
static long RARRAY_EMBED_LEN(VALUE ary)
Queries the length of the array.
Definition rarray.h:234
static long rb_array_len(VALUE a)
Queries the length of the array.
Definition rarray.h:254
#define RARRAY(obj)
Convenient casting macro.
Definition rarray.h:44
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:280
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
Definition rarray.h:385
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
Definition rarray.h:347
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
Definition rarray.h:365
ruby_rarray_consts
This is an enum because GDB wants it (rather than a macro).
Definition rarray.h:121
@ RARRAY_EMBED_LEN_SHIFT
Where RARRAY_EMBED_LEN_MASK resides.
Definition rarray.h:123
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
Defines struct RBasic.
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
C99 shim for <stdbool.h>
Ruby's array.
Definition rarray.h:127
struct RBasic basic
Basic part, including flags and class.
Definition rarray.h:130
union RArray::@55 as
Array's specific fields.
const VALUE shared_root
Parent of the array.
Definition rarray.h:165
long capa
Capacity of *ptr.
Definition rarray.h:152
long len
Number of elements of the array.
Definition rarray.h:142
const VALUE * ptr
Pointer to the C array that holds the elements of the array.
Definition rarray.h:174
Ruby object's base components.
Definition rbasic.h:69
Defines VALUE and ID.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Defines enum ruby_value_type.
@ RUBY_T_ARRAY
Definition value_type.h:122