1 #ifndef INTERNAL_STRUCT_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_STRUCT_H
4 * @author Ruby developers <ruby-core@ruby-lang.org>
5 * @copyright This file is a part of the programming language Ruby.
6 * Permission is hereby granted, to either redistribute and/or
7 * modify this file, provided that the conditions mentioned in the
8 * file COPYING are met. Consult the file for details.
9 * @brief Internal header for Struct.
11 #include "ruby/internal/stdbool.h" /* for bool */
12 #include "internal/gc.h" /* for RB_OBJ_WRITE */
13 #include "ruby/ruby.h" /* for struct RBasic */
16 RSTRUCT_EMBED_LEN_MAX
= RVALUE_EMBED_LEN_MAX
,
17 RSTRUCT_EMBED_LEN_MASK
= (RUBY_FL_USER2
|RUBY_FL_USER1
),
18 RSTRUCT_EMBED_LEN_SHIFT
= (RUBY_FL_USHIFT
+1),
19 RSTRUCT_TRANSIENT_FLAG
= FL_USER3
,
29 const VALUE ary
[RSTRUCT_EMBED_LEN_MAX
];
33 #define RSTRUCT(obj) ((struct RStruct *)(obj))
51 #define RSTRUCT_LEN internal_RSTRUCT_LEN
52 #define RSTRUCT_SET internal_RSTRUCT_SET
53 #define RSTRUCT_GET internal_RSTRUCT_GET
56 VALUE
rb_struct_init_copy(VALUE copy
, VALUE s
);
57 VALUE
rb_struct_lookup(VALUE s
, VALUE idx
);
58 VALUE
rb_struct_s_keyword_init(VALUE klass
);
59 static inline const VALUE
*rb_struct_const_heap_ptr(VALUE st
);
60 static inline bool RSTRUCT_TRANSIENT_P(VALUE st
);
61 static inline void RSTRUCT_TRANSIENT_SET(VALUE st
);
62 static inline void RSTRUCT_TRANSIENT_UNSET(VALUE st
);
63 static inline long RSTRUCT_EMBED_LEN(VALUE st
);
64 static inline long RSTRUCT_LEN(VALUE st
);
65 static inline int RSTRUCT_LENINT(VALUE st
);
66 static inline const VALUE
*RSTRUCT_CONST_PTR(VALUE st
);
67 static inline void RSTRUCT_SET(VALUE st
, long k
, VALUE v
);
68 static inline VALUE
RSTRUCT_GET(VALUE st
, long k
);
71 RSTRUCT_TRANSIENT_P(VALUE st
)
73 #if USE_TRANSIENT_HEAP
74 return FL_TEST_RAW(st
, RSTRUCT_TRANSIENT_FLAG
);
81 RSTRUCT_TRANSIENT_SET(VALUE st
)
83 #if USE_TRANSIENT_HEAP
84 FL_SET_RAW(st
, RSTRUCT_TRANSIENT_FLAG
);
89 RSTRUCT_TRANSIENT_UNSET(VALUE st
)
91 #if USE_TRANSIENT_HEAP
92 FL_UNSET_RAW(st
, RSTRUCT_TRANSIENT_FLAG
);
97 RSTRUCT_EMBED_LEN(VALUE st
)
99 long ret
= FL_TEST_RAW(st
, RSTRUCT_EMBED_LEN_MASK
);
100 ret
>>= RSTRUCT_EMBED_LEN_SHIFT
;
105 RSTRUCT_LEN(VALUE st
)
107 if (FL_TEST_RAW(st
, RSTRUCT_EMBED_LEN_MASK
)) {
108 return RSTRUCT_EMBED_LEN(st
);
111 return RSTRUCT(st
)->as
.heap
.len
;
116 RSTRUCT_LENINT(VALUE st
)
118 return rb_long2int(RSTRUCT_LEN(st
));
121 static inline const VALUE
*
122 RSTRUCT_CONST_PTR(VALUE st
)
124 const struct RStruct
*p
= RSTRUCT(st
);
126 if (FL_TEST_RAW(st
, RSTRUCT_EMBED_LEN_MASK
)) {
130 return p
->as
.heap
.ptr
;
135 RSTRUCT_SET(VALUE st
, long k
, VALUE v
)
137 RB_OBJ_WRITE(st
, &RSTRUCT_CONST_PTR(st
)[k
], v
);
141 RSTRUCT_GET(VALUE st
, long k
)
143 return RSTRUCT_CONST_PTR(st
)[k
];
146 static inline const VALUE
*
147 rb_struct_const_heap_ptr(VALUE st
)
149 /* TODO: check embed on debug mode */
150 return RSTRUCT(st
)->as
.heap
.ptr
;
153 #endif /* INTERNAL_STRUCT_H */