internal/*.h: skip doxygen
[ruby.git] / internal / struct.h
blob8acc00ec3ca5a15523e3e95fb6776e70fa05fc4c
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 "internal/gc.h" /* for RB_OBJ_WRITE */
13 #include "ruby/ruby.h" /* for struct RBasic */
15 enum {
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,
22 struct RStruct {
23 struct RBasic basic;
24 union {
25 struct {
26 long len;
27 const VALUE *ptr;
28 } heap;
29 const VALUE ary[RSTRUCT_EMBED_LEN_MAX];
30 } as;
33 #define RSTRUCT(obj) ((struct RStruct *)(obj))
35 #ifdef RSTRUCT_LEN
36 # undef RSTRUCT_LEN
37 #endif
39 #ifdef RSTRUCT_PTR
40 # undef RSTRUCT_PTR
41 #endif
43 #ifdef RSTRUCT_SET
44 # undef RSTRUCT_SET
45 #endif
47 #ifdef RSTRUCT_GET
48 # undef RSTRUCT_GET
49 #endif
51 #define RSTRUCT_LEN internal_RSTRUCT_LEN
52 #define RSTRUCT_SET internal_RSTRUCT_SET
53 #define RSTRUCT_GET internal_RSTRUCT_GET
55 /* struct.c */
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);
70 static inline bool
71 RSTRUCT_TRANSIENT_P(VALUE st)
73 #if USE_TRANSIENT_HEAP
74 return FL_TEST_RAW(st, RSTRUCT_TRANSIENT_FLAG);
75 #else
76 return false;
77 #endif
80 static inline void
81 RSTRUCT_TRANSIENT_SET(VALUE st)
83 #if USE_TRANSIENT_HEAP
84 FL_SET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
85 #endif
88 static inline void
89 RSTRUCT_TRANSIENT_UNSET(VALUE st)
91 #if USE_TRANSIENT_HEAP
92 FL_UNSET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
93 #endif
96 static inline long
97 RSTRUCT_EMBED_LEN(VALUE st)
99 long ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK);
100 ret >>= RSTRUCT_EMBED_LEN_SHIFT;
101 return ret;
104 static inline long
105 RSTRUCT_LEN(VALUE st)
107 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
108 return RSTRUCT_EMBED_LEN(st);
110 else {
111 return RSTRUCT(st)->as.heap.len;
115 static inline int
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)) {
127 return p->as.ary;
129 else {
130 return p->as.heap.ptr;
134 static inline void
135 RSTRUCT_SET(VALUE st, long k, VALUE v)
137 RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v);
140 static inline VALUE
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 */