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

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

rtypeddata.h
Go to the documentation of this file.
1#ifndef RBIMPL_RTYPEDDATA_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_RTYPEDDATA_H
23#include "ruby/internal/config.h"
24
25#ifdef STDC_HEADERS
26# include <stddef.h>
27#endif
28
29#include "ruby/internal/assume.h"
30#include "ruby/internal/attr/artificial.h"
31#include "ruby/internal/attr/pure.h"
32#include "ruby/internal/cast.h"
33#include "ruby/internal/core/rbasic.h"
34#include "ruby/internal/core/rdata.h"
35#include "ruby/internal/dllexport.h"
36#include "ruby/internal/error.h"
37#include "ruby/internal/fl_type.h"
38#include "ruby/internal/stdbool.h"
39#include "ruby/internal/value_type.h"
40
41#define HAVE_TYPE_RB_DATA_TYPE_T 1
42#define HAVE_RB_DATA_TYPE_T_FUNCTION 1
43#define HAVE_RB_DATA_TYPE_T_PARENT 1
44#define RUBY_TYPED_DEFAULT_FREE RUBY_DEFAULT_FREE
45#define RUBY_TYPED_NEVER_FREE RUBY_NEVER_FREE
46#define RTYPEDDATA(obj) RBIMPL_CAST((struct RTypedData *)(obj))
47#define RTYPEDDATA_DATA(v) (RTYPEDDATA(v)->data)
48#define Check_TypedStruct(v, t) \
49 rb_check_typeddata(RBIMPL_CAST((VALUE)(v)), (t))
50
52#define RTYPEDDATA_P RTYPEDDATA_P
53#define RTYPEDDATA_TYPE RTYPEDDATA_TYPE
54#define RUBY_TYPED_FREE_IMMEDIATELY RUBY_TYPED_FREE_IMMEDIATELY
55#define RUBY_TYPED_FROZEN_SHAREABLE RUBY_TYPED_FROZEN_SHAREABLE
56#define RUBY_TYPED_WB_PROTECTED RUBY_TYPED_WB_PROTECTED
57#define RUBY_TYPED_PROMOTED1 RUBY_TYPED_PROMOTED1
60/* bits for rb_data_type_struct::flags */
61 enum rbimpl_typeddata_flags {
62 RUBY_TYPED_FREE_IMMEDIATELY = 1,
63 RUBY_TYPED_FROZEN_SHAREABLE = RUBY_FL_SHAREABLE,
64 RUBY_TYPED_WB_PROTECTED = RUBY_FL_WB_PROTECTED, /* THIS FLAG DEPENDS ON Ruby version */
65 RUBY_TYPED_PROMOTED1 = RUBY_FL_PROMOTED1 /* THIS FLAG DEPENDS ON Ruby version */
66};
67
68 typedef struct rb_data_type_struct rb_data_type_t;
69
70 struct rb_data_type_struct {
71 const char *wrap_struct_name;
72 struct {
73 RUBY_DATA_FUNC dmark;
74 RUBY_DATA_FUNC dfree;
75 size_t (*dsize)(const void *);
76 RUBY_DATA_FUNC dcompact;
77 void *reserved[1]; /* For future extension.
78 This array *must* be filled with ZERO. */
79 } function;
80 const rb_data_type_t *parent;
81 void *data; /* This area can be used for any purpose
82 by a programmer who define the type. */
83 VALUE flags; /* RUBY_FL_WB_PROTECTED */
84};
85
86 struct RTypedData {
87 struct RBasic basic;
88 const rb_data_type_t *type;
89 VALUE typed_flag; /* 1 or not */
90 void *data;
91};
92
93 RBIMPL_SYMBOL_EXPORT_BEGIN()
94 VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *);
95 VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type);
96int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent);
97int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type);
98void *rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type);
99 RBIMPL_SYMBOL_EXPORT_END()
100
101#define TypedData_Wrap_Struct(klass,data_type,sval)\
102 rb_data_typed_object_wrap((klass),(sval),(data_type))
103
104#define TypedData_Make_Struct0(result, klass, type, size, data_type, sval) \
105 VALUE result = rb_data_typed_object_zalloc(klass, size, data_type); \
106 (sval) = RBIMPL_CAST((type *)RTYPEDDATA_DATA(result)); \
107 RBIMPL_CAST(/*suppress unused variable warnings*/(void)(sval))
108
109#ifdef HAVE_STMT_AND_DECL_IN_EXPR
110#define TypedData_Make_Struct(klass, type, data_type, sval) \
111 RB_GNUC_EXTENSION({ \
112 TypedData_Make_Struct0( \
113 data_struct_obj, \
114 klass, \
115 type, \
116 sizeof(type), \
117 data_type, \
118 sval); \
119 data_struct_obj; \
120 })
121#else
122#define TypedData_Make_Struct(klass, type, data_type, sval) \
123 rb_data_typed_object_make( \
124 (klass), \
125 (data_type), \
126 RBIMPL_CAST((void **)&(sval)), \
127 sizeof(type))
128#endif
129
130#define TypedData_Get_Struct(obj,type,data_type,sval) \
131 ((sval) = RBIMPL_CAST((type *)rb_check_typeddata((obj), (data_type))))
132
133 RBIMPL_ATTR_PURE()
134 RBIMPL_ATTR_ARTIFICIAL()
135static inline bool
136rbimpl_rtypeddata_p(VALUE obj)
137{
138 return RTYPEDDATA(obj)->typed_flag == 1;
139}
140
141 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
142 RBIMPL_ATTR_ARTIFICIAL()
143static inline bool
144RTYPEDDATA_P(VALUE obj)
145{
146#if RUBY_DEBUG
147 if (RB_UNLIKELY(! RB_TYPE_P(obj, RUBY_T_DATA))) {
148 Check_Type(obj, RUBY_T_DATA);
149 RBIMPL_UNREACHABLE_RETURN(false);
150 }
151#endif
152
153 return rbimpl_rtypeddata_p(obj);
154}
155
156 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
157 RBIMPL_ATTR_ARTIFICIAL()
158/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
159static inline const struct rb_data_type_struct *
160RTYPEDDATA_TYPE(VALUE obj)
161{
162#if RUBY_DEBUG
163 if (RB_UNLIKELY(! RTYPEDDATA_P(obj))) {
164 rb_unexpected_type(obj, RUBY_T_DATA);
165 RBIMPL_UNREACHABLE_RETURN(NULL);
166 }
167#endif
168
169 return RTYPEDDATA(obj)->type;
170}
171
172static inline VALUE
173rb_data_typed_object_make(VALUE klass, const rb_data_type_t *type, void **datap, size_t size)
174{
175 TypedData_Make_Struct0(result, klass, void, size, type, *datap);
176 return result;
177}
178
179 RBIMPL_ATTR_DEPRECATED(("by: rb_data_typed_object_wrap"))
180static inline VALUE
181 rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *type)
182{
183 return rb_data_typed_object_wrap(klass, datap, type);
184}
185
186#endif /* RBIMPL_RTYPEDDATA_H */
artificial.h
Defines RBIMPL_ATTR_ARTIFICIAL.
RBIMPL_ATTR_ARTIFICIAL
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition: artificial.h:43
RB_UNLIKELY
#define RB_UNLIKELY(x)
Definition: assume.h:40
cast.h
Defines RBIMPL_CAST.
RBIMPL_ATTR_DEPRECATED
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition: deprecated.h:60
dllexport.h
Tewaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
fl_type.h
Defines enum ruby_fl_type.
RUBY_FL_PROMOTED1
@ RUBY_FL_PROMOTED1
Definition: fl_type.h:165
RUBY_FL_WB_PROTECTED
@ RUBY_FL_WB_PROTECTED
Definition: fl_type.h:163
RUBY_FL_SHAREABLE
@ RUBY_FL_SHAREABLE
Definition: fl_type.h:169
rb_data_typed_object_alloc
#define rb_data_typed_object_alloc
Definition: gc.c:15
rb_typeddata_inherited_p
int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent)
Definition: error.c:997
rb_typeddata_is_kind_of
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:1007
rb_unexpected_type
void rb_unexpected_type(VALUE x, int t)
Definition: error.c:987
rb_check_typeddata
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:1024
config.h
Thin wrapper to ruby/config.h.
error.h
Declares rb_raise().
assume.h
Defines RBIMPL_ASSUME / RBIMPL_UNREACHABLE.
RBIMPL_UNREACHABLE_RETURN
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition: assume.h:51
size
voidpf void uLong size
Definition: ioapi.h:138
pure.h
Defines RBIMPL_ATTR_PURE.
RBIMPL_ATTR_PURE
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure))
Definition: pure.h:33
RBIMPL_ATTR_PURE_UNLESS_DEBUG
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE iff.
Definition: pure.h:38
rbasic.h
Defines struct RBasic.
rdata.h
Defines struct RData.
RUBY_DATA_FUNC
void(* RUBY_DATA_FUNC)(void *)
Definition: rdata.h:65
NULL
#define NULL
Definition: regenc.h:69
rb_data_typed_object_zalloc
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
Definition: gc.c:2513
TypedData_Make_Struct0
#define TypedData_Make_Struct0(result, klass, type, size, data_type, sval)
Definition: rtypeddata.h:104
RTYPEDDATA
#define RTYPEDDATA(obj)
Definition: rtypeddata.h:46
rb_data_typed_object_wrap
VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *)
Definition: gc.c:2505
rbimpl_typeddata_flags
rbimpl_typeddata_flags
Definition: rtypeddata.h:61
RUBY_TYPED_PROMOTED1
@ RUBY_TYPED_PROMOTED1
Definition: rtypeddata.h:65
RUBY_TYPED_FREE_IMMEDIATELY
@ RUBY_TYPED_FREE_IMMEDIATELY
Definition: rtypeddata.h:62
RUBY_TYPED_FROZEN_SHAREABLE
@ RUBY_TYPED_FROZEN_SHAREABLE
Definition: rtypeddata.h:63
RUBY_TYPED_WB_PROTECTED
@ RUBY_TYPED_WB_PROTECTED
Definition: rtypeddata.h:64
stdbool.h
C99 shim for <stdbool.h>
const
#define const
Definition: strftime.c:108
RBasic
Definition: rbasic.h:47
RBasic::klass
const VALUE klass
Definition: rbasic.h:49
RTypedData
Definition: rtypeddata.h:86
RTypedData::data
void * data
Definition: rtypeddata.h:90
RTypedData::typed_flag
VALUE typed_flag
Definition: rtypeddata.h:89
RTypedData::type
const rb_data_type_t * type
Definition: rtypeddata.h:88
RTypedData::basic
struct RBasic basic
Definition: rtypeddata.h:87
rb_data_type_struct
Definition: rtypeddata.h:70
rb_data_type_struct::dsize
size_t(* dsize)(const void *)
Definition: rtypeddata.h:75
rb_data_type_struct::parent
const rb_data_type_t * parent
Definition: rtypeddata.h:80
rb_data_type_struct::dfree
RUBY_DATA_FUNC dfree
Definition: rtypeddata.h:74
rb_data_type_struct::function
struct rb_data_type_struct::@103 function
rb_data_type_struct::reserved
void * reserved[1]
Definition: rtypeddata.h:77
rb_data_type_struct::dcompact
RUBY_DATA_FUNC dcompact
Definition: rtypeddata.h:76
rb_data_type_struct::wrap_struct_name
const char * wrap_struct_name
Definition: rtypeddata.h:71
rb_data_type_struct::dmark
RUBY_DATA_FUNC dmark
Definition: rtypeddata.h:73
rb_data_type_struct::data
void * data
Definition: rtypeddata.h:81
rb_data_type_struct::flags
VALUE flags
Definition: rtypeddata.h:83
VALUE
unsigned long VALUE
Definition: value.h:38
value_type.h
Defines enum ruby_value_type.
RUBY_T_DATA
@ RUBY_T_DATA
Definition: value_type.h:124