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