Loading...
--- libmalloc/libmalloc-283.100.6/src/magazine_tiny.c
+++ libmalloc/libmalloc-317.40.8/src/magazine_tiny.c
@@ -1147,10 +1147,13 @@
 	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);
@@ -1197,11 +1200,14 @@
 			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
@@ -1344,9 +1350,11 @@
 		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
@@ -2355,9 +2363,6 @@
 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);
 }
 
@@ -2401,6 +2406,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;
 			}