[ruby-core:60780] Re: volatile usages

From: Eric Wong <normalperson@...>
Date: 2014-02-16 04:48:05 UTC
List: ruby-core #60780
Eric Wong <normalperson@yhbt.net> wrote:
> array.c: volatile in rb_ary_each along with the explanation in r32201 is
> scaring me :x  If that volatile is needed, then it would also be
> necessary in similar functions such as rb_ary_each_with_index and
> rb_reverse_each.  Of course, those functions do not use volatile and
> seem fine after all these years.

I suspect it is because rb_ary_each is in the public C API,
so a guard becomes necessary.

--- a/array.c
+++ b/array.c
@@ -1784,16 +1784,21 @@ ary_enum_length(VALUE ary, VALUE args, VALUE eobj)
  */
 
 VALUE
-rb_ary_each(VALUE array)
+rb_ary_each(VALUE ary)
 {
     long i;
-    volatile VALUE ary = array;
 
     RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
     for (i=0; i<RARRAY_LEN(ary); i++) {
 	rb_yield(RARRAY_AREF(ary, i));
     }
-    return ary;
+
+    /*
+     * GC guard may be necessary if rb_ary_each is called like this:
+     *   rb_ary_each(rb_ary_new());
+     * (ary argument is allocated inline and the return value ignored)
+     */
+    return RB_GC_GUARD(ary);
 }
 
 /*

In This Thread

Prev Next