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