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

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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/flag_enum.h"
32#include "ruby/internal/attr/nonnull.h"
33#include "ruby/internal/attr/pure.h"
34#include "ruby/internal/cast.h"
35#include "ruby/internal/core/rbasic.h"
36#include "ruby/internal/core/rdata.h"
37#include "ruby/internal/dllexport.h"
38#include "ruby/internal/error.h"
39#include "ruby/internal/fl_type.h"
40#include "ruby/internal/stdbool.h"
41#include "ruby/internal/value_type.h"
42
50#define HAVE_TYPE_RB_DATA_TYPE_T 1
51
59#define HAVE_RB_DATA_TYPE_T_FUNCTION 1
60
68#define HAVE_RB_DATA_TYPE_T_PARENT 1
69
79#define RUBY_TYPED_DEFAULT_FREE RUBY_DEFAULT_FREE
80
86#define RUBY_TYPED_NEVER_FREE RUBY_NEVER_FREE
87
94#define RTYPEDDATA(obj) RBIMPL_CAST((struct RTypedData *)(obj))
95
102#define RTYPEDDATA_DATA(v) (RTYPEDDATA(v)->data)
103
105#define Check_TypedStruct(v, t) \
106 rb_check_typeddata(RBIMPL_CAST((VALUE)(v)), (t))
107
109#define RTYPEDDATA_P RTYPEDDATA_P
110#define RTYPEDDATA_TYPE RTYPEDDATA_TYPE
111#define RUBY_TYPED_FREE_IMMEDIATELY RUBY_TYPED_FREE_IMMEDIATELY
112#define RUBY_TYPED_FROZEN_SHAREABLE RUBY_TYPED_FROZEN_SHAREABLE
113#define RUBY_TYPED_WB_PROTECTED RUBY_TYPED_WB_PROTECTED
114#define RUBY_TYPED_PROMOTED1 RUBY_TYPED_PROMOTED1
122enum
123 RBIMPL_ATTR_FLAG_ENUM()
124rbimpl_typeddata_flags {
138 RUBY_TYPED_FREE_IMMEDIATELY = 1,
139
152 RUBY_TYPED_FROZEN_SHAREABLE = RUBY_FL_SHAREABLE,
153
173 RUBY_TYPED_WB_PROTECTED = RUBY_FL_WB_PROTECTED, /* THIS FLAG DEPENDS ON Ruby version */
174
179 RUBY_TYPED_PROMOTED1 = RUBY_FL_PROMOTED1 /* THIS FLAG DEPENDS ON Ruby version */
180};
181
187 typedef struct rb_data_type_struct rb_data_type_t;
188
190 struct rb_data_type_struct {
191
197 const char *wrap_struct_name;
198
200 struct {
201
211 RUBY_DATA_FUNC dmark;
212
220 RUBY_DATA_FUNC dfree;
221
230 size_t (*dsize)(const void *);
231
241 RUBY_DATA_FUNC dcompact;
242
247 void *reserved[1]; /* For future extension.
248 This array *must* be filled with ZERO. */
249 } function;
250
280 const rb_data_type_t *parent;
281
286 void *data; /* This area can be used for any purpose
287 by a programmer who define the type. */
288
299 VALUE flags; /* RUBY_FL_WB_PROTECTED */
300};
301
340 struct RTypedData {
341
343 struct RBasic basic;
344
350 const rb_data_type_t *type;
351
359 VALUE typed_flag;
360
362 void *data;
363};
364
365 RBIMPL_SYMBOL_EXPORT_BEGIN()
366 RBIMPL_ATTR_NONNULL((3))
377 VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type);
378
392 VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type);
393
406int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent);
407
416int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type);
417
428void *rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type);
429 RBIMPL_SYMBOL_EXPORT_END()
430
441#define TypedData_Wrap_Struct(klass,data_type,sval)\
442 rb_data_typed_object_wrap((klass),(sval),(data_type))
443
457#define TypedData_Make_Struct0(result, klass, type, size, data_type, sval) \
458 VALUE result = rb_data_typed_object_zalloc(klass, size, data_type); \
459 (sval) = RBIMPL_CAST((type *)RTYPEDDATA_DATA(result)); \
460 RBIMPL_CAST(/*suppress unused variable warnings*/(void)(sval))
461
476#ifdef HAVE_STMT_AND_DECL_IN_EXPR
477#define TypedData_Make_Struct(klass, type, data_type, sval) \
478 RB_GNUC_EXTENSION({ \
479 TypedData_Make_Struct0( \
480 data_struct_obj, \
481 klass, \
482 type, \
483 sizeof(type), \
484 data_type, \
485 sval); \
486 data_struct_obj; \
487 })
488#else
489#define TypedData_Make_Struct(klass, type, data_type, sval) \
490 rb_data_typed_object_make( \
491 (klass), \
492 (data_type), \
493 RBIMPL_CAST((void **)&(sval)), \
494 sizeof(type))
495#endif
496
507#define TypedData_Get_Struct(obj,type,data_type,sval) \
508 ((sval) = RBIMPL_CAST((type *)rb_check_typeddata((obj), (data_type))))
509
510 RBIMPL_ATTR_PURE()
511 RBIMPL_ATTR_ARTIFICIAL()
523static inline bool
524rbimpl_rtypeddata_p(VALUE obj)
525{
526 return RTYPEDDATA(obj)->typed_flag == 1;
527}
528
529 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
530 RBIMPL_ATTR_ARTIFICIAL()
539static inline bool
540RTYPEDDATA_P(VALUE obj)
541{
542#if RUBY_DEBUG
543 if (RB_UNLIKELY(! RB_TYPE_P(obj, RUBY_T_DATA))) {
544 Check_Type(obj, RUBY_T_DATA);
545 RBIMPL_UNREACHABLE_RETURN(false);
546 }
547#endif
548
549 return rbimpl_rtypeddata_p(obj);
550}
551
552 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
553 RBIMPL_ATTR_ARTIFICIAL()
554/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
562static inline const struct rb_data_type_struct *
563RTYPEDDATA_TYPE(VALUE obj)
564{
565#if RUBY_DEBUG
566 if (RB_UNLIKELY(! RTYPEDDATA_P(obj))) {
567 rb_unexpected_type(obj, RUBY_T_DATA);
568 RBIMPL_UNREACHABLE_RETURN(NULL);
569 }
570#endif
571
572 return RTYPEDDATA(obj)->type;
573}
574
589static inline VALUE
590rb_data_typed_object_make(VALUE klass, const rb_data_type_t *type, void **datap, size_t size)
591{
592 TypedData_Make_Struct0(result, klass, void, size, type, *datap);
593 return result;
594}
595
596 RBIMPL_ATTR_DEPRECATED(("by: rb_data_typed_object_wrap"))
598static inline VALUE
599rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *type)
600{
601 return rb_data_typed_object_wrap(klass, datap, type);
602}
603
604#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
RBIMPL_ATTR_DEPRECATED
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition deprecated.h:64
dllexport.h
Tweaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:106
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:97
fl_type.h
Defines enum ruby_fl_type.
RUBY_FL_PROMOTED1
@ RUBY_FL_PROMOTED1
This flag has something to do with our garbage collector.
Definition fl_type.h:240
RUBY_FL_WB_PROTECTED
@ RUBY_FL_WB_PROTECTED
Definition fl_type.h:207
RUBY_FL_SHAREABLE
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:298
flag_enum.h
Defines RBIMPL_ATTR_FLAG_ENUM.
RBIMPL_ATTR_FLAG_ENUM
#define RBIMPL_ATTR_FLAG_ENUM()
Wraps (or simulates) __attribute__((flag_enum)
Definition flag_enum.h:30
rb_typeddata_inherited_p
int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent)
Checks for the domestic relationship between the two.
Definition error.c:1031
rb_typeddata_is_kind_of
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition error.c:1041
rb_check_typeddata
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
Definition error.c:1058
rb_unexpected_type
void rb_unexpected_type(VALUE x, int t)
Fails with the given object's type incompatibility to the type.
Definition error.c:1021
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:48
nonnull.h
Defines RBIMPL_ATTR_NONNULL.
RBIMPL_ATTR_NONNULL
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
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 if and only if.
Definition pure.h:38
rbasic.h
Defines struct RBasic.
rdata.h
Defines struct RData.
RUBY_DATA_FUNC
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
Definition rdata.h:108
RTYPEDDATA_P
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:540
rb_data_typed_object_wrap
VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
This is the primitive way to wrap an existing C struct into RTypedData.
Definition gc.c:3132
rb_data_typed_object_zalloc
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
Identical to rb_data_typed_object_wrap(), except it allocates a new data region internally instead of...
Definition gc.c:3140
TypedData_Make_Struct0
#define TypedData_Make_Struct0(result, klass, type, size, data_type, sval)
This is an implementation detail of TypedData_Make_Struct.
Definition rtypeddata.h:457
RTYPEDDATA
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition rtypeddata.h:94
rb_data_typed_object_make
static VALUE rb_data_typed_object_make(VALUE klass, const rb_data_type_t *type, void **datap, size_t size)
While we don't stop you from using this function, it seems to be an implementation detail of TypedDat...
Definition rtypeddata.h:590
RTYPEDDATA_TYPE
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:563
stdbool.h
C99 shim for <stdbool.h>
RBasic
Ruby's object's, base components.
Definition rbasic.h:64
RBasic::klass
const VALUE klass
Class of an object.
Definition rbasic.h:88
RTypedData
"Typed" user data.
Definition rtypeddata.h:340
RTypedData::data
void * data
Pointer to the actual C level struct that you want to wrap.
Definition rtypeddata.h:362
RTypedData::typed_flag
VALUE typed_flag
This has to be always 1.
Definition rtypeddata.h:359
RTypedData::type
const rb_data_type_t * type
This field stores various information about how Ruby should handle a data.
Definition rtypeddata.h:350
RTypedData::basic
struct RBasic basic
The part that all ruby objects have in common.
Definition rtypeddata.h:343
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:190
rb_data_type_struct::dsize
size_t(* dsize)(const void *)
This function is to query the size of the underlying memory regions.
Definition rtypeddata.h:230
rb_data_type_struct::parent
const rb_data_type_t * parent
Parent of this class.
Definition rtypeddata.h:280
rb_data_type_struct::dfree
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rtypeddata.h:220
rb_data_type_struct::reserved
void * reserved[1]
This field is reserved for future extension.
Definition rtypeddata.h:247
rb_data_type_struct::dcompact
RUBY_DATA_FUNC dcompact
This function is called when the object is relocated.
Definition rtypeddata.h:241
rb_data_type_struct::wrap_struct_name
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:197
rb_data_type_struct::dmark
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rtypeddata.h:211
rb_data_type_struct::data
void * data
Type-specific static data.
Definition rtypeddata.h:286
rb_data_type_struct::function
struct rb_data_type_struct::@53 function
Function pointers.
rb_data_type_struct::flags
VALUE flags
Type-specific behavioural characteristics.
Definition rtypeddata.h:299
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
value_type.h
Defines enum ruby_value_type.
Check_Type
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:432
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375
RUBY_T_DATA
@ RUBY_T_DATA
Definition value_type.h:126