From 18140cf69462a5036e46852707cadbed7156d3ca Mon Sep 17 00:00:00 2001
From: Qingqing Li <liqingqing3@huawei.com>
Date: Tue, 28 Dec 2021 08:51:54 +0800
Subject: [PATCH] Continue to release span until the end of one round. This is
 also sync from the google/tcmalloc project.

upstream link: https://github.com/gperftools/gperftools/pull/1320

---
 src/page_heap.cc | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/page_heap.cc b/src/page_heap.cc
index 44ad654..68c5416 100644
--- a/src/page_heap.cc
+++ b/src/page_heap.cc
@@ -483,11 +483,18 @@ Length PageHeap::ReleaseSpan(Span* s) {
 
 Length PageHeap::ReleaseAtLeastNPages(Length num_pages) {
   Length released_pages = 0;
+  Length prev_released_pages = 1;
 
   // Round robin through the lists of free spans, releasing a
   // span from each list.  Stop after releasing at least num_pages
   // or when there is nothing more to release.
   while (released_pages < num_pages && stats_.free_bytes > 0) {
+    if (released_pages == prev_released_pages) {
+      // Last iteration of while loop made no progress.
+      break;
+    }
+    prev_released_pages = released_pages;
+
     for (int i = 0; i < kMaxPages+1 && released_pages < num_pages;
          i++, release_index_++) {
       Span *s;
@@ -510,8 +517,6 @@ Length PageHeap::ReleaseAtLeastNPages(Length num_pages) {
       // large freelist, should we carve s instead of releasing?
       // the whole thing?
       Length released_len = ReleaseSpan(s);
-      // Some systems do not support release
-      if (released_len == 0) return released_pages;
       released_pages += released_len;
     }
   }
-- 
1.8.3.1

