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

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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/token_paste.h"
36#include "ruby/internal/value.h"
37#include "ruby/internal/value_type.h"
38#include "ruby/defines.h"
39
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
49#define RBIMPL_DATA_FUNC(f) RBIMPL_CAST((void (*)(void *))(f))
50#define RBIMPL_ATTRSET_UNTYPED_DATA_FUNC() \
51 RBIMPL_ATTR_WARNING(("untyped Data is unsafe; use TypedData instead")) \
52 RBIMPL_ATTR_DEPRECATED(("by TypedData"))
55#define RDATA(obj) RBIMPL_CAST((struct RData *)(obj))
56#define DATA_PTR(obj) RDATA(obj)->data
57#define RUBY_MACRO_SELECT RBIMPL_TOKEN_PASTE
58#define RUBY_DEFAULT_FREE RBIMPL_DATA_FUNC(-1)
59#define RUBY_NEVER_FREE RBIMPL_DATA_FUNC(0)
60#define RUBY_UNTYPED_DATA_FUNC(f) f RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
61
62/*
63#define RUBY_DATA_FUNC(func) ((void (*)(void*))(func))
64*/
65 typedef void (*RUBY_DATA_FUNC)(void*);
66
67 struct RData {
68 struct RBasic basic;
69 RUBY_DATA_FUNC dmark;
70 RUBY_DATA_FUNC dfree;
71 void *data;
72};
73
74 RBIMPL_SYMBOL_EXPORT_BEGIN()
75 VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree);
76 VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree);
77 RUBY_EXTERN VALUE rb_cObject;
78 RBIMPL_SYMBOL_EXPORT_END()
79
80#define Data_Wrap_Struct(klass, mark, free, sval) \
81 rb_data_object_wrap( \
82 (klass), \
83 (sval), \
84 RBIMPL_DATA_FUNC(mark), \
85 RBIMPL_DATA_FUNC(free))
86
87#define Data_Make_Struct0(result, klass, type, size, mark, free, sval) \
88 VALUE result = rb_data_object_zalloc( \
89 (klass), \
90 (size), \
91 RBIMPL_DATA_FUNC(mark), \
92 RBIMPL_DATA_FUNC(free)); \
93 (sval) = RBIMPL_CAST((type *)DATA_PTR(result)); \
94 RBIMPL_CAST(/*suppress unused variable warnings*/(void)(sval))
95
96#ifdef HAVE_STMT_AND_DECL_IN_EXPR
97#define Data_Make_Struct(klass, type, mark, free, sval) \
98 RB_GNUC_EXTENSION({ \
99 Data_Make_Struct0( \
100 data_struct_obj, \
101 klass, \
102 type, \
103 sizeof(type), \
104 mark, \
105 free, \
106 sval); \
107 data_struct_obj; \
108 })
109#else
110#define Data_Make_Struct(klass, type, mark, free, sval) \
111 rb_data_object_make( \
112 (klass), \
113 RBIMPL_DATA_FUNC(mark), \
114 RBIMPL_DATA_FUNC(free), \
115 RBIMPL_CAST((void **)&(sval)), \
116 sizeof(type))
117#endif
118
119#define Data_Get_Struct(obj, type, sval) \
120 ((sval) = RBIMPL_CAST((type*)rb_data_object_get(obj)))
121
122RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
123static inline VALUE
124rb_data_object_wrap_warning(VALUE klass, void *ptr, RUBY_DATA_FUNC mark, RUBY_DATA_FUNC free)
125{
126 return rb_data_object_wrap(klass, ptr, mark, free);
127}
128
129static inline void *
130 rb_data_object_get(VALUE obj)
131{
132 Check_Type(obj, RUBY_T_DATA);
133 return DATA_PTR(obj);
134}
135
136RBIMPL_ATTRSET_UNTYPED_DATA_FUNC()
137static inline void *
138rb_data_object_get_warning(VALUE obj)
139{
140 return rb_data_object_get(obj);
141}
142
143#if defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P)
144# define rb_data_object_wrap_warning(klass, ptr, mark, free) \
145 RB_GNUC_EXTENSION( \
146 __builtin_choose_expr( \
147 __builtin_constant_p(klass) && !(klass), \
148 rb_data_object_wrap(klass, ptr, mark, free), \
149 (rb_data_object_wrap_warning)(klass, ptr, mark, free)))
150#endif
151
152static inline VALUE
153 rb_data_object_make(VALUE klass, RUBY_DATA_FUNC mark_func, RUBY_DATA_FUNC free_func, void **datap, size_t size)
154{
155 Data_Make_Struct0(result, klass, void, size, mark_func, free_func, *datap);
156 return result;
157}
158
159 RBIMPL_ATTR_DEPRECATED(("by: rb_data_object_wrap"))
160static inline VALUE
161 rb_data_object_alloc(VALUE klass, void *data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
162{
163 return rb_data_object_wrap(klass, data, dmark, dfree);
164}
165
166 RBIMPL_ATTR_DEPRECATED(("by: rb_cObject. Will be removed in 3.1."))
167 RBIMPL_ATTR_PURE()
168static inline VALUE
169 rb_cData(void)
170{
171 return rb_cObject;
172}
173#define rb_cData rb_cData()
174
175#define rb_data_object_wrap_0 rb_data_object_wrap
176#define rb_data_object_wrap_1 rb_data_object_wrap_warning
177#define rb_data_object_wrap RUBY_MACRO_SELECT(rb_data_object_wrap_, RUBY_UNTYPED_DATA_WARNING)
178#define rb_data_object_get_0 rb_data_object_get
179#define rb_data_object_get_1 rb_data_object_get_warning
180#define rb_data_object_get RUBY_MACRO_SELECT(rb_data_object_get_, RUBY_UNTYPED_DATA_WARNING)
181#define rb_data_object_make_0 rb_data_object_make
182#define rb_data_object_make_1 rb_data_object_make_warning
183#define rb_data_object_make RUBY_MACRO_SELECT(rb_data_object_make_, RUBY_UNTYPED_DATA_WARNING)
184#endif /* RBIMPL_RDATA_H */
warning.h
Defines RBIMPL_ATTR_WARNING.
cast.h
Defines RBIMPL_CAST.
ptr
struct RIMemo * ptr
Definition: debug.c:88
defines.h
deprecated.h
Defines RBIMPL_ATTR_DEPRECATED.
RBIMPL_ATTR_DEPRECATED
#define RBIMPL_ATTR_DEPRECATED(msg)
Wraps (or simulates) [[deprecated]]
Definition: deprecated.h:60
dllexport.h
Tewaking visibility of C variables/functions.
RUBY_EXTERN
#define RUBY_EXTERN
Definition: dllexport.h:36
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
free
#define free(x)
Definition: dln.c:52
fl_type.h
Defines enum ruby_fl_type.
rb_data_object_alloc
#define rb_data_object_alloc
Definition: gc.c:14
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:49
config.h
Thin wrapper to ruby/config.h.
size
voidpf void uLong size
Definition: ioapi.h:138
RBIMPL_ATTR_PURE
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure))
Definition: pure.h:33
rbasic.h
Defines struct RBasic.
rb_data_object_get
#define rb_data_object_get
Definition: rdata.h:180
DATA_PTR
#define DATA_PTR(obj)
Definition: rdata.h:56
rb_data_object_zalloc
VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
Definition: gc.c:2497
rb_cData
#define rb_cData
Definition: rdata.h:173
RUBY_DATA_FUNC
void(* RUBY_DATA_FUNC)(void *)
Definition: rdata.h:65
Data_Make_Struct0
#define Data_Make_Struct0(result, klass, type, size, mark, free, sval)
Definition: rdata.h:87
rb_data_object_wrap
#define rb_data_object_wrap
Definition: rdata.h:177
rb_data_object_make
#define rb_data_object_make
Definition: rdata.h:183
RBasic
Definition: rbasic.h:47
RBasic::klass
const VALUE klass
Definition: rbasic.h:49
RData
Definition: rdata.h:67
RData::dfree
RUBY_DATA_FUNC dfree
Definition: rdata.h:70
RData::dmark
RUBY_DATA_FUNC dmark
Definition: rdata.h:69
RData::data
void * data
Definition: rdata.h:71
RData::basic
struct RBasic basic
Definition: rdata.h:68
token_paste.h
Defines RBIMPL_TOKEN_PASTE.
value.h
Defines VALUE and ID.
VALUE
unsigned long VALUE
Definition: value.h:38
value_type.h
Defines enum ruby_value_type.
RUBY_T_DATA
@ RUBY_T_DATA
Definition: value_type.h:124