1 #ifndef INTERNAL_STRUCT_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_STRUCT_H
5 * @author Ruby developers <ruby-core@ruby-lang.org>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @brief Internal header for Struct.
12 #include "ruby/impl/stdbool.h" /* for bool */
13 #include "internal/gc.h" /* for RB_OBJ_WRITE */
14 #include "ruby/ruby.h" /* for struct RBasic */
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
,
30 const VALUE ary
[RSTRUCT_EMBED_LEN_MAX
];
34 #define RSTRUCT(obj) (R_CAST(RStruct)(obj))
52 #define RSTRUCT_LEN internal_RSTRUCT_LEN
53 #define RSTRUCT_SET internal_RSTRUCT_SET
54 #define RSTRUCT_GET internal_RSTRUCT_GET
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
);
60 static inline const VALUE
*rb_struct_const_heap_ptr(VALUE st
);
61 static inline bool RSTRUCT_TRANSIENT_P(VALUE st
);
62 static inline void RSTRUCT_TRANSIENT_SET(VALUE st
);
63 static inline void RSTRUCT_TRANSIENT_UNSET(VALUE st
);
64 static inline long RSTRUCT_EMBED_LEN(VALUE st
);
65 static inline long RSTRUCT_LEN(VALUE st
);
66 static inline int RSTRUCT_LENINT(VALUE st
);
67 static inline const VALUE
*RSTRUCT_CONST_PTR(VALUE st
);
68 static inline void RSTRUCT_SET(VALUE st
, long k
, VALUE v
);
69 static inline VALUE
RSTRUCT_GET(VALUE st
, long k
);
72 RSTRUCT_TRANSIENT_P(VALUE st
)
74 #if USE_TRANSIENT_HEAP
75 return FL_TEST_RAW(st
, RSTRUCT_TRANSIENT_FLAG
);
82 RSTRUCT_TRANSIENT_SET(VALUE st
)
84 #if USE_TRANSIENT_HEAP
85 FL_SET_RAW(st
, RSTRUCT_TRANSIENT_FLAG
);
90 RSTRUCT_TRANSIENT_UNSET(VALUE st
)
92 #if USE_TRANSIENT_HEAP
93 FL_UNSET_RAW(st
, RSTRUCT_TRANSIENT_FLAG
);
98 RSTRUCT_EMBED_LEN(VALUE st
)
100 long ret
= FL_TEST_RAW(st
, RSTRUCT_EMBED_LEN_MASK
);
101 ret
>>= RSTRUCT_EMBED_LEN_SHIFT
;
106 RSTRUCT_LEN(VALUE st
)
108 if (FL_TEST_RAW(st
, RSTRUCT_EMBED_LEN_MASK
)) {
109 return RSTRUCT_EMBED_LEN(st
);
112 return RSTRUCT(st
)->as
.heap
.len
;
117 RSTRUCT_LENINT(VALUE st
)
119 return rb_long2int(RSTRUCT_LEN(st
));
122 static inline const VALUE
*
123 RSTRUCT_CONST_PTR(VALUE st
)
125 const struct RStruct
*p
= RSTRUCT(st
);
127 if (FL_TEST_RAW(st
, RSTRUCT_EMBED_LEN_MASK
)) {
131 return p
->as
.heap
.ptr
;
136 RSTRUCT_SET(VALUE st
, long k
, VALUE v
)
138 RB_OBJ_WRITE(st
, &RSTRUCT_CONST_PTR(st
)[k
], v
);
142 RSTRUCT_GET(VALUE st
, long k
)
144 return RSTRUCT_CONST_PTR(st
)[k
];
147 static inline const VALUE
*
148 rb_struct_const_heap_ptr(VALUE st
)
150 /* TODO: check embed on debug mode */
151 return RSTRUCT(st
)->as
.heap
.ptr
;
154 #endif /* INTERNAL_STRUCT_H */