Ruby 3.5.0dev (2025-06-27 revision 64a52c25fef8e156630fea559ced7286fe5c3beb)
rdata.h
Go to the documentation of this file.
1#ifndef RBIMPL_RDATA_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_RDATA_H
23#include "ruby/internal/config.h"
24
25#ifdef STDC_HEADERS
26# include <stddef.h>
27#endif
28
31#include "ruby/internal/cast.h"
35#include "ruby/internal/value.h"
37#include "ruby/defines.h"
38
40#ifndef RUBY_UNTYPED_DATA_WARNING
41#define RUBY_UNTYPED_DATA_WARNING 1
42#endif
43
44#define RBIMPL_DATA_FUNC(f) RBIMPL_CAST((void (*)(void *))(f))
45#define RBIMPL_ATTRSET_UNTYPED_DATA_FUNC() \
46 RBIMPL_ATTR_WARNING(("untyped Data is unsafe; use TypedData instead")) \
47 RBIMPL_ATTR_DEPRECATED(("by TypedData"))
48
49#define RBIMPL_MACRO_SELECT(x, y) x ## y
50#define RUBY_MACRO_SELECT(x, y) RBIMPL_MACRO_SELECT(x, y)
59#define RDATA(obj) RBIMPL_CAST((struct RData *)(obj))
60
67#define DATA_PTR(obj) RDATA(obj)->data
68
78#define RUBY_DEFAULT_FREE RBIMPL_DATA_FUNC(-1)
79
85#define RUBY_NEVER_FREE RBIMPL_DATA_FUNC(0)
86
94#define RUBY_UNTYPED_DATA_FUNC(f) f RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
95
96/*
97#define RUBY_DATA_FUNC(func) ((void (*)(void*))(func))
98*/
99
104typedef void (*RUBY_DATA_FUNC)(void*);
105
120struct RData {
121
123 struct RBasic basic;
124
135
140 void *data;
141
150};
151
153
154
166
182
187RUBY_EXTERN VALUE rb_cObject;
189
201#define Data_Wrap_Struct(klass, mark, free, sval) \
202 rb_data_object_wrap( \
203 (klass), \
204 (sval), \
205 RBIMPL_DATA_FUNC(mark), \
206 RBIMPL_DATA_FUNC(free))
207
222#define Data_Make_Struct0(result, klass, type, size, mark, free, sval) \
223 VALUE result = rb_data_object_zalloc( \
224 (klass), \
225 (size), \
226 RBIMPL_DATA_FUNC(mark), \
227 RBIMPL_DATA_FUNC(free)); \
228 (sval) = RBIMPL_CAST((type *)DATA_PTR(result)); \
229 RBIMPL_CAST(/*suppress unused variable warnings*/(void)(sval))
230
246#ifdef HAVE_STMT_AND_DECL_IN_EXPR
247#define Data_Make_Struct(klass, type, mark, free, sval) \
248 RB_GNUC_EXTENSION({ \
249 Data_Make_Struct0( \
250 data_struct_obj, \
251 klass, \
252 type, \
253 sizeof(type), \
254 mark, \
255 free, \
256 sval); \
257 data_struct_obj; \
258 })
259#else
260#define Data_Make_Struct(klass, type, mark, free, sval) \
261 rb_data_object_make( \
262 (klass), \
263 RBIMPL_DATA_FUNC(mark), \
264 RBIMPL_DATA_FUNC(free), \
265 RBIMPL_CAST((void **)&(sval)), \
266 sizeof(type))
267#endif
268
277#define Data_Get_Struct(obj, type, sval) \
278 ((sval) = RBIMPL_CAST((type*)rb_data_object_get(obj)))
279
280RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
295static inline VALUE
296rb_data_object_wrap_warning(VALUE klass, void *ptr, RUBY_DATA_FUNC mark, RUBY_DATA_FUNC free)
297{
298 return rb_data_object_wrap(klass, ptr, mark, free);
299}
300
310static inline void *
311rb_data_object_get(VALUE obj)
312{
314 return DATA_PTR(obj);
315}
316
317RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
327static inline void *
328rb_data_object_get_warning(VALUE obj)
329{
330 return rb_data_object_get(obj);
331}
332
347static inline VALUE
348rb_data_object_make(VALUE klass, RUBY_DATA_FUNC mark_func, RUBY_DATA_FUNC free_func, void **datap, size_t size)
349{
350 Data_Make_Struct0(result, klass, void, size, mark_func, free_func, *datap);
351 return result;
352}
353
354RBIMPL_ATTR_DEPRECATED(("by: rb_data_object_wrap"))
356static inline VALUE
357rb_data_object_alloc(VALUE klass, void *data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
358{
359 return rb_data_object_wrap(klass, data, dmark, dfree);
360}
361
363#define rb_data_object_wrap_0 rb_data_object_wrap
364#define rb_data_object_wrap_1 rb_data_object_wrap_warning
365#define rb_data_object_wrap_2 rb_data_object_wrap_ /* Used here vvvv */
366#define rb_data_object_wrap RUBY_MACRO_SELECT(rb_data_object_wrap_2, RUBY_UNTYPED_DATA_WARNING)
367#define rb_data_object_get_0 rb_data_object_get
368#define rb_data_object_get_1 rb_data_object_get_warning
369#define rb_data_object_get_2 rb_data_object_get_ /* Used here vvvv */
370#define rb_data_object_get RUBY_MACRO_SELECT(rb_data_object_get_2, RUBY_UNTYPED_DATA_WARNING)
371#define rb_data_object_make_0 rb_data_object_make
372#define rb_data_object_make_1 rb_data_object_make_warning
373#define rb_data_object_make_2 rb_data_object_make_ /* Used here vvvv */
374#define rb_data_object_make RUBY_MACRO_SELECT(rb_data_object_make_2, RUBY_UNTYPED_DATA_WARNING)
376#endif /* RBIMPL_RDATA_H */
Defines RBIMPL_ATTR_WARNING.
Defines RBIMPL_ATTR_DEPRECATED.
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition deprecated.h:64
Tweaking visibility of C variables/functions.
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
#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.
#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 struct RBasic.
VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
This is the primitive way to wrap an existing C struct into RData.
Definition gc.c:1049
static VALUE rb_data_object_make(VALUE klass, RUBY_DATA_FUNC mark_func, RUBY_DATA_FUNC free_func, void **datap, size_t size)
This is an implementation detail of Data_Make_Struct.
Definition rdata.h:348
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
Identical to rb_data_object_wrap(), except it allocates a new data region internally instead of takin...
Definition gc.c:1057
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
Definition rdata.h:104
#define Data_Make_Struct0(result, klass, type, size, mark, free, sval)
This is an implementation detail of Data_Make_Struct.
Definition rdata.h:222
Ruby object's base components.
Definition rbasic.h:69
const VALUE klass
Class of an object.
Definition rbasic.h:92
Definition rdata.h:120
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rdata.h:149
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rdata.h:134
void * data
Pointer to the actual C level struct that you want to wrap.
Definition rdata.h:140
struct RBasic basic
Basic part, including flags and class.
Definition rdata.h:123
Defines VALUE and ID.
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:433
@ RUBY_T_DATA
Definition value_type.h:127