Debian Codegraph @

include/ruby/internal/core/robject.h

raw
1 #ifndef RBIMPL_ROBJECT_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_ROBJECT_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
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 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Defines struct ::RObject.
22 */
23 #include "ruby/internal/config.h"
24
25 #ifdef HAVE_STDINT_H
26 # include <stdint.h>
27 #endif
28
29 #include "ruby/internal/attr/artificial.h"
30 #include "ruby/internal/attr/deprecated.h"
31 #include "ruby/internal/attr/pure.h"
32 #include "ruby/internal/cast.h"
33 #include "ruby/internal/fl_type.h"
34 #include "ruby/internal/value.h"
35 #include "ruby/internal/value_type.h"
36
37 /**
38 * Convenient casting macro.
39 *
40 * @param obj An object, which is in fact an ::RObject.
41 * @return The passed object casted to ::RObject.
42 */
43 #define ROBJECT(obj) RBIMPL_CAST((struct RObject *)(obj))
44 /** @cond INTERNAL_MACRO */
45 #define ROBJECT_EMBED_LEN_MAX ROBJECT_EMBED_LEN_MAX
46 #define ROBJECT_EMBED ROBJECT_EMBED
47 #define ROBJECT_IV_CAPACITY ROBJECT_IV_CAPACITY
48 #define ROBJECT_IVPTR ROBJECT_IVPTR
49 /** @endcond */
50
51 /**
52 * @private
53 *
54 * Bits that you can set to ::RBasic::flags.
55 */
56 enum ruby_robject_flags {
57 /**
58 * This flag has something to do with memory footprint. If the object is
59 * "small" enough, ruby tries to be creative to abuse padding bits of
60 * struct ::RObject for storing instance variables. This flag denotes that
61 * situation.
62 *
63 * @warning This bit has to be considered read-only. Setting/clearing
64 * this bit without corresponding fix up must cause immediate
65 * SEGV. Also, internal structures of an object change
66 * dynamically and transparently throughout of its lifetime.
67 * Don't assume it being persistent.
68 *
69 * @internal
70 *
71 * 3rd parties must not be aware that there even is more than one way to
72 * store instance variables. Might better be hidden.
73 */
74 ROBJECT_EMBED = RUBY_FL_USER1
75 };
76
77 struct st_table;
78
79 /**
80 * Ruby's ordinal objects. Unless otherwise special cased, all predefined and
81 * user-defined classes share this struct to hold their instances.
82 */
83 struct RObject {
84
85 /** Basic part, including flags and class. */
86 struct RBasic basic;
87
88 /** Object's specific fields. */
89 union {
90
91 /**
92 * Object that use separated memory region for instance variables use
93 * this pattern.
94 */
95 struct {
96 /** Pointer to a C array that holds instance variables. */
97 VALUE *ivptr;
98
99 /**
100 * This is a table that holds instance variable name to index
101 * mapping. Used when accessing instance variables using names.
102 *
103 * @internal
104 *
105 * This is a shortcut for `RCLASS_IV_INDEX_TBL(rb_obj_class(obj))`.
106 */
107 struct rb_id_table *iv_index_tbl;
108 } heap;
109
110 /* Embedded instance variables. When an object is small enough, it
111 * uses this area to store the instance variables.
112 *
113 * This is a length 1 array because:
114 * 1. GCC has a bug that does not optimize C flexible array members
115 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
116 * 2. Zero length arrays are not supported by all compilers
117 */
118 VALUE ary[1];
119 } as;
120 };
121
122 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
123 RBIMPL_ATTR_ARTIFICIAL()
124 /**
125 * Queries the instance variables.
126 *
127 * @param[in] obj Object in question.
128 * @return Its instance variables, in C array.
129 * @pre `obj` must be an instance of ::RObject.
130 *
131 * @internal
132 *
133 * @shyouhei finds no reason for this to be visible from extension libraries.
134 */
135 static inline VALUE *
136 ROBJECT_IVPTR(VALUE obj)
137 {
138 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
139
140 struct RObject *const ptr = ROBJECT(obj);
141
142 if (RB_FL_ANY_RAW(obj, ROBJECT_EMBED)) {
143 return ptr->as.ary;
144 }
145 else {
146 return ptr->as.heap.ivptr;
147 }
148 }
149
150 #endif /* RBIMPL_ROBJECT_H */