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

ruby-changes:62701

From: Peter <ko1@a...>
Date: Wed, 26 Aug 2020 02:14:30 +0900 (JST)
Subject: [ruby-changes:62701] 326d89b7ce (master): Correctly account for heap_pages_final_slots so it does not underflow

https://git.ruby-lang.org/ruby.git/commit/?id=326d89b7ce

From 326d89b7cee05b33e6f73fb293a4ae9d5af6f7f2 Mon Sep 17 00:00:00 2001
From: Peter Zhu <peter@p...>
Date: Mon, 24 Aug 2020 10:53:11 -0400
Subject: Correctly account for heap_pages_final_slots so it does not underflow

`rb_objspace_call_finalizer` creates zombies, but does not do the correct accounting (it should increment `heap_pages_final_slots` whenever it creates a zombie). When we do correct accounting, `heap_pages_final_slots` should never underflow (the check for underflow was introduced in 39725a4db6b121c7779b2b34f7da9d9339415a1c).

The implementation moves the accounting from the functions that call `make_zombie` into `make_zombie` itself, which reduces code duplication.

diff --git a/gc.c b/gc.c
index 11a79e6..f1232d3 100644
--- a/gc.c
+++ b/gc.c
@@ -2597,6 +2597,10 @@ make_zombie(rb_objspace_t *objspace, VALUE obj, void (*dfree)(void *), void *dat https://github.com/ruby/ruby/blob/trunk/gc.c#L2597
     zombie->data = data;
     zombie->next = heap_pages_deferred_final;
     heap_pages_deferred_final = (VALUE)zombie;
+
+    struct heap_page *page = GET_HEAP_PAGE(obj);
+    page->final_slots++;
+    heap_pages_final_slots++;
 }
 
 static inline void
@@ -3484,7 +3488,9 @@ finalize_list(rb_objspace_t *objspace, VALUE zombie) https://github.com/ruby/ruby/blob/trunk/gc.c#L3488
         }
 
 	RZOMBIE(zombie)->basic.flags = 0;
-        if (LIKELY(heap_pages_final_slots)) heap_pages_final_slots--;
+        GC_ASSERT(heap_pages_final_slots > 0);
+        GC_ASSERT(page->final_slots > 0);
+        heap_pages_final_slots--;
 	page->final_slots--;
 	page->free_slots++;
 	heap_page_add_freeobj(objspace, GET_HEAP_PAGE(zombie), zombie);
@@ -4324,8 +4330,6 @@ gc_page_sweep(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *sweep_ https://github.com/ruby/ruby/blob/trunk/gc.c#L4330
 
     sweep_page->free_slots = freed_slots + empty_slots;
     objspace->profile.total_freed_objects += freed_slots;
-    heap_pages_final_slots += final_slots;
-    sweep_page->final_slots += final_slots;
 
     if (heap_pages_deferred_final && !finalizing) {
         rb_thread_t *th = GET_THREAD();
-- 
cgit v0.10.2


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

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