Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
struct.h
Go to the documentation of this file.
1#ifndef INTERNAL_STRUCT_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_STRUCT_H
12#include "ruby/internal/stdbool.h" /* for bool */
13#include "internal/gc.h" /* for RB_OBJ_WRITE */
14#include "ruby/ruby.h" /* for struct RBasic */
15
16enum {
18 RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1),
21};
22
23struct RStruct {
24 struct RBasic basic;
25 union {
26 struct {
27 long len;
28 const VALUE *ptr;
29 } heap;
31 } as;
32};
33
34#define RSTRUCT(obj) ((struct RStruct *)(obj))
35
36#ifdef RSTRUCT_LEN
37# undef RSTRUCT_LEN
38#endif
39
40#ifdef RSTRUCT_PTR
41# undef RSTRUCT_PTR
42#endif
43
44#ifdef RSTRUCT_SET
45# undef RSTRUCT_SET
46#endif
47
48#ifdef RSTRUCT_GET
49# undef RSTRUCT_GET
50#endif
51
52#define RSTRUCT_LEN internal_RSTRUCT_LEN
53#define RSTRUCT_SET internal_RSTRUCT_SET
54#define RSTRUCT_GET internal_RSTRUCT_GET
55
56/* struct.c */
60static inline const VALUE *rb_struct_const_heap_ptr(VALUE st);
61static inline bool RSTRUCT_TRANSIENT_P(VALUE st);
62static inline void RSTRUCT_TRANSIENT_SET(VALUE st);
63static inline void RSTRUCT_TRANSIENT_UNSET(VALUE st);
64static inline long RSTRUCT_EMBED_LEN(VALUE st);
65static inline long RSTRUCT_LEN(VALUE st);
66static inline int RSTRUCT_LENINT(VALUE st);
67static inline const VALUE *RSTRUCT_CONST_PTR(VALUE st);
68static inline void RSTRUCT_SET(VALUE st, long k, VALUE v);
69static inline VALUE RSTRUCT_GET(VALUE st, long k);
70
71static inline bool
72RSTRUCT_TRANSIENT_P(VALUE st)
73{
74#if USE_TRANSIENT_HEAP
76#else
77 return false;
78#endif
79}
80
81static inline void
82RSTRUCT_TRANSIENT_SET(VALUE st)
83{
84#if USE_TRANSIENT_HEAP
86#endif
87}
88
89static inline void
90RSTRUCT_TRANSIENT_UNSET(VALUE st)
91{
92#if USE_TRANSIENT_HEAP
94#endif
95}
96
97static inline long
98RSTRUCT_EMBED_LEN(VALUE st)
99{
100 long ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK);
102 return ret;
103}
104
105static inline long
107{
109 return RSTRUCT_EMBED_LEN(st);
110 }
111 else {
112 return RSTRUCT(st)->as.heap.len;
113 }
114}
115
116static inline int
117RSTRUCT_LENINT(VALUE st)
118{
119 return rb_long2int(RSTRUCT_LEN(st));
120}
121
122static inline const VALUE *
123RSTRUCT_CONST_PTR(VALUE st)
124{
125 const struct RStruct *p = RSTRUCT(st);
126
128 return p->as.ary;
129 }
130 else {
131 return p->as.heap.ptr;
132 }
133}
134
135static inline void
136RSTRUCT_SET(VALUE st, long k, VALUE v)
137{
138 RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v);
139}
140
141static inline VALUE
142RSTRUCT_GET(VALUE st, long k)
143{
144 return RSTRUCT_CONST_PTR(st)[k];
145}
146
147static inline const VALUE *
148rb_struct_const_heap_ptr(VALUE st)
149{
150 /* TODO: check embed on debug mode */
151 return RSTRUCT(st)->as.heap.ptr;
152}
153
154#endif /* INTERNAL_STRUCT_H */
@ RUBY_FL_USHIFT
Definition: fl_type.h:150
#define FL_USER3
Definition: fl_type.h:66
#define FL_UNSET_RAW
Definition: fl_type.h:133
#define FL_TEST_RAW
Definition: fl_type.h:131
#define FL_SET_RAW
Definition: fl_type.h:129
#define RSTRUCT_GET
Definition: struct.h:54
VALUE rb_struct_init_copy(VALUE copy, VALUE s)
Definition: struct.c:1010
#define RSTRUCT_LEN
Definition: struct.h:52
#define RSTRUCT(obj)
Definition: struct.h:34
#define RSTRUCT_SET
Definition: struct.h:53
VALUE rb_struct_lookup(VALUE s, VALUE idx)
Definition: struct.c:1136
@ RSTRUCT_EMBED_LEN_MASK
Definition: struct.h:18
@ RSTRUCT_EMBED_LEN_MAX
Definition: struct.h:17
@ RSTRUCT_TRANSIENT_FLAG
Definition: struct.h:20
@ RSTRUCT_EMBED_LEN_SHIFT
Definition: struct.h:19
VALUE rb_struct_s_keyword_init(VALUE klass)
Definition: struct.c:60
#define rb_long2int
Definition: long.h:62
#define RVALUE_EMBED_LEN_MAX
Definition: rbasic.h:36
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
C99 shim for <stdbool.h>
Definition: rbasic.h:47
Definition: struct.h:23
struct RBasic basic
Definition: struct.h:24
const VALUE * ptr
Definition: struct.h:28
const VALUE ary[RSTRUCT_EMBED_LEN_MAX]
Definition: struct.h:30
union RStruct::@117 as
long len
Definition: struct.h:27
struct RStruct::@117::@118 heap
unsigned long VALUE
Definition: value.h:38