Implement Struct on VWA
[ruby.git] / internal / struct.h
blob9b56254541149e951ed47d162a0089eb18111ac0
1 #ifndef INTERNAL_STRUCT_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_STRUCT_H
3 /**
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 "ruby/ruby.h" /* for struct RBasic */
14 enum {
15 RSTRUCT_EMBED_LEN_MASK = RUBY_FL_USER7 | RUBY_FL_USER6 | RUBY_FL_USER5 | RUBY_FL_USER4 |
16 RUBY_FL_USER3 | RUBY_FL_USER2 | RUBY_FL_USER1,
17 RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1),
18 RSTRUCT_TRANSIENT_FLAG = RUBY_FL_USER8,
21 struct RStruct {
22 struct RBasic basic;
23 union {
24 struct {
25 long len;
26 const VALUE *ptr;
27 } heap;
28 /* This is a length 1 array because:
29 * 1. GCC has a bug that does not optimize C flexible array members
30 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
31 * 2. Zero length arrays are not supported by all compilers
33 const VALUE ary[1];
34 } as;
37 #define RSTRUCT(obj) ((struct RStruct *)(obj))
39 #ifdef RSTRUCT_LEN
40 # undef RSTRUCT_LEN
41 #endif
43 #ifdef RSTRUCT_PTR
44 # undef RSTRUCT_PTR
45 #endif
47 #ifdef RSTRUCT_SET
48 # undef RSTRUCT_SET
49 #endif
51 #ifdef RSTRUCT_GET
52 # undef RSTRUCT_GET
53 #endif
55 #define RSTRUCT_LEN internal_RSTRUCT_LEN
56 #define RSTRUCT_SET internal_RSTRUCT_SET
57 #define RSTRUCT_GET internal_RSTRUCT_GET
59 /* struct.c */
60 VALUE rb_struct_init_copy(VALUE copy, VALUE s);
61 VALUE rb_struct_lookup(VALUE s, VALUE idx);
62 VALUE rb_struct_s_keyword_init(VALUE klass);
63 static inline const VALUE *rb_struct_const_heap_ptr(VALUE st);
64 static inline bool RSTRUCT_TRANSIENT_P(VALUE st);
65 static inline void RSTRUCT_TRANSIENT_SET(VALUE st);
66 static inline void RSTRUCT_TRANSIENT_UNSET(VALUE st);
67 static inline long RSTRUCT_EMBED_LEN(VALUE st);
68 static inline long RSTRUCT_LEN(VALUE st);
69 static inline int RSTRUCT_LENINT(VALUE st);
70 static inline const VALUE *RSTRUCT_CONST_PTR(VALUE st);
71 static inline void RSTRUCT_SET(VALUE st, long k, VALUE v);
72 static inline VALUE RSTRUCT_GET(VALUE st, long k);
74 static inline bool
75 RSTRUCT_TRANSIENT_P(VALUE st)
77 #if USE_TRANSIENT_HEAP
78 return FL_TEST_RAW(st, RSTRUCT_TRANSIENT_FLAG);
79 #else
80 return false;
81 #endif
84 static inline void
85 RSTRUCT_TRANSIENT_SET(VALUE st)
87 #if USE_TRANSIENT_HEAP
88 FL_SET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
89 #endif
92 static inline void
93 RSTRUCT_TRANSIENT_UNSET(VALUE st)
95 #if USE_TRANSIENT_HEAP
96 FL_UNSET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
97 #endif
100 static inline long
101 RSTRUCT_EMBED_LEN(VALUE st)
103 long ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK);
104 ret >>= RSTRUCT_EMBED_LEN_SHIFT;
105 return ret;
108 static inline long
109 RSTRUCT_LEN(VALUE st)
111 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
112 return RSTRUCT_EMBED_LEN(st);
114 else {
115 return RSTRUCT(st)->as.heap.len;
119 static inline int
120 RSTRUCT_LENINT(VALUE st)
122 return rb_long2int(RSTRUCT_LEN(st));
125 static inline const VALUE *
126 RSTRUCT_CONST_PTR(VALUE st)
128 const struct RStruct *p = RSTRUCT(st);
130 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
131 return p->as.ary;
133 else {
134 return p->as.heap.ptr;
138 static inline void
139 RSTRUCT_SET(VALUE st, long k, VALUE v)
141 RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v);
144 static inline VALUE
145 RSTRUCT_GET(VALUE st, long k)
147 return RSTRUCT_CONST_PTR(st)[k];
150 static inline const VALUE *
151 rb_struct_const_heap_ptr(VALUE st)
153 assert(!FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK));
154 return RSTRUCT(st)->as.heap.ptr;
157 #endif /* INTERNAL_STRUCT_H */