ruby-changes:71369
From: Peter <ko1@a...>
Date: Sat, 12 Mar 2022 03:17:50 +0900 (JST)
Subject: [ruby-changes:71369] 9a4bddd761 (master): Add rb_ary_reset
https://git.ruby-lang.org/ruby.git/commit/?id=9a4bddd761 From 9a4bddd76165b2848a6275d2a3bfd3166288e106 Mon Sep 17 00:00:00 2001 From: Peter Zhu <peter@p...> Date: Fri, 11 Mar 2022 13:16:29 -0500 Subject: Add rb_ary_reset rb_ary_reset will free heap allocated arrays and unshare shared arrays. --- array.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/array.c b/array.c index f4dbf87fbb..2790c2a61f 100644 --- a/array.c +++ b/array.c @@ -528,11 +528,14 @@ rb_ary_unshare(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L528 FL_UNSET_SHARED(ary); } -static inline void -rb_ary_unshare_safe(VALUE ary) +static void +rb_ary_reset(VALUE ary) { - if (ARY_SHARED_P(ary) && !ARY_EMBED_P(ary)) { - rb_ary_unshare(ary); + if (ARY_OWNS_HEAP_P(ary)) { + ary_heap_free(ary); + } + else if (ARY_SHARED_P(ary)) { + rb_ary_unshare(ary); } } @@ -1075,10 +1078,7 @@ rb_ary_initialize(int argc, VALUE *argv, VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L1078 rb_ary_modify(ary); if (argc == 0) { - if (ARY_OWNS_HEAP_P(ary) && ARY_HEAP_PTR(ary) != NULL) { - ary_heap_free(ary); - } - rb_ary_unshare_safe(ary); + rb_ary_reset(ary); FL_SET_EMBED(ary); ARY_SET_EMBED_LEN(ary, 0); if (rb_block_given_p()) { @@ -4391,12 +4391,7 @@ rb_ary_replace(VALUE copy, VALUE orig) https://github.com/ruby/ruby/blob/trunk/array.c#L4391 orig = to_ary(orig); if (copy == orig) return copy; - if (ARY_OWNS_HEAP_P(copy)) { - ary_heap_free(copy); - } - else if (ARY_SHARED_P(copy)) { - rb_ary_unshare(copy); - } + rb_ary_reset(copy); if (RARRAY_LEN(orig) <= RARRAY_EMBED_LEN_MAX) { FL_SET_EMBED(copy); -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/