Ruby: include/ruby/internal/core/robject.h Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

robject.h
Go to the documentation of this file.
1#ifndef RBIMPL_ROBJECT_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_ROBJECT_H
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
43#define ROBJECT(obj) RBIMPL_CAST((struct RObject *)(obj))
45#define ROBJECT_EMBED_LEN_MAX ROBJECT_EMBED_LEN_MAX
46#define ROBJECT_HEAP ROBJECT_HEAP
47#define ROBJECT_FIELDS_CAPACITY ROBJECT_FIELDS_CAPACITY
48#define ROBJECT_FIELDS ROBJECT_FIELDS
50
56enum ruby_robject_flags {
76 ROBJECT_HEAP = RUBY_FL_USER4
77};
78
79struct st_table;
80
85 struct RObject {
86
88 struct RBasic basic;
89
91 union {
92
97 struct {
99 VALUE *fields;
100 } heap;
101
102 /* Embedded instance variables. When an object is small enough, it
103 * uses this area to store the instance variables.
104 *
105 * This is a length 1 array because:
106 * 1. GCC has a bug that does not optimize C flexible array members
107 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
108 * 2. Zero length arrays are not supported by all compilers
109 */
110 VALUE ary[1];
111 } as;
112};
113
114 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
115 RBIMPL_ATTR_ARTIFICIAL()
127static inline VALUE *
128ROBJECT_FIELDS(VALUE obj)
129{
130 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
131
132 struct RObject *const ptr = ROBJECT(obj);
133
134 if (RB_UNLIKELY(RB_FL_ANY_RAW(obj, ROBJECT_HEAP))) {
135 return ptr->as.heap.fields;
136 }
137 else {
138 return ptr->as.ary;
139 }
140}
141
142#endif /* RBIMPL_ROBJECT_H */
artificial.h
Defines RBIMPL_ATTR_ARTIFICIAL.
RBIMPL_ATTR_ARTIFICIAL
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial)).
Definition artificial.h:43
deprecated.h
Defines RBIMPL_ATTR_DEPRECATED.
fl_type.h
Defines enum ruby_fl_type.
RB_FL_ANY_RAW
static bool RB_FL_ANY_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_ANY().
Definition fl_type.h:512
RUBY_FL_USER4
@ RUBY_FL_USER4
User-defined flag.
Definition fl_type.h:328
inline
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
pure.h
Defines RBIMPL_ATTR_PURE.
RBIMPL_ATTR_PURE_UNLESS_DEBUG
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition pure.h:38
ROBJECT
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
ROBJECT_FIELDS
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
Definition robject.h:128
RBasic
Ruby object's base components.
Definition rbasic.h:69
RObject
Ruby's ordinal objects.
Definition robject.h:85
RObject::fields
VALUE * fields
Pointer to a C array that holds instance variables.
Definition robject.h:99
RObject::basic
struct RBasic basic
Basic part, including flags and class.
Definition robject.h:88
RObject::as
union RObject::@210145204263252121304200074116325373231002256127 as
Object's specific fields.
value.h
Defines VALUE and ID.
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
value_type.h
Defines enum ruby_value_type.
RUBY_T_OBJECT
@ RUBY_T_OBJECT
Definition value_type.h:116