| 1 |
#include "ruby.h" |
| 2 |
|
| 3 |
#ifdef HAVE_RUBY_MEMORY_VIEW_H |
| 4 |
#include "ruby/memory_view.h" |
| 5 |
|
| 6 |
#define STRUCT_ALIGNOF(T, result) do { \ |
| 7 |
(result) = RUBY_ALIGNOF(T); \ |
| 8 |
} while(0) |
| 9 |
|
| 10 |
static ID id_str; |
| 11 |
static VALUE sym_format; |
| 12 |
static VALUE sym_native_size_p; |
| 13 |
static VALUE sym_offset; |
| 14 |
static VALUE sym_size; |
| 15 |
static VALUE sym_repeat; |
| 16 |
static VALUE sym_obj; |
| 17 |
static VALUE sym_byte_size; |
| 18 |
static VALUE sym_readonly; |
| 19 |
static VALUE sym_format; |
| 20 |
static VALUE sym_item_size; |
| 21 |
static VALUE sym_ndim; |
| 22 |
static VALUE sym_shape; |
| 23 |
static VALUE sym_strides; |
| 24 |
static VALUE sym_sub_offsets; |
| 25 |
static VALUE sym_endianness; |
| 26 |
static VALUE sym_little_endian; |
| 27 |
static VALUE sym_big_endian; |
| 28 |
|
| 29 |
static bool |
| 30 |
exportable_string_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags) |
| 31 |
{ |
| 32 |
VALUE str = rb_ivar_get(obj, id_str); |
| 33 |
rb_memory_view_init_as_byte_array(view, obj, RSTRING_PTR(str), RSTRING_LEN(str), true); |
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
static bool |
| 38 |
exportable_string_memory_view_available_p(VALUE obj) |
| 39 |
{ |
| 40 |
VALUE str = rb_ivar_get(obj, id_str); |
| 41 |
return !NIL_P(str); |
| 42 |
} |
| 43 |
|
| 44 |
static const rb_memory_view_entry_t exportable_string_memory_view_entry = { |
| 45 |
exportable_string_get_memory_view, |
| 46 |
NULL, |
| 47 |
exportable_string_memory_view_available_p |
| 48 |
}; |
| 49 |
|
| 50 |
static VALUE |
| 51 |
memory_view_available_p(VALUE mod, VALUE obj) |
| 52 |
{ |
| 53 |
return rb_memory_view_available_p(obj) ? Qtrue : Qfalse; |
| 54 |
} |
| 55 |
|
| 56 |
static VALUE |
| 57 |
memory_view_register(VALUE mod, VALUE obj) |
| 58 |
{ |
| 59 |
return rb_memory_view_register(obj, &exportable_string_memory_view_entry) ? Qtrue : Qfalse; |
| 60 |
} |
| 61 |
|
| 62 |
static VALUE |
| 63 |
memory_view_item_size_from_format(VALUE mod, VALUE format) |
| 64 |
{ |
| 65 |
const char *c_str = NULL; |
| 66 |
if (!NIL_P(format)) |
| 67 |
c_str = StringValueCStr(format); |
| 68 |
const char *err = NULL; |
| 69 |
ssize_t item_size = rb_memory_view_item_size_from_format(c_str, &err); |
| 70 |
if (!err) |
| 71 |
return rb_assoc_new(SSIZET2NUM(item_size), Qnil); |
| 72 |
else |
| 73 |
return rb_assoc_new(SSIZET2NUM(item_size), rb_str_new_cstr(err)); |
| 74 |
} |
| 75 |
|
| 76 |
static VALUE |
| 77 |
memory_view_parse_item_format(VALUE mod, VALUE format) |
| 78 |
{ |
| 79 |
const char *c_str = NULL; |
| 80 |
if (!NIL_P(format)) |
| 81 |
c_str = StringValueCStr(format); |
| 82 |
const char *err = NULL; |
| 83 |
|
| 84 |
rb_memory_view_item_component_t *members; |
| 85 |
size_t n_members; |
| 86 |
ssize_t item_size = rb_memory_view_parse_item_format(c_str, &members, &n_members, &err); |
| 87 |
|
| 88 |
VALUE result = rb_ary_new_capa(3); |
| 89 |
rb_ary_push(result, SSIZET2NUM(item_size)); |
| 90 |
|
| 91 |
if (!err) { |
| 92 |
VALUE ary = rb_ary_new_capa((long)n_members); |
| 93 |
size_t i; |
| 94 |
for (i = 0; i < n_members; ++i) { |
| 95 |
VALUE member = rb_hash_new(); |
| 96 |
rb_hash_aset(member, sym_format, rb_str_new(&members[i].format, 1)); |
| 97 |
rb_hash_aset(member, sym_native_size_p, members[i].native_size_p ? Qtrue : Qfalse); |
| 98 |
rb_hash_aset(member, sym_endianness, members[i].little_endian_p ? sym_little_endian : sym_big_endian); |
| 99 |
rb_hash_aset(member, sym_offset, SSIZET2NUM(members[i].offset)); |
| 100 |
rb_hash_aset(member, sym_size, SSIZET2NUM(members[i].size)); |
| 101 |
rb_hash_aset(member, sym_repeat, SSIZET2NUM(members[i].repeat)); |
| 102 |
rb_ary_push(ary, member); |
| 103 |
} |
| 104 |
xfree(members); |
| 105 |
rb_ary_push(result, ary); |
| 106 |
rb_ary_push(result, Qnil); |
| 107 |
} |
| 108 |
else { |
| 109 |
rb_ary_push(result, Qnil); // members |
| 110 |
rb_ary_push(result, rb_str_new_cstr(err)); |
| 111 |
} |
| 112 |
|
| 113 |
return result; |
| 114 |
} |
| 115 |
|
| 116 |
static VALUE |
| 117 |
memory_view_get_memory_view_info(VALUE mod, VALUE obj) |
| 118 |
{ |
| 119 |
rb_memory_view_t view; |
| 120 |
|
| 121 |
if (!rb_memory_view_get(obj, &view, 0)) { |
| 122 |
return Qnil; |
| 123 |
} |
| 124 |
|
| 125 |
VALUE hash = rb_hash_new(); |
| 126 |
rb_hash_aset(hash, sym_obj, view.obj); |
| 127 |
rb_hash_aset(hash, sym_byte_size, SSIZET2NUM(view.byte_size)); |
| 128 |
rb_hash_aset(hash, sym_readonly, view.readonly ? Qtrue : Qfalse); |
| 129 |
rb_hash_aset(hash, sym_format, view.format ? rb_str_new_cstr(view.format) : Qnil); |
| 130 |
rb_hash_aset(hash, sym_item_size, SSIZET2NUM(view.item_size)); |
| 131 |
rb_hash_aset(hash, sym_ndim, SSIZET2NUM(view.ndim)); |
| 132 |
|
| 133 |
if (view.shape) { |
| 134 |
VALUE shape = rb_ary_new_capa(view.ndim); |
| 135 |
rb_hash_aset(hash, sym_shape, shape); |
| 136 |
} |
| 137 |
else { |
| 138 |
rb_hash_aset(hash, sym_shape, Qnil); |
| 139 |
} |
| 140 |
|
| 141 |
if (view.strides) { |
| 142 |
VALUE strides = rb_ary_new_capa(view.ndim); |
| 143 |
rb_hash_aset(hash, sym_strides, strides); |
| 144 |
} |
| 145 |
else { |
| 146 |
rb_hash_aset(hash, sym_strides, Qnil); |
| 147 |
} |
| 148 |
|
| 149 |
if (view.sub_offsets) { |
| 150 |
VALUE sub_offsets = rb_ary_new_capa(view.ndim); |
| 151 |
rb_hash_aset(hash, sym_sub_offsets, sub_offsets); |
| 152 |
} |
| 153 |
else { |
| 154 |
rb_hash_aset(hash, sym_sub_offsets, Qnil); |
| 155 |
} |
| 156 |
|
| 157 |
rb_memory_view_release(&view); |
| 158 |
|
| 159 |
return hash; |
| 160 |
} |
| 161 |
|
| 162 |
static VALUE |
| 163 |
memory_view_fill_contiguous_strides(VALUE mod, VALUE ndim_v, VALUE item_size_v, VALUE shape_v, VALUE row_major_p) |
| 164 |
{ |
| 165 |
ssize_t i, ndim = NUM2SSIZET(ndim_v); |
| 166 |
|
| 167 |
Check_Type(shape_v, T_ARRAY); |
| 168 |
ssize_t *shape = ALLOC_N(ssize_t, ndim); |
| 169 |
for (i = 0; i < ndim; ++i) { |
| 170 |
shape[i] = NUM2SSIZET(RARRAY_AREF(shape_v, i)); |
| 171 |
} |
| 172 |
|
| 173 |
ssize_t *strides = ALLOC_N(ssize_t, ndim); |
| 174 |
rb_memory_view_fill_contiguous_strides(ndim, NUM2SSIZET(item_size_v), shape, RTEST(row_major_p), strides); |
| 175 |
|
| 176 |
VALUE result = rb_ary_new_capa(ndim); |
| 177 |
for (i = 0; i < ndim; ++i) { |
| 178 |
rb_ary_push(result, SSIZET2NUM(strides[i])); |
| 179 |
} |
| 180 |
|
| 181 |
xfree(strides); |
| 182 |
xfree(shape); |
| 183 |
|
| 184 |
return result; |
| 185 |
} |
| 186 |
|
| 187 |
static VALUE |
| 188 |
memory_view_get_ref_count(VALUE obj) |
| 189 |
{ |
| 190 |
if (rb_memory_view_exported_object_registry == Qundef) { |
| 191 |
return Qnil; |
| 192 |
} |
| 193 |
|
| 194 |
st_table *table; |
| 195 |
TypedData_Get_Struct(rb_memory_view_exported_object_registry, st_table, |
| 196 |
&rb_memory_view_exported_object_registry_data_type, |
| 197 |
table); |
| 198 |
|
| 199 |
st_data_t count; |
| 200 |
if (st_lookup(table, (st_data_t)obj, &count)) { |
| 201 |
return ULL2NUM(count); |
| 202 |
} |
| 203 |
|
| 204 |
return Qnil; |
| 205 |
} |
| 206 |
|
| 207 |
static VALUE |
| 208 |
memory_view_ref_count_while_exporting_i(VALUE obj, long n) |
| 209 |
{ |
| 210 |
if (n == 0) { |
| 211 |
return memory_view_get_ref_count(obj); |
| 212 |
} |
| 213 |
|
| 214 |
rb_memory_view_t view; |
| 215 |
if (!rb_memory_view_get(obj, &view, 0)) { |
| 216 |
return Qnil; |
| 217 |
} |
| 218 |
|
| 219 |
VALUE ref_count = memory_view_ref_count_while_exporting_i(obj, n-1); |
| 220 |
rb_memory_view_release(&view); |
| 221 |
|
| 222 |
return ref_count; |
| 223 |
} |
| 224 |
|
| 225 |
static VALUE |
| 226 |
memory_view_ref_count_while_exporting(VALUE mod, VALUE obj, VALUE n) |
| 227 |
{ |
| 228 |
Check_Type(n, T_FIXNUM); |
| 229 |
return memory_view_ref_count_while_exporting_i(obj, FIX2LONG(n)); |
| 230 |
} |
| 231 |
|
| 232 |
static VALUE |
| 233 |
memory_view_extract_item_members(VALUE mod, VALUE str, VALUE format) |
| 234 |
{ |
| 235 |
StringValue(str); |
| 236 |
StringValue(format); |
| 237 |
|
| 238 |
rb_memory_view_item_component_t *members; |
| 239 |
size_t n_members; |
| 240 |
const char *err = NULL; |
| 241 |
(void)rb_memory_view_parse_item_format(RSTRING_PTR(format), &members, &n_members, &err); |
| 242 |
if (err != NULL) { |
| 243 |
rb_raise(rb_eArgError, "Unable to parse item format"); |
| 244 |
} |
| 245 |
|
| 246 |
VALUE item = rb_memory_view_extract_item_members(RSTRING_PTR(str), members, n_members); |
| 247 |
xfree(members); |
| 248 |
|
| 249 |
return item; |
| 250 |
} |
| 251 |
|
| 252 |
static VALUE |
| 253 |
expstr_initialize(VALUE obj, VALUE s) |
| 254 |
{ |
| 255 |
if (!NIL_P(s)) { |
| 256 |
Check_Type(s, T_STRING); |
| 257 |
} |
| 258 |
rb_ivar_set(obj, id_str, s); |
| 259 |
return Qnil; |
| 260 |
} |
| 261 |
|
| 262 |
static bool |
| 263 |
mdview_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags) |
| 264 |
{ |
| 265 |
VALUE buf_v = rb_ivar_get(obj, id_str); |
| 266 |
VALUE format_v = rb_ivar_get(obj, SYM2ID(sym_format)); |
| 267 |
VALUE shape_v = rb_ivar_get(obj, SYM2ID(sym_shape)); |
| 268 |
VALUE strides_v = rb_ivar_get(obj, SYM2ID(sym_strides)); |
| 269 |
|
| 270 |
const char *err; |
| 271 |
const ssize_t item_size = rb_memory_view_item_size_from_format(RSTRING_PTR(format_v), &err); |
| 272 |
if (item_size < 0) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
ssize_t ndim = RARRAY_LEN(shape_v); |
| 277 |
if (!NIL_P(strides_v) && RARRAY_LEN(strides_v) != ndim) { |
| 278 |
rb_raise(rb_eArgError, "strides has an invalid dimension"); |
| 279 |
} |
| 280 |
|
| 281 |
ssize_t *shape = ALLOC_N(ssize_t, ndim); |
| 282 |
ssize_t *strides = ALLOC_N(ssize_t, ndim); |
| 283 |
ssize_t i; |
| 284 |
if (!NIL_P(strides_v)) { |
| 285 |
for (i = 0; i < ndim; ++i) { |
| 286 |
shape[i] = NUM2SSIZET(RARRAY_AREF(shape_v, i)); |
| 287 |
strides[i] = NUM2SSIZET(RARRAY_AREF(strides_v, i)); |
| 288 |
} |
| 289 |
} |
| 290 |
else { |
| 291 |
for (i = 0; i < ndim; ++i) { |
| 292 |
shape[i] = NUM2SSIZET(RARRAY_AREF(shape_v, i)); |
| 293 |
} |
| 294 |
|
| 295 |
i = ndim - 1; |
| 296 |
strides[i] = item_size; |
| 297 |
for (; i > 0; --i) { |
| 298 |
strides[i-1] = strides[i] * shape[i]; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
rb_memory_view_init_as_byte_array(view, obj, RSTRING_PTR(buf_v), RSTRING_LEN(buf_v), true); |
| 303 |
view->format = RSTRING_PTR(format_v); |
| 304 |
view->item_size = item_size; |
| 305 |
view->ndim = ndim; |
| 306 |
view->shape = shape; |
| 307 |
view->strides = strides; |
| 308 |
view->sub_offsets = NULL; |
| 309 |
|
| 310 |
return true; |
| 311 |
} |
| 312 |
|
| 313 |
static bool |
| 314 |
mdview_release_memory_view(VALUE obj, rb_memory_view_t *view) |
| 315 |
{ |
| 316 |
if (view->shape) xfree((void *)view->shape); |
| 317 |
if (view->strides) xfree((void *)view->strides); |
| 318 |
|
| 319 |
return true; |
| 320 |
} |
| 321 |
|
| 322 |
static bool |
| 323 |
mdview_memory_view_available_p(VALUE obj) |
| 324 |
{ |
| 325 |
return true; |
| 326 |
} |
| 327 |
|
| 328 |
static const rb_memory_view_entry_t mdview_memory_view_entry = { |
| 329 |
mdview_get_memory_view, |
| 330 |
mdview_release_memory_view, |
| 331 |
mdview_memory_view_available_p |
| 332 |
}; |
| 333 |
|
| 334 |
static VALUE |
| 335 |
mdview_initialize(VALUE obj, VALUE buf, VALUE format, VALUE shape, VALUE strides) |
| 336 |
{ |
| 337 |
Check_Type(buf, T_STRING); |
| 338 |
StringValue(format); |
| 339 |
Check_Type(shape, T_ARRAY); |
| 340 |
if (!NIL_P(strides)) Check_Type(strides, T_ARRAY); |
| 341 |
|
| 342 |
rb_ivar_set(obj, id_str, buf); |
| 343 |
rb_ivar_set(obj, SYM2ID(sym_format), format); |
| 344 |
rb_ivar_set(obj, SYM2ID(sym_shape), shape); |
| 345 |
rb_ivar_set(obj, SYM2ID(sym_strides), strides); |
| 346 |
return Qnil; |
| 347 |
} |
| 348 |
|
| 349 |
static VALUE |
| 350 |
mdview_aref(VALUE obj, VALUE indices_v) |
| 351 |
{ |
| 352 |
Check_Type(indices_v, T_ARRAY); |
| 353 |
|
| 354 |
rb_memory_view_t view; |
| 355 |
if (!rb_memory_view_get(obj, &view, 0)) { |
| 356 |
rb_raise(rb_eRuntimeError, "rb_memory_view_get: failed"); |
| 357 |
} |
| 358 |
|
| 359 |
if (RARRAY_LEN(indices_v) != view.ndim) { |
| 360 |
rb_raise(rb_eKeyError, "Indices has an invalid dimension"); |
| 361 |
} |
| 362 |
|
| 363 |
VALUE buf_indices; |
| 364 |
ssize_t *indices = ALLOCV_N(ssize_t, buf_indices, view.ndim); |
| 365 |
|
| 366 |
ssize_t i; |
| 367 |
for (i = 0; i < view.ndim; ++i) { |
| 368 |
indices[i] = NUM2SSIZET(RARRAY_AREF(indices_v, i)); |
| 369 |
} |
| 370 |
|
| 371 |
VALUE result = rb_memory_view_get_item(&view, indices); |
| 372 |
ALLOCV_END(buf_indices); |
| 373 |
rb_memory_view_release(&view); |
| 374 |
|
| 375 |
return result; |
| 376 |
} |
| 377 |
|
| 378 |
#endif /* HAVE_RUBY_MEMORY_VIEW_H */ |
| 379 |
|
| 380 |
void |
| 381 |
Init_memory_view(void) |
| 382 |
{ |
| 383 |
rb_ext_ractor_safe(true); |
| 384 |
#ifdef HAVE_RUBY_MEMORY_VIEW_H |
| 385 |
VALUE mMemoryViewTestUtils = rb_define_module("MemoryViewTestUtils"); |
| 386 |
|
| 387 |
rb_define_module_function(mMemoryViewTestUtils, "available?", memory_view_available_p, 1); |
| 388 |
rb_define_module_function(mMemoryViewTestUtils, "register", memory_view_register, 1); |
| 389 |
rb_define_module_function(mMemoryViewTestUtils, "item_size_from_format", memory_view_item_size_from_format, 1); |
| 390 |
rb_define_module_function(mMemoryViewTestUtils, "parse_item_format", memory_view_parse_item_format, 1); |
| 391 |
rb_define_module_function(mMemoryViewTestUtils, "get_memory_view_info", memory_view_get_memory_view_info, 1); |
| 392 |
rb_define_module_function(mMemoryViewTestUtils, "fill_contiguous_strides", memory_view_fill_contiguous_strides, 4); |
| 393 |
rb_define_module_function(mMemoryViewTestUtils, "ref_count_while_exporting", memory_view_ref_count_while_exporting, 2); |
| 394 |
rb_define_module_function(mMemoryViewTestUtils, "extract_item_members", memory_view_extract_item_members, 2); |
| 395 |
|
| 396 |
VALUE cExportableString = rb_define_class_under(mMemoryViewTestUtils, "ExportableString", rb_cObject); |
| 397 |
rb_define_method(cExportableString, "initialize", expstr_initialize, 1); |
| 398 |
rb_memory_view_register(cExportableString, &exportable_string_memory_view_entry); |
| 399 |
|
| 400 |
VALUE cMDView = rb_define_class_under(mMemoryViewTestUtils, "MultiDimensionalView", rb_cObject); |
| 401 |
rb_define_method(cMDView, "initialize", mdview_initialize, 4); |
| 402 |
rb_define_method(cMDView, "[]", mdview_aref, 1); |
| 403 |
rb_memory_view_register(cMDView, &mdview_memory_view_entry); |
| 404 |
|
| 405 |
id_str = rb_intern_const("__str__"); |
| 406 |
sym_format = ID2SYM(rb_intern_const("format")); |
| 407 |
sym_native_size_p = ID2SYM(rb_intern_const("native_size_p")); |
| 408 |
sym_offset = ID2SYM(rb_intern_const("offset")); |
| 409 |
sym_size = ID2SYM(rb_intern_const("size")); |
| 410 |
sym_repeat = ID2SYM(rb_intern_const("repeat")); |
| 411 |
sym_obj = ID2SYM(rb_intern_const("obj")); |
| 412 |
sym_byte_size = ID2SYM(rb_intern_const("byte_size")); |
| 413 |
sym_readonly = ID2SYM(rb_intern_const("readonly")); |
| 414 |
sym_format = ID2SYM(rb_intern_const("format")); |
| 415 |
sym_item_size = ID2SYM(rb_intern_const("item_size")); |
| 416 |
sym_ndim = ID2SYM(rb_intern_const("ndim")); |
| 417 |
sym_shape = ID2SYM(rb_intern_const("shape")); |
| 418 |
sym_strides = ID2SYM(rb_intern_const("strides")); |
| 419 |
sym_sub_offsets = ID2SYM(rb_intern_const("sub_offsets")); |
| 420 |
sym_endianness = ID2SYM(rb_intern_const("endianness")); |
| 421 |
sym_little_endian = ID2SYM(rb_intern_const("little_endian")); |
| 422 |
sym_big_endian = ID2SYM(rb_intern_const("big_endian")); |
| 423 |
|
| 424 |
#ifdef WORDS_BIGENDIAN |
| 425 |
rb_const_set(mMemoryViewTestUtils, rb_intern_const("NATIVE_ENDIAN"), sym_big_endian); |
| 426 |
#else |
| 427 |
rb_const_set(mMemoryViewTestUtils, rb_intern_const("NATIVE_ENDIAN"), sym_little_endian); |
| 428 |
#endif |
| 429 |
|
| 430 |
#define DEF_ALIGNMENT_CONST(type, TYPE) do { \ |
| 431 |
int alignment; \ |
| 432 |
STRUCT_ALIGNOF(type, alignment); \ |
| 433 |
rb_const_set(mMemoryViewTestUtils, rb_intern_const(#TYPE "_ALIGNMENT"), INT2FIX(alignment)); \ |
| 434 |
} while(0) |
| 435 |
|
| 436 |
DEF_ALIGNMENT_CONST(short, SHORT); |
| 437 |
DEF_ALIGNMENT_CONST(int, INT); |
| 438 |
DEF_ALIGNMENT_CONST(long, LONG); |
| 439 |
DEF_ALIGNMENT_CONST(LONG_LONG, LONG_LONG); |
| 440 |
DEF_ALIGNMENT_CONST(int16_t, INT16); |
| 441 |
DEF_ALIGNMENT_CONST(int32_t, INT32); |
| 442 |
DEF_ALIGNMENT_CONST(int64_t, INT64); |
| 443 |
DEF_ALIGNMENT_CONST(intptr_t, INTPTR); |
| 444 |
DEF_ALIGNMENT_CONST(float, FLOAT); |
| 445 |
DEF_ALIGNMENT_CONST(double, DOUBLE); |
| 446 |
|
| 447 |
#undef DEF_ALIGNMENT_CONST |
| 448 |
|
| 449 |
#endif /* HAVE_RUBY_MEMORY_VIEW_H */ |
| 450 |
} |