From 8ddc93293cd8370870f221225ef1e013fbff6d65 Mon Sep 17 00:00:00 2001
From: Jason Evans <jasone@canonware.com>
Date: Fri, 30 Jan 2015 21:22:54 -0800
Subject: Fix chunk_recycle()'s new_addr functionality.

Fix chunk_recycle()'s new_addr functionality to search by address rather
than just size if new_addr is specified.  The functionality added by
a95018ee819abf897562d9d1f3bc31d4dd725a8d (Attempt to expand huge
allocations in-place.) only worked if the two search orders happened to
return the same results (e.g. in simple test cases).
---
 src/chunk.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/chunk.c b/src/chunk.c
index 7bfcdb8..a3ae548 100644
--- a/src/chunk.c
+++ b/src/chunk.c
@@ -48,6 +48,8 @@ chunk_recycle(extent_tree_t *chunks_szad, extent_tree_t *chunks_ad,
 	size_t alloc_size, leadsize, trailsize;
 	bool zeroed;
 
+	assert(new_addr == NULL || alignment == chunksize);
+
 	if (base) {
 		/*
 		 * This function may need to call base_node_{,de}alloc(), but
@@ -65,13 +67,15 @@ chunk_recycle(extent_tree_t *chunks_szad, extent_tree_t *chunks_ad,
 	key.addr = new_addr;
 	key.size = alloc_size;
 	malloc_mutex_lock(&chunks_mtx);
-	node = extent_tree_szad_nsearch(chunks_szad, &key);
-	if (node == NULL || (new_addr && node->addr != new_addr)) {
+	node = (new_addr != NULL) ? extent_tree_ad_search(chunks_ad, &key) :
+	    extent_tree_szad_nsearch(chunks_szad, &key);
+	if (node == NULL) {
 		malloc_mutex_unlock(&chunks_mtx);
 		return (NULL);
 	}
 	leadsize = ALIGNMENT_CEILING((uintptr_t)node->addr, alignment) -
 	    (uintptr_t)node->addr;
+	assert(new_addr == NULL || leadsize == 0);
 	assert(node->size >= leadsize + size);
 	trailsize = node->size - leadsize - size;
 	ret = (void *)((uintptr_t)node->addr + leadsize);
-- 
cgit v0.12