1 #include "test/jemalloc_test.h"
2 
3 #ifdef JEMALLOC_FILL
4 const char *malloc_conf =
5     "abort:false,junk:false,zero:true";
6 #endif
7 
8 static void
test_zero(size_t sz_min,size_t sz_max)9 test_zero(size_t sz_min, size_t sz_max)
10 {
11 	uint8_t *s;
12 	size_t sz_prev, sz, i;
13 #define	MAGIC	((uint8_t)0x61)
14 
15 	sz_prev = 0;
16 	s = (uint8_t *)mallocx(sz_min, 0);
17 	assert_ptr_not_null((void *)s, "Unexpected mallocx() failure");
18 
19 	for (sz = sallocx(s, 0); sz <= sz_max;
20 	    sz_prev = sz, sz = sallocx(s, 0)) {
21 		if (sz_prev > 0) {
22 			assert_u_eq(s[0], MAGIC,
23 			    "Previously allocated byte %zu/%zu is corrupted",
24 			    ZU(0), sz_prev);
25 			assert_u_eq(s[sz_prev-1], MAGIC,
26 			    "Previously allocated byte %zu/%zu is corrupted",
27 			    sz_prev-1, sz_prev);
28 		}
29 
30 		for (i = sz_prev; i < sz; i++) {
31 			assert_u_eq(s[i], 0x0,
32 			    "Newly allocated byte %zu/%zu isn't zero-filled",
33 			    i, sz);
34 			s[i] = MAGIC;
35 		}
36 
37 		if (xallocx(s, sz+1, 0, 0) == sz) {
38 			s = (uint8_t *)rallocx(s, sz+1, 0);
39 			assert_ptr_not_null((void *)s,
40 			    "Unexpected rallocx() failure");
41 		}
42 	}
43 
44 	dallocx(s, 0);
45 #undef MAGIC
46 }
47 
TEST_BEGIN(test_zero_small)48 TEST_BEGIN(test_zero_small)
49 {
50 	test_skip_if(!config_fill);
51 	test_zero(1, SMALL_MAXCLASS-1);
52 }
53 TEST_END
54 
TEST_BEGIN(test_zero_large)55 TEST_BEGIN(test_zero_large)
56 {
57 	test_skip_if(!config_fill);
58 	test_zero(SMALL_MAXCLASS+1, (1U << (LG_LARGE_MINCLASS+1)));
59 }
60 TEST_END
61 
62 int
main(void)63 main(void)
64 {
65 	return (test(
66 	    test_zero_small,
67 	    test_zero_large));
68 }
69