Loading...
tests/magazine_small_test.c libmalloc-283 libmalloc-140.1.1
--- libmalloc/libmalloc-283/tests/magazine_small_test.c
+++ libmalloc/libmalloc-140.1.1/tests/magazine_small_test.c
@@ -1,5 +1,5 @@
 //
-//  magazine_small_test.c
+//  magazine_tiny_test.c
 //  libmalloc
 //
 //  Created by Matt Wright on 8/22/16.
@@ -10,8 +10,6 @@
 
 #include "../src/magazine_small.c"
 #include "magazine_testing.h"
-
-T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
 
 static inline void
 test_rack_setup(rack_t *rack)
@@ -36,7 +34,7 @@
 	T_ASSERT_EQ((int)sz, 512, "size == 32");
 }
 
-T_DECL(basic_small_teardown, "small rack init, alloc, teardown")
+T_DECL(basic_tiny_teardown, "small rack init, alloc, teardown")
 {
 	struct rack_s rack;
 	test_rack_setup(&rack);
@@ -93,35 +91,41 @@
 	struct rack_s rack;
 	test_rack_setup(&rack);
 
-	// Allocate two blocks and free the second, then try to realloc() the first.
-	// This should extend in-place using the one-level death row cache that's
-	// occupied by the second block.
 	void *ptr = small_malloc_should_clear(&rack, SMALL_MSIZE_FOR_BYTES(512), false);
 	T_ASSERT_NOTNULL(ptr, "allocation");
 
 	size_t sz = small_size(&rack, ptr);
 	T_ASSERT_EQ((int)sz, 512, "size == 512");
 
+	// Due to <rdar://problem/28032870>, we can't just do alloc+realloc here because
+	// the allocator won't see the remainder of the region as free (the
+	// mag_bytes_free_at_end section is marked as busy and realloc doesn't check
+	// for it).
+
+	// On top of that, <rdar://problem/28033016> means we can't do
+	// alloc+alloc+free+realloc because the free'd block gets put in the small cache
+	// and the realloc path *also* doesn't check that. That means to actually do a
+	// fast-path realloc you need to:
+
+	//   1. alloc three times (to get the blocks out of the free_at_end region)
+	//   2. free the logically consecutive block
+	//   3. free any other block, to get the consecutive block out of the small cache
+
 	void *ptr2 = small_malloc_should_clear(&rack, SMALL_MSIZE_FOR_BYTES(512), false);
 	T_ASSERT_NOTNULL(ptr2, "allocation 2");
 	T_ASSERT_EQ_PTR(ptr2, (void *)((uintptr_t)ptr + 512), "sequential allocations");
 
+	void *ptr3 = small_malloc_should_clear(&rack, SMALL_MSIZE_FOR_BYTES(512), false);
+	T_ASSERT_NOTNULL(ptr3, "allocation 3");
+
 	free_small(&rack, ptr2, SMALL_REGION_FOR_PTR(ptr2), 0);
+	free_small(&rack, ptr3, SMALL_REGION_FOR_PTR(ptr3), 0);
 
-	// Attempt to realloc up to 1024 bytes, this should happen in place
-	// because of the death-row cache.
+	// attempt to realloc up to 1024 bytes, this should happen in place
+	// as the rack is otherwise completely empty
 	boolean_t reallocd = small_try_realloc_in_place(&rack, ptr, sz, 1024);
 	T_ASSERT_TRUE(reallocd, "realloced");
 
 	size_t nsz = small_size(&rack, ptr);
 	T_ASSERT_EQ((int)nsz, 1024, "realloc size == 1024");
-
-	// Try another realloc(). This should extend in place because the rest of
-	// the rack is empty.
-	reallocd = small_try_realloc_in_place(&rack, ptr, nsz, 2048);
-	T_ASSERT_TRUE(reallocd, "realloced #2");
-	nsz = small_size(&rack, ptr);
-	T_ASSERT_EQ((int)nsz, 2048, "realloc size == 2048");
-
-	free_small(&rack, ptr, SMALL_REGION_FOR_PTR(ptr), 0);
 }