summaryrefslogtreecommitdiff
path: root/ext/-test-
diff options
context:
space:
mode:
authorKenta Murata <mrkn@users.noreply.github.com>2020-12-08 02:01:31 +0900
committerGitHub <noreply@github.com>2020-12-08 02:01:31 +0900
commit7e69296a2be8db8814aa0cd0ce6471b80d55b464 (patch)
tree6fd3c93d3de03d3d2166e1befc2946b8404ac48f /ext/-test-
parentca76337a00244635faa331afd04f4b75161ce6fb (diff)
memory_view.c: Add rb_memory_view_extract_item_members (#3855)
Notes
Notes: Merged-By: mrkn <mrkn@ruby-lang.org>
Diffstat (limited to 'ext/-test-')
-rw-r--r--ext/-test-/memory_view/memory_view.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/ext/-test-/memory_view/memory_view.c b/ext/-test-/memory_view/memory_view.c
index f7c50900876..621614b1111 100644
--- a/ext/-test-/memory_view/memory_view.c
+++ b/ext/-test-/memory_view/memory_view.c
@@ -80,15 +80,15 @@ memory_view_parse_item_format(VALUE mod, VALUE format)
const char *err = NULL;
rb_memory_view_item_component_t *members;
- ssize_t n_members;
+ size_t n_members;
ssize_t item_size = rb_memory_view_parse_item_format(c_str, &members, &n_members, &err);
VALUE result = rb_ary_new_capa(3);
rb_ary_push(result, SSIZET2NUM(item_size));
if (!err) {
- VALUE ary = rb_ary_new_capa(n_members);
- ssize_t i;
+ VALUE ary = rb_ary_new_capa((long)n_members);
+ size_t i;
for (i = 0; i < n_members; ++i) {
VALUE member = rb_hash_new();
rb_hash_aset(member, sym_format, rb_str_new(&members[i].format, 1));
@@ -231,6 +231,26 @@ memory_view_ref_count_while_exporting(VALUE mod, VALUE obj, VALUE n)
}
static VALUE
+memory_view_extract_item_members(VALUE mod, VALUE str, VALUE format)
+{
+ StringValue(str);
+ StringValue(format);
+
+ rb_memory_view_item_component_t *members;
+ size_t n_members;
+ const char *err = NULL;
+ (void)rb_memory_view_parse_item_format(RSTRING_PTR(format), &members, &n_members, &err);
+ if (err != NULL) {
+ rb_raise(rb_eArgError, "Unable to parse item format");
+ }
+
+ VALUE item = rb_memory_view_extract_item_members(RSTRING_PTR(str), members, n_members);
+ xfree(members);
+
+ return item;
+}
+
+static VALUE
expstr_initialize(VALUE obj, VALUE s)
{
if (!NIL_P(s)) {
@@ -346,6 +366,7 @@ Init_memory_view(void)
rb_define_module_function(mMemoryViewTestUtils, "get_memory_view_info", memory_view_get_memory_view_info, 1);
rb_define_module_function(mMemoryViewTestUtils, "fill_contiguous_strides", memory_view_fill_contiguous_strides, 4);
rb_define_module_function(mMemoryViewTestUtils, "ref_count_while_exporting", memory_view_ref_count_while_exporting, 2);
+ rb_define_module_function(mMemoryViewTestUtils, "extract_item_members", memory_view_extract_item_members, 2);
VALUE cExportableString = rb_define_class_under(mMemoryViewTestUtils, "ExportableString", rb_cObject);
rb_define_method(cExportableString, "initialize", expstr_initialize, 1);