Ruby  3.1.4p223 (2023-03-30 revision HEAD)
array.h
1 #ifndef INTERNAL_ARRAY_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_ARRAY_H
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 */
16 
17 #ifndef ARRAY_DEBUG
18 # define ARRAY_DEBUG (0+RUBY_DEBUG)
19 #endif
20 
21 #define RARRAY_PTR_IN_USE_FLAG FL_USER14
22 
23 /* array.c */
24 VALUE rb_ary_last(int, const VALUE *, VALUE);
25 void rb_ary_set_len(VALUE, long);
26 void rb_ary_delete_same(VALUE, VALUE);
27 VALUE rb_ary_tmp_new_fill(long capa);
28 VALUE rb_ary_at(VALUE, VALUE);
29 size_t rb_ary_memsize(VALUE);
30 VALUE rb_to_array_type(VALUE obj);
31 VALUE rb_to_array(VALUE obj);
32 void rb_ary_cancel_sharing(VALUE ary);
33 
34 static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
35 static inline bool ARY_PTR_USING_P(VALUE ary);
36 static inline void RARY_TRANSIENT_SET(VALUE ary);
37 static inline void RARY_TRANSIENT_UNSET(VALUE ary);
38 
39 MJIT_SYMBOL_EXPORT_BEGIN
40 VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
41 VALUE rb_check_to_array(VALUE ary);
42 VALUE rb_ary_behead(VALUE, long);
43 VALUE rb_ary_aref1(VALUE ary, VALUE i);
44 
46 VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts);
47 MJIT_SYMBOL_EXPORT_END
48 
49 // YJIT needs this function to never allocate and never raise
50 static inline VALUE
51 rb_ary_entry_internal(VALUE ary, long offset)
52 {
53  long len = RARRAY_LEN(ary);
54  const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
55  if (len == 0) return Qnil;
56  if (offset < 0) {
57  offset += len;
58  if (offset < 0) return Qnil;
59  }
60  else if (len <= offset) {
61  return Qnil;
62  }
63  return ptr[offset];
64 }
65 
66 static inline bool
67 ARY_PTR_USING_P(VALUE ary)
68 {
69  return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
70 }
71 
72 static inline void
73 RARY_TRANSIENT_SET(VALUE ary)
74 {
75 #if USE_TRANSIENT_HEAP
76  FL_SET_RAW(ary, RARRAY_TRANSIENT_FLAG);
77 #endif
78 }
79 
80 static inline void
81 RARY_TRANSIENT_UNSET(VALUE ary)
82 {
83 #if USE_TRANSIENT_HEAP
84  FL_UNSET_RAW(ary, RARRAY_TRANSIENT_FLAG);
85 #endif
86 }
87 
88 #undef rb_ary_new_from_args
89 #if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
90 # /* Skip it; clang -pedantic doesn't like the following */
91 #elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
92 #define rb_ary_new_from_args(n, ...) \
93  __extension__ ({ \
94  const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
95  if (__builtin_constant_p(n)) { \
96  STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
97  } \
98  rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
99  })
100 #endif
101 
102 #undef RARRAY_AREF
105 static inline VALUE
106 RARRAY_AREF(VALUE ary, long i)
107 {
108  RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
109 
110  return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
111 }
112 
113 #endif /* INTERNAL_ARRAY_H */
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition: artificial.h:41
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition: fl_type.h:142
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition: fl_type.h:140
#define Qnil
Old name of RUBY_Qnil.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition: fl_type.h:138
#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:68
#define RARRAY_CONST_PTR_TRANSIENT
Just another name of rb_array_const_ptr_transient.
Definition: rarray.h:70
#define RARRAY_AREF(a, i)
Definition: rarray.h:588
C99 shim for <stdbool.h>
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
@ RUBY_T_ARRAY
Definition: value_type.h:121