Loading...
--- libmalloc/libmalloc-140.1.1/src/magazine_small.c
+++ libmalloc/libmalloc-166.251.2/src/magazine_small.c
@@ -23,6 +23,7 @@
 
 #include "internal.h"
 
+
 /*********************	SMALL FREE LIST UTILITIES	************************/
 
 #pragma mark meta header helpers
@@ -38,6 +39,15 @@
 }
 
 /*
+ * Mark a block as not free, preserving its size.
+ */
+static MALLOC_INLINE void
+small_meta_header_set_not_free(msize_t *meta_headers, msize_t index)
+{
+	meta_headers[index] &= ~SMALL_IS_FREE;
+}
+
+/*
  * Mark a block as in use.  Only the first quantum of a block is marked thusly,
  * the remainder are marked "middle".
  */
@@ -56,6 +66,25 @@
 	meta_headers[index] = 0;
 }
 
+static MALLOC_INLINE MALLOC_ALWAYS_INLINE
+mag_index_t
+small_mag_get_thread_index(void)
+{
+#if CONFIG_SMALL_USES_HYPER_SHIFT
+	if (os_likely(_os_cpu_number_override == -1)) {
+		return _os_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();
+	} else {
+		return _os_cpu_number_override;
+	}
+#endif // CONFIG_SMALL_USES_HYPER_SHIFT
+}
+
 #pragma mark in-place free list
 
 static MALLOC_INLINE void
@@ -69,8 +98,8 @@
 static MALLOC_INLINE free_list_t
 small_inplace_unchecksum_ptr(rack_t *rack, inplace_linkage_s *linkage)
 {
-	if (linkage->checksum != free_list_gen_checksum((uintptr_t)linkage->ptr ^ rack->cookie ^ (uintptr_t)rack)) {
-		free_list_checksum_botch(rack, linkage);
+	if (linkage->checksum != (uint8_t)free_list_gen_checksum((uintptr_t)linkage->ptr ^ rack->cookie ^ (uintptr_t)rack)) {
+		free_list_checksum_botch(rack, linkage, linkage->ptr);
 		__builtin_trap();
 	}
 
@@ -184,7 +213,7 @@
 	}
 
 #if DEBUG_MALLOC
-	malloc_printf("used all slots of OOB entries\n");
+	malloc_report(ASL_LEVEL_INFO, "used all slots of OOB entries\n");
 #endif
 	return NULL;
 }
@@ -198,7 +227,9 @@
 	// If this changes, then a linear search through the list may
 	// become an unsuitable choice.
 	for (int i=0; i < SMALL_OOB_COUNT; i++) {
-		if (small_oob_free_entry_get_ptr(&region->small_oob_free_entries[i]) == ptr) {
+		oob_free_entry_t oob = &region->small_oob_free_entries[i];
+		if (small_oob_free_entry_get_ptr(oob) == ptr &&
+				oob->ptr & SMALL_IS_OOB) {
 			return &region->small_oob_free_entries[i];
 		}
 	}
@@ -303,7 +334,7 @@
 }
 
 static MALLOC_INLINE void
-small_free_mark_unfree(rack_t *rack, free_list_t entry, msize_t msize)
+small_free_mark_middle(rack_t *rack, free_list_t entry, msize_t msize)
 {
 	// Marks both the start and end block of a free-list entry as "middle" (unfree).
 	void *ptr = small_free_list_get_ptr(rack, entry);
@@ -311,9 +342,24 @@
 	uintptr_t start_index = SMALL_META_INDEX_FOR_PTR(ptr);
 	uintptr_t end_index = SMALL_META_INDEX_FOR_PTR(ptr + SMALL_BYTES_FOR_MSIZE(msize) - 1);
 	MALLOC_ASSERT(start_index <= end_index);
+	MALLOC_ASSERT((meta_headers[start_index] & ~SMALL_IS_FREE) == msize);
 
 	small_meta_header_set_middle(meta_headers, start_index);
 	small_meta_header_set_middle(meta_headers, end_index);
+}
+
+static MALLOC_INLINE void
+small_free_mark_unfree(rack_t *rack, free_list_t entry, msize_t msize)
+{
+	// Marks both the start and end block of a free-list entry as not free.
+	void *ptr = small_free_list_get_ptr(rack, entry);
+	msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
+	uintptr_t start_index = SMALL_META_INDEX_FOR_PTR(ptr);
+	uintptr_t end_index = SMALL_META_INDEX_FOR_PTR(ptr + SMALL_BYTES_FOR_MSIZE(msize) - 1);
+	MALLOC_ASSERT(start_index <= end_index);
+
+	small_meta_header_set_not_free(meta_headers, start_index);
+	small_meta_header_set_not_free(meta_headers, end_index);
 }
 
 static MALLOC_INLINE unsigned int
@@ -344,10 +390,10 @@
 
 #if DEBUG_MALLOC
 	if (LOG(szone, ptr)) {
-		malloc_printf("in %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
+		malloc_report(ASL_LEVEL_INFO, "in %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
 	}
 	if (((uintptr_t)ptr) & (SMALL_QUANTUM - 1)) {
-		szone_error(rack->debug_flags, 1, "small_free_list_add_ptr: Unaligned ptr", ptr, NULL);
+		malloc_zone_error(rack->debug_flags, true, "small_free_list_add_ptr: Unaligned ptr %p\n", ptr);
 	}
 #endif
 
@@ -361,11 +407,11 @@
 	if (small_free_list_get_ptr(rack, free_head)) {
 #if DEBUG_MALLOC
 		if (small_free_list_get_previous(szone, free_head)) {
-			szone_error(rack->debug_flags, 1, "small_free_list_add_ptr: Internal invariant broken (free_head->previous != NULL)", ptr,
+			malloc_zone_error(rack->debug_flags, true, "small_free_list_add_ptr: Internal invariant broken (free_head->previous != NULL)\n"
 						"ptr=%p slot=%d free_head=%p previous=%p\n", ptr, slot, (void *)free_head, free_head->previous.p);
 		}
 		if (!SMALL_PTR_IS_FREE(small_free_list_get_ptr(szone, free_head))) {
-			szone_error(rack->debug_flags, 1, "small_free_list_add_ptr: Internal invariant broken (free_head is not a free pointer)", ptr,
+			malloc_zone_error(rack->debug_flags, true, "small_free_list_add_ptr: Internal invariant broken (free_head is not a free pointer)\n"
 						"ptr=%p slot=%d free_head=%p\n", ptr, slot, (void *)small_free_list_get_ptr(szone, free_head));
 		}
 #endif
@@ -390,7 +436,7 @@
 
 #if DEBUG_MALLOC
 	if (LOG(szone, ptr)) {
-		malloc_printf("In %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
+		malloc_report(ASL_LEVEL_INFO, "In %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
 	}
 #endif
 
@@ -401,10 +447,10 @@
 		// The block to remove is the head of the free list
 #if DEBUG_MALLOC
 		if (small_mag_ptr->mag_free_list[slot] != ptr) {
-			szone_error(rack->debug_flags, 1,
-						"small_free_list_remove_ptr: Internal invariant broken (small_mag_ptr->mag_free_list[slot])", ptr,
-						"ptr=%p slot=%d msize=%d small_mag_ptr->mag_free_list[slot]=%p\n", ptr, slot, msize,
-						(void *)small_mag_ptr->mag_free_list[slot]);
+			malloc_zone_error(rack->debug_flags, true,
+					"small_free_list_remove_ptr_no_clear: Internal invariant broken (small_mag_ptr->mag_free_list[slot])\n"
+					"ptr=%p slot=%d msize=%d small_mag_ptr->mag_free_list[slot]=%p\n", ptr, slot, msize,
+					(void *)small_mag_ptr->mag_free_list[slot]);
 			return;
 		}
 #endif
@@ -413,10 +459,26 @@
 			BITMAPN_CLR(small_mag_ptr->mag_bitmap, slot);
 		}
 	} else {
+		// Check that the next pointer of "previous" points to "entry".
+		free_list_t prev_next = small_free_list_get_next(rack, previous);
+		if (small_free_list_get_ptr(rack, prev_next) != small_free_list_get_ptr(rack, entry)) {
+			malloc_zone_error(rack->debug_flags, true,
+					"small_free_list_remove_ptr_no_clear: Internal invariant broken (next ptr of prev) for %p, prev_next=%p\n",
+					small_free_list_get_ptr(rack, entry), small_free_list_get_ptr(rack, prev_next));
+			__builtin_unreachable(); // Always crashes in malloc_zone_error().
+		}
 		small_free_list_set_next(rack, previous, next);
 	}
 
 	if (small_free_list_get_ptr(rack, next)) {
+		// Check that the previous pointer of "next" points to "entry".
+		free_list_t next_prev = small_free_list_get_previous(rack, next);
+		if (small_free_list_get_ptr(rack, next_prev) != small_free_list_get_ptr(rack, entry)) {
+			malloc_zone_error(rack->debug_flags, true,
+					"small_free_list_remove_ptr_no_clear: Internal invariant broken (prev ptr of next) for %p, next_prev=%p\n",
+					small_free_list_get_ptr(rack, entry), small_free_list_get_ptr(rack, next_prev));
+			__builtin_unreachable(); // Always crashes in malloc_zone_error().
+		}
 		small_free_list_set_previous(rack, next, previous);
 	}
 
@@ -433,7 +495,7 @@
 	// from/to the recirc depot we rely on the metadata bits being intact to
 	// reconstruct the free list. In that case we have to be able to skip this
 	// metadata manipulation.
-	small_free_mark_unfree(rack, entry, msize);
+	small_free_mark_middle(rack, entry, msize);
 	small_free_list_remove_ptr_no_clear(rack, small_mag_ptr, entry, msize);
 }
 
@@ -463,8 +525,9 @@
 		return (free_list_t){ .p = ptr };
 	}
 
-	szone_error(rack->debug_flags, 1, "small_free_list_find_by_ptr: ptr is not free (ptr metadata !SMALL_IS_FREE)", ptr,
-				"ptr=%p msize=%d metadata=0x%x", ptr, msize, *SMALL_METADATA_FOR_PTR(ptr));
+	malloc_zone_error(rack->debug_flags, true,
+			"small_free_list_find_by_ptr: ptr is not free (ptr metadata !SMALL_IS_FREE), "
+			"ptr=%p msize=%d metadata=0x%x\n", ptr, msize, *SMALL_METADATA_FOR_PTR(ptr));
 	__builtin_trap();
 }
 
@@ -557,7 +620,7 @@
 		if (!msize) {
 #if DEBUG_MALLOC
 			boolean_t is_free = msize_and_free & SMALL_IS_FREE;
-			malloc_printf("*** small_free_detach_region error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
+			malloc_report(ASL_LEVEL_ERR, "*** small_free_detach_region error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
 #endif
 			break;
 		}
@@ -591,7 +654,7 @@
 
 		if (!msize) {
 #if DEBUG_MALLOC
-			malloc_printf("*** small_free_reattach_region error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
+			malloc_report(ASL_LEVEL_ERR, "*** small_free_reattach_region error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
 #endif
 			break;
 		}
@@ -631,7 +694,7 @@
 		if (is_free && !msize && (current == start)) {
 #if DEBUG_MALLOC
 			// first block is all free
-			malloc_printf("*** small_free_scan_madvise_free first block is all free! %p: msize=%d is_free =%d\n", (void *)current,
+			malloc_report(ASL_LEVEL_ERR, "*** small_free_scan_madvise_free first block is all free! %p: msize=%d is_free=%d\n", (void *)current,
 						  msize, is_free);
 #endif
 			uintptr_t pgLo = round_page_kernel(start + sizeof(free_list_t) + sizeof(msize_t));
@@ -646,8 +709,8 @@
 		}
 		if (!msize) {
 #if DEBUG_MALLOC
-			malloc_printf(
-						  "*** small_free_scan_madvise_free error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
+			malloc_report(ASL_LEVEL_ERR,
+						  "*** small_free_scan_madvise_free error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
 #endif
 			break;
 		}
@@ -673,7 +736,7 @@
 			uintptr_t addr = (advisory[i].pnum << vm_page_quanta_shift) + start;
 			size_t size = advisory[i].size << vm_page_quanta_shift;
 
-			mvm_madvise_free(rack, r, addr, addr + size, NULL);
+			mvm_madvise_free(rack, r, addr, addr + size, NULL, rack->debug_flags & MALLOC_DO_SCRIBBLE);
 		}
 		SZONE_MAGAZINE_PTR_LOCK(depot_ptr);
 		OSAtomicDecrement32Barrier(&(REGION_TRAILER_FOR_SMALL_REGION(r)->pinned_to_depot));
@@ -701,34 +764,27 @@
 
 	// Mask off the bits representing slots holding free blocks smaller than
 	// the size we need.
-	if (rack->debug_flags & MALLOC_EXTENDED_SMALL_SLOTS) {
-		// BITMAPN_CTZ implementation
-		unsigned idx = slot >> 5;
-		bitmap = 0;
-		unsigned mask = ~((1 << (slot & 31)) - 1);
-		for (; idx < SMALL_BITMAP_WORDS; ++idx) {
-			bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
-			if (bitmap != 0) {
-				break;
-			}
-			mask = ~0U;
-		}
-		// Check for fallthrough: No bits set in bitmap
-		if ((bitmap == 0) && (idx == SMALL_BITMAP_WORDS)) {
-			return NULL;
-		}
-
-		// Start looking at the first set bit, plus 32 bits for every word of
-		// zeroes or entries that were too small.
-		slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
-	} else {
-		bitmap = small_mag_ptr->mag_bitmap[0] & ~((1 << slot) - 1);
-		if (!bitmap) {
-			return NULL;
-		}
-
-		slot = BITMAP32_CTZ((&bitmap));
-	}
+	//
+	// BITMAPN_CTZ implementation
+	unsigned idx = slot >> 5;
+	bitmap = 0;
+	unsigned mask = ~((1 << (slot & 31)) - 1);
+	for (; idx < SMALL_FREELIST_BITMAP_WORDS(rack); ++idx) {
+		bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
+		if (bitmap != 0) {
+			break;
+		}
+		mask = ~0U;
+	}
+	// Check for fallthrough: No bits set in bitmap
+	if ((bitmap == 0) && (idx == SMALL_FREELIST_BITMAP_WORDS(rack))) {
+		return NULL;
+	}
+
+	// Start looking at the first set bit, plus 32 bits for every word of
+	// zeroes or entries that were too small.
+	slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
+
 	limit = free_list + SMALL_FREE_SLOT_COUNT(rack) - 1;
 	free_list += slot;
 
@@ -739,7 +795,7 @@
 		} else {
 			/* Shouldn't happen. Fall through to look at last slot. */
 #if DEBUG_MALLOC
-			malloc_printf("in small_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
+			malloc_report(ASL_LEVEL_ERR, "in small_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
 #endif
 		}
 	}
@@ -766,7 +822,7 @@
 
 #if DEBUG_MALLOC
 	if (DEPOT_MAGAZINE_INDEX == mag_index) {
-		szone_error(rack->debug_flags, 1, "small_get_region_from_depot called for magazine index -1", NULL, NULL);
+		malloc_zone_error(rack->debug_flags, true, "small_get_region_from_depot called for magazine index -1\n", NULL, NULL);
 		return 0;
 	}
 #endif
@@ -820,15 +876,94 @@
 
 	SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
 
-	// madvise() outside the Depot lock
-	(void)mvm_madvise_reuse(sparse_region, (uintptr_t)sparse_region,
-			(uintptr_t)sparse_region + SMALL_REGION_PAYLOAD_BYTES, rack->debug_flags);
-
 	MAGMALLOC_DEPOTREGION(SMALL_SZONE_FROM_RACK(rack), (int)mag_index, (void *)sparse_region, SMALL_REGION_SIZE,
 						  (int)BYTES_USED_FOR_SMALL_REGION(sparse_region)); // DTrace USDT Probe
 
 	return 1;
 }
+
+#if CONFIG_MADVISE_PRESSURE_RELIEF
+void
+small_madvise_pressure_relief(rack_t *rack)
+{
+	mag_index_t mag_index;
+	magazine_t *small_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) {
+			SZONE_LOCK(SMALL_SZONE_FROM_RACK(rack));
+
+			region_t small = rack->region_generation->hashed_regions[index];
+			if (!small || small == HASHRING_REGION_DEALLOCATED) {
+				SZONE_UNLOCK(SMALL_SZONE_FROM_RACK(rack));
+				continue;
+			}
+
+			magazine_t *mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
+					REGION_TRAILER_FOR_SMALL_REGION(small),
+					MAGAZINE_INDEX_FOR_SMALL_REGION(small));
+			SZONE_UNLOCK(SMALL_SZONE_FROM_RACK(rack));
+
+			/* 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_SMALL_REGION(small);
+
+			/* 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 (small == mag_ptr->mag_last_region && (mag_ptr->mag_bytes_free_at_end || mag_ptr->mag_bytes_free_at_start)) {
+				small_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_SMALL_REGION(small));
+			int objects_in_use = small_free_detach_region(rack, mag_ptr, small);
+
+			SZONE_MAGAZINE_PTR_LOCK(small_depot_ptr);
+			MAGAZINE_INDEX_FOR_SMALL_REGION(small) = DEPOT_MAGAZINE_INDEX;
+			REGION_TRAILER_FOR_SMALL_REGION(small)->pinned_to_depot = 0;
+
+			size_t bytes_inplay = small_free_reattach_region(rack, small_depot_ptr, small);
+
+			/* 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 -= SMALL_REGION_PAYLOAD_BYTES;
+			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);
+
+			small_depot_ptr->mag_num_bytes_in_objects += bytes_inplay;
+			small_depot_ptr->num_bytes_in_magazine += SMALL_REGION_PAYLOAD_BYTES;
+			small_depot_ptr->mag_num_objects -= objects_in_use;
+
+			recirc_list_splice_last(rack, small_depot_ptr, REGION_TRAILER_FOR_SMALL_REGION(small));
+
+			/* Actually do the scan, done holding the depot lock, the call will drop the lock
+			 * around the actual madvise syscalls.
+			 */
+			small_free_scan_madvise_free(rack, small_depot_ptr, small);
+
+			/* 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(small_depot_ptr);
+		}
+	}
+}
+#endif // CONFIG_MADVISE_PRESSURE_RELIEF
 
 #if CONFIG_AGGRESSIVE_MADVISE || CONFIG_RECIRC_DEPOT
 static MALLOC_INLINE void
@@ -872,10 +1007,16 @@
 		uintptr_t free_hi = MIN(trunc_extent, hi);
 
 		if (free_lo < free_hi) {
-			small_free_list_remove_ptr(rack, small_mag_ptr, freee, fmsize);
+			// Before unlocking, ensure that the metadata for the freed region
+			// makes it look not free but includes the length. This ensures that
+			// any code that inspects the metadata while we are unlocked sees
+			// a valid state and will not try to use or coalesce freed memory
+			// into it.
+			small_free_mark_unfree(rack, freee, fmsize);
+			small_free_list_remove_ptr_no_clear(rack, small_mag_ptr, freee, fmsize);
 			OSAtomicIncrement32Barrier(&(node->pinned_to_depot));
 			SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
-			mvm_madvise_free(rack, region, free_lo, free_hi, &rack->last_madvise);
+			mvm_madvise_free(rack, region, free_lo, free_hi, &rack->last_madvise, rack->debug_flags & MALLOC_DO_SCRIBBLE);
 			SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
 			OSAtomicDecrement32Barrier(&(node->pinned_to_depot));
 			small_free_list_add_ptr(rack, small_mag_ptr, ptr, fmsize);
@@ -888,7 +1029,7 @@
 static region_t
 small_free_try_depot_unmap_no_lock(rack_t *rack, magazine_t *depot_ptr, region_trailer_t *node)
 {
-	if (0 < node->bytes_used || 0 < node->pinned_to_depot || depot_ptr->recirculation_entries < (rack->num_magazines * 2)) {
+	if (0 < node->bytes_used || 0 < node->pinned_to_depot || depot_ptr->recirculation_entries < recirc_retained_regions) {
 		return NULL;
 	}
 
@@ -907,7 +1048,7 @@
 													rack->region_generation->num_regions_allocated_shift,
 													sparse_region);
 		if (NULL == pSlot) {
-			szone_error(rack->debug_flags, 1, "small_free_try_depot_unmap_no_lock hash lookup failed:", NULL, "%p\n", sparse_region);
+			malloc_zone_error(rack->debug_flags, true, "small_free_try_depot_unmap_no_lock hash lookup failed: %p\n", sparse_region);
 			return NULL;
 		}
 		*pSlot = HASHRING_REGION_DEALLOCATED;
@@ -924,7 +1065,7 @@
 		return sparse_region;
 
 	} else {
-		szone_error(rack->debug_flags, 1, "small_free_try_depot_unmap_no_lock objects_in_use not zero:", NULL, "%d\n", objects_in_use);
+		malloc_zone_error(rack->debug_flags, true, "small_free_try_depot_unmap_no_lock objects_in_use not zero: %d\n", objects_in_use);
 		return NULL;
 	}
 }
@@ -937,13 +1078,13 @@
 	// is at least fraction "f" empty.) Such a region will be marked "suitable" on the recirculation list.
 	region_trailer_t *node = small_mag_ptr->firstNode;
 
-	while (node && !node->recirc_suitable) {
+	while (node && (!node->recirc_suitable || node->pinned_to_depot)) {
 		node = node->next;
 	}
 
 	if (NULL == node) {
 #if DEBUG_MALLOC
-		malloc_printf("*** small_free_do_recirc_to_depot end of list\n");
+		malloc_report(ASL_LEVEL_ERR, "*** small_free_do_recirc_to_depot end of list\n");
 #endif
 		return TRUE; // Caller must SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 	}
@@ -1084,11 +1225,11 @@
 
 #if DEBUG_MALLOC
 	if (LOG(szone, ptr)) {
-		malloc_printf("in small_free_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
+		malloc_report(ASL_LEVEL_ERR, "in small_free_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
 	}
 	if (!msize) {
-		szone_error(rack->debug_flags, 1, "trying to free small block that is too small", ptr, "in small_free_no_lock(), ptr=%p, msize=%d\n",
-					ptr, msize);
+		malloc_zone_error(rack->debug_flags, true, "trying to free small block that is too small in small_free_no_lock(), ptr=%p, msize=%d\n",
+				ptr, msize);
 	}
 #endif
 
@@ -1097,12 +1238,13 @@
 		msize_t previous_msize = meta_headers[index - 1] & ~SMALL_IS_FREE;
 		grain_t previous_index = index - previous_msize;
 
-		// Check if the metadata for the start of the region is also free.
+		// Check if the metadata for the start of the block is also free.
 		if (meta_headers[previous_index] == (previous_msize | SMALL_IS_FREE)) {
 			void *previous_ptr = (void *)((uintptr_t)ptr - SMALL_BYTES_FOR_MSIZE(previous_msize));
 			free_list_t previous = small_free_list_find_by_ptr(rack, small_mag_ptr, previous_ptr, previous_msize);
 			small_free_list_remove_ptr(rack, small_mag_ptr, previous, previous_msize);
 			ptr = previous_ptr;
+			small_meta_header_set_middle(meta_headers, index); // This block is now a middle block.
 			msize += previous_msize;
 			index -= previous_msize;
 		} else {
@@ -1121,7 +1263,7 @@
 
 	if (rack->debug_flags & MALLOC_DO_SCRIBBLE) {
 		if (!msize) {
-			szone_error(rack->debug_flags, 1, "incorrect size information - block header was damaged", ptr, NULL);
+			malloc_zone_error(rack->debug_flags, true, "incorrect size information for %p - block header was damaged\n", ptr);
 		} else {
 			memset(ptr, SCRABBLE_BYTE, SMALL_BYTES_FOR_MSIZE(msize));
 		}
@@ -1186,7 +1328,7 @@
 		offset_msize = strtol(getenv("MallocASLRForce"), NULL, 0) & SMALL_ENTROPY_MASK;
 	}
 	if (getenv("MallocASLRPrint")) {
-		malloc_printf("Region: %p offset: %d\n", aligned_address, offset_msize);
+		malloc_report(ASL_LEVEL_INFO, "Region: %p offset: %d\n", aligned_address, offset_msize);
 	}
 #endif
 #else
@@ -1221,14 +1363,8 @@
 void *
 small_memalign(szone_t *szone, size_t alignment, size_t size, size_t span)
 {
-	if (size <= SMALL_THRESHOLD) {
-		// ensure block allocated by small does not have a tiny-possible size
-		size = SMALL_THRESHOLD + TINY_QUANTUM;
-		span = size + alignment - 1;
-	}
-
 	msize_t mspan = SMALL_MSIZE_FOR_BYTES(span + SMALL_QUANTUM - 1);
-	void *p = szone_malloc(szone, span); // avoid inlining small_malloc_should_clear(szone, mspan, 0);
+	void *p = small_malloc_should_clear(&szone->small_rack, mspan, 0);
 
 	if (NULL == p) {
 		return NULL;
@@ -1254,8 +1390,7 @@
 		SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 
 		// Give up mpad blocks beginning at p to the small free list
-		// region_t r = SMALL_REGION_FOR_PTR(p);
-		szone_free(szone, p); // avoid inlining free_small(szone, p, &r);
+		free_small(&szone->small_rack, p, SMALL_REGION_FOR_PTR(p), SMALL_BYTES_FOR_MSIZE(mpad));
 
 		p = q; // advance p to the desired alignment
 	}
@@ -1271,11 +1406,17 @@
 		SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 
 		// Give up mwaste blocks beginning at q to the small free list
-		// region_t r = SMALL_REGION_FOR_PTR(q);
-		szone_free(szone, q); // avoid inlining free_small(szone, q, &r);
+		free_small(&szone->small_rack, q, SMALL_REGION_FOR_PTR(q), SMALL_BYTES_FOR_MSIZE(mwaste));
 	}
 
 	return p; // p has the desired size and alignment, and can later be free()'d
+}
+
+boolean_t
+small_claimed_address(rack_t *rack, void *ptr)
+{
+	region_t r = small_region_for_ptr_no_lock(rack, ptr);
+	return r && ptr < (void *)SMALL_REGION_END(r);
 }
 
 void *
@@ -1328,10 +1469,10 @@
 
 #if DEBUG_MALLOC
 	if ((uintptr_t)next_block & (SMALL_QUANTUM - 1)) {
-		szone_error(rack->debug_flags, 1, "internal invariant broken in realloc(next_block)", next_block, NULL);
+		malloc_zone_error(rack->debug_flags, true, "internal invariant broken in realloc(next_block) for %p\n", next_block);
 	}
 	if (meta_headers[index] != old_msize) {
-		malloc_printf("*** small_try_realloc_in_place incorrect old %d %d\n", meta_headers[index], old_msize);
+		malloc_report(ASL_LEVEL_ERR, "*** small_try_realloc_in_place incorrect old %d %d\n", meta_headers[index], old_msize);
 	}
 #endif
 
@@ -1343,44 +1484,90 @@
 		return 0;
 	}
 
-	/*
-	 * Look for a free block immediately afterwards.  If it's large enough, we can consume (part of)
-	 * it.
-	 */
-	next_msize_and_free = meta_headers[next_index];
-	is_free = next_msize_and_free & SMALL_IS_FREE;
-	if (!is_free) {
-		SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
-		return 0; // next_block is in use;
-	}
-
-	next_msize = next_msize_and_free & ~SMALL_IS_FREE;
-	if (old_msize + next_msize < new_msize) {
-		SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
-		return 0; // even with next block, not enough
-	}
-
-	// The following block is big enough; pull it from its freelist and chop off enough to satisfy
-	// our needs.
-	free_list_t freee = small_free_list_find_by_ptr(rack, small_mag_ptr, next_block, next_msize);
-	small_free_list_remove_ptr(rack, small_mag_ptr, freee, next_msize);
-	small_meta_header_set_middle(meta_headers, next_index);
-	leftover_msize = old_msize + next_msize - new_msize;
-	if (leftover_msize) {
-		/* there's some left, so put the remainder back */
-		leftover = (unsigned char *)ptr + SMALL_BYTES_FOR_MSIZE(new_msize);
-
-		small_free_list_add_ptr(rack, small_mag_ptr, leftover, leftover_msize);
-	}
+	msize_t coalesced_msize = new_msize - old_msize;
+#if CONFIG_SMALL_CACHE
+	void *last_free_ptr = small_mag_ptr->mag_last_free;
+	msize_t last_free_msize = small_mag_ptr->mag_last_free_msize;
+	if (last_free_ptr == next_block && old_msize + last_free_msize >= new_msize) {
+		/*
+		 * There is a block in mag_last_free and it's immediately after
+		 * this block and it's large enough. We can use some or all of it.
+		 */
+		leftover_msize = last_free_msize - coalesced_msize;
+		if (leftover_msize) {
+			small_mag_ptr->mag_last_free_msize -= coalesced_msize;
+			small_mag_ptr->mag_last_free += new_size - old_size;
+			// The block in mag_last_free is still marked as header and in-use, so copy that
+			// state to the block that remains. The state for the block that we're going to
+			// use is adjusted by the small_meta_header_set_middle() call below.
+			small_meta_header_set_in_use(meta_headers, index + new_msize, leftover_msize);
+		} else {
+			// Using the whole block.
+			small_mag_ptr->mag_last_free = NULL;
+			small_mag_ptr->mag_last_free_msize = 0;
+			small_mag_ptr->mag_last_free_rgn = NULL;
+		}
+		small_meta_header_set_in_use(meta_headers, index, new_msize);
+		small_meta_header_set_middle(meta_headers, next_index);
+	} else {
+#endif // CONFIG_SMALL_CACHE
+		/*
+		 * Try to expand into unused space immediately after this block.
+		 */
+		msize_t unused_msize = SMALL_MSIZE_FOR_BYTES(small_mag_ptr->mag_bytes_free_at_end);
+		void *unused_start = SMALL_REGION_END(SMALL_REGION_FOR_PTR(ptr)) - small_mag_ptr->mag_bytes_free_at_end;
+		if (small_mag_ptr->mag_last_region == SMALL_REGION_FOR_PTR(ptr)
+				&& coalesced_msize < unused_msize && unused_start == ptr + old_size) {
+			// Extend the in-use for this block to the new size
+			small_meta_header_set_in_use(meta_headers, index, new_msize);
+
+			// Clear the in-use size for the start of the area we extended into
+			small_meta_header_set_middle(meta_headers, next_index);
+
+			// Reduce mag_bytes_free_at_end and update its in-use size.
+			small_mag_ptr->mag_bytes_free_at_end -= SMALL_BYTES_FOR_MSIZE(coalesced_msize);
+			small_meta_header_set_in_use(meta_headers, index + new_msize, SMALL_MSIZE_FOR_BYTES(small_mag_ptr->mag_bytes_free_at_end));
+		} else {
+			/*
+			 * Look for a free block immediately afterwards.  If it's large enough, we can consume (part of)
+			 * it.
+			 */
+			next_msize_and_free = meta_headers[next_index];
+			is_free = next_msize_and_free & SMALL_IS_FREE;
+			if (!is_free) {
+				SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
+				return 0; // next_block is in use;
+			}
+
+			next_msize = next_msize_and_free & ~SMALL_IS_FREE;
+			if (old_msize + next_msize < new_msize) {
+				SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
+				return 0; // even with next block, not enough
+			}
+
+			// The following block is big enough; pull it from its freelist and chop off enough to satisfy
+			// our needs.
+			free_list_t freee = small_free_list_find_by_ptr(rack, small_mag_ptr, next_block, next_msize);
+			small_free_list_remove_ptr(rack, small_mag_ptr, freee, next_msize);
+			small_meta_header_set_middle(meta_headers, next_index);
+			leftover_msize = old_msize + next_msize - new_msize;
+			if (leftover_msize) {
+				/* there's some left, so put the remainder back */
+				leftover = (unsigned char *)ptr + SMALL_BYTES_FOR_MSIZE(new_msize);
+				small_free_list_add_ptr(rack, small_mag_ptr, leftover, leftover_msize);
+			}
+			small_meta_header_set_in_use(meta_headers, index, new_msize);
+		}
+#if CONFIG_SMALL_CACHE
+	}
+#endif // CONFIG_SMALL_CACHE
 #if DEBUG_MALLOC
 	if (SMALL_BYTES_FOR_MSIZE(new_msize) > szone->large_threshold) {
-		malloc_printf("*** realloc in place for %p exceeded msize=%d\n", new_msize);
-	}
-#endif
-	small_meta_header_set_in_use(meta_headers, index, new_msize);
-#if DEBUG_MALLOC
+		malloc_report(ASL_LEVEL_ERR, "*** realloc in place for %p exceeded msize=%d\n", new_msize);
+	}
+
 	if (LOG(szone, ptr)) {
-		malloc_printf("in small_try_realloc_in_place(), ptr=%p, msize=%d\n", ptr, *SMALL_METADATA_FOR_PTR(ptr));
+		malloc_report(ASL_LEVEL_INFO, "in small_try_realloc_in_place(), ptr=%p, msize=%d\n", ptr, *SMALL_METADATA_FOR_PTR(ptr));
 	}
 #endif
 	small_mag_ptr->mag_num_bytes_in_objects += SMALL_BYTES_FOR_MSIZE(new_msize - old_msize);
@@ -1405,8 +1592,15 @@
 	return 1;
 }
 
+static char *small_check_fail_msg = "check: incorrect small region ";
+
+#define SMALL_CHECK_FAIL(fmt, ...) \
+	malloc_zone_check_fail(small_check_fail_msg, \
+			"%ld, counter=%d\n" fmt,  region_index, counter, __VA_ARGS__);
+
 boolean_t
-small_check_region(rack_t *rack, region_t region)
+small_check_region(rack_t *rack, region_t region, size_t region_index,
+		unsigned counter)
 {
 	unsigned char *ptr = SMALL_REGION_ADDRESS(region);
 	msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
@@ -1435,13 +1629,13 @@
 			// block is in use
 			msize = msize_and_free;
 			if (!msize) {
-				malloc_printf("*** invariant broken: null msize ptr=%p num_small_regions=%d end=%p\n", ptr,
-							  rack->num_regions, region_end);
+				SMALL_CHECK_FAIL("*** invariant broken: null msize ptr=%p num_small_regions=%d end=%p\n", ptr,
+							  (int)rack->num_regions, region_end);
 				return 0;
 			}
 #if !CONFIG_RELAXED_INVARIANT_CHECKS
 			if (SMALL_BYTES_FOR_MSIZE(msize) > szone->large_threshold) {
-				malloc_printf("*** invariant broken for %p this small msize=%d - size is too large\n", ptr, msize_and_free);
+				SMALL_CHECK_FAIL("*** invariant broken for %p this small msize=%d - size is too large\n", ptr, msize_and_free);
 				return 0;
 			}
 #endif // CONFIG_RELAXED_INVARIANT_CHECKS
@@ -1453,12 +1647,12 @@
 			free_head = (free_list_t){ .p = ptr };
 			follower = (msize_t *)FOLLOWING_SMALL_PTR(ptr, msize);
 			if (!msize) {
-				malloc_printf("*** invariant broken for free block %p this msize=%d\n", ptr, msize);
+				SMALL_CHECK_FAIL("*** invariant broken for free block %p this msize=%d\n", ptr, msize);
 				return 0;
 			}
 #if !CONFIG_RELAXED_INVARIANT_CHECKS
 			if (prev_free) {
-				malloc_printf("*** invariant broken for %p (2 free in a row)\n", ptr);
+				SMALL_CHECK_FAIL("*** invariant broken for %p (2 free in a row)\n", ptr);
 				return 0;
 			}
 #endif
@@ -1474,18 +1668,17 @@
 			previous = small_free_list_get_previous(rack, free_head);
 			next = small_free_list_get_next(rack, free_head);
 			if (previous.p && !SMALL_PTR_IS_FREE(small_free_list_get_ptr(rack, previous))) {
-				malloc_printf("*** invariant broken for %p (previous %p is not a free pointer)\n", ptr, small_free_list_get_ptr(rack, previous));
+				SMALL_CHECK_FAIL("*** invariant broken for %p (previous %p is not a free pointer)\n", ptr, small_free_list_get_ptr(rack, previous));
 				return 0;
 			}
 			if (next.p && !SMALL_PTR_IS_FREE(small_free_list_get_ptr(rack, next))) {
-				malloc_printf("*** invariant broken for %p (next %p is not a free pointer)\n", ptr, small_free_list_get_ptr(rack, next));
+				SMALL_CHECK_FAIL("*** invariant broken for %p (next %p is not a free pointer)\n", ptr, small_free_list_get_ptr(rack, next));
 				return 0;
 			}
 			if (SMALL_PREVIOUS_MSIZE(follower) != msize) {
-				malloc_printf(
-							  "*** invariant broken for small free %p followed by %p in region [%p-%p] "
-							  "(end marker incorrect) should be %d; in fact %d\n",
-							  ptr, follower, SMALL_REGION_ADDRESS(region), region_end, msize, SMALL_PREVIOUS_MSIZE(follower));
+				SMALL_CHECK_FAIL("*** invariant broken for small free %p followed by %p in region [%p-%p] "
+						"(end marker incorrect) should be %d; in fact %d\n",
+						ptr, follower, SMALL_REGION_ADDRESS(region), region_end, msize, SMALL_PREVIOUS_MSIZE(follower));
 				return 0;
 			}
 			ptr = (unsigned char *)follower;
@@ -1558,8 +1751,7 @@
 				recorder(task, context, MALLOC_PTR_REGION_RANGE_TYPE, &ptr_range, 1);
 			}
 			if (type_mask & MALLOC_PTR_IN_USE_RANGE_TYPE) {
-				void *mag_last_free;
-				vm_address_t mag_last_free_ptr = 0;
+				vm_address_t mag_last_free = 0;
 				msize_t mag_last_free_msize = 0;
 
 				err = reader(task, range.address, range.size, (void **)&mapped_region);
@@ -1571,19 +1763,13 @@
 				magazine_t *small_mag_ptr = small_mag_base + mag_index;
 
 				if (DEPOT_MAGAZINE_INDEX != mag_index) {
-					mag_last_free = small_mag_ptr->mag_last_free;
-					if (mag_last_free) {
-						mag_last_free_ptr = (uintptr_t)mag_last_free & ~(SMALL_QUANTUM - 1);
-						mag_last_free_msize = (uintptr_t)mag_last_free & (SMALL_QUANTUM - 1);
-					}
+					mag_last_free = (uintptr_t)small_mag_ptr->mag_last_free;
+					mag_last_free_msize = small_mag_ptr->mag_last_free_msize;
 				} else {
 					for (mag_index = 0; mag_index < szone->small_rack.num_magazines; mag_index++) {
 						if ((void *)range.address == (small_mag_base + mag_index)->mag_last_free_rgn) {
-							mag_last_free = (small_mag_base + mag_index)->mag_last_free;
-							if (mag_last_free) {
-								mag_last_free_ptr = (uintptr_t)mag_last_free & ~(SMALL_QUANTUM - 1);
-								mag_last_free_msize = (uintptr_t)mag_last_free & (SMALL_QUANTUM - 1);
-							}
+							mag_last_free = (uintptr_t)(small_mag_base + mag_index)->mag_last_free;
+							mag_last_free_msize = (small_mag_base + mag_index)->mag_last_free_msize;
 						}
 					}
 				}
@@ -1599,7 +1785,7 @@
 					msize_and_free = block_header[block_index];
 					msize = msize_and_free & ~SMALL_IS_FREE;
 					if (!(msize_and_free & SMALL_IS_FREE) &&
-						range.address + SMALL_BYTES_FOR_MSIZE(block_index) != mag_last_free_ptr) {
+						range.address + SMALL_BYTES_FOR_MSIZE(block_index) != mag_last_free) {
 						// Block in use
 						buffer[count].address = range.address + SMALL_BYTES_FOR_MSIZE(block_index);
 						buffer[count].size = SMALL_BYTES_FOR_MSIZE(msize);
@@ -1642,7 +1828,6 @@
 	CHECK_MAGAZINE_PTR_LOCKED(szone, small_mag_ptr, __PRETTY_FUNCTION__);
 
 	// Look for an exact match by checking the freelist for this msize.
-	//
 	if (small_free_list_get_ptr(rack, *the_slot)) {
 		ptr = small_free_list_get_ptr(rack, *the_slot);
 		this_msize = msize;
@@ -1653,34 +1838,27 @@
 	// Mask off the bits representing slots holding free blocks smaller than
 	// the size we need.  If there are no larger free blocks, try allocating
 	// from the free space at the end of the small region.
-	if (rack->debug_flags & MALLOC_EXTENDED_SMALL_SLOTS) {
-		// BITMAPN_CTZ implementation
-		unsigned idx = slot >> 5;
-		bitmap = 0;
-		unsigned mask = ~((1 << (slot & 31)) - 1);
-		for (; idx < SMALL_BITMAP_WORDS; ++idx) {
-			bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
-			if (bitmap != 0) {
-				break;
-			}
-			mask = ~0U;
-		}
-		// Check for fallthrough: No bits set in bitmap
-		if ((bitmap == 0) && (idx == SMALL_BITMAP_WORDS)) {
-			goto try_small_from_end;
-		}
-
-		// Start looking at the first set bit, plus 32 bits for every word of
-		// zeroes or entries that were too small.
-		slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
-	} else {
-		bitmap = small_mag_ptr->mag_bitmap[0] & ~((1 << slot) - 1);
-		if (!bitmap) {
-			goto try_small_from_end;
-		}
-
-		slot = BITMAP32_CTZ((&bitmap));
-	}
+	//
+	// BITMAPN_CTZ implementation
+	unsigned idx = slot >> 5;
+	bitmap = 0;
+	unsigned mask = ~((1 << (slot & 31)) - 1);
+	for (; idx < SMALL_FREELIST_BITMAP_WORDS(rack); ++idx) {
+		bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
+		if (bitmap != 0) {
+			break;
+		}
+		mask = ~0U;
+	}
+	// Check for fallthrough: No bits set in bitmap
+	if ((bitmap == 0) && (idx == SMALL_FREELIST_BITMAP_WORDS(rack))) {
+		goto try_small_from_end;
+	}
+
+	// Start looking at the first set bit, plus 32 bits for every word of
+	// zeroes or entries that were too small.
+	slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
+
 	// FIXME: Explain use of - 1 here, last slot has special meaning
 	limit = free_list + SMALL_FREE_SLOT_COUNT(rack) - 1;
 	free_list += slot;
@@ -1693,7 +1871,7 @@
 	}
 
 #if DEBUG_MALLOC
-	malloc_printf("in small_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
+	malloc_report(ASL_LEVEL_ERR, "in small_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
 #endif
 
 try_small_from_end:
@@ -1733,7 +1911,7 @@
 		leftover_ptr = (unsigned char *)ptr + SMALL_BYTES_FOR_MSIZE(msize);
 #if DEBUG_MALLOC
 		if (LOG(szone, ptr)) {
-			malloc_printf("in small_malloc_from_free_list(), adding leftover ptr=%p, this_msize=%d\n", ptr, this_msize);
+			malloc_report(ASL_LEVEL_INFO, "in small_malloc_from_free_list(), adding leftover ptr=%p, this_msize=%d\n", ptr, this_msize);
 		}
 #endif
 		small_free_list_add_ptr(rack, small_mag_ptr, leftover_ptr, leftover_msize);
@@ -1760,7 +1938,7 @@
 	}
 #if DEBUG_MALLOC
 	if (LOG(szone, ptr)) {
-		malloc_printf("in small_malloc_from_free_list(), ptr=%p, this_msize=%d, msize=%d\n", ptr, this_msize, msize);
+		malloc_report(ASL_LEVEL_INFO, "in small_malloc_from_free_list(), ptr=%p, this_msize=%d, msize=%d\n", ptr, this_msize, msize);
 	}
 #endif
 	small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(ptr), SMALL_META_INDEX_FOR_PTR(ptr), this_msize);
@@ -1771,7 +1949,7 @@
 small_malloc_should_clear(rack_t *rack, msize_t msize, boolean_t cleared_requested)
 {
 	void *ptr;
-	mag_index_t mag_index = mag_get_thread_index() % rack->num_magazines;
+	mag_index_t mag_index = small_mag_get_thread_index() % rack->num_magazines;
 	magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
 
 	MALLOC_TRACE(TRACE_small_malloc, (uintptr_t)rack, SMALL_BYTES_FOR_MSIZE(msize), (uintptr_t)small_mag_ptr, cleared_requested);
@@ -1779,15 +1957,15 @@
 	SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
 
 #if CONFIG_SMALL_CACHE
-	ptr = (void *)small_mag_ptr->mag_last_free;
-
-	if ((((uintptr_t)ptr) & (SMALL_QUANTUM - 1)) == msize) {
+	ptr = small_mag_ptr->mag_last_free;
+
+	if (small_mag_ptr->mag_last_free_msize == msize) {
 		// we have a winner
 		small_mag_ptr->mag_last_free = NULL;
+		small_mag_ptr->mag_last_free_msize = 0;
 		small_mag_ptr->mag_last_free_rgn = NULL;
 		SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 		CHECK(szone, __PRETTY_FUNCTION__);
-		ptr = (void *)((uintptr_t)ptr & ~(SMALL_QUANTUM - 1));
 		if (cleared_requested) {
 			memset(ptr, 0, SMALL_BYTES_FOR_MSIZE(msize));
 		}
@@ -1878,14 +2056,14 @@
 			if (DEPOT_MAGAZINE_INDEX != mag_index) {
 				magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
 
-				if (ptr == (void *)((uintptr_t)(small_mag_ptr->mag_last_free) & ~(SMALL_QUANTUM - 1))) {
+				if (ptr == small_mag_ptr->mag_last_free) {
 					return 0;
 				}
 			} else {
 				for (mag_index = 0; mag_index < rack->num_magazines; mag_index++) {
 					magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
 
-					if (ptr == (void *)((uintptr_t)(small_mag_ptr->mag_last_free) & ~(SMALL_QUANTUM - 1))) {
+					if (ptr == small_mag_ptr->mag_last_free) {
 						return 0;
 					}
 				}
@@ -1904,7 +2082,7 @@
 	mag_index_t mag_index = MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr));
 	magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
 	SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
-	szone_error(rack->debug_flags, 1, "double free", ptr, NULL);
+	malloc_zone_error(rack->debug_flags, true, "double free for ptr %p\n", ptr);
 }
 
 void
@@ -1931,10 +2109,11 @@
 	// Depot does not participate in CONFIG_SMALL_CACHE since it can't be directly malloc()'d
 	if (DEPOT_MAGAZINE_INDEX != mag_index) {
 		void *ptr2 = small_mag_ptr->mag_last_free; // Might be NULL
+		msize_t msize2 = small_mag_ptr->mag_last_free_msize;
 		region_t rgn2 = small_mag_ptr->mag_last_free_rgn;
 
 		/* check that we don't already have this pointer in the cache */
-		if (ptr == (void *)((uintptr_t)ptr2 & ~(SMALL_QUANTUM - 1))) {
+		if (ptr == ptr2) {
 			free_small_botch(rack, ptr);
 			return;
 		}
@@ -1943,7 +2122,8 @@
 			memset(ptr, SCRABBLE_BYTE, SMALL_BYTES_FOR_MSIZE(msize));
 		}
 
-		small_mag_ptr->mag_last_free = (void *)(((uintptr_t)ptr) | msize);
+		small_mag_ptr->mag_last_free = ptr;
+		small_mag_ptr->mag_last_free_msize = msize;
 		small_mag_ptr->mag_last_free_rgn = small_region;
 
 		if (!ptr2) {
@@ -1952,8 +2132,8 @@
 			return;
 		}
 
-		msize = (uintptr_t)ptr2 & (SMALL_QUANTUM - 1);
-		ptr = (void *)(((uintptr_t)ptr2) & ~(SMALL_QUANTUM - 1));
+		msize = msize2;
+		ptr = ptr2;
 		small_region = rgn2;
 	}
 #endif /* CONFIG_SMALL_CACHE */
@@ -2003,7 +2183,7 @@
 			}
 			_simple_sappend(b, "\n");
 		}
-		_malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
+		malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
 		_simple_sfree(b);
 	}
 }
@@ -2025,7 +2205,7 @@
 	if (region == HASHRING_REGION_DEALLOCATED) {
 		if ((b = _simple_salloc()) != NULL) {
 			_simple_sprintf(b, "Small region [unknown address] was returned to the OS\n");
-			_malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
+			malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
 			_simple_sfree(b);
 		}
 		return;
@@ -2036,7 +2216,7 @@
 		msize_and_free = *SMALL_METADATA_FOR_PTR(current);
 		msize = msize_and_free & ~SMALL_IS_FREE;
 		if (!msize) {
-			malloc_printf("*** error with %p: msize=%d\n", (void *)current, (unsigned)msize);
+			malloc_report(ASL_LEVEL_ERR, "*** error with %p: msize=%d\n", (void *)current, (unsigned)msize);
 			break;
 		}
 		if (!(msize_and_free & SMALL_IS_FREE)) {
@@ -2075,13 +2255,19 @@
 				}
 			}
 		}
-		_malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
+		malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
 		_simple_sfree(b);
 	}
 }
 
+static char *small_freelist_fail_msg = "check: small free list incorrect";
+
+#define SMALL_FREELIST_FAIL(fmt, ...) \
+	malloc_zone_check_fail(small_freelist_fail_msg, \
+			" (slot=%u), counter=%d\n" fmt,  slot, counter, __VA_ARGS__);
+
 boolean_t
-small_free_list_check(rack_t *rack, grain_t slot)
+small_free_list_check(rack_t *rack, grain_t slot, unsigned counter)
 {
 	mag_index_t mag_index;
 
@@ -2098,22 +2284,22 @@
 		while ((ptr = small_free_list_get_ptr(rack, current))) {
 			msize_and_free = *SMALL_METADATA_FOR_PTR(ptr);
 			if (!(msize_and_free & SMALL_IS_FREE)) {
-				malloc_printf("*** in-use ptr in free list slot=%d count=%d ptr=%p\n", slot, count, ptr);
+				SMALL_FREELIST_FAIL("*** in-use ptr in free list slot=%u count=%d ptr=%p\n", slot, count, ptr);
 				SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 				return 0;
 			}
 			if (((uintptr_t)ptr) & (SMALL_QUANTUM - 1)) {
-				malloc_printf("*** unaligned ptr in free list slot=%d  count=%d ptr=%p\n", slot, count, ptr);
+				SMALL_FREELIST_FAIL("*** unaligned ptr in free list slot=%u count=%d ptr=%p\n", slot, count, ptr);
 				SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 				return 0;
 			}
 			if (!small_region_for_ptr_no_lock(rack, ptr)) {
-				malloc_printf("*** ptr not in szone slot=%d  count=%d ptr=%p\n", slot, count, ptr);
+				SMALL_FREELIST_FAIL("*** ptr not in szone slot=%d count=%d ptr=%p\n", slot, count, ptr);
 				SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 				return 0;
 			}
 			if (small_free_list_get_previous(rack, current).p != previous.p) {
-				malloc_printf("*** previous incorrectly set slot=%d  count=%d ptr=%p\n", slot, count, ptr);
+				SMALL_FREELIST_FAIL("*** previous incorrectly set slot=%u count=%d ptr=%p\n", slot, count, ptr);
 				SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 				return 0;
 			}
@@ -2121,7 +2307,7 @@
 			current = small_free_list_get_next(rack, current);
 			count++;
 		}
-		
+
 		SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
 	}
 	return 1;