Ruby  3.1.4p223 (2023-03-30 revision HEAD)
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"
34 #include "ruby/internal/cast.h"
38 #include "ruby/internal/error.h"
39 #include "ruby/internal/fl_type.h"
40 #include "ruby/internal/stdbool.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
122 enum
124 rbimpl_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 
191 
197  const char *wrap_struct_name;
198 
200  struct {
201 
212 
221 
230  size_t (*dsize)(const void *);
231 
242 
247  void *reserved[1]; /* For future extension.
248  This array *must* be filled with ZERO. */
249  } function;
250 
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 
351 
360 
362  void *data;
363 };
364 
378 
393 
406 int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent);
407 
416 int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type);
417 
428 void *rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type);
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 
523 static inline bool
524 rbimpl_rtypeddata_p(VALUE obj)
525 {
526  return RTYPEDDATA(obj)->typed_flag == 1;
527 }
528 
539 static inline bool
541 {
542 #if RUBY_DEBUG
543  if (RB_UNLIKELY(! RB_TYPE_P(obj, RUBY_T_DATA))) {
544  Check_Type(obj, RUBY_T_DATA);
546  }
547 #endif
548 
549  return rbimpl_rtypeddata_p(obj);
550 }
551 
554 /* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
562 static inline const struct rb_data_type_struct *
564 {
565 #if RUBY_DEBUG
566  if (RB_UNLIKELY(! RTYPEDDATA_P(obj))) {
569  }
570 #endif
571 
572  return RTYPEDDATA(obj)->type;
573 }
574 
589 static inline VALUE
590 rb_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"))
598 static inline VALUE
600 {
601  return rb_data_typed_object_wrap(klass, datap, type);
602 }
603 
604 #endif /* RBIMPL_RTYPEDDATA_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition: artificial.h:41
#define RB_UNLIKELY(x)
Asserts that the given Boolean expression likely doesn't hold.
Definition: assume.h:52
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition: deprecated.h:36
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:106
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:97
Defines enum ruby_fl_type.
@ RUBY_FL_PROMOTED1
This flag has something to do with our garbage collector.
Definition: fl_type.h:240
@ RUBY_FL_WB_PROTECTED
Definition: fl_type.h:207
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition: fl_type.h:298
Defines RBIMPL_ATTR_FLAG_ENUM.
#define RBIMPL_ATTR_FLAG_ENUM()
Wraps (or simulates) __attribute__((flag_enum)
Definition: flag_enum.h:28
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:1066
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:1039
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:1049
void rb_unexpected_type(VALUE x, int t)
Fails with the given object's type incompatibility to the type.
Definition: error.c:1029
Declares rb_raise().
RBIMPL_ATTR_PURE() int rb_io_read_pending(rb_io_t *fptr)
Queries if the passed IO has any pending reads.
Defines RBIMPL_ASSUME / RBIMPL_UNREACHABLE.
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition: assume.h:45
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
Defines RBIMPL_ATTR_PURE.
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition: pure.h:38
Defines struct RBasic.
Defines struct RData.
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
Definition: rdata.h:108
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition: rtypeddata.h:540
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:2766
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:2774
#define TypedData_Make_Struct0(result, klass, type, size, data_type, sval)
This is an implementation detail of TypedData_Make_Struct.
Definition: rtypeddata.h:457
static VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *type)
Definition: rtypeddata.h:599
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition: rtypeddata.h:94
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition: rtypeddata.h:563
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
C99 shim for <stdbool.h>
Ruby's object's, base components.
Definition: rbasic.h:64
const VALUE klass
Class of an object.
Definition: rbasic.h:88
"Typed" user data.
Definition: rtypeddata.h:340
void * data
Pointer to the actual C level struct that you want to wrap.
Definition: rtypeddata.h:362
VALUE typed_flag
This has to be always 1.
Definition: rtypeddata.h:359
const rb_data_type_t * type
This field stores various information about how Ruby should handle a data.
Definition: rtypeddata.h:350
struct RBasic basic
The part that all ruby objects have in common.
Definition: rtypeddata.h:343
This is the struct that holds necessary info for a struct.
Definition: rtypeddata.h:190
size_t(* dsize)(const void *)
This function is to query the size of the underlying memory regions.
Definition: rtypeddata.h:230
const rb_data_type_t * parent
Parent of this class.
Definition: rtypeddata.h:280
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition: rtypeddata.h:220
void * reserved[1]
This field is reserved for future extension.
Definition: rtypeddata.h:247
RUBY_DATA_FUNC dcompact
This function is called when the object is relocated.
Definition: rtypeddata.h:241
const char * wrap_struct_name
Name of structs of this kind.
Definition: rtypeddata.h:197
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition: rtypeddata.h:211
void * data
Type-specific static data.
Definition: rtypeddata.h:286
VALUE flags
Type-specific behavioural characteristics.
Definition: rtypeddata.h:299
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
Defines enum ruby_value_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
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
Definition: value_type.h:126