Ruby 4.1.0dev (2026-11-17 revision cd5a4468da76f4942b4796686f51c9f6fe59b0d7)
struct.h
1#ifndef INTERNAL_STRUCT_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_STRUCT_H
11#include "internal/gc.h" /* for RB_OBJ_ATOMIC_WRITE */
12#include "ruby/internal/stdbool.h" /* for bool */
13#include "ruby/ruby.h" /* for struct RBasic */
14
15/* Flags of RStruct
16 *
17 * 1-7: RSTRUCT_EMBED_LEN
18 * If non-zero, the struct is embedded (its contents follow the
19 * header, rather than being on a separately allocated buffer) and
20 * these bits are the length of the Struct.
21 */
22enum ruby_rstruct_flags {
23 RSTRUCT_EMBED_LEN_MASK = RUBY_FL_USER7 | RUBY_FL_USER6 | RUBY_FL_USER5 | RUBY_FL_USER4 |
25 RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1),
26};
27
28struct RStruct {
29 struct RBasic basic;
30 VALUE fields_obj;
31 union {
32 struct {
33 rb_len_t len;
34 const VALUE *ptr;
35 } heap;
36 /* This is a length 1 array because:
37 * 1. GCC has a bug that does not optimize C flexible array members
38 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
39 * 2. Zero length arrays are not supported by all compilers
40 */
41 const VALUE ary[1];
42 } as;
43};
44
45#define RSTRUCT(obj) ((struct RStruct *)(obj))
46
47/* struct.c */
48VALUE rb_struct_init_copy(VALUE copy, VALUE s);
49VALUE rb_struct_lookup(VALUE s, VALUE idx);
50VALUE rb_struct_s_keyword_init(VALUE klass);
51void rb_struct_define_aref_method(VALUE nstr, ID name, unsigned int off);
52
53static inline rb_len_t RSTRUCT_EMBED_LEN(VALUE st);
54static inline rb_len_t RSTRUCT_LEN_RAW(VALUE st);
55static inline int RSTRUCT_LENINT(VALUE st);
56static inline const VALUE *RSTRUCT_CONST_PTR(VALUE st);
57static inline void RSTRUCT_SET_RAW(VALUE st, rb_len_t k, VALUE v);
58static inline VALUE RSTRUCT_GET_RAW(VALUE st, rb_len_t k);
59
60static inline rb_len_t
61RSTRUCT_EMBED_LEN(VALUE st)
62{
63 rb_len_t ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK);
64 ret >>= RSTRUCT_EMBED_LEN_SHIFT;
65 return ret;
66}
67
68static inline rb_len_t
69RSTRUCT_LEN_RAW(VALUE st)
70{
71 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
72 return RSTRUCT_EMBED_LEN(st);
73 }
74 else {
75 return RSTRUCT(st)->as.heap.len;
76 }
77}
78
79static inline int
80RSTRUCT_LENINT(VALUE st)
81{
82 return rb_len2int(RSTRUCT_LEN_RAW(st));
83}
84
85static inline const VALUE *
86RSTRUCT_CONST_PTR(VALUE st)
87{
88 const struct RStruct *p = RSTRUCT(st);
89
90 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
91 return p->as.ary;
92 }
93 else {
94 return p->as.heap.ptr;
95 }
96}
97
98static inline void
99RSTRUCT_SET_RAW(VALUE st, rb_len_t k, VALUE v)
100{
101 RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v);
102}
103
104static inline VALUE
105RSTRUCT_GET_RAW(VALUE st, rb_len_t k)
106{
107 return RSTRUCT_CONST_PTR(st)[k];
108}
109
110static inline VALUE
111RSTRUCT_FIELDS_OBJ(VALUE st)
112{
113 return rbimpl_atomic_value_load(&RSTRUCT(st)->fields_obj, RBIMPL_ATOMIC_ACQUIRE);
114}
115
116static inline void
117RSTRUCT_SET_FIELDS_OBJ(VALUE st, VALUE fields_obj)
118{
119 RB_OBJ_ATOMIC_WRITE(st, &RSTRUCT(st)->fields_obj, fields_obj);
120}
121#endif /* INTERNAL_STRUCT_H */
@ RUBY_FL_USHIFT
Number of bits in ruby_fl_type that are not open to users.
Definition fl_type.h:146
@ 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_USER2
User-defined flag.
Definition fl_type.h:284
@ RUBY_FL_USER4
User-defined flag.
Definition fl_type.h:286
@ RUBY_FL_USER1
User-defined flag.
Definition fl_type.h:283
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
int off
Offset inside of ptr.
Definition io.h:5
#define rb_len2int
Just another name of rb_len2int_inline.
Definition len_t.h:37
C99 shim for <stdbool.h>.
Ruby object's base components.
Definition rbasic.h:69
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40