summaryrefslogtreecommitdiff
path: root/ext/-test-
diff options
context:
space:
mode:
authorKenta Murata <mrkn@users.noreply.github.com>2020-12-10 00:24:36 +0900
committerGitHub <noreply@github.com>2020-12-10 00:24:36 +0900
commitbb489aca5815acf3afd43ec9e3cdae008d882e3a (patch)
tree172f0eb0f43ccbbf470d41163aca6e3aa98f67fb /ext/-test-
parent549118b3e29713835226d4088ba65f58eee7c054 (diff)
memory_view.c: Add rb_memory_view_get_item and rb_memory_view_prepare_item_desc (#3871)
Notes
Notes: Merged-By: mrkn <mrkn@ruby-lang.org>
Diffstat (limited to 'ext/-test-')
-rw-r--r--ext/-test-/memory_view/memory_view.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/ext/-test-/memory_view/memory_view.c b/ext/-test-/memory_view/memory_view.c
index 621614b1111..53656b145c6 100644
--- a/ext/-test-/memory_view/memory_view.c
+++ b/ext/-test-/memory_view/memory_view.c
@@ -264,9 +264,17 @@ static int
mdview_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags)
{
VALUE buf_v = rb_ivar_get(obj, id_str);
+ VALUE format_v = rb_ivar_get(obj, SYM2ID(sym_format));
VALUE shape_v = rb_ivar_get(obj, SYM2ID(sym_shape));
VALUE strides_v = rb_ivar_get(obj, SYM2ID(sym_strides));
+ const char *format = RSTRING_PTR(format_v);
+ const char *err;
+ const ssize_t item_size = rb_memory_view_item_size_from_format(format, &err);
+ if (item_size < 0) {
+ return 0;
+ }
+
ssize_t i, ndim = RARRAY_LEN(shape_v);
ssize_t *shape = ALLOC_N(ssize_t, ndim);
ssize_t *strides = NULL;
@@ -288,8 +296,8 @@ mdview_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags)
}
rb_memory_view_init_as_byte_array(view, obj, RSTRING_PTR(buf_v), RSTRING_LEN(buf_v), true);
- view->format = "l";
- view->item_size = sizeof(long);
+ view->format = StringValueCStr(format_v);
+ view->item_size = item_size;
view->ndim = ndim;
view->shape = shape;
view->strides = strides;
@@ -310,13 +318,15 @@ static const rb_memory_view_entry_t mdview_memory_view_entry = {
};
static VALUE
-mdview_initialize(VALUE obj, VALUE buf, VALUE shape, VALUE strides)
+mdview_initialize(VALUE obj, VALUE buf, VALUE format, VALUE shape, VALUE strides)
{
Check_Type(buf, T_STRING);
+ StringValue(format);
Check_Type(shape, T_ARRAY);
if (!NIL_P(strides)) Check_Type(strides, T_ARRAY);
rb_ivar_set(obj, id_str, buf);
+ rb_ivar_set(obj, SYM2ID(sym_format), format);
rb_ivar_set(obj, SYM2ID(sym_shape), shape);
rb_ivar_set(obj, SYM2ID(sym_strides), strides);
return Qnil;
@@ -344,11 +354,8 @@ mdview_aref(VALUE obj, VALUE indices_v)
indices[i] = NUM2SSIZET(RARRAY_AREF(indices_v, i));
}
- char *ptr = rb_memory_view_get_item_pointer(&view, indices);
+ VALUE result = rb_memory_view_get_item(&view, indices);
ALLOCV_END(buf_indices);
-
- long x = *(long *)ptr;
- VALUE result = LONG2FIX(x);
rb_memory_view_release(&view);
return result;
@@ -373,7 +380,7 @@ Init_memory_view(void)
rb_memory_view_register(cExportableString, &exportable_string_memory_view_entry);
VALUE cMDView = rb_define_class_under(mMemoryViewTestUtils, "MultiDimensionalView", rb_cObject);
- rb_define_method(cMDView, "initialize", mdview_initialize, 3);
+ rb_define_method(cMDView, "initialize", mdview_initialize, 4);
rb_define_method(cMDView, "[]", mdview_aref, 1);
rb_memory_view_register(cMDView, &mdview_memory_view_entry);