xref: /netbsd-src/external/bsd/jemalloc/dist/test/unit/zero_realloc_free.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 #include "test/jemalloc_test.h"
2 
3 static uint64_t
4 deallocated() {
5 	if (!config_stats) {
6 		return 0;
7 	}
8 	uint64_t deallocated;
9 	size_t sz = sizeof(deallocated);
10 	expect_d_eq(mallctl("thread.deallocated", (void *)&deallocated, &sz,
11 	    NULL, 0), 0, "Unexpected mallctl failure");
12 	return deallocated;
13 }
14 
15 TEST_BEGIN(test_realloc_free) {
16 	void *ptr = mallocx(42, 0);
17 	expect_ptr_not_null(ptr, "Unexpected mallocx error");
18 	uint64_t deallocated_before = deallocated();
19 	ptr = realloc(ptr, 0);
20 	uint64_t deallocated_after = deallocated();
21 	expect_ptr_null(ptr, "Realloc didn't free");
22 	if (config_stats) {
23 		expect_u64_gt(deallocated_after, deallocated_before,
24 		    "Realloc didn't free");
25 	}
26 }
27 TEST_END
28 
29 int
30 main(void) {
31 	return test(
32 	    test_realloc_free);
33 }
34