[前][次][番号順一覧][スレッド一覧]

ruby-changes:71412

From: Peter <ko1@a...>
Date: Mon, 14 Mar 2022 22:46:25 +0900 (JST)
Subject: [ruby-changes:71412] 412991268f (master): Assume that refcnt of shared root is non-negative

https://git.ruby-lang.org/ruby.git/commit/?id=412991268f

From 412991268fe6afb84c8044acfcdd76c215af65be Mon Sep 17 00:00:00 2001
From: Peter Zhu <peter@p...>
Date: Mon, 14 Mar 2022 09:44:50 -0400
Subject: Assume that refcnt of shared root is non-negative

The refcnt of a shared root array should always be non-negative.
---
 array.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/array.c b/array.c
index 53e69f2a32..f47c1509ad 100644
--- a/array.c
+++ b/array.c
@@ -165,6 +165,7 @@ should_not_be_shared_and_embedded(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L165
 #define ARY_SHARED_ROOT_OCCUPIED(ary) (ARY_SHARED_ROOT_REFCNT(ary) == 1)
 #define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
     assert(ARY_SHARED_ROOT_P(ary)); \
+    assert((value) >= 0); \
     RARRAY(ary)->as.heap.aux.capa = (value); \
 } while (0)
 #define FL_SET_SHARED_ROOT(ary) do { \
@@ -512,10 +513,8 @@ ary_double_capa(VALUE ary, long min) https://github.com/ruby/ruby/blob/trunk/array.c#L513
 static void
 rb_ary_decrement_share(VALUE shared_root)
 {
-    long num = ARY_SHARED_ROOT_REFCNT(shared_root) - 1;
-    if (num > 0) {
-        ARY_SET_SHARED_ROOT_REFCNT(shared_root, num);
-    }
+    long num = ARY_SHARED_ROOT_REFCNT(shared_root);
+    ARY_SET_SHARED_ROOT_REFCNT(shared_root, num - 1);
 }
 
 static void
@@ -544,9 +543,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L543
 rb_ary_increment_share(VALUE shared_root)
 {
     long num = ARY_SHARED_ROOT_REFCNT(shared_root);
-    if (num >= 0) {
-        ARY_SET_SHARED_ROOT_REFCNT(shared_root, num + 1);
-    }
+    assert(num >= 0);
+    ARY_SET_SHARED_ROOT_REFCNT(shared_root, num + 1);
     return shared_root;
 }
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]