Ruby: internal/array.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

array.h
Go to the documentation of this file.
1#ifndef INTERNAL_ARRAY_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_ARRAY_H
12#include "ruby/internal/config.h"
13#include <stddef.h> /* for size_t */
14#include "internal/static_assert.h" /* for STATIC_ASSERT */
15#include "ruby/internal/stdbool.h" /* for bool */
16#include "ruby/ruby.h" /* for RARRAY_LEN */
17
18#ifndef ARRAY_DEBUG
19# define ARRAY_DEBUG (0+RUBY_DEBUG)
20#endif
21
22#define RARRAY_PTR_IN_USE_FLAG FL_USER14
23
24/* array.c */
25 VALUE rb_ary_last(int, const VALUE *, VALUE);
26void rb_ary_set_len(VALUE, long);
27void rb_ary_delete_same(VALUE, VALUE);
28 VALUE rb_ary_tmp_new_fill(long capa);
29 VALUE rb_ary_at(VALUE, VALUE);
30size_t rb_ary_memsize(VALUE);
31 VALUE rb_to_array_type(VALUE obj);
32void rb_ary_cancel_sharing(VALUE ary);
33
34static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
35static inline bool ARY_PTR_USING_P(VALUE ary);
36static inline void RARY_TRANSIENT_SET(VALUE ary);
37static inline void RARY_TRANSIENT_UNSET(VALUE ary);
38
39RUBY_SYMBOL_EXPORT_BEGIN
40/* array.c (export) */
41void rb_ary_detransient(VALUE a);
42 VALUE *rb_ary_ptr_use_start(VALUE ary);
43void rb_ary_ptr_use_end(VALUE ary);
44RUBY_SYMBOL_EXPORT_END
45
46 MJIT_SYMBOL_EXPORT_BEGIN
47 VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
48 VALUE rb_check_to_array(VALUE ary);
49 VALUE rb_ary_behead(VALUE, long);
50 VALUE rb_ary_aref1(VALUE ary, VALUE i);
51
52struct rb_execution_context_struct;
53 VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts);
54 MJIT_SYMBOL_EXPORT_END
55
56static inline VALUE
57rb_ary_entry_internal(VALUE ary, long offset)
58{
59 long len = RARRAY_LEN(ary);
60 const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
61 if (len == 0) return Qnil;
62 if (offset < 0) {
63 offset += len;
64 if (offset < 0) return Qnil;
65 }
66 else if (len <= offset) {
67 return Qnil;
68 }
69 return ptr[offset];
70}
71
72static inline bool
73ARY_PTR_USING_P(VALUE ary)
74{
75 return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
76}
77
78static inline void
79RARY_TRANSIENT_SET(VALUE ary)
80{
81#if USE_TRANSIENT_HEAP
82 FL_SET_RAW(ary, RARRAY_TRANSIENT_FLAG);
83#endif
84}
85
86static inline void
87RARY_TRANSIENT_UNSET(VALUE ary)
88{
89#if USE_TRANSIENT_HEAP
90 FL_UNSET_RAW(ary, RARRAY_TRANSIENT_FLAG);
91#endif
92}
93
94#undef rb_ary_new_from_args
95#if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
96# /* Skip it; clang -pedantic doesn't like the following */
97#elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
98#define rb_ary_new_from_args(n, ...) \
99 __extension__ ({ \
100 const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
101 if (__builtin_constant_p(n)) { \
102 STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
103 } \
104 rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
105 })
106#endif
107
108#undef RARRAY_AREF
109 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
110 RBIMPL_ATTR_ARTIFICIAL()
111static inline VALUE
112 RARRAY_AREF(VALUE ary, long i)
113{
114 RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
115
116 return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
117}
118
119#endif /* INTERNAL_ARRAY_H */
RBIMPL_ATTR_ARTIFICIAL
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition: artificial.h:43
ptr
struct RIMemo * ptr
Definition: debug.c:88
MJIT_SYMBOL_EXPORT_END
#define MJIT_SYMBOL_EXPORT_END
Definition: dllexport.h:63
MJIT_SYMBOL_EXPORT_BEGIN
#define MJIT_SYMBOL_EXPORT_BEGIN
Definition: dllexport.h:62
len
uint8_t len
Definition: escape.c:17
FL_UNSET_RAW
#define FL_UNSET_RAW
Definition: fl_type.h:133
FL_TEST_RAW
#define FL_TEST_RAW
Definition: fl_type.h:131
FL_SET_RAW
#define FL_SET_RAW
Definition: fl_type.h:129
config.h
Thin wrapper to ruby/config.h.
rb_ec_ary_new_from_values
VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts)
Definition: array.c:832
rb_check_to_array
VALUE rb_check_to_array(VALUE ary)
Definition: array.c:994
rb_to_array_type
VALUE rb_to_array_type(VALUE obj)
Definition: array.c:981
rb_ary_tmp_new_fill
VALUE rb_ary_tmp_new_fill(long capa)
Definition: array.c:854
rb_ary_last
VALUE rb_ary_last(int, const VALUE *, VALUE)
Definition: array.c:1932
rb_ary_set_len
void rb_ary_set_len(VALUE, long)
Definition: array.c:2212
rb_ary_detransient
void rb_ary_detransient(VALUE a)
Definition: array.c:432
rb_ary_ptr_use_start
VALUE * rb_ary_ptr_use_start(VALUE ary)
Definition: array.c:243
rb_ary_ptr_use_end
void rb_ary_ptr_use_end(VALUE ary)
Definition: array.c:252
rb_ary_delete_same
void rb_ary_delete_same(VALUE, VALUE)
Definition: array.c:3987
rb_ary_at
VALUE rb_ary_at(VALUE, VALUE)
Definition: array.c:1855
rb_ary_tmp_new_from_values
VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *)
Definition: array.c:774
rb_ary_memsize
size_t rb_ary_memsize(VALUE)
Definition: array.c:894
rb_ary_behead
VALUE rb_ary_behead(VALUE, long)
Definition: array.c:1521
rb_ary_cancel_sharing
void rb_ary_cancel_sharing(VALUE ary)
Definition: array.c:565
rb_ary_aref1
VALUE rb_ary_aref1(VALUE ary, VALUE i)
Definition: array.c:1823
RARRAY_PTR_IN_USE_FLAG
#define RARRAY_PTR_IN_USE_FLAG
Definition: array.h:22
offset
voidpf uLong offset
Definition: ioapi.h:144
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: psych_emitter.c:7
RBIMPL_ATTR_PURE_UNLESS_DEBUG
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE iff.
Definition: pure.h:38
RARRAY_TRANSIENT_FLAG
#define RARRAY_TRANSIENT_FLAG
Definition: rarray.h:50
RARRAY_LEN
#define RARRAY_LEN
Definition: rarray.h:52
RARRAY_CONST_PTR_TRANSIENT
#define RARRAY_CONST_PTR_TRANSIENT
Definition: rarray.h:54
ruby.h
Qnil
#define Qnil
Definition: special_consts.h:51
stdbool.h
C99 shim for <stdbool.h>
rb_execution_context_struct
Definition: vm_core.h:854
VALUE
unsigned long VALUE
Definition: value.h:38
RUBY_T_ARRAY
@ RUBY_T_ARRAY
Definition: value_type.h:119