summaryrefslogtreecommitdiff
path: root/internal/array.h
blob: 97b707f271e074384d409359d7d3a40a1ef6da83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef INTERNAL_ARRAY_H                                 /*-*-C-*-vi:se ft=c:*/
#define INTERNAL_ARRAY_H
/**
 * @file
 * @author     Ruby developers <ruby-core@ruby-lang.org>
 * @copyright  This  file  is   a  part  of  the   programming  language  Ruby.
 *             Permission  is hereby  granted,  to  either redistribute  and/or
 *             modify this file, provided that  the conditions mentioned in the
 *             file COPYING are met.  Consult the file for details.
 * @brief      Internal header for Array.
 */
#include "ruby/internal/config.h"
#include <stddef.h>                 /* for size_t */
#include "internal/static_assert.h" /* for STATIC_ASSERT */
#include "ruby/internal/stdbool.h"         /* for bool */
#include "ruby/ruby.h"              /* for RARRAY_LEN */

#ifndef ARRAY_DEBUG
# define ARRAY_DEBUG (0+RUBY_DEBUG)
#endif

#define RARRAY_PTR_IN_USE_FLAG FL_USER14

/* array.c */
VALUE rb_ary_last(int, const VALUE *, VALUE);
void rb_ary_set_len(VALUE, long);
void rb_ary_delete_same(VALUE, VALUE);
VALUE rb_ary_tmp_new_fill(long capa);
VALUE rb_ary_at(VALUE, VALUE);
size_t rb_ary_memsize(VALUE);
VALUE rb_to_array_type(VALUE obj);
VALUE rb_to_array(VALUE obj);
void rb_ary_cancel_sharing(VALUE ary);

static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
static inline bool ARY_PTR_USING_P(VALUE ary);
static inline void RARY_TRANSIENT_SET(VALUE ary);
static inline void RARY_TRANSIENT_UNSET(VALUE ary);

RUBY_SYMBOL_EXPORT_BEGIN
/* array.c (export) */
RUBY_SYMBOL_EXPORT_END

MJIT_SYMBOL_EXPORT_BEGIN
VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
VALUE rb_check_to_array(VALUE ary);
VALUE rb_ary_behead(VALUE, long);
VALUE rb_ary_aref1(VALUE ary, VALUE i);

struct rb_execution_context_struct;
VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts);
MJIT_SYMBOL_EXPORT_END

static inline VALUE
rb_ary_entry_internal(VALUE ary, long offset)
{
    long len = RARRAY_LEN(ary);
    const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
    if (len == 0) return Qnil;
    if (offset < 0) {
        offset += len;
        if (offset < 0) return Qnil;
    }
    else if (len <= offset) {
        return Qnil;
    }
    return ptr[offset];
}

static inline bool
ARY_PTR_USING_P(VALUE ary)
{
    return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
}

static inline void
RARY_TRANSIENT_SET(VALUE ary)
{
#if USE_TRANSIENT_HEAP
    FL_SET_RAW(ary, RARRAY_TRANSIENT_FLAG);
#endif
}

static inline void
RARY_TRANSIENT_UNSET(VALUE ary)
{
#if USE_TRANSIENT_HEAP
    FL_UNSET_RAW(ary, RARRAY_TRANSIENT_FLAG);
#endif
}

#undef rb_ary_new_from_args
#if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
# /* Skip it; clang -pedantic doesn't like the following */
#elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
#define rb_ary_new_from_args(n, ...) \
    __extension__ ({ \
        const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
        if (__builtin_constant_p(n)) { \
            STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
        } \
        rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
    })
#endif

#undef RARRAY_AREF
RBIMPL_ATTR_PURE_UNLESS_DEBUG()
RBIMPL_ATTR_ARTIFICIAL()
static inline VALUE
RARRAY_AREF(VALUE ary, long i)
{
    RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);

    return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
}

#endif /* INTERNAL_ARRAY_H */