| 1 |
#ifndef RBIMPL_RDATA_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_RDATA_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Defines struct ::RData. |
| 22 |
*/ |
| 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 |
|
| 39 |
/** @cond INTERNAL_MACRO */ |
| 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) |
| 55 |
/** @endcond */ |
| 56 |
|
| 57 |
/** |
| 58 |
* Convenient casting macro. |
| 59 |
* |
| 60 |
* @param obj An object, which is in fact an ::RData. |
| 61 |
* @return The passed object casted to ::RData. |
| 62 |
*/ |
| 63 |
#define RDATA(obj) RBIMPL_CAST((struct RData *)(obj)) |
| 64 |
|
| 65 |
/** |
| 66 |
* Convenient getter macro. |
| 67 |
* |
| 68 |
* @param obj An object, which is in fact an ::RData. |
| 69 |
* @return The passed object's ::RData::data field. |
| 70 |
*/ |
| 71 |
#define DATA_PTR(obj) RDATA(obj)->data |
| 72 |
|
| 73 |
/** |
| 74 |
* This is a value you can set to ::RData::dfree. Setting this means the data |
| 75 |
* was allocated using ::ruby_xmalloc() (or variants), and shall be freed using |
| 76 |
* ::ruby_xfree(). |
| 77 |
* |
| 78 |
* @warning Do not use this if you want to use system malloc, because the |
| 79 |
* system and Ruby might or might not share the same malloc |
| 80 |
* implementation. |
| 81 |
*/ |
| 82 |
#define RUBY_DEFAULT_FREE RBIMPL_DATA_FUNC(-1) |
| 83 |
|
| 84 |
/** |
| 85 |
* This is a value you can set to ::RData::dfree. Setting this means the data |
| 86 |
* is managed by someone else, like, statically allocated. Of course you are |
| 87 |
* on your own then. |
| 88 |
*/ |
| 89 |
#define RUBY_NEVER_FREE RBIMPL_DATA_FUNC(0) |
| 90 |
|
| 91 |
/** |
| 92 |
* @private |
| 93 |
* |
| 94 |
* @deprecated This macro once was a thing in the old days, but makes no sense |
| 95 |
* any longer today. Exists here for backwards compatibility |
| 96 |
* only. You can safely forget about it. |
| 97 |
*/ |
| 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 |
|
| 104 |
/** |
| 105 |
* This is the type of callbacks registered to ::RData. The argument is the |
| 106 |
* `data` field. |
| 107 |
*/ |
| 108 |
typedef void (*RUBY_DATA_FUNC)(void*); |
| 109 |
|
| 110 |
/** |
| 111 |
* @deprecated |
| 112 |
* |
| 113 |
* Old "untyped" user data. It has roughly the same usage as struct |
| 114 |
* ::RTypedData, but lacked several features such as support for compaction GC. |
| 115 |
* Use of this struct is not recommended any longer. If it is dead necessary, |
| 116 |
* please inform the core devs about your usage. |
| 117 |
* |
| 118 |
* @internal |
| 119 |
* |
| 120 |
* @shyouhei tried to add RBIMPL_ATTR_DEPRECATED for this type but that yielded |
| 121 |
* too many warnings in the core. Maybe we want to retry later... Just add |
| 122 |
* deprecated document for now. |
| 123 |
*/ |
| 124 |
struct RData { |
| 125 |
|
| 126 |
/** Basic part, including flags and class. */ |
| 127 |
struct RBasic basic; |
| 128 |
|
| 129 |
/** |
| 130 |
* This function is called when the object is experiencing GC marks. If it |
| 131 |
* contains references to other Ruby objects, you need to mark them also. |
| 132 |
* Otherwise GC will smash your data. |
| 133 |
* |
| 134 |
* @see rb_gc_mark() |
| 135 |
* @warning This is called during GC runs. Object allocations are |
| 136 |
* impossible at that moment (that is why GC runs). |
| 137 |
*/ |
| 138 |
RUBY_DATA_FUNC dmark; |
| 139 |
|
| 140 |
/** |
| 141 |
* This function is called when the object is no longer used. You need to |
| 142 |
* do whatever necessary to avoid memory leaks. |
| 143 |
* |
| 144 |
* @warning This is called during GC runs. Object allocations are |
| 145 |
* impossible at that moment (that is why GC runs). |
| 146 |
*/ |
| 147 |
RUBY_DATA_FUNC dfree; |
| 148 |
|
| 149 |
/** Pointer to the actual C level struct that you want to wrap. */ |
| 150 |
void *data; |
| 151 |
}; |
| 152 |
|
| 153 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 154 |
|
| 155 |
/** |
| 156 |
* This is the primitive way to wrap an existing C struct into ::RData. |
| 157 |
* |
| 158 |
* @param[in] klass Ruby level class of the returning object. |
| 159 |
* @param[in] datap Pointer to the target C struct. |
| 160 |
* @param[in] dmark Mark function. |
| 161 |
* @param[in] dfree Free function. |
| 162 |
* @exception rb_eTypeError `klass` is not a class. |
| 163 |
* @exception rb_eNoMemError Out of memory. |
| 164 |
* @return An allocated object that wraps `datap`. |
| 165 |
*/ |
| 166 |
VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree); |
| 167 |
|
| 168 |
/** |
| 169 |
* Identical to rb_data_object_wrap(), except it allocates a new data region |
| 170 |
* internally instead of taking an existing one. The allocation is done using |
| 171 |
* ruby_calloc(). Hence it makes no sense to pass anything other than |
| 172 |
* ::RUBY_DEFAULT_FREE to the last argument. |
| 173 |
* |
| 174 |
* @param[in] klass Ruby level class of the returning object. |
| 175 |
* @param[in] size Requested size of memory to allocate. |
| 176 |
* @param[in] dmark Mark function. |
| 177 |
* @param[in] dfree Free function. |
| 178 |
* @exception rb_eTypeError `klass` is not a class. |
| 179 |
* @exception rb_eNoMemError Out of memory. |
| 180 |
* @return An allocated object that wraps a new `size` byte region. |
| 181 |
*/ |
| 182 |
VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree); |
| 183 |
|
| 184 |
/** |
| 185 |
* @private |
| 186 |
* Documented in include/ruby/internal/globals.h |
| 187 |
*/ |
| 188 |
RUBY_EXTERN VALUE rb_cObject; |
| 189 |
RBIMPL_SYMBOL_EXPORT_END() |
| 190 |
|
| 191 |
/** |
| 192 |
* Converts sval, a pointer to your struct, into a Ruby object. |
| 193 |
* |
| 194 |
* @param klass A ruby level class. |
| 195 |
* @param mark Mark function. |
| 196 |
* @param free Free function. |
| 197 |
* @param sval A pointer to your struct. |
| 198 |
* @exception rb_eTypeError `klass` is not a class. |
| 199 |
* @exception rb_eNoMemError Out of memory. |
| 200 |
* @return A created Ruby object. |
| 201 |
*/ |
| 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 |
|
| 209 |
/** |
| 210 |
* @private |
| 211 |
* |
| 212 |
* This is an implementation detail of #Data_Make_Struct. People don't use it |
| 213 |
* directly. |
| 214 |
* |
| 215 |
* @param result Variable name of created Ruby object. |
| 216 |
* @param klass Ruby level class of the object. |
| 217 |
* @param type Type name of the C struct. |
| 218 |
* @param size Size of the C struct. |
| 219 |
* @param mark Mark function. |
| 220 |
* @param free Free function. |
| 221 |
* @param sval Variable name of created C struct. |
| 222 |
*/ |
| 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 |
|
| 232 |
/** |
| 233 |
* Identical to #Data_Wrap_Struct, except it allocates a new data region |
| 234 |
* internally instead of taking an existing one. The allocation is done using |
| 235 |
* ruby_calloc(). Hence it makes no sense to pass anything other than |
| 236 |
* ::RUBY_DEFAULT_FREE to the `free` argument. |
| 237 |
* |
| 238 |
* @param klass Ruby level class of the returning object. |
| 239 |
* @param type Type name of the C struct. |
| 240 |
* @param mark Mark function. |
| 241 |
* @param free Free function. |
| 242 |
* @param sval Variable name of created C struct. |
| 243 |
* @exception rb_eTypeError `klass` is not a class. |
| 244 |
* @exception rb_eNoMemError Out of memory. |
| 245 |
* @return A created Ruby object. |
| 246 |
*/ |
| 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 |
|
| 270 |
/** |
| 271 |
* Obtains a C struct from inside of a wrapper Ruby object. |
| 272 |
* |
| 273 |
* @param obj An instance of ::RData. |
| 274 |
* @param type Type name of the C struct. |
| 275 |
* @param sval Variable name of obtained C struct. |
| 276 |
* @return Unwrapped C struct that `obj` holds. |
| 277 |
*/ |
| 278 |
#define Data_Get_Struct(obj, type, sval) \ |
| 279 |
((sval) = RBIMPL_CAST((type*)rb_data_object_get(obj))) |
| 280 |
|
| 281 |
RBIMPL_ATTRSET_UNTYPED_DATA_FUNC() |
| 282 |
/** |
| 283 |
* @private |
| 284 |
* |
| 285 |
* This is an implementation detail of rb_data_object_wrap(). People don't use |
| 286 |
* it directly. |
| 287 |
* |
| 288 |
* @param[in] klass Ruby level class of the returning object. |
| 289 |
* @param[in] ptr Pointer to the target C struct. |
| 290 |
* @param[in] mark Mark function. |
| 291 |
* @param[in] free Free function. |
| 292 |
* @exception rb_eTypeError `klass` is not a class. |
| 293 |
* @exception rb_eNoMemError Out of memory. |
| 294 |
* @return An allocated object that wraps `datap`. |
| 295 |
*/ |
| 296 |
static inline VALUE |
| 297 |
rb_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 |
|
| 302 |
/** |
| 303 |
* @private |
| 304 |
* |
| 305 |
* This is an implementation detail of #Data_Get_Struct. People don't use it |
| 306 |
* directly. |
| 307 |
* |
| 308 |
* @param[in] obj An instance of ::RData. |
| 309 |
* @return Unwrapped C struct that `obj` holds. |
| 310 |
*/ |
| 311 |
static inline void * |
| 312 |
rb_data_object_get(VALUE obj) |
| 313 |
{ |
| 314 |
Check_Type(obj, RUBY_T_DATA); |
| 315 |
return DATA_PTR(obj); |
| 316 |
} |
| 317 |
|
| 318 |
RBIMPL_ATTRSET_UNTYPED_DATA_FUNC() |
| 319 |
/** |
| 320 |
* @private |
| 321 |
* |
| 322 |
* This is an implementation detail of #Data_Get_Struct. People don't use it |
| 323 |
* directly. |
| 324 |
* |
| 325 |
* @param[in] obj An instance of ::RData. |
| 326 |
* @return Unwrapped C struct that `obj` holds. |
| 327 |
*/ |
| 328 |
static inline void * |
| 329 |
rb_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 |
|
| 343 |
/** |
| 344 |
* This is an implementation detail of #Data_Make_Struct. People don't use it |
| 345 |
* directly. |
| 346 |
* |
| 347 |
* @param[in] klass Ruby level class of the returning object. |
| 348 |
* @param[in] mark_func Mark function. |
| 349 |
* @param[in] free_func Free function. |
| 350 |
* @param[in] datap Variable of created C struct. |
| 351 |
* @param[in] size Requested size of allocation. |
| 352 |
* @exception rb_eTypeError `klass` is not a class. |
| 353 |
* @exception rb_eNoMemError Out of memory. |
| 354 |
* @return A created Ruby object. |
| 355 |
* @post `*datap` holds the created C struct. |
| 356 |
*/ |
| 357 |
static inline VALUE |
| 358 |
rb_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")) |
| 365 |
/** @deprecated This function was renamed to rb_data_object_wrap(). */ |
| 366 |
static inline VALUE |
| 367 |
rb_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 |
|
| 372 |
/** @cond INTERNAL_MACRO */ |
| 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) |
| 385 |
/** @endcond */ |
| 386 |
#endif /* RBIMPL_RDATA_H */ |