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