Ruby  3.4.0dev (2024-11-22 revision 37a72b0150ec36b4ea27175039afc28c62207b0c)
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"
34 #include "ruby/internal/fl_type.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 
104 typedef void (*RUBY_DATA_FUNC)(void*);
105 
120 struct RData {
121 
123  struct RBasic basic;
124 
135 
144 
146  void *data;
147 };
148 
150 
151 
163 
179 
184 RUBY_EXTERN VALUE rb_cObject;
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 
277 RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
292 static inline VALUE
293 rb_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 
307 static inline void *
308 rb_data_object_get(VALUE obj)
309 {
310  Check_Type(obj, RUBY_T_DATA);
311  return DATA_PTR(obj);
312 }
313 
314 RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
324 static inline void *
325 rb_data_object_get_warning(VALUE obj)
326 {
327  return rb_data_object_get(obj);
328 }
329 
344 static inline VALUE
345 rb_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"))
353 static inline VALUE
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 */
Defines RBIMPL_ATTR_WARNING.
Defines RBIMPL_ATTR_DEPRECATED.
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition: deprecated.h:36
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.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition: defines.h:88
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:961
static VALUE rb_data_object_alloc(VALUE klass, void *data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
Definition: rdata.h:354
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
#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:969
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:219
Ruby object's base components.
Definition: rbasic.h:63
const VALUE klass
Class of an object.
Definition: rbasic.h:86
Definition: rdata.h:120
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition: rdata.h:143
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:146
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