Loading...
--- libmalloc/libmalloc-166.220.1/src/magazine_tiny.c
+++ libmalloc/libmalloc-317.140.5/src/magazine_tiny.c
@@ -44,17 +44,23 @@
 {
 #if CONFIG_TINY_USES_HYPER_SHIFT
 	if (os_likely(_os_cpu_number_override == -1)) {
-		return _os_cpu_number() >> hyper_shift;
+		return _malloc_cpu_number() >> hyper_shift;
 	} else {
 		return _os_cpu_number_override >> hyper_shift;
 	}
 #else // CONFIG_SMALL_USES_HYPER_SHIFT
 	if (os_likely(_os_cpu_number_override == -1)) {
-		return _os_cpu_number();
+		return _malloc_cpu_number();
 	} else {
 		return _os_cpu_number_override;
 	}
 #endif // CONFIG_SMALL_USES_HYPER_SHIFT
+}
+
+static inline grain_t
+tiny_slot_from_msize(msize_t msize)
+{
+	return (!msize || (msize > NUM_TINY_SLOTS) ? NUM_TINY_SLOTS : msize - 1);
 }
 
 /*
@@ -68,7 +74,7 @@
 	// check whether the previous block is in the tiny region and a block header
 	// if so, then the size of the previous block is one, and there is no stored
 	// size.
-	if (ptr != TINY_REGION_FOR_PTR(ptr)) {
+	if (ptr != TINY_REGION_HEAP_BASE(TINY_REGION_FOR_PTR(ptr))) {
 		void *prev_block = (void *)((uintptr_t)ptr - TINY_QUANTUM);
 		uint32_t *prev_header = TINY_BLOCK_HEADER_FOR_PTR(prev_block);
 		msize_t prev_index = TINY_INDEX_FOR_PTR(prev_block);
@@ -91,11 +97,14 @@
 	uint32_t val = (1 << (index & 31));
 
 #if DEBUG_MALLOC
-	if (msize >= NUM_TINY_SLOTS) {
+	if (msize > NUM_TINY_SLOTS) {
 		malloc_report(ASL_LEVEL_ERR, "set_tiny_meta_header_in_use() invariant broken %p %d\n", ptr, msize);
 	}
 	if ((unsigned)index + (unsigned)msize > 0x10000) {
 		malloc_report(ASL_LEVEL_ERR, "set_tiny_meta_header_in_use() invariant broken (2) %p %d\n", ptr, msize);
+	}
+	if (msize > TINY_BITMAP_RANGE_LIMIT) {
+		malloc_report(ASL_LEVEL_ERROR, "set_tiny_meta_header_in_use() invariant broken (3) %p %d\n", ptr, msize);
 	}
 #endif
 
@@ -268,7 +277,7 @@
 	}
 
 	previous_index = index - previous_msize;
-	previous_ptr = (void *)((uintptr_t)TINY_REGION_FOR_PTR(ptr) + TINY_BYTES_FOR_MSIZE(previous_index));
+	previous_ptr = TINY_PTR_FOR_INDEX(previous_index, TINY_REGION_FOR_PTR(ptr));
 	if (!BITARRAY_BIT(block_header, previous_index)) {
 		return NULL;
 	}
@@ -282,6 +291,72 @@
 	// conservative check did match true check
 	*prev_msize = previous_msize;
 	return previous_ptr;
+}
+
+// Given a region and a slot index, scans regions that precede the region on
+// the magazine region list for one that has something on its free list for
+// the given slot and returns the address of the last such block, or NULL if
+// there is none. To reduce overhead, we scan forward from the first region
+// looking for our region and noting the last on the freelist for the given slot
+// for each earlier region, stopping once we have looked at 5 regions if we did
+// not reach our own. This has the effect of keeping the blocks for early
+// regions near the front of the freelist while not spending large amounts of
+// time looking for the "best" place to put this region's free blocks when there
+// are many regions in the magazine.
+static MALLOC_INLINE void *
+tiny_earlier_region_last_free(magazine_t *tiny_mag_ptr,
+		region_trailer_t *trailer, grain_t slot)
+{
+	int count = 0;
+	uint16_t target_block = 0;
+	region_trailer_t *target_trailer = NULL;
+	region_trailer_t *next_trailer = tiny_mag_ptr->firstNode;
+
+	while (next_trailer && next_trailer != trailer && count++ < 5) {
+		tiny_region_t r = TINY_REGION_FOR_PTR(next_trailer);
+		uint16_t block = r->free_blocks_by_slot[slot].last_block;
+		if (block) {
+			target_block = block;
+			target_trailer = next_trailer;
+		}
+		next_trailer = next_trailer->next;
+	}
+	return target_block ? TINY_PTR_FOR_INDEX(target_block - 1,
+			TINY_REGION_FOR_PTR(target_trailer)) : NULL;
+}
+
+static MALLOC_INLINE void
+tiny_update_region_free_list_for_remove(grain_t slot, tiny_free_list_t *ptr,
+		tiny_free_list_t *new_head)
+{
+	uint16_t ptr_index = TINY_INDEX_FOR_PTR(ptr);
+	tiny_region_t ptr_region = TINY_REGION_FOR_PTR(ptr);
+	region_free_blocks_t *blocks = &ptr_region->free_blocks_by_slot[slot];
+	MALLOC_ASSERT(ptr_index == blocks->first_block - 1);
+
+	if (new_head && (TINY_REGION_FOR_PTR(new_head) == TINY_REGION_FOR_PTR(ptr))) {
+		uint16_t new_head_block = TINY_INDEX_FOR_PTR(new_head) + 1;
+		if (blocks->first_block == blocks->last_block) {
+			blocks->last_block = new_head_block;
+		}
+		blocks->first_block = new_head_block;
+	} else {
+		// No more entries in this region.
+		blocks->first_block = blocks->last_block = 0;
+	}
+}
+
+void
+tiny_print_region_free_list(void *ptr, grain_t slot)
+{
+	tiny_region_t region = TINY_REGION_FOR_PTR(ptr);
+	region_free_blocks_t *blocks = &region->free_blocks_by_slot[slot];
+	malloc_printf("For region %p, first block: %d (%p), last block: %d (%p)\n",
+			region,
+			blocks->first_block,
+			blocks->first_block ? TINY_PTR_FOR_INDEX(blocks->first_block - 1, region) : (void *)0,
+			blocks->last_block,
+			blocks->last_block ? TINY_PTR_FOR_INDEX(blocks->last_block - 1, region) : (void *)0);
 }
 
 /*
@@ -292,7 +367,7 @@
 static void
 tiny_free_list_add_ptr(rack_t *rack, magazine_t *tiny_mag_ptr, void *ptr, msize_t msize)
 {
-	grain_t slot = (!msize || (msize >= NUM_TINY_SLOTS)) ? NUM_TINY_SLOTS - 1 : msize - 1;
+	grain_t slot = (!msize || (msize > NUM_TINY_SLOTS)) ? NUM_TINY_SLOTS : msize - 1;
 	tiny_free_list_t *free_ptr = ptr;
 	tiny_free_list_t *free_head = tiny_mag_ptr->mag_free_list[slot].p;
 
@@ -318,14 +393,77 @@
 					"ptr=%p slot=%d free_head=%p\n", ptr, slot, (void *)free_head);
 		}
 #endif
-		free_head->previous.u = free_list_checksum_ptr(rack, free_ptr);
 	} else {
 		BITMAPV_SET(tiny_mag_ptr->mag_bitmap, slot);
 	}
-	free_ptr->previous.u = free_list_checksum_ptr(rack, NULL);
-	free_ptr->next.u = free_list_checksum_ptr(rack, free_head);
-
-	tiny_mag_ptr->mag_free_list[slot].p = free_ptr;
+
+	tiny_region_t region = TINY_REGION_FOR_PTR(ptr);
+	region_free_blocks_t *free_blocks = &region->free_blocks_by_slot[slot];
+	uint16_t first_free_block_index = free_blocks->first_block;
+	uint16_t this_block_index = TINY_INDEX_FOR_PTR(ptr);
+
+	if (first_free_block_index) {
+		// This region already has something on its free list.
+		tiny_free_list_t *old_first_free = TINY_PTR_FOR_INDEX(first_free_block_index - 1, region);
+		tiny_free_list_t *prev_ptr = free_list_unchecksum_ptr(rack, &old_first_free->previous);
+		if (!prev_ptr) {
+			// Old first item was the first item in the magazine free list -
+			// update the magazine head pointer to point to this block.
+			tiny_mag_ptr->mag_free_list[slot].p = free_ptr;
+		} else {
+			prev_ptr->next.u = free_list_checksum_ptr(rack, free_ptr); // XXX
+		}
+
+		// Set our previous pointer to the one from the old first block.
+		// It's already checksummed, so just copy it directly. It is NULL if
+		// the old first block was also the first block on the magazine free
+		// list.
+		free_ptr->previous.u = old_first_free->previous.u;
+
+		// Our "next" pointer always points to the block that used to be first
+		// and we are always its predecessor.
+		free_ptr->next.u = free_list_checksum_ptr(rack, old_first_free);
+		old_first_free->previous.u = free_list_checksum_ptr(rack, free_ptr);
+
+		// Update the first free block index for this region.
+		free_blocks->first_block = this_block_index + 1;
+	} else {
+		// Free list for this region is empty. Add ourselves to the magazine
+		// free list between the last block of the preceding region that has
+		// a free block and the first block of the next region with a free
+		// block (either of which may not exist).
+		tiny_free_list_t *prev_free = NULL;
+		tiny_free_list_t *next_free;
+
+		// If the magazine free list is empty, we know we are going to add at
+		// the front. Otherwise, find the correct place. If we are freeing to
+		// the recirc depot, we can always free to the front because we don't
+		// care about ordering in the depot (since no allocations occur there).
+		mag_index_t mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(region);
+		if (mag_index != DEPOT_MAGAZINE_INDEX
+				&& tiny_mag_ptr->mag_free_list[slot].p) {
+			region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(region);
+			prev_free = tiny_earlier_region_last_free(tiny_mag_ptr, trailer, slot);
+		}
+		if (!prev_free) {
+			// We are becoming the head of the magazine free list.
+			next_free = tiny_mag_ptr->mag_free_list[slot].p;
+			tiny_mag_ptr->mag_free_list[slot].p = free_ptr;
+		} else {
+			next_free = free_list_unchecksum_ptr(rack, &prev_free->next);
+			prev_free->next.u = free_list_checksum_ptr(rack, free_ptr);
+		}
+		free_ptr->previous.u = free_list_checksum_ptr(rack, prev_free);
+
+		if (next_free) {
+			next_free->previous.u = free_list_checksum_ptr(rack, free_ptr);
+		}
+		free_ptr->next.u = free_list_checksum_ptr(rack, next_free);
+
+		// Set the first and last free block index for this region.
+		free_blocks->first_block = free_blocks->last_block =
+				this_block_index + 1;
+	}
 }
 
 /*
@@ -335,7 +473,7 @@
 static void
 tiny_free_list_remove_ptr(rack_t *rack, magazine_t *tiny_mag_ptr, void *ptr, msize_t msize)
 {
-	grain_t slot = (!msize || (msize >= NUM_TINY_SLOTS)) ? NUM_TINY_SLOTS - 1 : msize - 1;
+	grain_t slot = tiny_slot_from_msize(msize);
 	tiny_free_list_t *free_ptr = ptr, *next, *previous;
 
 	next = free_list_unchecksum_ptr(rack, &free_ptr->next);
@@ -388,6 +526,24 @@
 		// We know free_ptr is already checksummed, so we don't need to do it
 		// again.
 		next->previous = free_ptr->previous;
+	}
+
+	tiny_region_t region = TINY_REGION_FOR_PTR(ptr);
+	region_free_blocks_t *free_blocks = &region->free_blocks_by_slot[slot];
+	uint16_t this_block_index = TINY_INDEX_FOR_PTR(ptr);
+
+	boolean_t is_first = free_blocks->first_block == this_block_index + 1;
+	boolean_t is_last = free_blocks->last_block == this_block_index + 1;
+
+	if (is_first && is_last) {
+		// Removing the one and only item on the list. Set both block indices to 0.
+		free_blocks->first_block = free_blocks->last_block = 0;
+	} else if (is_first) {
+		MALLOC_ASSERT(next);
+		free_blocks->first_block = TINY_INDEX_FOR_PTR(next) + 1;
+	} else if (is_last) {
+		MALLOC_ASSERT(previous);
+		free_blocks->last_block	= TINY_INDEX_FOR_PTR(previous) + 1;
 	}
 }
 
@@ -410,7 +566,7 @@
 	//
 
 	if (tiny_mag_ptr->mag_bytes_free_at_end) {
-		last_block = (void *)((uintptr_t)TINY_REGION_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
+		last_block = (void *)((uintptr_t)TINY_REGION_HEAP_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
 		last_msize = TINY_MSIZE_FOR_BYTES(tiny_mag_ptr->mag_bytes_free_at_end);
 		last_header = TINY_BLOCK_HEADER_FOR_PTR(last_block);
 		last_index = TINY_INDEX_FOR_PTR(last_block);
@@ -445,7 +601,7 @@
 #if CONFIG_ASLR_INTERNAL
 	// Coalesce the big free block at start with any following free blocks
 	if (tiny_mag_ptr->mag_bytes_free_at_start) {
-		last_block = TINY_REGION_ADDRESS(tiny_mag_ptr->mag_last_region);
+		last_block = TINY_REGION_HEAP_BASE(tiny_mag_ptr->mag_last_region);
 		last_msize = TINY_MSIZE_FOR_BYTES(tiny_mag_ptr->mag_bytes_free_at_start);
 
 		void *next_block = (void *)((uintptr_t)last_block + tiny_mag_ptr->mag_bytes_free_at_start);
@@ -473,12 +629,12 @@
 int
 tiny_free_detach_region(rack_t *rack, magazine_t *tiny_mag_ptr, region_t r)
 {
-	uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(r);
+	uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(r);
 	uintptr_t current = start;
-	uintptr_t limit = (uintptr_t)TINY_REGION_END(r);
+	uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(r);
 	boolean_t is_free;
 	msize_t msize;
-	int total_alloc = 0;
+	region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(r);
 
 	while (current < limit) {
 		msize = get_tiny_meta_header((void *)current, &is_free);
@@ -494,23 +650,21 @@
 		}
 		if (is_free) {
 			tiny_free_list_remove_ptr(rack, tiny_mag_ptr, (void *)current, msize);
-		} else {
-			total_alloc++;
 		}
 		current += TINY_BYTES_FOR_MSIZE(msize);
 	}
-	return total_alloc;
+	return trailer->objects_in_use;
 }
 
 size_t
 tiny_free_reattach_region(rack_t *rack, magazine_t *tiny_mag_ptr, region_t r)
 {
-	uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(r);
+	uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(r);
 	uintptr_t current = start;
-	uintptr_t limit = (uintptr_t)TINY_REGION_END(r);
+	uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(r);
 	boolean_t is_free;
 	msize_t msize;
-	size_t total_alloc = 0;
+	size_t bytes_used = REGION_TRAILER_FOR_TINY_REGION(r)->bytes_used;
 
 	while (current < limit) {
 		msize = get_tiny_meta_header((void *)current, &is_free);
@@ -526,12 +680,10 @@
 		}
 		if (is_free) {
 			tiny_free_list_add_ptr(rack, tiny_mag_ptr, (void *)current, msize);
-		} else {
-			total_alloc += TINY_BYTES_FOR_MSIZE(msize);
 		}
 		current += TINY_BYTES_FOR_MSIZE(msize);
 	}
-	return total_alloc;
+	return bytes_used;
 }
 
 typedef struct {
@@ -541,12 +693,12 @@
 void
 tiny_free_scan_madvise_free(rack_t *rack, magazine_t *depot_ptr, region_t r)
 {
-	uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(r);
+	uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(r);
 	uintptr_t current = start;
-	uintptr_t limit = (uintptr_t)TINY_REGION_END(r);
+	uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(r);
 	boolean_t is_free;
 	msize_t msize;
-	tiny_pg_pair_t advisory[((TINY_REGION_PAYLOAD_BYTES + vm_page_quanta_size - 1) >> vm_page_quanta_shift) >>
+	tiny_pg_pair_t advisory[((TINY_HEAP_SIZE + vm_kernel_page_size - 1) >> vm_kernel_page_shift) >>
 							1]; // 256bytes stack allocated
 	int advisories = 0;
 
@@ -581,7 +733,7 @@
 			uintptr_t pgHi = trunc_page_kernel(current + TINY_BYTES_FOR_MSIZE(msize) - sizeof(msize_t));
 
 			if (pgLo < pgHi) {
-				advisory[advisories].pnum = (pgLo - start) >> vm_kernel_page_shift;
+				advisory[advisories].pnum = (pgLo - (uintptr_t)r) >> vm_kernel_page_shift;
 				advisory[advisories].size = (pgHi - pgLo) >> vm_kernel_page_shift;
 				advisories++;
 			}
@@ -602,7 +754,7 @@
 		OSAtomicIncrement32Barrier(&(REGION_TRAILER_FOR_TINY_REGION(r)->pinned_to_depot));
 		SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
 		for (i = 0; i < advisories; ++i) {
-			uintptr_t addr = (advisory[i].pnum << vm_kernel_page_shift) + start;
+			uintptr_t addr = (advisory[i].pnum << vm_kernel_page_shift) + (uintptr_t)r;
 			size_t size = advisory[i].size << vm_kernel_page_shift;
 
 			mvm_madvise_free(rack, r, addr, addr + size, NULL, rack->debug_flags & MALLOC_DO_SCRIBBLE);
@@ -612,11 +764,12 @@
 	}
 }
 
+#if CONFIG_RECIRC_DEPOT
 static region_t
 tiny_find_msize_region(rack_t *rack, magazine_t *tiny_mag_ptr, mag_index_t mag_index, msize_t msize)
 {
 	tiny_free_list_t *ptr;
-	grain_t slot = msize - 1;
+	grain_t slot = tiny_slot_from_msize(msize);
 	free_list_t *free_list = tiny_mag_ptr->mag_free_list;
 	free_list_t *the_slot = free_list + slot;
 	free_list_t *limit;
@@ -647,7 +800,7 @@
 	}
 
 	slot = BITMAPV_CTZ(bitmap);
-	limit = free_list + NUM_TINY_SLOTS - 1;
+	limit = free_list + NUM_TINY_SLOTS;
 	free_list += slot;
 
 	if (free_list < limit) {
@@ -663,7 +816,7 @@
 	}
 
 	// We are now looking at the last slot, which contains blocks equal to, or
-	// due to coalescing of free blocks, larger than (NUM_TINY_SLOTS - 1) * tiny quantum size.
+	// due to coalescing of free blocks, larger than NUM_TINY_SLOTS * tiny quantum size.
 	ptr = limit->p;
 	if (ptr) {
 		return TINY_REGION_FOR_PTR(ptr);
@@ -671,6 +824,102 @@
 
 	return NULL;
 }
+#endif // CONFIG_RECIRC_DEPOT
+
+#if CONFIG_MADVISE_PRESSURE_RELIEF
+void
+tiny_madvise_pressure_relief(rack_t *rack)
+{
+	mag_index_t mag_index;
+	magazine_t *tiny_depot_ptr = (&rack->magazines[DEPOT_MAGAZINE_INDEX]);
+
+	for (mag_index = 0; mag_index < rack->num_magazines; mag_index++) {
+		size_t index;
+		for (index = 0; index < rack->region_generation->num_regions_allocated; ++index) {
+			rack_region_lock(rack);
+
+			region_t tiny = rack->region_generation->hashed_regions[index];
+			if (!tiny || tiny == HASHRING_REGION_DEALLOCATED) {
+				rack_region_unlock(rack);
+				continue;
+			}
+
+			region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(tiny);
+			// Make sure that the owning magazine doesn't try and take this out
+			// from under our feet.
+			trailer->dispose_flags |= RACK_DISPOSE_DELAY;
+			rack_region_unlock(rack);
+
+			magazine_t *mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
+					trailer, MAGAZINE_INDEX_FOR_TINY_REGION(tiny));
+
+			// If acquiring the region lock was enough to prevent the owning
+			// magazine from deallocating the region, free it now so we don't
+			// do wasted work.
+			if (rack_region_maybe_dispose(rack, tiny, TINY_REGION_SIZE, trailer)) {
+				SZONE_MAGAZINE_PTR_UNLOCK(mag_ptr);
+				continue;
+			}
+
+			/* Ordering is important here, the magazine of a region may potentially change
+			 * during mag_lock_zine_for_region_trailer, so src_mag_index must be taken
+			 * after we've obtained the lock.
+			 */
+			mag_index_t src_mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(tiny);
+
+			/* We can (and must) ignore magazines that are already in the recirc depot. */
+			if (src_mag_index == DEPOT_MAGAZINE_INDEX) {
+				SZONE_MAGAZINE_PTR_UNLOCK(mag_ptr);
+				continue;
+			}
+
+			if (tiny == mag_ptr->mag_last_region && (mag_ptr->mag_bytes_free_at_end || mag_ptr->mag_bytes_free_at_start)) {
+				tiny_finalize_region(rack, mag_ptr);
+			}
+
+			/* Because this region is currently in use, we can't safely madvise it while
+			 * it's attached to the magazine. For this operation we have to remove it from
+			 * the current mag, attach it to the depot and then madvise.
+			 */
+
+			recirc_list_extract(rack, mag_ptr, REGION_TRAILER_FOR_TINY_REGION(tiny));
+			int objects_in_use = tiny_free_detach_region(rack, mag_ptr, tiny);
+
+			SZONE_MAGAZINE_PTR_LOCK(tiny_depot_ptr);
+			MAGAZINE_INDEX_FOR_TINY_REGION(tiny) = DEPOT_MAGAZINE_INDEX;
+			REGION_TRAILER_FOR_TINY_REGION(tiny)->pinned_to_depot = 0;
+
+			size_t bytes_inplay = tiny_free_reattach_region(rack, tiny_depot_ptr, tiny);
+
+			/* Fix up the metadata of the target magazine while the region is in the depot. */
+			mag_ptr->mag_num_bytes_in_objects -= bytes_inplay;
+			mag_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
+			mag_ptr->mag_num_objects -= objects_in_use;
+
+			/* Now we can drop the magazine lock of the source mag. */
+			SZONE_MAGAZINE_PTR_UNLOCK(mag_ptr);
+
+			tiny_depot_ptr->mag_num_bytes_in_objects += bytes_inplay;
+			tiny_depot_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
+			tiny_depot_ptr->mag_num_objects -= objects_in_use;
+
+			recirc_list_splice_last(rack, tiny_depot_ptr, REGION_TRAILER_FOR_TINY_REGION(tiny));
+
+			/* Actually do the scan, done holding the depot lock, the call will drop the lock
+			 * around the actual madvise syscalls.
+			 */
+			tiny_free_scan_madvise_free(rack, tiny_depot_ptr, tiny);
+
+			/* Now the region is in the recirc depot, the next allocations to require more
+			 * blocks will come along and take one of these regions back out of the depot.
+			 * As OS X madvise's reuse on an per-region basis, we leave as many of these
+			 * regions in the depot as possible after memory pressure.
+			 */
+			SZONE_MAGAZINE_PTR_UNLOCK(tiny_depot_ptr);
+		}
+	}
+}
+#endif // CONFIG_MADVISE_PRESSURE_RELIEF
 
 static MALLOC_INLINE void
 tiny_madvise_free_range_no_lock(rack_t *rack,
@@ -721,6 +970,7 @@
 	}
 }
 
+#if CONFIG_RECIRC_DEPOT
 static boolean_t
 tiny_get_region_from_depot(rack_t *rack, magazine_t *tiny_mag_ptr, mag_index_t mag_index, msize_t msize)
 {
@@ -775,15 +1025,15 @@
 	size_t bytes_inplay = tiny_free_reattach_region(rack, tiny_mag_ptr, sparse_region);
 
 	depot_ptr->mag_num_bytes_in_objects -= bytes_inplay;
-	depot_ptr->num_bytes_in_magazine -= TINY_REGION_PAYLOAD_BYTES;
+	depot_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
 	depot_ptr->mag_num_objects -= objects_in_use;
 
 	tiny_mag_ptr->mag_num_bytes_in_objects += bytes_inplay;
-	tiny_mag_ptr->num_bytes_in_magazine += TINY_REGION_PAYLOAD_BYTES;
+	tiny_mag_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
 	tiny_mag_ptr->mag_num_objects += objects_in_use;
 
-	// connect to magazine as first node
-	recirc_list_splice_first(rack, tiny_mag_ptr, node);
+	// connect to magazine as last node
+	recirc_list_splice_last(rack, tiny_mag_ptr, node);
 
 	SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
 
@@ -794,7 +1044,6 @@
 	return 1;
 }
 
-#if CONFIG_RECIRC_DEPOT
 static region_t
 tiny_free_try_depot_unmap_no_lock(rack_t *rack, magazine_t *depot_ptr, region_trailer_t *node)
 {
@@ -812,32 +1061,19 @@
 	int objects_in_use = tiny_free_detach_region(rack, depot_ptr, sparse_region);
 
 	if (0 == objects_in_use) {
-		// Invalidate the hash table entry for this region with HASHRING_REGION_DEALLOCATED.
-		// Using HASHRING_REGION_DEALLOCATED preserves the collision chain, using HASHRING_OPEN_ENTRY (0) would not.
-		rgnhdl_t pSlot = hash_lookup_region_no_lock(rack->region_generation->hashed_regions,
-				rack->region_generation->num_regions_allocated,
-				rack->region_generation->num_regions_allocated_shift,
-				sparse_region);
-
-		if (NULL == pSlot) {
-			malloc_zone_error(rack->debug_flags, true, "tiny_free_try_depot_unmap_no_lock hash lookup failed: %p\n", sparse_region);
+		if (!rack_region_remove(rack, sparse_region, node)) {
 			return NULL;
 		}
-		*pSlot = HASHRING_REGION_DEALLOCATED;
-		depot_ptr->num_bytes_in_magazine -= TINY_REGION_PAYLOAD_BYTES;
-
-		// Atomically increment num_regions_dealloc
-#ifdef __LP64___
-		OSAtomicIncrement64(&rack->num_regions_dealloc);
-#else
-		OSAtomicIncrement32((int32_t *)&rack->num_regions_dealloc);
-#endif
+		depot_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
 
 		// Caller will transfer ownership of the region back to the OS with no locks held
-		MAGMALLOC_DEALLOCREGION(TINY_SZONE_FROM_RACK(rack), (void *)sparse_region, TINY_REGION_SIZE); // DTrace USDT Probe
+		MAGMALLOC_DEALLOCREGION(TINY_SZONE_FROM_RACK(rack),
+				(void *)sparse_region, TINY_REGION_SIZE); // DTrace USDT Probe
 		return sparse_region;
 	} else {
-		malloc_zone_error(rack->debug_flags, true, "tiny_free_try_depot_unmap_no_lock objects_in_use not zero: %d\n", objects_in_use);
+		malloc_zone_error(rack->debug_flags, true,
+				"tiny_free_try_depot_unmap_no_lock objects_in_use not zero: %d\n",
+				objects_in_use);
 		return NULL;
 	}
 }
@@ -848,7 +1084,9 @@
 	// The entire magazine crossed the "emptiness threshold". Transfer a region
 	// from this magazine to the Depot. Choose a region that itself has crossed the emptiness threshold (i.e
 	// is at least fraction "f" empty.) Such a region will be marked "suitable" on the recirculation list.
-	region_trailer_t *node = tiny_mag_ptr->firstNode;
+	// Start from the last node in order to keep the regions created earlier at the front of the list for
+	// as long as possible. This helps reduce fragmentation.
+	region_trailer_t *node = tiny_mag_ptr->lastNode;
 
 	while (node && (!node->recirc_suitable || node->pinned_to_depot)) {
 		// If we skip a node due to pinned_to_depot being non-zero, it must be
@@ -857,10 +1095,10 @@
 		// When that's done, the same thread will enter tiny_free_try_recirc_to_depot()
 		// for the same region, which will come back here. So this just defers
 		// recirculation of the region.
-		node = node->next;
-	}
-
-	if (NULL == node) {
+		node = node->prev;
+	}
+
+	if (!node) {
 #if DEBUG_MALLOC
 		malloc_report(ASL_LEVEL_INFO, "*** tiny_free_do_recirc_to_depot end of list\n");
 #endif
@@ -893,13 +1131,13 @@
 	size_t bytes_inplay = tiny_free_reattach_region(rack, depot_ptr, sparse_region);
 
 	tiny_mag_ptr->mag_num_bytes_in_objects -= bytes_inplay;
-	tiny_mag_ptr->num_bytes_in_magazine -= TINY_REGION_PAYLOAD_BYTES;
+	tiny_mag_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
 	tiny_mag_ptr->mag_num_objects -= objects_in_use;
 
 	SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr); // Unlock the originating magazine
 
 	depot_ptr->mag_num_bytes_in_objects += bytes_inplay;
-	depot_ptr->num_bytes_in_magazine += TINY_REGION_PAYLOAD_BYTES;
+	depot_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
 	depot_ptr->mag_num_objects += objects_in_use;
 
 	// connect to Depot as last node
@@ -908,16 +1146,20 @@
 	MAGMALLOC_RECIRCREGION(TINY_SZONE_FROM_RACK(rack), (int)mag_index, (void *)sparse_region, TINY_REGION_SIZE,
 						   (int)BYTES_USED_FOR_TINY_REGION(sparse_region)); // DTrace USDT Probe
 
-#if !CONFIG_AGGRESSIVE_MADVISE
-	// Mark free'd dirty pages with MADV_FREE to reduce memory pressure
-	tiny_free_scan_madvise_free(rack, depot_ptr, sparse_region);
-#endif
+#if CONFIG_AGGRESSIVE_MADVISE
+	if (!aggressive_madvise_enabled)
+#endif
+	{
+		// Mark free'd dirty pages with MADV_FREE to reduce memory pressure
+		tiny_free_scan_madvise_free(rack, depot_ptr, sparse_region);
+	}
 
 	// If the region is entirely empty vm_deallocate() it outside the depot lock
 	region_t r_dealloc = tiny_free_try_depot_unmap_no_lock(rack, depot_ptr, node);
 	SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
 	if (r_dealloc) {
-		mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE, 0);
+		mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE,
+				MALLOC_FIX_GUARD_PAGE_FLAGS(rack->debug_flags));
 	}
 	return FALSE; // Caller need not unlock the originating magazine
 }
@@ -941,7 +1183,7 @@
 		return TRUE; // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr)
 	} else if (DEPOT_MAGAZINE_INDEX != mag_index) {
 		// Emptiness discriminant
-		if (bytes_used < DENSITY_THRESHOLD(TINY_REGION_PAYLOAD_BYTES)) {
+		if (tiny_region_below_recirc_threshold(region)) {
 			/* Region has crossed threshold from density to sparsity. Mark it "suitable" on the
 			 * recirculation candidates list. */
 			node->recirc_suitable = TRUE;
@@ -953,18 +1195,18 @@
 		// Has the entire magazine crossed the "emptiness threshold"? If so, transfer a region
 		// from this magazine to the Depot. Choose a region that itself has crossed the emptiness threshold (i.e
 		// is at least fraction "f" empty.) Such a region will be marked "suitable" on the recirculation list.
-		size_t a = tiny_mag_ptr->num_bytes_in_magazine;	// Total bytes allocated to this magazine
-		size_t u = tiny_mag_ptr->mag_num_bytes_in_objects; // In use (malloc'd) from this magaqzine
-
-		if (a - u > ((3 * TINY_REGION_PAYLOAD_BYTES) / 2) && u < DENSITY_THRESHOLD(a)) {
+		if (tiny_magazine_below_recirc_threshold(tiny_mag_ptr)) {
 			return tiny_free_do_recirc_to_depot(rack, tiny_mag_ptr, mag_index);
 		}
 	} else {
-#if !CONFIG_AGGRESSIVE_MADVISE
-		// We are free'ing into the depot, so madvise as we do so unless we were madvising every incoming
-		// allocation anyway.
-		tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, headptr, headsize, ptr, msize);
-#endif
+#if CONFIG_AGGRESSIVE_MADVISE
+		if (!aggressive_madvise_enabled)
+#endif
+		{
+			// We are free'ing into the depot, so madvise as we do so unless we were madvising every incoming
+			// allocation anyway.
+			tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, headptr, headsize, ptr, msize);
+		}
 
 		if (0 < bytes_used || 0 < node->pinned_to_depot) {
 			/* Depot'd region is still live. Leave it in place on the Depot's recirculation list
@@ -975,7 +1217,8 @@
 			region_t r_dealloc = tiny_free_try_depot_unmap_no_lock(rack, tiny_mag_ptr, node);
 			SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
 			if (r_dealloc) {
-				mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE, 0);
+				mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE,
+						MALLOC_FIX_GUARD_PAGE_FLAGS(rack->debug_flags));
 			}
 			return FALSE; // Caller need not unlock
 		}
@@ -985,7 +1228,7 @@
 #endif // CONFIG_RECIRC_DEPOT
 
 boolean_t
-tiny_free_no_lock(rack_t *rack, magazine_t *tiny_mag_ptr, mag_index_t mag_index, region_t region, void *ptr, msize_t msize)
+tiny_free_no_lock(rack_t *rack, magazine_t *tiny_mag_ptr, mag_index_t mag_index, region_t region, void *ptr, msize_t msize, boolean_t partial_free)
 {
 	void *original_ptr = ptr;
 	size_t original_size = TINY_BYTES_FOR_MSIZE(msize);
@@ -1007,6 +1250,9 @@
 	}
 #endif
 
+	// Check that the region cookie is intact.
+	region_check_cookie(region, &REGION_COOKIE_FOR_TINY_REGION(region));
+
 	// We try to coalesce this block with the preceeding one
 	previous = tiny_previous_preceding_free(ptr, &previous_msize);
 	if (previous) {
@@ -1023,7 +1269,7 @@
 		msize += previous_msize;
 	}
 	// We try to coalesce with the next block
-	if ((next_block < TINY_REGION_END(region)) && tiny_meta_header_is_free(next_block)) {
+	if ((next_block < TINY_REGION_HEAP_END(region)) && tiny_meta_header_is_free(next_block)) {
 		next_msize = get_tiny_free_size(next_block);
 #if DEBUG_MALLOC
 		if (LOG(szone, ptr) || LOG(szone, next_block)) {
@@ -1033,9 +1279,9 @@
 #endif
 		// If we are coalescing with the next block, and the next block is in
 		// the last slot of the free list, then we optimize this case here to
-		// avoid removing next_block from the slot (NUM_TINY_SLOTS - 1) and then adding ptr back
-		// to slot (NUM_TINY_SLOTS - 1).
-		if (next_msize >= NUM_TINY_SLOTS) {
+		// avoid removing next_block from the slot NUM_TINY_SLOTS and then adding ptr back
+		// to slot NUM_TINY_SLOTS.
+		if (next_msize > NUM_TINY_SLOTS) {
 			msize += next_msize;
 
 			big_free_block = (tiny_free_list_t *)next_block;
@@ -1043,7 +1289,7 @@
 			before_next_block = free_list_unchecksum_ptr(rack, &big_free_block->previous);
 
 			if (!before_next_block) {
-				tiny_mag_ptr->mag_free_list[NUM_TINY_SLOTS - 1].p = ptr;
+				tiny_mag_ptr->mag_free_list[NUM_TINY_SLOTS].p = ptr;
 			} else {
 				before_next_block->next.u = free_list_checksum_ptr(rack, ptr);
 			}
@@ -1060,6 +1306,16 @@
 			set_tiny_meta_header_middle(big_free_block);
 			set_tiny_meta_header_free(ptr, msize);
 
+			uint16_t next_block_index = TINY_INDEX_FOR_PTR(big_free_block) + 1;
+			uint16_t ptr_index = TINY_INDEX_FOR_PTR(ptr) + 1;
+			const grain_t slot = NUM_TINY_SLOTS;
+			region_free_blocks_t *free_blocks = &((tiny_region_t)region)->free_blocks_by_slot[slot];
+			if (free_blocks->first_block == next_block_index) {
+				free_blocks->first_block = ptr_index;
+			}
+			if (free_blocks->last_block == next_block_index) {
+				free_blocks->last_block = ptr_index;
+			}
 			goto tiny_free_ending;
 		}
 		tiny_free_list_remove_ptr(rack, tiny_mag_ptr, next_block, next_msize);
@@ -1076,20 +1332,28 @@
 	tiny_free_list_add_ptr(rack, tiny_mag_ptr, ptr, msize);
 
 tiny_free_ending:
-
-	tiny_mag_ptr->mag_num_objects--;
 	// we use original_size and not msize to avoid double counting the coalesced blocks
 	tiny_mag_ptr->mag_num_bytes_in_objects -= original_size;
 
 	// Update this region's bytes in use count
-	region_trailer_t *node = REGION_TRAILER_FOR_TINY_REGION(region);
-	size_t bytes_used = node->bytes_used - original_size;
-	node->bytes_used = (unsigned int)bytes_used;
-
+	region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(region);
+	size_t bytes_used = trailer->bytes_used - original_size;
+	trailer->bytes_used = (unsigned int)bytes_used;
+
+	// Partial free accounts for the case where we allocate a block for
+	// posix_memalign and then free some range of bytes at the start and/or
+	// the end. In that case, we aren't changing the number of allocated objects.
+	// Similarly for realloc() in the case where we shrink in place.
+	if (!partial_free) {
+		trailer->objects_in_use--;
+		tiny_mag_ptr->mag_num_objects--;
+	}
 #if CONFIG_AGGRESSIVE_MADVISE
-	// Platforms that want to madvise every freed allocation do so here, even if we continue
-	// on to use the recirc depot after.
-	tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, original_ptr, original_size, ptr, msize);
+	if (aggressive_madvise_enabled) {
+		// Platforms that want to madvise every freed allocation do so here, even if we continue
+		// on to use the recirc depot after.
+		tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, original_ptr, original_size, ptr, msize);
+	}
 #endif
 
 	// Caller must do SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr) if this function
@@ -1098,7 +1362,7 @@
 
 #if CONFIG_RECIRC_DEPOT
 	needs_unlock = tiny_free_try_recirc_to_depot(rack, tiny_mag_ptr, mag_index, region, original_ptr, original_size, ptr, msize);
-#endif
+#endif // CONFIG_RECIRC_DEPOT
 	return needs_unlock;
 }
 
@@ -1117,25 +1381,28 @@
 		tiny_finalize_region(rack, tiny_mag_ptr);
 	}
 
+	tiny_region_t region = (tiny_region_t)aligned_address;
+
 	// We set the unused bits of the header in the last pair to be all ones, and those of the inuse to zeroes.
 #if NUM_TINY_BLOCKS & 31
 	const uint32_t header = 0xFFFFFFFFU << (NUM_TINY_BLOCKS & 31);
 #else
 	const uint32_t header = 0;
 #endif
-	((tiny_region_t)aligned_address)->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].header = header;
-	((tiny_region_t)aligned_address)->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].inuse = 0;
+	region->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].header = header;
+	region->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].inuse = 0;
 
 	// Tag the region at "aligned_address" as belonging to us,
 	// and so put it under the protection of the magazine lock we are holding.
 	// Do this before advertising "aligned_address" on the hash ring(!)
-	MAGAZINE_INDEX_FOR_TINY_REGION(aligned_address) = mag_index;
+	MAGAZINE_INDEX_FOR_TINY_REGION(region) = mag_index;
 
 	// Insert the new region into the hash ring
-	rack_region_insert(rack, (region_t)aligned_address);
-
-	tiny_mag_ptr->mag_last_region = aligned_address;
-	BYTES_USED_FOR_TINY_REGION(aligned_address) = TINY_BYTES_FOR_MSIZE(msize);
+	rack_region_insert(rack, region);
+
+	tiny_mag_ptr->mag_last_region = region;
+	BYTES_USED_FOR_TINY_REGION(region) = TINY_BYTES_FOR_MSIZE(msize);
+	OBJECTS_IN_USE_FOR_TINY_REGION(region) = 1;
 
 #if CONFIG_ASLR_INTERNAL
 	int offset_msize = malloc_entropy[0] & TINY_ENTROPY_MASK;
@@ -1144,17 +1411,17 @@
 		offset_msize = strtol(getenv("MallocASLRForce"), NULL, 0) & TINY_ENTROPY_MASK;
 	}
 	if (getenv("MallocASLRPrint")) {
-		malloc_report(ASL_LEVEL_INFO, "Region: %p offset: %d\n", aligned_address, offset_msize);
+		malloc_report(ASL_LEVEL_INFO, "Region: %p offset: %d\n", region, offset_msize);
 	}
 #endif
 #else
 	int offset_msize = 0;
 #endif
-	ptr = (void *)((uintptr_t)aligned_address + TINY_BYTES_FOR_MSIZE(offset_msize));
+	ptr = (void *)(TINY_REGION_HEAP_BASE(region) + TINY_BYTES_FOR_MSIZE(offset_msize));
 	set_tiny_meta_header_in_use(ptr, msize);
 	tiny_mag_ptr->mag_num_objects++;
 	tiny_mag_ptr->mag_num_bytes_in_objects += TINY_BYTES_FOR_MSIZE(msize);
-	tiny_mag_ptr->num_bytes_in_magazine += TINY_REGION_PAYLOAD_BYTES;
+	tiny_mag_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
 
 	// We put a header on the last block so that it appears in use (for coalescing, etc...)
 	set_tiny_meta_header_in_use_1((void *)((uintptr_t)ptr + TINY_BYTES_FOR_MSIZE(msize)));
@@ -1171,7 +1438,7 @@
 #endif
 
 	// connect to magazine as last node
-	recirc_list_splice_last(rack, tiny_mag_ptr, REGION_TRAILER_FOR_TINY_REGION(aligned_address));
+	recirc_list_splice_last(rack, tiny_mag_ptr, REGION_TRAILER_FOR_TINY_REGION(region));
 
 #if DEBUG_MALLOC
 	if (LOG(szone, ptr)) {
@@ -1216,7 +1483,7 @@
 		SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
 
 		// Give up mpad blocks beginning at p to the tiny free list
-		free_tiny(&szone->tiny_rack, p, TINY_REGION_FOR_PTR(p), TINY_BYTES_FOR_MSIZE(mpad));
+		free_tiny(&szone->tiny_rack, p, TINY_REGION_FOR_PTR(p), TINY_BYTES_FOR_MSIZE(mpad), true);
 
 		p = q; // advance p to the desired alignment
 	}
@@ -1232,7 +1499,7 @@
 		SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
 
 		// Give up mwaste blocks beginning at q to the tiny free list
-		free_tiny(&szone->tiny_rack, q, TINY_REGION_FOR_PTR(q), TINY_BYTES_FOR_MSIZE(mwaste));
+		free_tiny(&szone->tiny_rack, q, TINY_REGION_FOR_PTR(q), TINY_BYTES_FOR_MSIZE(mwaste), true);
 	}
 
 	return p; // p has the desired size and alignment, and can later be free()'d
@@ -1242,7 +1509,8 @@
 tiny_claimed_address(rack_t *rack, void *ptr)
 {
 	region_t r = tiny_region_for_ptr_no_lock(rack, ptr);
-	return r && ptr < TINY_REGION_END(r);
+	return r && ptr >= TINY_REGION_HEAP_BASE(r)
+			&& ptr < TINY_REGION_HEAP_END(r);
 }
 
 void *
@@ -1262,7 +1530,7 @@
 		tiny_mag_ptr->mag_num_objects++;
 
 		SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
-		free_tiny(rack, q, TINY_REGION_FOR_PTR(q), 0);
+		free_tiny(rack, q, TINY_REGION_FOR_PTR(q), 0, true);
 	}
 	return ptr;
 }
@@ -1278,6 +1546,7 @@
 	boolean_t is_free;
 	msize_t next_msize, coalesced_msize, leftover_msize, new_msize;
 	void *leftover;
+	region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(TINY_REGION_FOR_PTR(ptr));
 
 	index = TINY_INDEX_FOR_PTR(ptr);
 	old_msize = TINY_MSIZE_FOR_BYTES(old_size);
@@ -1290,7 +1559,7 @@
 	next_block = (char *)ptr + old_size;
 
 	magazine_t *tiny_mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
-			REGION_TRAILER_FOR_TINY_REGION(TINY_REGION_FOR_PTR(ptr)),
+			trailer,
 			MAGAZINE_INDEX_FOR_TINY_REGION(TINY_REGION_FOR_PTR(ptr)));
 
 	if (DEPOT_MAGAZINE_INDEX == MAGAZINE_INDEX_FOR_TINY_REGION(TINY_REGION_FOR_PTR(ptr))) {
@@ -1320,15 +1589,17 @@
 			tiny_mag_ptr->mag_last_free = NULL;
 			tiny_mag_ptr->mag_last_free_msize = 0;
 			tiny_mag_ptr->mag_last_free_rgn = NULL;
+			trailer->objects_in_use--;
 		}
 		set_tiny_meta_header_middle(next_block);
+		coalesced_msize = 0; // No net change in memory use
 	} else {
 #endif // CONFIG_TINY_CACHE
 		/*
 		 * Try to expand into unused space immediately after this block.
 		 */
 		msize_t unused_msize = TINY_MSIZE_FOR_BYTES(tiny_mag_ptr->mag_bytes_free_at_end);
-		void *unused_start = TINY_REGION_END(TINY_REGION_FOR_PTR(ptr)) - tiny_mag_ptr->mag_bytes_free_at_end;
+		void *unused_start = TINY_REGION_HEAP_END(TINY_REGION_FOR_PTR(ptr)) - tiny_mag_ptr->mag_bytes_free_at_end;
 		if (tiny_mag_ptr->mag_last_region == TINY_REGION_FOR_PTR(ptr)
 				&& coalesced_msize < unused_msize && unused_start == ptr + old_size) {
 			// The block at the start of mag_bytes_free_at_end is marked as
@@ -1390,7 +1661,7 @@
 	node->bytes_used = (unsigned int)bytes_used;
 
 	// Emptiness discriminant
-	if (bytes_used < DENSITY_THRESHOLD(TINY_REGION_PAYLOAD_BYTES)) {
+	if (bytes_used < DENSITY_THRESHOLD(TINY_HEAP_SIZE)) {
 		/* After this reallocation the region is still sparse, so it must have been even more so before
 		 * the reallocation. That implies the region is already correctly marked. Do nothing. */
 	} else {
@@ -1436,7 +1707,7 @@
 	}
 
 	/* establish region limits */
-	start = (uintptr_t)TINY_REGION_ADDRESS(region);
+	start = (uintptr_t)TINY_REGION_HEAP_BASE(region);
 	ptr = start;
 	if (region == tiny_mag_ptr->mag_last_region) {
 		ptr += tiny_mag_ptr->mag_bytes_free_at_start;
@@ -1453,7 +1724,7 @@
 			}
 		}
 	}
-	region_end = (uintptr_t)TINY_REGION_END(region);
+	region_end = (uintptr_t)TINY_REGION_HEAP_END(region);
 
 	/*
 	 * The last region may have a trailing chunk which has not been converted into inuse/freelist
@@ -1487,10 +1758,10 @@
 
 		if (!is_free) {
 			/*
-			 * In use blocks cannot be more than (NUM_TINY_SLOTS - 1) quanta large.
+			 * In use blocks cannot be more than NUM_TINY_SLOTS quanta large.
 			 */
 			prev_free = 0;
-			if (msize > (NUM_TINY_SLOTS - 1)) {
+			if (msize > NUM_TINY_SLOTS) {
 				TINY_CHECK_FAIL("*** invariant broken for %p this tiny msize=%d - size is too large\n", (void *)ptr, msize);
 				return 0;
 			}
@@ -1527,9 +1798,9 @@
 			 */
 			follower = FOLLOWING_TINY_PTR(ptr, msize);
 			if (((uintptr_t)follower != region_end) && (get_tiny_previous_free_msize(follower) != msize)) {
-				TINY_CHECK_FAIL("*** invariant broken for tiny free %p followed by %p in region [%p-%p] "
+				TINY_CHECK_FAIL("*** invariant broken for tiny free %p followed by %p in region %p [%p-%p] "
 						"(end marker incorrect) should be %d; in fact %d\n",
-						(void *)ptr, follower, TINY_REGION_ADDRESS(region), (void *)region_end,
+						(void *)ptr, follower, region, TINY_REGION_HEAP_BASE(region), (void *)region_end,
 						msize, get_tiny_previous_free_msize(follower));
 				return 0;
 			}
@@ -1612,23 +1883,20 @@
 	for (index = 0; index < num_regions; ++index) {
 		region = regions[index];
 		if (HASHRING_OPEN_ENTRY != region && HASHRING_REGION_DEALLOCATED != region) {
-			range.address = (vm_address_t)TINY_REGION_ADDRESS(region);
-			range.size = (vm_size_t)TINY_REGION_SIZE;
+			range.address = (vm_address_t)TINY_REGION_HEAP_BASE(region);
+			range.size = (vm_size_t)TINY_HEAP_SIZE;
 			if (type_mask & MALLOC_ADMIN_REGION_RANGE_TYPE) {
-				admin_range.address = range.address + TINY_METADATA_START;
+				admin_range.address = TINY_REGION_METADATA(region);
 				admin_range.size = TINY_METADATA_SIZE;
 				recorder(task, context, MALLOC_ADMIN_REGION_RANGE_TYPE, &admin_range, 1);
 			}
 			if (type_mask & (MALLOC_PTR_REGION_RANGE_TYPE | MALLOC_ADMIN_REGION_RANGE_TYPE)) {
 				ptr_range.address = range.address;
-				ptr_range.size = NUM_TINY_BLOCKS * TINY_QUANTUM;
+				ptr_range.size = TINY_HEAP_SIZE;
 				recorder(task, context, MALLOC_PTR_REGION_RANGE_TYPE, &ptr_range, 1);
 			}
 			if (type_mask & MALLOC_PTR_IN_USE_RANGE_TYPE) {
-				vm_address_t mag_last_free;
-				msize_t mag_last_free_msize = 0;
-
-				err = reader(task, range.address, range.size, (void **)&mapped_region);
+				err = reader(task, (vm_address_t)region, (vm_size_t)TINY_REGION_SIZE, (void **)&mapped_region);
 				if (err) {
 					return err;
 				}
@@ -1636,19 +1904,18 @@
 				mag_index_t mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(mapped_region);
 				magazine_t *tiny_mag_ptr = tiny_mag_base + mag_index;
 
-				if (DEPOT_MAGAZINE_INDEX != mag_index) {
-					mag_last_free = (uintptr_t)tiny_mag_ptr->mag_last_free;
-					mag_last_free_msize = tiny_mag_ptr->mag_last_free_msize;
-				} else {
-					for (mag_index = 0; mag_index < szone->tiny_rack.num_magazines; mag_index++) {
-						if ((void *)range.address == (tiny_mag_base + mag_index)->mag_last_free_rgn) {
-							mag_last_free = (uintptr_t)(tiny_mag_base + mag_index)->mag_last_free;
-							mag_last_free_msize = (tiny_mag_base + mag_index)->mag_last_free_msize;
-						}
+				int cached_free_blocks = 0;
+#if CONFIG_TINY_CACHE
+				// Each magazine could have a pointer to a cached free block from
+				// this region. Count the regions that have such a pointer.
+				for (mag_index = 0; mag_index < szone->tiny_rack.num_magazines; mag_index++) {
+					if (region == (tiny_mag_base + mag_index)->mag_last_free_rgn) {
+						cached_free_blocks++;
 					}
 				}
-
-				block_header = (uint32_t *)(mapped_region + TINY_METADATA_START + sizeof(region_trailer_t));
+#endif // CONFIG_TINY_CACHE
+
+				block_header = TINY_BLOCK_HEADER_FOR_REGION(mapped_region);
 				in_use = TINY_INUSE_FOR_HEADER(block_header);
 				block_index = 0;
 				block_limit = NUM_TINY_BLOCKS;
@@ -1657,11 +1924,11 @@
 					block_limit -= TINY_MSIZE_FOR_BYTES(tiny_mag_ptr->mag_bytes_free_at_end);
 				}
 
-				while (block_index < block_limit) {
+				for (; block_index < block_limit; block_index += msize) {
 					vm_size_t block_offset = TINY_BYTES_FOR_MSIZE(block_index);
 					is_free = !BITARRAY_BIT(in_use, block_index);
 					if (is_free) {
-						mapped_ptr = mapped_region + block_offset;
+						mapped_ptr = TINY_REGION_HEAP_BASE(mapped_region) + block_offset;
 
 						// mapped_region, the address at which 'range' in 'task' has been
 						// mapped into our process, is not necessarily aligned to
@@ -1677,32 +1944,48 @@
 						} else {
 							msize = 1;
 						}
-
-					} else if (range.address + block_offset != mag_last_free) {
+					} else {
+#if CONFIG_TINY_CACHE
+						// If there are still magazines that have cached free
+						// blocks in this region, check whether this is one of
+						// them and don't return the block pointer if it is.
+						void *ptr = TINY_REGION_HEAP_BASE(region) + block_offset;
+						boolean_t block_cached = false;
+						if (cached_free_blocks) {
+							for (mag_index = 0; mag_index < szone->tiny_rack.num_magazines; mag_index++) {
+								if (ptr == (tiny_mag_base + mag_index)->mag_last_free) {
+									block_cached = true;
+									cached_free_blocks--;
+									msize = (tiny_mag_base + mag_index)->mag_last_free_msize;
+									break;
+								}
+							}
+						}
+						if (block_cached) {
+							if (!msize) {
+								return KERN_FAILURE; // Somethings amiss. Avoid looping at this block_index.
+							}
+							continue;
+						}
+#endif // CONFIG_TINY_CACHE
 						msize = 1;
 						bit = block_index + 1;
 						while (!BITARRAY_BIT(block_header, bit)) {
 							bit++;
 							msize++;
 						}
-						buffer[count].address = range.address + block_offset;
+						buffer[count].address = (vm_address_t)TINY_REGION_HEAP_BASE(region) + block_offset;
 						buffer[count].size = TINY_BYTES_FOR_MSIZE(msize);
 						count++;
 						if (count >= MAX_RECORDER_BUFFER) {
 							recorder(task, context, MALLOC_PTR_IN_USE_RANGE_TYPE, buffer, count);
 							count = 0;
 						}
-					} else {
-						// Block is not free but it matches mag_last_free_ptr so even
-						// though it is not marked free in the bitmap, we treat it as if
-						// it is and move on
-						msize = mag_last_free_msize;
 					}
 
 					if (!msize) {
 						return KERN_FAILURE; // Somethings amiss. Avoid looping at this block_index.
 					}
-					block_index += msize;
 				}
 				if (count) {
 					recorder(task, context, MALLOC_PTR_IN_USE_RANGE_TYPE, buffer, count);
@@ -1719,7 +2002,7 @@
 {
 	tiny_free_list_t *ptr;
 	msize_t this_msize;
-	grain_t slot = msize - 1;
+	grain_t slot = tiny_slot_from_msize(msize);
 	free_list_t *free_list = tiny_mag_ptr->mag_free_list;
 	free_list_t *the_slot = free_list + slot;
 	tiny_free_list_t *next;
@@ -1752,6 +2035,8 @@
 			malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_from_free_list(), exact match ptr=%p, this_msize=%d\n", ptr, this_msize);
 		}
 #endif
+		tiny_update_region_free_list_for_remove(slot, ptr, next);
+
 		goto return_tiny_alloc;
 	}
 
@@ -1768,7 +2053,7 @@
 	}
 
 	slot = BITMAPV_CTZ(bitmap);
-	limit = free_list + NUM_TINY_SLOTS - 1;
+	limit = free_list + NUM_TINY_SLOTS;
 	free_list += slot;
 
 	if (free_list < limit) {
@@ -1782,6 +2067,7 @@
 				BITMAPV_CLR(tiny_mag_ptr->mag_bitmap, slot);
 			}
 			this_msize = get_tiny_free_size(ptr);
+			tiny_update_region_free_list_for_remove(slot, ptr, next);
 			goto add_leftover_and_proceed;
 		}
 #if DEBUG_MALLOC
@@ -1790,14 +2076,14 @@
 	}
 
 	// We are now looking at the last slot, which contains blocks equal to, or
-	// due to coalescing of free blocks, larger than (NUM_TINY_SLOTS - 1) * tiny quantum size.
+	// due to coalescing of free blocks, larger than NUM_TINY_SLOTS * tiny quantum size.
 	// If the last freelist is not empty, and the head contains a block that is
 	// larger than our request, then the remainder is put back on the free list.
 	ptr = limit->p;
 	if (ptr) {
 		this_msize = get_tiny_free_size(ptr);
 		next = free_list_unchecksum_ptr(rack, &ptr->next);
-		if (this_msize - msize >= NUM_TINY_SLOTS) {
+		if (this_msize - msize > NUM_TINY_SLOTS) {
 			// the leftover will go back to the free list, so we optimize by
 			// modifying the free list rather than a pop and push of the head
 			leftover_msize = this_msize - msize;
@@ -1816,12 +2102,15 @@
 			}
 #endif
 			this_msize = msize;
+			tiny_update_region_free_list_for_remove(NUM_TINY_SLOTS, ptr, leftover_ptr);
+
 			goto return_tiny_alloc;
 		}
 		if (next) {
 			next->previous = ptr->previous;
 		}
 		limit->p = next;
+		tiny_update_region_free_list_for_remove(slot, ptr, next);
 		goto add_leftover_and_proceed;
 		/* NOTREACHED */
 	}
@@ -1829,7 +2118,7 @@
 try_tiny_malloc_from_end:
 	// Let's see if we can use tiny_mag_ptr->mag_bytes_free_at_end
 	if (tiny_mag_ptr->mag_bytes_free_at_end >= TINY_BYTES_FOR_MSIZE(msize)) {
-		ptr = (tiny_free_list_t *)((uintptr_t)TINY_REGION_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
+		ptr = (tiny_free_list_t *)((uintptr_t)TINY_REGION_HEAP_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
 		tiny_mag_ptr->mag_bytes_free_at_end -= TINY_BYTES_FOR_MSIZE(msize);
 		if (tiny_mag_ptr->mag_bytes_free_at_end) {
 			// let's add an in use block after ptr to serve as boundary
@@ -1846,7 +2135,7 @@
 #if CONFIG_ASLR_INTERNAL
 	// Try from start if nothing left at end
 	if (tiny_mag_ptr->mag_bytes_free_at_start >= TINY_BYTES_FOR_MSIZE(msize)) {
-		ptr = (tiny_free_list_t *)(TINY_REGION_ADDRESS(tiny_mag_ptr->mag_last_region) + tiny_mag_ptr->mag_bytes_free_at_start -
+		ptr = (tiny_free_list_t *)(TINY_REGION_HEAP_BASE(tiny_mag_ptr->mag_last_region) + tiny_mag_ptr->mag_bytes_free_at_start -
 							  TINY_BYTES_FOR_MSIZE(msize));
 		tiny_mag_ptr->mag_bytes_free_at_start -= TINY_BYTES_FOR_MSIZE(msize);
 		if (tiny_mag_ptr->mag_bytes_free_at_start) {
@@ -1881,19 +2170,23 @@
 	tiny_mag_ptr->mag_num_objects++;
 	tiny_mag_ptr->mag_num_bytes_in_objects += TINY_BYTES_FOR_MSIZE(this_msize);
 
-	// Update this region's bytes in use count
-	region_trailer_t *node = REGION_TRAILER_FOR_TINY_REGION(TINY_REGION_FOR_PTR(ptr));
-	size_t bytes_used = node->bytes_used + TINY_BYTES_FOR_MSIZE(this_msize);
-	node->bytes_used = (unsigned int)bytes_used;
+	// Check that the region cookie is intact and update the region's bytes in use count
+	tiny_region_t region = TINY_REGION_FOR_PTR(ptr);
+	region_check_cookie(region, &REGION_COOKIE_FOR_TINY_REGION(region));
+
+	region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(region);
+	size_t bytes_used = trailer->bytes_used + TINY_BYTES_FOR_MSIZE(this_msize);
+	trailer->bytes_used = (unsigned int)bytes_used;
+	trailer->objects_in_use++;
 
 	// Emptiness discriminant
-	if (bytes_used < DENSITY_THRESHOLD(TINY_REGION_PAYLOAD_BYTES)) {
+	if (bytes_used < DENSITY_THRESHOLD(TINY_HEAP_SIZE)) {
 		/* After this allocation the region is still sparse, so it must have been even more so before
 		 * the allocation. That implies the region is already correctly marked. Do nothing. */
 	} else {
 		/* Region has crossed threshold from sparsity to density. Mark it not "suitable" on the
 		 * recirculation candidates list. */
-		node->recirc_suitable = FALSE;
+		trailer->recirc_suitable = FALSE;
 	}
 #if DEBUG_MALLOC
 	if (LOG(szone, ptr)) {
@@ -1964,6 +2257,7 @@
 			return ptr;
 		}
 
+#if CONFIG_RECIRC_DEPOT
 		if (tiny_get_region_from_depot(rack, tiny_mag_ptr, mag_index, msize)) {
 			ptr = tiny_malloc_from_free_list(rack, tiny_mag_ptr, mag_index, msize);
 			if (ptr) {
@@ -1975,6 +2269,7 @@
 				return ptr;
 			}
 		}
+#endif // CONFIG_RECIRC_DEPOT
 
 		// The magazine is exhausted. A new region (heap) must be allocated to satisfy this call to malloc().
 		// The allocation, an mmap() system call, will be performed outside the magazine spin locks by the first
@@ -1989,7 +2284,10 @@
 			tiny_mag_ptr->alloc_underway = TRUE;
 			OSMemoryBarrier();
 			SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
-			fresh_region = mvm_allocate_pages_securely(TINY_REGION_SIZE, TINY_BLOCKS_ALIGN, VM_MEMORY_MALLOC_TINY, rack->debug_flags);
+			fresh_region = mvm_allocate_pages(TINY_REGION_SIZE,
+					TINY_BLOCKS_ALIGN,
+					MALLOC_FIX_GUARD_PAGE_FLAGS(rack->debug_flags),
+					VM_MEMORY_MALLOC_TINY);
 			SZONE_MAGAZINE_PTR_LOCK(tiny_mag_ptr);
 
 			// DTrace USDT Probe
@@ -2002,6 +2300,7 @@
 				return NULL;
 			}
 
+			region_set_cookie(&REGION_COOKIE_FOR_TINY_REGION(fresh_region));
 			ptr = tiny_malloc_from_region_no_lock(rack, tiny_mag_ptr, mag_index, msize, fresh_region);
 
 			// we don't clear because this freshly allocated space is pristine
@@ -2024,6 +2323,7 @@
 {
 	if (tiny_region_for_ptr_no_lock(rack, ptr)) {
 		if (TINY_INDEX_FOR_PTR(ptr) >= NUM_TINY_BLOCKS) {
+malloc_printf("NO ZONE for ptr %p\n", ptr);
 			return 0;
 		}
 
@@ -2062,14 +2362,12 @@
 static MALLOC_NOINLINE void
 free_tiny_botch(rack_t *rack, tiny_free_list_t *ptr)
 {
-	mag_index_t mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(TINY_REGION_FOR_PTR(ptr));
-	magazine_t *tiny_mag_ptr = &(rack->magazines[mag_index]);
-	SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
 	malloc_zone_error(rack->debug_flags, true, "Double free of object %p\n", ptr);
 }
 
 void
-free_tiny(rack_t *rack, void *ptr, region_t tiny_region, size_t known_size)
+free_tiny(rack_t *rack, void *ptr, region_t tiny_region, size_t known_size,
+		boolean_t partial_free)
 {
 	msize_t msize;
 	boolean_t is_free;
@@ -2099,7 +2397,7 @@
 
 #if CONFIG_TINY_CACHE
 	// Depot does not participate in CONFIG_TINY_CACHE since it can't be directly malloc()'d
-	if (DEPOT_MAGAZINE_INDEX != mag_index) {
+	if (DEPOT_MAGAZINE_INDEX != mag_index && !partial_free) {
 		if (msize < TINY_QUANTUM) {					  // to see if the bits fit in the last 4 bits
 			void *ptr2 = tiny_mag_ptr->mag_last_free; // Might be NULL
 			msize_t msize2 = tiny_mag_ptr->mag_last_free_msize;
@@ -2107,6 +2405,7 @@
 
 			/* check that we don't already have this pointer in the cache */
 			if (ptr == ptr2) {
+				SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
 				free_tiny_botch(rack, ptr);
 				return;
 			}
@@ -2146,7 +2445,8 @@
 		SZONE_MAGAZINE_PTR_LOCK(tiny_mag_ptr);
 	}
 
-	if (tiny_free_no_lock(rack, tiny_mag_ptr, mag_index, tiny_region, ptr, msize)) {
+	if (tiny_free_no_lock(rack, tiny_mag_ptr, mag_index, tiny_region, ptr,
+			msize, partial_free)) {
 		SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
 	}
 
@@ -2233,7 +2533,7 @@
 				if (is_free) {
 					break; // a double free; let the standard free deal with it
 				}
-				if (!tiny_free_no_lock(&szone->tiny_rack, tiny_mag_ptr, mag_index, tiny_region, ptr, msize)) {
+				if (!tiny_free_no_lock(&szone->tiny_rack, tiny_mag_ptr, mag_index, tiny_region, ptr, msize, false)) {
 					// Arrange to re-acquire magazine lock
 					tiny_mag_ptr = NULL;
 					tiny_region = NULL;
@@ -2255,50 +2555,83 @@
 
 
 void
-print_tiny_free_list(rack_t *rack)
+print_tiny_free_list(task_t task, memory_reader_t reader,
+		print_task_printer_t printer, rack_t *rack)
 {
 	tiny_free_list_t *ptr;
 	_SIMPLE_STRING b = _simple_salloc();
 	mag_index_t mag_index;
 
 	if (b) {
-		_simple_sappend(b, "tiny free sizes:\n");
-		for (mag_index = -1; mag_index < rack->num_magazines; mag_index++) {
+		rack_t *mapped_rack;
+		magazine_t *mapped_magazines;
+		if (reader(task, (vm_address_t)rack, sizeof(struct rack_s),
+				(void **)&mapped_rack)) {
+			printer("Failed to map tiny rack\n");
+			return;
+		}
+		_simple_sappend(b, "Tiny free sizes:\n");
+		if (reader(task, (vm_address_t)mapped_rack->magazines,
+				mapped_rack->num_magazines * sizeof(magazine_t),
+				(void **)&mapped_magazines)) {
+			printer("Failed to map tiny rack magazines\n");
+			return;
+		}
+
+		for (mag_index = -1; mag_index < mapped_rack->num_magazines; mag_index++) {
 			grain_t slot = 0;
-			_simple_sprintf(b, "\tMagazine %d: ", mag_index);
-			while (slot < NUM_TINY_SLOTS) {
-				ptr = rack->magazines[mag_index].mag_free_list[slot].p;
+			if (mag_index == -1) {
+				_simple_sprintf(b, "\tRecirc depot: ");
+			} else {
+				_simple_sprintf(b, "\tMagazine %d: ", mag_index);
+			}
+			while (slot <= NUM_TINY_SLOTS) {
+				ptr = mapped_magazines[mag_index].mag_free_list[slot].p;
 				if (ptr) {
-					_simple_sprintf(b, "%s%y[%d]; ", (slot == NUM_TINY_SLOTS - 1) ? ">=" : "", (slot + 1) * TINY_QUANTUM,
-									free_list_count(rack, (free_list_t){ .p = ptr }));
+					_simple_sprintf(b, "%s%y[%d]; ",
+							(slot == NUM_TINY_SLOTS) ? ">=" : "",
+							(slot + 1) * TINY_QUANTUM,
+							free_list_count(task, reader, printer, mapped_rack,
+									(free_list_t){ .p = ptr }));
 				}
 				slot++;
 			}
 			_simple_sappend(b, "\n");
 		}
-		malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
+		printer("%s\n", _simple_string(b));
 		_simple_sfree(b);
 	}
 }
 
 void
-print_tiny_region(boolean_t verbose, region_t region, size_t bytes_at_start, size_t bytes_at_end)
+print_tiny_region(task_t task, memory_reader_t reader,
+		print_task_printer_t printer, int level, region_t region,
+		size_t bytes_at_start, size_t bytes_at_end)
 {
 	unsigned counts[1024];
 	unsigned in_use = 0;
-	uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(region);
+	uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(region);
 	uintptr_t current = start + bytes_at_start;
-	uintptr_t limit = (uintptr_t)TINY_REGION_END(region) - bytes_at_end;
+	uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(region) - bytes_at_end;
+	uintptr_t mapped_start;
 	boolean_t is_free;
 	msize_t msize;
 	unsigned ci;
 	_SIMPLE_STRING b;
 	uintptr_t pgTot = 0;
 
+	if (reader(task, (vm_address_t)start, TINY_REGION_SIZE,
+            (void **)&mapped_start)) {
+        printer("Failed to map tiny region at %p\n", start);
+        return;
+    }
+    off_t start_offset = mapped_start - start;
+    region_t mapped_region = (region_t)mapped_start;
+
 	if (region == HASHRING_REGION_DEALLOCATED) {
 		if ((b = _simple_salloc()) != NULL) {
 			_simple_sprintf(b, "Tiny region [unknown address] was returned to the OS\n");
-			malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
+			printer("%s\n", _simple_string(b));
 			_simple_sfree(b);
 		}
 		return;
@@ -2306,7 +2639,7 @@
 
 	memset(counts, 0, sizeof(counts));
 	while (current < limit) {
-		msize = get_tiny_meta_header((void *)current, &is_free);
+		msize = get_tiny_meta_header_offset((void *)current, start_offset, &is_free);
 		if (is_free && !msize && (current == start)) {
 			// first block is all free
 			uintptr_t pgLo = round_page_quanta(start + sizeof(tiny_free_list_t) + sizeof(msize_t));
@@ -2318,13 +2651,13 @@
 			break;
 		}
 		if (!msize) {
-			malloc_report(ASL_LEVEL_ERR, "*** error with %p: msize=%d\n", (void *)current, (unsigned)msize);
+			printer("*** error with %p: msize=%d\n", (void *)current, (unsigned)msize);
 			break;
 		}
 		if (!is_free) {
 			// block in use
 			if (msize > NUM_TINY_SLOTS) {
-				malloc_report(ASL_LEVEL_ERR, "*** error at %p msize for in_use is %d\n", (void *)current, msize);
+				printer("*** error at %p msize for in_use is %d\n", (void *)current, msize);
 			}
 			if (msize < 1024) {
 				counts[msize]++;
@@ -2341,26 +2674,38 @@
 		current += TINY_BYTES_FOR_MSIZE(msize);
 	}
 	if ((b = _simple_salloc()) != NULL) {
-		_simple_sprintf(b, "Tiny region [%p-%p, %y] \t", (void *)start, TINY_REGION_END(region), (int)TINY_REGION_SIZE);
-		_simple_sprintf(b, "Magazine=%d \t", MAGAZINE_INDEX_FOR_TINY_REGION(region));
-		_simple_sprintf(b, "Allocations in use=%d \t Bytes in use=%ly \t", in_use, BYTES_USED_FOR_TINY_REGION(region));
+		mag_index_t mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(mapped_region);
+		_simple_sprintf(b, "Tiny region [%p-%p, %y] \t", (void *)start, TINY_REGION_HEAP_END(region), (int)TINY_REGION_SIZE);
+        if (mag_index == DEPOT_MAGAZINE_INDEX) {
+            _simple_sprintf(b, "Recirc depot \t");
+        } else {
+		    _simple_sprintf(b, "Magazine=%d \t", mag_index);
+        }
+		_simple_sprintf(b, "Allocations in use=%d \t Bytes in use=%ly (%d%%) \t",
+				in_use, BYTES_USED_FOR_TINY_REGION(mapped_region),
+				(int)(100.0F * BYTES_USED_FOR_TINY_REGION(mapped_region))/TINY_REGION_SIZE);
 		if (bytes_at_end || bytes_at_start) {
 			_simple_sprintf(b, "Untouched=%ly ", bytes_at_end + bytes_at_start);
 		}
-		if (DEPOT_MAGAZINE_INDEX == MAGAZINE_INDEX_FOR_TINY_REGION(region)) {
+		if (mag_index == DEPOT_MAGAZINE_INDEX) {
 			_simple_sprintf(b, "Advised MADV_FREE=%ly", pgTot);
 		} else {
 			_simple_sprintf(b, "Fragments subject to reclamation=%ly", pgTot);
-		}
-		if (verbose && in_use) {
+#if CONFIG_RECIRC_DEPOT
+			_simple_sprintf(b, tiny_region_below_recirc_threshold(mapped_region) ?
+					"\tEmpty enough to be moved to recirc depot" :
+					"\tNot empty enough to be moved to recirc depot");
+#endif // CONFIG_RECIRC_DEPOT
+		}
+		if (level >= MALLOC_VERBOSE_PRINT_LEVEL && in_use) {
 			_simple_sappend(b, "\n\tSizes in use: ");
 			for (ci = 0; ci < 1024; ci++) {
 				if (counts[ci]) {
-					_simple_sprintf(b, "%d[%d] ", TINY_BYTES_FOR_MSIZE(ci), counts[ci]);
+					_simple_sprintf(b, "%y[%d] ", TINY_BYTES_FOR_MSIZE(ci), counts[ci]);
 				}
 			}
 		}
-		malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
+		printer("%s\n", _simple_string(b));
 		_simple_sfree(b);
 	}
 }