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

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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#ifdef RUBY_UNTYPED_DATA_WARNING
41# /* Take that. */
42#elif defined(RUBY_EXPORT)
43# define RUBY_UNTYPED_DATA_WARNING 1
44#else
45# define RUBY_UNTYPED_DATA_WARNING 0
46#endif
47
48#define RBIMPL_DATA_FUNC(f) RBIMPL_CAST((void (*)(void *))(f))
49#define RBIMPL_ATTRSET_UNTYPED_DATA_FUNC() \
50 RBIMPL_ATTR_WARNING(("untyped Data is unsafe; use TypedData instead")) \
51 RBIMPL_ATTR_DEPRECATED(("by TypedData"))
52
53#define RBIMPL_MACRO_SELECT(x, y) x ## y
54#define RUBY_MACRO_SELECT(x, y) RBIMPL_MACRO_SELECT(x, y)
63#define RDATA(obj) RBIMPL_CAST((struct RData *)(obj))
64
71#define DATA_PTR(obj) RDATA(obj)->data
72
82#define RUBY_DEFAULT_FREE RBIMPL_DATA_FUNC(-1)
83
89#define RUBY_NEVER_FREE RBIMPL_DATA_FUNC(0)
90
98#define RUBY_UNTYPED_DATA_FUNC(f) f RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
99
100/*
101#define RUBY_DATA_FUNC(func) ((void (*)(void*))(func))
102*/
103
108 typedef void (*RUBY_DATA_FUNC)(void*);
109
124 struct RData {
125
127 struct RBasic basic;
128
138 RUBY_DATA_FUNC dmark;
139
147 RUBY_DATA_FUNC dfree;
148
150 void *data;
151};
152
153 RBIMPL_SYMBOL_EXPORT_BEGIN()
154
155
166 VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree);
167
182 VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree);
183
188 RUBY_EXTERN VALUE rb_cObject;
189 RBIMPL_SYMBOL_EXPORT_END()
190
202#define Data_Wrap_Struct(klass, mark, free, sval) \
203 rb_data_object_wrap( \
204 (klass), \
205 (sval), \
206 RBIMPL_DATA_FUNC(mark), \
207 RBIMPL_DATA_FUNC(free))
208
223#define Data_Make_Struct0(result, klass, type, size, mark, free, sval) \
224 VALUE result = rb_data_object_zalloc( \
225 (klass), \
226 (size), \
227 RBIMPL_DATA_FUNC(mark), \
228 RBIMPL_DATA_FUNC(free)); \
229 (sval) = RBIMPL_CAST((type *)DATA_PTR(result)); \
230 RBIMPL_CAST(/*suppress unused variable warnings*/(void)(sval))
231
247#ifdef HAVE_STMT_AND_DECL_IN_EXPR
248#define Data_Make_Struct(klass, type, mark, free, sval) \
249 RB_GNUC_EXTENSION({ \
250 Data_Make_Struct0( \
251 data_struct_obj, \
252 klass, \
253 type, \
254 sizeof(type), \
255 mark, \
256 free, \
257 sval); \
258 data_struct_obj; \
259 })
260#else
261#define Data_Make_Struct(klass, type, mark, free, sval) \
262 rb_data_object_make( \
263 (klass), \
264 RBIMPL_DATA_FUNC(mark), \
265 RBIMPL_DATA_FUNC(free), \
266 RBIMPL_CAST((void **)&(sval)), \
267 sizeof(type))
268#endif
269
278#define Data_Get_Struct(obj, type, sval) \
279 ((sval) = RBIMPL_CAST((type*)rb_data_object_get(obj)))
280
281RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
296static inline VALUE
297rb_data_object_wrap_warning(VALUE klass, void *ptr, RUBY_DATA_FUNC mark, RUBY_DATA_FUNC free)
298{
299 return rb_data_object_wrap(klass, ptr, mark, free);
300}
301
311static inline void *
312rb_data_object_get(VALUE obj)
313{
314 Check_Type(obj, RUBY_T_DATA);
315 return DATA_PTR(obj);
316}
317
318RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
328static inline void *
329rb_data_object_get_warning(VALUE obj)
330{
331 return rb_data_object_get(obj);
332}
333
334#if defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P)
335# define rb_data_object_wrap_warning(klass, ptr, mark, free) \
336 RB_GNUC_EXTENSION( \
337 __builtin_choose_expr( \
338 __builtin_constant_p(klass) && !(klass), \
339 rb_data_object_wrap(klass, ptr, mark, free), \
340 (rb_data_object_wrap_warning)(klass, ptr, mark, free)))
341#endif
342
357static inline VALUE
358rb_data_object_make(VALUE klass, RUBY_DATA_FUNC mark_func, RUBY_DATA_FUNC free_func, void **datap, size_t size)
359{
360 Data_Make_Struct0(result, klass, void, size, mark_func, free_func, *datap);
361 return result;
362}
363
364 RBIMPL_ATTR_DEPRECATED(("by: rb_data_object_wrap"))
366static inline VALUE
367rb_data_object_alloc(VALUE klass, void *data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
368{
369 return rb_data_object_wrap(klass, data, dmark, dfree);
370}
371
373#define rb_data_object_wrap_0 rb_data_object_wrap
374#define rb_data_object_wrap_1 rb_data_object_wrap_warning
375#define rb_data_object_wrap_2 rb_data_object_wrap_ /* Used here vvvv */
376#define rb_data_object_wrap RUBY_MACRO_SELECT(rb_data_object_wrap_2, RUBY_UNTYPED_DATA_WARNING)
377#define rb_data_object_get_0 rb_data_object_get
378#define rb_data_object_get_1 rb_data_object_get_warning
379#define rb_data_object_get_2 rb_data_object_get_ /* Used here vvvv */
380#define rb_data_object_get RUBY_MACRO_SELECT(rb_data_object_get_2, RUBY_UNTYPED_DATA_WARNING)
381#define rb_data_object_make_0 rb_data_object_make
382#define rb_data_object_make_1 rb_data_object_make_warning
383#define rb_data_object_make_2 rb_data_object_make_ /* Used here vvvv */
384#define rb_data_object_make RUBY_MACRO_SELECT(rb_data_object_make_2, RUBY_UNTYPED_DATA_WARNING)
386#endif /* RBIMPL_RDATA_H */
warning.h
Defines RBIMPL_ATTR_WARNING.
defines.h
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:47
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:106
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:97
fl_type.h
Defines enum ruby_fl_type.
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:3116
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:358
DATA_PTR
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:71
rb_data_object_zalloc
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:3124
RUBY_DATA_FUNC
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
Definition rdata.h:108
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:223
RBasic
Ruby's object's, base components.
Definition rbasic.h:64
RBasic::klass
const VALUE klass
Class of an object.
Definition rbasic.h:88
RData
Definition rdata.h:124
RData::dfree
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rdata.h:147
RData::dmark
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rdata.h:138
RData::data
void * data
Pointer to the actual C level struct that you want to wrap.
Definition rdata.h:150
RData::basic
struct RBasic basic
Basic part, including flags and class.
Definition rdata.h:127
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:432
RUBY_T_DATA
@ RUBY_T_DATA
Definition value_type.h:126