da6346
From 62536afb90ebb3d7421485c018abd90ba72b919c Mon Sep 17 00:00:00 2001
da6346
From: "Barton E. Schaefer" <schaefer@zsh.org>
da6346
Date: Sat, 18 Jan 2014 21:22:11 -0800
da6346
Subject: [PATCH 1/2] 32285: restart the fheap search in freeheap if the
da6346
 current fheap arena is about to be discarded; fixes crash
da6346
da6346
Upstream-commit: 23f98c3e1d4792e32c616e1f73c383988bd86a9c
da6346
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
da6346
---
da6346
 Src/mem.c | 9 +++++++++
da6346
 1 file changed, 9 insertions(+)
da6346
da6346
diff --git a/Src/mem.c b/Src/mem.c
da6346
index aeca3d9..eb5a091 100644
da6346
--- a/Src/mem.c
da6346
+++ b/Src/mem.c
da6346
@@ -367,6 +367,15 @@ freeheap(void)
da6346
 	    }
da6346
 #endif
da6346
 	} else {
da6346
+	    if (h == fheap && h != heaps) {
da6346
+		/*
da6346
+		 * When deallocating the last arena with free space,
da6346
+		 * loop back through the list to find another one.
da6346
+		 */
da6346
+		fheap = NULL;
da6346
+		hn = heaps;
da6346
+		continue;
da6346
+	    }
da6346
 #ifdef USE_MMAP
da6346
 	    munmap((void *) h, h->size);
da6346
 #else
da6346
-- 
da6346
2.13.5
da6346
da6346
da6346
From d968fe1061acabd72465a276c2de060f0f8bb668 Mon Sep 17 00:00:00 2001
da6346
From: "Barton E. Schaefer" <schaefer@zsh.org>
da6346
Date: Wed, 22 Jan 2014 21:47:29 -0800
da6346
Subject: [PATCH 2/2] unposted: reformulate 32285 to lift the fheap->sp test
da6346
 out of the loop, improve commentary
da6346
da6346
Upstream-commit: 6c603a412751c810ba04bcd463cd3595091ca391
da6346
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
da6346
---
da6346
 Src/mem.c | 24 +++++++++---------------
da6346
 1 file changed, 9 insertions(+), 15 deletions(-)
da6346
da6346
diff --git a/Src/mem.c b/Src/mem.c
da6346
index eb5a091..3a7e31d 100644
da6346
--- a/Src/mem.c
da6346
+++ b/Src/mem.c
da6346
@@ -319,23 +319,26 @@ freeheap(void)
da6346
     h_free++;
da6346
 #endif
da6346
 
da6346
-    /* At this point we used to do:
da6346
-    fheap = NULL;
da6346
-     *
da6346
+    /*
da6346
      * When pushheap() is called, it sweeps over the entire heaps list of
da6346
      * arenas and marks every one of them with the amount of free space in
da6346
      * that arena at that moment.  zhalloc() is then allowed to grab bits
da6346
      * out of any of those arenas that have free space.
da6346
      *
da6346
-     * With the above reset of fheap, the loop below sweeps back over the
da6346
+     * Whenever fheap is NULL here, the loop below sweeps back over the
da6346
      * entire heap list again, resetting the free space in every arena to
da6346
      * the amount stashed by pushheap() and finding the first arena with
da6346
      * free space to optimize zhalloc()'s next search.  When there's a lot
da6346
      * of stuff already on the heap, this is an enormous amount of work,
da6346
      * and performance goes to hell.
da6346
      *
da6346
-     * However, there doesn't seem to be any reason to reset fheap before
da6346
-     * beginning this loop.  Either it's already correct, or it has never
da6346
+     * However, if the arena to which fheap points is unused, we want to
da6346
+     * free it, so we have no choice but to do the sweep for a new fheap.
da6346
+     */
da6346
+    if (fheap && !fheap->sp)
da6346
+	fheap = NULL;	/* We used to do this unconditionally */
da6346
+    /*
da6346
+     * In other cases, either fheap is already correct, or it has never
da6346
      * been set and this loop will do it, or it'll be reset from scratch
da6346
      * on the next popheap().  So all that's needed here is to pick up
da6346
      * the scan wherever the last pass [or the last popheap()] left off.
da6346
@@ -367,15 +370,6 @@ freeheap(void)
da6346
 	    }
da6346
 #endif
da6346
 	} else {
da6346
-	    if (h == fheap && h != heaps) {
da6346
-		/*
da6346
-		 * When deallocating the last arena with free space,
da6346
-		 * loop back through the list to find another one.
da6346
-		 */
da6346
-		fheap = NULL;
da6346
-		hn = heaps;
da6346
-		continue;
da6346
-	    }
da6346
 #ifdef USE_MMAP
da6346
 	    munmap((void *) h, h->size);
da6346
 #else
da6346
-- 
da6346
2.13.5
da6346