Ruby: internal/struct.h Source File

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 {
17 RSTRUCT_EMBED_LEN_MAX = RVALUE_EMBED_LEN_MAX,
18 RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1),
19 RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1),
20 RSTRUCT_TRANSIENT_FLAG = FL_USER3,
21};
22
23 struct RStruct {
24 struct RBasic basic;
25 union {
26 struct {
27 long len;
28 const VALUE *ptr;
29 } heap;
30 const VALUE ary[RSTRUCT_EMBED_LEN_MAX];
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 */
57 VALUE rb_struct_init_copy(VALUE copy, VALUE s);
58 VALUE rb_struct_lookup(VALUE s, VALUE idx);
59 VALUE rb_struct_s_keyword_init(VALUE klass);
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
75 return FL_TEST_RAW(st, RSTRUCT_TRANSIENT_FLAG);
76#else
77 return false;
78#endif
79}
80
81static inline void
82RSTRUCT_TRANSIENT_SET(VALUE st)
83{
84#if USE_TRANSIENT_HEAP
85 FL_SET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
86#endif
87}
88
89static inline void
90RSTRUCT_TRANSIENT_UNSET(VALUE st)
91{
92#if USE_TRANSIENT_HEAP
93 FL_UNSET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
94#endif
95}
96
97static inline long
98RSTRUCT_EMBED_LEN(VALUE st)
99{
100 long ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK);
101 ret >>= RSTRUCT_EMBED_LEN_SHIFT;
102 return ret;
103}
104
105static inline long
106 RSTRUCT_LEN(VALUE st)
107{
108 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
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
127 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
128 return p->as.ary;
129 }
130 else {
131 return p->as.heap.ptr;
132 }
133}
134
135static inline void
136 RSTRUCT_SET(VALUE st, long k, VALUE v)
137{
138 RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v);
139}
140
141static inline VALUE
142 RSTRUCT_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
@ RUBY_FL_USHIFT
Definition: fl_type.h:150
FL_USER3
#define FL_USER3
Definition: fl_type.h:66
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
RSTRUCT_GET
#define RSTRUCT_GET
Definition: struct.h:54
rb_struct_init_copy
VALUE rb_struct_init_copy(VALUE copy, VALUE s)
Definition: struct.c:1010
RSTRUCT_LEN
#define RSTRUCT_LEN
Definition: struct.h:52
RSTRUCT
#define RSTRUCT(obj)
Definition: struct.h:34
RSTRUCT_SET
#define RSTRUCT_SET
Definition: struct.h:53
rb_struct_lookup
VALUE rb_struct_lookup(VALUE s, VALUE idx)
Definition: struct.c:1136
RSTRUCT_EMBED_LEN_MASK
@ RSTRUCT_EMBED_LEN_MASK
Definition: struct.h:18
RSTRUCT_EMBED_LEN_MAX
@ RSTRUCT_EMBED_LEN_MAX
Definition: struct.h:17
RSTRUCT_TRANSIENT_FLAG
@ RSTRUCT_TRANSIENT_FLAG
Definition: struct.h:20
RSTRUCT_EMBED_LEN_SHIFT
@ RSTRUCT_EMBED_LEN_SHIFT
Definition: struct.h:19
rb_struct_s_keyword_init
VALUE rb_struct_s_keyword_init(VALUE klass)
Definition: struct.c:60
rb_long2int
#define rb_long2int
Definition: long.h:62
RVALUE_EMBED_LEN_MAX
#define RVALUE_EMBED_LEN_MAX
Definition: rbasic.h:36
RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
ruby.h
stdbool.h
C99 shim for <stdbool.h>
RBasic
Definition: rbasic.h:47
RStruct
Definition: struct.h:23
RStruct::basic
struct RBasic basic
Definition: struct.h:24
RStruct::ptr
const VALUE * ptr
Definition: struct.h:28
RStruct::ary
const VALUE ary[RSTRUCT_EMBED_LEN_MAX]
Definition: struct.h:30
RStruct::as
union RStruct::@117 as
RStruct::len
long len
Definition: struct.h:27
RStruct::heap
struct RStruct::@117::@118 heap
VALUE
unsigned long VALUE
Definition: value.h:38