1#include "test/jemalloc_test.h"
2
3static void
4test_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
42TEST_BEGIN(test_zero_small) {
43	test_skip_if(!config_fill);
44	test_zero(1, SMALL_MAXCLASS-1);
45}
46TEST_END
47
48TEST_BEGIN(test_zero_large) {
49	test_skip_if(!config_fill);
50	test_zero(SMALL_MAXCLASS+1, (1U << (LG_LARGE_MINCLASS+1)));
51}
52TEST_END
53
54int
55main(void) {
56	return test(
57	    test_zero_small,
58	    test_zero_large);
59}
60