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

Ruby 3.4.6p54 (2025-09-16 revision dbd83256b1cec76c69756ecb8758b9e1079833de)

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
29#include "ruby/internal/attr/deprecated.h"
30#include "ruby/internal/attr/warning.h"
31#include "ruby/internal/cast.h"
32#include "ruby/internal/core/rbasic.h"
33#include "ruby/internal/dllexport.h"
34#include "ruby/internal/fl_type.h"
35#include "ruby/internal/value.h"
36#include "ruby/internal/value_type.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)
52
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
104 typedef void (*RUBY_DATA_FUNC)(void*);
105
120 struct RData {
121
123 struct RBasic basic;
124
134 RUBY_DATA_FUNC dmark;
135
143 RUBY_DATA_FUNC dfree;
144
146 void *data;
147};
148
149 RBIMPL_SYMBOL_EXPORT_BEGIN()
150
151
162 VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree);
163
178 VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree);
179
184 RUBY_EXTERN VALUE rb_cObject;
185 RBIMPL_SYMBOL_EXPORT_END()
186
198#define Data_Wrap_Struct(klass, mark, free, sval) \
199 rb_data_object_wrap( \
200 (klass), \
201 (sval), \
202 RBIMPL_DATA_FUNC(mark), \
203 RBIMPL_DATA_FUNC(free))
204
219#define Data_Make_Struct0(result, klass, type, size, mark, free, sval) \
220 VALUE result = rb_data_object_zalloc( \
221 (klass), \
222 (size), \
223 RBIMPL_DATA_FUNC(mark), \
224 RBIMPL_DATA_FUNC(free)); \
225 (sval) = RBIMPL_CAST((type *)DATA_PTR(result)); \
226 RBIMPL_CAST(/*suppress unused variable warnings*/(void)(sval))
227
243#ifdef HAVE_STMT_AND_DECL_IN_EXPR
244#define Data_Make_Struct(klass, type, mark, free, sval) \
245 RB_GNUC_EXTENSION({ \
246 Data_Make_Struct0( \
247 data_struct_obj, \
248 klass, \
249 type, \
250 sizeof(type), \
251 mark, \
252 free, \
253 sval); \
254 data_struct_obj; \
255 })
256#else
257#define Data_Make_Struct(klass, type, mark, free, sval) \
258 rb_data_object_make( \
259 (klass), \
260 RBIMPL_DATA_FUNC(mark), \
261 RBIMPL_DATA_FUNC(free), \
262 RBIMPL_CAST((void **)&(sval)), \
263 sizeof(type))
264#endif
265
274#define Data_Get_Struct(obj, type, sval) \
275 ((sval) = RBIMPL_CAST((type*)rb_data_object_get(obj)))
276
277RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
292static inline VALUE
293rb_data_object_wrap_warning(VALUE klass, void *ptr, RUBY_DATA_FUNC mark, RUBY_DATA_FUNC free)
294{
295 return rb_data_object_wrap(klass, ptr, mark, free);
296}
297
307static inline void *
308rb_data_object_get(VALUE obj)
309{
310 Check_Type(obj, RUBY_T_DATA);
311 return DATA_PTR(obj);
312}
313
314RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
324static inline void *
325rb_data_object_get_warning(VALUE obj)
326{
327 return rb_data_object_get(obj);
328}
329
344static inline VALUE
345rb_data_object_make(VALUE klass, RUBY_DATA_FUNC mark_func, RUBY_DATA_FUNC free_func, void **datap, size_t size)
346{
347 Data_Make_Struct0(result, klass, void, size, mark_func, free_func, *datap);
348 return result;
349}
350
351 RBIMPL_ATTR_DEPRECATED(("by: rb_data_object_wrap"))
353static inline VALUE
354rb_data_object_alloc(VALUE klass, void *data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
355{
356 return rb_data_object_wrap(klass, data, dmark, dfree);
357}
358
360#define rb_data_object_wrap_0 rb_data_object_wrap
361#define rb_data_object_wrap_1 rb_data_object_wrap_warning
362#define rb_data_object_wrap_2 rb_data_object_wrap_ /* Used here vvvv */
363#define rb_data_object_wrap RUBY_MACRO_SELECT(rb_data_object_wrap_2, RUBY_UNTYPED_DATA_WARNING)
364#define rb_data_object_get_0 rb_data_object_get
365#define rb_data_object_get_1 rb_data_object_get_warning
366#define rb_data_object_get_2 rb_data_object_get_ /* Used here vvvv */
367#define rb_data_object_get RUBY_MACRO_SELECT(rb_data_object_get_2, RUBY_UNTYPED_DATA_WARNING)
368#define rb_data_object_make_0 rb_data_object_make
369#define rb_data_object_make_1 rb_data_object_make_warning
370#define rb_data_object_make_2 rb_data_object_make_ /* Used here vvvv */
371#define rb_data_object_make RUBY_MACRO_SELECT(rb_data_object_make_2, RUBY_UNTYPED_DATA_WARNING)
373#endif /* RBIMPL_RDATA_H */
warning.h
Defines RBIMPL_ATTR_WARNING.
deprecated.h
Defines RBIMPL_ATTR_DEPRECATED.
RBIMPL_ATTR_DEPRECATED
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition deprecated.h:64
dllexport.h
Tweaking visibility of C variables/functions.
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
fl_type.h
Defines enum ruby_fl_type.
defines.h
inline
#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
rbasic.h
Defines struct RBasic.
rb_data_object_wrap
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:1090
rb_data_object_make
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:345
DATA_PTR
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
RUBY_DATA_FUNC
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
Definition rdata.h:104
Data_Make_Struct0
#define Data_Make_Struct0(result, klass, type, size, mark, free, sval)
This is an implementation detail of Data_Make_Struct.
Definition rdata.h:219
RBasic
Ruby object's base components.
Definition rbasic.h:63
RBasic::klass
const VALUE klass
Class of an object.
Definition rbasic.h:86
RData
Definition rdata.h:120
RData::dfree
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rdata.h:143
RData::dmark
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rdata.h:134
RData::data
void * data
Pointer to the actual C level struct that you want to wrap.
Definition rdata.h:146
RData::basic
struct RBasic basic
Basic part, including flags and class.
Definition rdata.h:123
value.h
Defines VALUE and ID.
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:433
RUBY_T_DATA
@ RUBY_T_DATA
Definition value_type.h:127