1#include "test/jemalloc_test.h"
2
3static const bool config_stats =
4#ifdef JEMALLOC_STATS
5    true
6#else
7    false
8#endif
9    ;
10
11void *
12thd_start(void *arg)
13{
14	int err;
15	void *p;
16	uint64_t a0, a1, d0, d1;
17	uint64_t *ap0, *ap1, *dp0, *dp1;
18	size_t sz, usize;
19
20	sz = sizeof(a0);
21	if ((err = mallctl("thread.allocated", (void *)&a0, &sz, NULL, 0))) {
22		if (err == ENOENT)
23			goto label_ENOENT;
24		test_fail("%s(): Error in mallctl(): %s", __func__,
25		    strerror(err));
26	}
27	sz = sizeof(ap0);
28	if ((err = mallctl("thread.allocatedp", (void *)&ap0, &sz, NULL, 0))) {
29		if (err == ENOENT)
30			goto label_ENOENT;
31		test_fail("%s(): Error in mallctl(): %s", __func__,
32		    strerror(err));
33	}
34	assert_u64_eq(*ap0, a0,
35	    "\"thread.allocatedp\" should provide a pointer to internal "
36	    "storage");
37
38	sz = sizeof(d0);
39	if ((err = mallctl("thread.deallocated", (void *)&d0, &sz, NULL, 0))) {
40		if (err == ENOENT)
41			goto label_ENOENT;
42		test_fail("%s(): Error in mallctl(): %s", __func__,
43		    strerror(err));
44	}
45	sz = sizeof(dp0);
46	if ((err = mallctl("thread.deallocatedp", (void *)&dp0, &sz, NULL,
47	    0))) {
48		if (err == ENOENT)
49			goto label_ENOENT;
50		test_fail("%s(): Error in mallctl(): %s", __func__,
51		    strerror(err));
52	}
53	assert_u64_eq(*dp0, d0,
54	    "\"thread.deallocatedp\" should provide a pointer to internal "
55	    "storage");
56
57	p = malloc(1);
58	assert_ptr_not_null(p, "Unexpected malloc() error");
59
60	sz = sizeof(a1);
61	mallctl("thread.allocated", (void *)&a1, &sz, NULL, 0);
62	sz = sizeof(ap1);
63	mallctl("thread.allocatedp", (void *)&ap1, &sz, NULL, 0);
64	assert_u64_eq(*ap1, a1,
65	    "Dereferenced \"thread.allocatedp\" value should equal "
66	    "\"thread.allocated\" value");
67	assert_ptr_eq(ap0, ap1,
68	    "Pointer returned by \"thread.allocatedp\" should not change");
69
70	usize = malloc_usable_size(p);
71	assert_u64_le(a0 + usize, a1,
72	    "Allocated memory counter should increase by at least the amount "
73	    "explicitly allocated");
74
75	free(p);
76
77	sz = sizeof(d1);
78	mallctl("thread.deallocated", (void *)&d1, &sz, NULL, 0);
79	sz = sizeof(dp1);
80	mallctl("thread.deallocatedp", (void *)&dp1, &sz, NULL, 0);
81	assert_u64_eq(*dp1, d1,
82	    "Dereferenced \"thread.deallocatedp\" value should equal "
83	    "\"thread.deallocated\" value");
84	assert_ptr_eq(dp0, dp1,
85	    "Pointer returned by \"thread.deallocatedp\" should not change");
86
87	assert_u64_le(d0 + usize, d1,
88	    "Deallocated memory counter should increase by at least the amount "
89	    "explicitly deallocated");
90
91	return (NULL);
92label_ENOENT:
93	assert_false(config_stats,
94	    "ENOENT should only be returned if stats are disabled");
95	test_skip("\"thread.allocated\" mallctl not available");
96	return (NULL);
97}
98
99TEST_BEGIN(test_main_thread)
100{
101	thd_start(NULL);
102}
103TEST_END
104
105TEST_BEGIN(test_subthread)
106{
107	thd_t thd;
108
109	thd_create(&thd, thd_start, NULL);
110	thd_join(thd, NULL);
111}
112TEST_END
113
114int
115main(void)
116{
117	/* Run tests multiple times to check for bad interactions. */
118	return (test(
119	    test_main_thread,
120	    test_subthread,
121	    test_main_thread,
122	    test_subthread,
123	    test_main_thread));
124}
125