diff options
author | Kenta Murata <mrkn@users.noreply.github.com> | 2020-11-30 14:53:13 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-30 14:53:13 +0900 |
commit | 73a337e21461469290005f169c05bc1791112d67 (patch) | |
tree | 2fd489d7b3c6993b3a818fa7fe305695648d0268 /ext | |
parent | 7e1dbe59759247ae0df26ef3f669009f00e058fd (diff) | |
download | ruby-73a337e21461469290005f169c05bc1791112d67.tar.gz |
Keep references of memory-view-exported objects (#3816)
* memory_view.c: remove a reference in view->obj at rb_memory_view_release
* memory_view.c: keep references of memory-view-exported objects
* Update common.mk
* memory_view.c: Use st_update
Diffstat (limited to 'ext')
-rw-r--r-- | ext/-test-/memory_view/memory_view.c | 103 |
1 files changed, 51 insertions, 52 deletions
diff --git a/ext/-test-/memory_view/memory_view.c b/ext/-test-/memory_view/memory_view.c index 0ae9f457ac..7f1f007ba4 100644 --- a/ext/-test-/memory_view/memory_view.c +++ b/ext/-test-/memory_view/memory_view.c @@ -24,36 +24,11 @@ static VALUE sym_endianness; static VALUE sym_little_endian; static VALUE sym_big_endian; -static VALUE exported_objects; - static int exportable_string_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags) { VALUE str = rb_ivar_get(obj, id_str); rb_memory_view_init_as_byte_array(view, obj, RSTRING_PTR(str), RSTRING_LEN(str), true); - - VALUE count = rb_hash_lookup2(exported_objects, obj, INT2FIX(0)); - count = rb_funcall(count, '+', 1, INT2FIX(1)); - rb_hash_aset(exported_objects, obj, count); - - return 1; -} - -static int -exportable_string_release_memory_view(VALUE obj, rb_memory_view_t *view) -{ - VALUE count = rb_hash_lookup2(exported_objects, obj, INT2FIX(0)); - if (INT2FIX(1) == count) { - rb_hash_delete(exported_objects, obj); - } - else if (INT2FIX(0) == count) { - rb_raise(rb_eRuntimeError, "Duplicated releasing of a memory view has been occurred for %"PRIsVALUE, obj); - } - else { - count = rb_funcall(count, '-', 1, INT2FIX(1)); - rb_hash_aset(exported_objects, obj, count); - } - return 1; } @@ -65,7 +40,7 @@ exportable_string_memory_view_available_p(VALUE obj) static const rb_memory_view_entry_t exportable_string_memory_view_entry = { exportable_string_get_memory_view, - exportable_string_release_memory_view, + NULL, exportable_string_memory_view_available_p }; @@ -207,6 +182,54 @@ memory_view_fill_contiguous_strides(VALUE mod, VALUE ndim_v, VALUE item_size_v, } static VALUE +memory_view_get_ref_count(VALUE obj) +{ + extern VALUE rb_memory_view_exported_object_registry; + extern const rb_data_type_t rb_memory_view_exported_object_registry_data_type; + + if (rb_memory_view_exported_object_registry == Qundef) { + return Qnil; + } + + st_table *table; + TypedData_Get_Struct(rb_memory_view_exported_object_registry, st_table, + &rb_memory_view_exported_object_registry_data_type, + table); + + st_data_t count; + if (st_lookup(table, (st_data_t)obj, &count)) { + return ULL2NUM(count); + } + + return Qnil; +} + +static VALUE +memory_view_ref_count_while_exporting_i(VALUE obj, long n) +{ + if (n == 0) { + return memory_view_get_ref_count(obj); + } + + rb_memory_view_t view; + if (!rb_memory_view_get(obj, &view, 0)) { + return Qnil; + } + + VALUE ref_count = memory_view_ref_count_while_exporting_i(obj, n-1); + rb_memory_view_release(&view); + + return ref_count; +} + +static VALUE +memory_view_ref_count_while_exporting(VALUE mod, VALUE obj, VALUE n) +{ + Check_Type(n, T_FIXNUM); + return memory_view_ref_count_while_exporting_i(obj, FIX2LONG(n)); +} + +static VALUE expstr_initialize(VALUE obj, VALUE s) { rb_ivar_set(obj, id_str, s); @@ -247,28 +270,6 @@ mdview_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags) view->shape = shape; view->strides = strides; - VALUE count = rb_hash_lookup2(exported_objects, obj, INT2FIX(0)); - count = rb_funcall(count, '+', 1, INT2FIX(1)); - rb_hash_aset(exported_objects, obj, count); - - return 1; -} - -static int -mdview_release_memory_view(VALUE obj, rb_memory_view_t *view) -{ - VALUE count = rb_hash_lookup2(exported_objects, obj, INT2FIX(0)); - if (INT2FIX(1) == count) { - rb_hash_delete(exported_objects, obj); - } - else if (INT2FIX(0) == count) { - rb_raise(rb_eRuntimeError, "Duplicated releasing of a memory view has been occurred for %"PRIsVALUE, obj); - } - else { - count = rb_funcall(count, '-', 1, INT2FIX(1)); - rb_hash_aset(exported_objects, obj, count); - } - return 1; } @@ -280,7 +281,7 @@ mdview_memory_view_available_p(VALUE obj) static const rb_memory_view_entry_t mdview_memory_view_entry = { mdview_get_memory_view, - mdview_release_memory_view, + NULL, mdview_memory_view_available_p }; @@ -340,6 +341,7 @@ Init_memory_view(void) rb_define_module_function(mMemoryViewTestUtils, "parse_item_format", memory_view_parse_item_format, 1); 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); VALUE cExportableString = rb_define_class_under(mMemoryViewTestUtils, "ExportableString", rb_cObject); rb_define_method(cExportableString, "initialize", expstr_initialize, 1); @@ -393,7 +395,4 @@ Init_memory_view(void) DEF_ALIGNMENT_CONST(double, DOUBLE); #undef DEF_ALIGNMENT_CONST - - exported_objects = rb_hash_new(); - rb_gc_register_mark_object(exported_objects); } |