Ruby 4.1.0dev (2026-03-05 revision 8a87cebd1874f8f9f68af8928191ee3f0d97bb28)
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
34#include "ruby/internal/cast.h"
38#include "ruby/internal/error.h"
43
51#define HAVE_TYPE_RB_DATA_TYPE_T 1
52
60#define HAVE_RB_DATA_TYPE_T_FUNCTION 1
61
69#define HAVE_RB_DATA_TYPE_T_PARENT 1
70
80#define RUBY_TYPED_DEFAULT_FREE RUBY_DEFAULT_FREE
81
87#define RUBY_TYPED_NEVER_FREE RUBY_NEVER_FREE
88
95#define RTYPEDDATA(obj) RBIMPL_CAST((struct RTypedData *)(obj))
96
103#define RTYPEDDATA_DATA(v) (RTYPEDDATA(v)->data)
104
106#define Check_TypedStruct(v, t) \
107 rb_check_typeddata(RBIMPL_CAST((VALUE)(v)), (t))
108
110#define RTYPEDDATA_P RTYPEDDATA_P
111#define RTYPEDDATA_TYPE RTYPEDDATA_TYPE
112#define TYPED_DATA_EMBEDDED ((VALUE)1)
113#define TYPED_DATA_PTR_MASK (~(TYPED_DATA_EMBEDDED))
119#define RUBY_TYPED_FREE_IMMEDIATELY RUBY_TYPED_FREE_IMMEDIATELY
120#define RUBY_TYPED_FROZEN_SHAREABLE RUBY_TYPED_FROZEN_SHAREABLE
121#define RUBY_TYPED_WB_PROTECTED RUBY_TYPED_WB_PROTECTED
122#define RUBY_TYPED_PROMOTED1 RUBY_TYPED_PROMOTED1
123
129enum
131rbimpl_typeddata_flags {
146
147 RUBY_TYPED_EMBEDDABLE = 2,
148
161 RUBY_TYPED_FROZEN_SHAREABLE = RUBY_FL_SHAREABLE,
162
163 // experimental flag
164 // Similar to RUBY_TYPED_FROZEN_SHAREABLE, but doesn't make shareable
165 // reachable objects from this T_DATA object on the Ractor.make_shareable.
166 // If it refers to unshareable objects, simply raise an error.
167 // RUBY_TYPED_FROZEN_SHAREABLE_NO_REC = RUBY_FL_FINALIZE,
168
188 RUBY_TYPED_WB_PROTECTED = RUBY_FL_WB_PROTECTED, /* THIS FLAG DEPENDS ON Ruby version */
189
193 RUBY_TYPED_FL_IS_TYPED_DATA = RUBY_FL_USERPRIV0,
194
200 RUBY_TYPED_DECL_MARKING = RUBY_FL_USER2
201};
202
209
212
218 const char *wrap_struct_name;
219
221 struct {
222
233
242
251 size_t (*dsize)(const void *);
252
263
267 void (*handle_weak_references)(void *);
268
273 void *reserved[7]; /* For future extension.
274 This array *must* be filled with ZERO. */
276
307
312 void *data; /* This area can be used for any purpose
313 by a programmer who define the type. */
314
325 VALUE flags; /* RUBY_FL_WB_PROTECTED */
326};
327
367
369 struct RBasic basic;
370
373
383 const VALUE type;
384
386 void *data;
387};
388
389#if !defined(__cplusplus) || __cplusplus >= 201103L
390RBIMPL_STATIC_ASSERT(data_in_rtypeddata, offsetof(struct RData, data) == offsetof(struct RTypedData, data));
391#endif
392
405VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type);
406
422
436int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent);
437
447int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type);
448
460void *rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type);
461
475void rb_unexpected_object_type(VALUE obj, const char *expected);
476
490void rb_unexpected_typeddata(const rb_data_type_t *actual, const rb_data_type_t *expected);
492
493#if RUBY_DEBUG
494# define RBIMPL_TYPEDDATA_PRECONDITION(obj, unreachable) \
495 while (RB_UNLIKELY(!RB_TYPE_P(obj, RUBY_T_DATA))) { \
496 rb_unexpected_object_type(obj, "Data"); \
497 unreachable; \
498 }
499#else
500# define RBIMPL_TYPEDDATA_PRECONDITION(obj, unreachable) \
501 RBIMPL_ASSERT_NOTHING
502#endif
503
514#define TypedData_Wrap_Struct(klass,data_type,sval)\
515 rb_data_typed_object_wrap((klass),(sval),(data_type))
516
530#define TypedData_Make_Struct0(result, klass, type, size, data_type, sval) \
531 VALUE result = rb_data_typed_object_zalloc(klass, size, data_type); \
532 (sval) = RBIMPL_CAST((type *)rbimpl_typeddata_get_data(result)); \
533 RBIMPL_CAST(/*suppress unused variable warnings*/(void)(sval))
534
548#ifdef HAVE_STMT_AND_DECL_IN_EXPR
549#define TypedData_Make_Struct(klass, type, data_type, sval) \
550 RB_GNUC_EXTENSION({ \
551 TypedData_Make_Struct0( \
552 data_struct_obj, \
553 klass, \
554 type, \
555 sizeof(type), \
556 data_type, \
557 sval); \
558 data_struct_obj; \
559 })
560#else
561#define TypedData_Make_Struct(klass, type, data_type, sval) \
562 rb_data_typed_object_make( \
563 (klass), \
564 (data_type), \
565 RBIMPL_CAST((void **)&(sval)), \
566 sizeof(type))
567#endif
568
569static inline bool
570rbimpl_typeddata_embedded_p(VALUE obj)
571{
572 return (RTYPEDDATA(obj)->type) & TYPED_DATA_EMBEDDED;
573}
574
575RBIMPL_ATTR_DEPRECATED_INTERNAL_ONLY()
576static inline bool
577RTYPEDDATA_EMBEDDED_P(VALUE obj)
578{
579 RBIMPL_TYPEDDATA_PRECONDITION(obj, RBIMPL_UNREACHABLE_RETURN(false));
580
581 return rbimpl_typeddata_embedded_p(obj);
582}
583
584static inline void *
585rbimpl_typeddata_get_data(VALUE obj)
586{
587 /* We reuse the data pointer in embedded TypedData. */
588 return rbimpl_typeddata_embedded_p(obj) ?
589 RBIMPL_CAST((void *)&RTYPEDDATA_DATA(obj)) :
590 RTYPEDDATA_DATA(obj);
591}
592
593static inline void *
594RTYPEDDATA_GET_DATA(VALUE obj)
595{
596 RBIMPL_TYPEDDATA_PRECONDITION(obj, RBIMPL_UNREACHABLE_RETURN(NULL));
597
598 return rbimpl_typeddata_get_data(obj);
599}
600
614static inline bool
615rbimpl_rtypeddata_p(VALUE obj)
616{
617 return FL_TEST_RAW(obj, RUBY_TYPED_FL_IS_TYPED_DATA);
618}
619
635static inline bool
636rbimpl_obj_typeddata_p(VALUE obj)
637{
638 return RB_TYPE_P(obj, RUBY_T_DATA) && rbimpl_rtypeddata_p(obj);
639}
640
651static inline bool
653{
654 RBIMPL_TYPEDDATA_PRECONDITION(obj, RBIMPL_UNREACHABLE_RETURN(false));
655
656 return rbimpl_rtypeddata_p(obj);
657}
658
669static inline const rb_data_type_t *
671{
672 RBIMPL_TYPEDDATA_PRECONDITION(obj, RBIMPL_UNREACHABLE_RETURN(NULL));
673
674 VALUE type = RTYPEDDATA(obj)->type & TYPED_DATA_PTR_MASK;
675 const rb_data_type_t *ptr = RBIMPL_CAST((const rb_data_type_t *)type);
677 return ptr;
678}
679
682static inline bool
683rbimpl_typeddata_inherited_p_inline(const rb_data_type_t *child, const rb_data_type_t *parent)
684{
685 do {
686 if (RB_LIKELY(child == parent)) return true;
687 } while ((child = child->parent) != NULL);
688 return false;
689}
690#define rb_typeddata_inherited_p rbimpl_typeddata_inherited_p_inline
691
694static inline bool
695rbimpl_typeddata_is_kind_of_inline(VALUE obj, const rb_data_type_t *data_type)
696{
697 if (RB_UNLIKELY(!rbimpl_obj_typeddata_p(obj))) return false;
698 return rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type);
699}
700#define rb_typeddata_is_kind_of rbimpl_typeddata_is_kind_of_inline
701
710static inline void *
711rbimpl_check_typeddata(VALUE obj, const rb_data_type_t *expected_type)
712{
713 if (RB_UNLIKELY(!rbimpl_obj_typeddata_p(obj))) {
714 rb_unexpected_object_type(obj, expected_type->wrap_struct_name);
715 }
716
717 const rb_data_type_t *actual_type = RTYPEDDATA_TYPE(obj);
718 if (RB_UNLIKELY(!rb_typeddata_inherited_p(actual_type, expected_type))){
719 rb_unexpected_typeddata(actual_type, expected_type);
720 }
721
722 return RTYPEDDATA_GET_DATA(obj);
723}
724
725
736#define TypedData_Get_Struct(obj,type,data_type,sval) \
737 ((sval) = RBIMPL_CAST((type *)rbimpl_check_typeddata((obj), (data_type))))
738
754static inline VALUE
755rb_data_typed_object_make(VALUE klass, const rb_data_type_t *type, void **datap, size_t size)
756{
757 TypedData_Make_Struct0(result, klass, void, size, type, *datap);
758 return result;
759}
760
761#endif /* RBIMPL_RTYPEDDATA_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:311
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
Defines enum ruby_fl_type.
@ RUBY_FL_WB_PROTECTED
Definition fl_type.h:186
@ RUBY_FL_USER2
User-defined flag.
Definition fl_type.h:284
@ RUBY_FL_USERPRIV0
This flag meaning is type dependent, currently only used by T_DATA.
Definition fl_type.h:212
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:253
Defines RBIMPL_ATTR_FLAG_ENUM.
#define RBIMPL_ATTR_FLAG_ENUM()
Wraps (or simulates) __attribute__((flag_enum)
Definition flag_enum.h:30
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
void rb_unexpected_object_type(VALUE obj, const char *expected)
Fails with the given object's type incompatibility to the type.
Definition error.c:1324
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:1404
void rb_unexpected_typeddata(const rb_data_type_t *actual, const rb_data_type_t *expected)
Fails with the given object's type incompatibility to the type.
Definition error.c:1317
Declares rb_raise().
Defines RBIMPL_STATIC_ASSERT.
#define RBIMPL_STATIC_ASSERT
Wraps (or simulates) static_assert
Defines RBIMPL_ASSUME / RBIMPL_UNREACHABLE.
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:48
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
#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
Defines RBIMPL_ATTR_PURE.
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure))
Definition pure.h:33
#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:104
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:652
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition rtypeddata.h:103
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:1136
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:1146
#define TypedData_Make_Struct0(result, klass, type, size, data_type, sval)
This is an implementation detail of TypedData_Make_Struct.
Definition rtypeddata.h:530
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:119
static const rb_data_type_t * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:670
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition rtypeddata.h:95
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:755
C99 shim for <stdbool.h>
Ruby object's base components.
Definition rbasic.h:69
const VALUE klass
Class of an object.
Definition rbasic.h:92
Definition rdata.h:120
"Typed" user data.
Definition rtypeddata.h:366
void * data
Pointer to the actual C level struct that you want to wrap.
Definition rtypeddata.h:386
const VALUE type
This is a const rb_data_type_t *const value, with the low bits set:
Definition rtypeddata.h:383
VALUE fields_obj
Direct reference to the slots that holds instance variables, if any.
Definition rtypeddata.h:372
struct RBasic basic
The part that all ruby objects have in common.
Definition rtypeddata.h:369
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:211
size_t(* dsize)(const void *)
This function is to query the size of the underlying memory regions.
Definition rtypeddata.h:251
const rb_data_type_t * parent
Parent of this class.
Definition rtypeddata.h:306
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rtypeddata.h:241
RUBY_DATA_FUNC dcompact
This function is called when the object is relocated.
Definition rtypeddata.h:262
struct rb_data_type_struct::@56 function
Function pointers.
void * reserved[7]
This field is reserved for future extension.
Definition rtypeddata.h:273
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:218
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rtypeddata.h:232
void * data
Type-specific static data.
Definition rtypeddata.h:312
VALUE flags
Type-specific behavioural characteristics.
Definition rtypeddata.h:325
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Defines enum ruby_value_type.
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:376
@ RUBY_T_DATA
Definition value_type.h:127