diff options
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | array.c | 16 | ||||
-rw-r--r-- | include/ruby/intern.h | 1 | ||||
-rw-r--r-- | load.c | 2 |
4 files changed, 27 insertions, 1 deletions
@@ -1,3 +1,12 @@ +Mon Nov 5 23:23:51 2012 Greg Price <price@mit.edu> + + * array.c (rb_ary_shared_with_p): new function. + Expose whether two arrays are shared (read-only, C only). + + * include/ruby/intern.h (rb_ary_shared_with_p): declare. + Patch by Greg Price. + [ruby-core:47970] [Bug #7158] + Mon Nov 5 23:21:14 2012 Greg Price <price@mit.edu> * load.c (loaded_feature_path): clarify and briefly comment @@ -305,6 +305,22 @@ rb_ary_frozen_p(VALUE ary) return Qfalse; } +/* This can be used to take a snapshot of an array (with + e.g. rb_ary_replace) and check later whether the array has been + modified from the snapshot. The snapshot is cheap, though if + something does modify the array it will pay the cost of copying + it. */ +VALUE +rb_ary_shared_with_p(VALUE ary1, VALUE ary2) +{ + if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) + && !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) + && RARRAY(ary1)->as.heap.aux.shared == RARRAY(ary2)->as.heap.aux.shared) { + return Qtrue; + } + return Qfalse; +} + static VALUE ary_alloc(VALUE klass) { diff --git a/include/ruby/intern.h b/include/ruby/intern.h index f954232b48..1b3179fbc6 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -56,6 +56,7 @@ VALUE rb_ary_tmp_new(long); void rb_ary_free(VALUE); void rb_ary_modify(VALUE); VALUE rb_ary_freeze(VALUE); +VALUE rb_ary_shared_with_p(VALUE, VALUE); VALUE rb_ary_aref(int, VALUE*, VALUE); VALUE rb_ary_subseq(VALUE, long, long); void rb_ary_store(VALUE, long, VALUE); @@ -88,7 +88,7 @@ loaded_feature_path(const char *name, long vlen, const char *feature, long len, long plen; const char *e; - if (vlen < len+1) return 0 + if (vlen < len+1) return 0; if (!strncmp(name+(vlen-len), feature, len)) { plen = vlen - len - 1; } |