xref: /netbsd-src/external/bsd/jemalloc/dist/test/unit/zero_realloc_alloc.c (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
1*7bdf38e5Schristos #include "test/jemalloc_test.h"
2*7bdf38e5Schristos 
3*7bdf38e5Schristos static uint64_t
4*7bdf38e5Schristos allocated() {
5*7bdf38e5Schristos 	if (!config_stats) {
6*7bdf38e5Schristos 		return 0;
7*7bdf38e5Schristos 	}
8*7bdf38e5Schristos 	uint64_t allocated;
9*7bdf38e5Schristos 	size_t sz = sizeof(allocated);
10*7bdf38e5Schristos 	expect_d_eq(mallctl("thread.allocated", (void *)&allocated, &sz, NULL,
11*7bdf38e5Schristos 	    0), 0, "Unexpected mallctl failure");
12*7bdf38e5Schristos 	return allocated;
13*7bdf38e5Schristos }
14*7bdf38e5Schristos 
15*7bdf38e5Schristos static uint64_t
16*7bdf38e5Schristos deallocated() {
17*7bdf38e5Schristos 	if (!config_stats) {
18*7bdf38e5Schristos 		return 0;
19*7bdf38e5Schristos 	}
20*7bdf38e5Schristos 	uint64_t deallocated;
21*7bdf38e5Schristos 	size_t sz = sizeof(deallocated);
22*7bdf38e5Schristos 	expect_d_eq(mallctl("thread.deallocated", (void *)&deallocated, &sz,
23*7bdf38e5Schristos 	    NULL, 0), 0, "Unexpected mallctl failure");
24*7bdf38e5Schristos 	return deallocated;
25*7bdf38e5Schristos }
26*7bdf38e5Schristos 
27*7bdf38e5Schristos TEST_BEGIN(test_realloc_alloc) {
28*7bdf38e5Schristos 	void *ptr = mallocx(1, 0);
29*7bdf38e5Schristos 	expect_ptr_not_null(ptr, "Unexpected mallocx error");
30*7bdf38e5Schristos 	uint64_t allocated_before = allocated();
31*7bdf38e5Schristos 	uint64_t deallocated_before = deallocated();
32*7bdf38e5Schristos 	ptr = realloc(ptr, 0);
33*7bdf38e5Schristos 	uint64_t allocated_after = allocated();
34*7bdf38e5Schristos 	uint64_t deallocated_after = deallocated();
35*7bdf38e5Schristos 	if (config_stats) {
36*7bdf38e5Schristos 		expect_u64_lt(allocated_before, allocated_after,
37*7bdf38e5Schristos 		    "Unexpected stats change");
38*7bdf38e5Schristos 		expect_u64_lt(deallocated_before, deallocated_after,
39*7bdf38e5Schristos 		    "Unexpected stats change");
40*7bdf38e5Schristos 	}
41*7bdf38e5Schristos 	dallocx(ptr, 0);
42*7bdf38e5Schristos }
43*7bdf38e5Schristos TEST_END
44*7bdf38e5Schristos int
45*7bdf38e5Schristos main(void) {
46*7bdf38e5Schristos 	return test(
47*7bdf38e5Schristos 	    test_realloc_alloc);
48*7bdf38e5Schristos }
49