1#include "test/jemalloc_test.h"
2
3#define	NTHREADS 10
4
5static bool have_dss =
6#ifdef JEMALLOC_DSS
7    true
8#else
9    false
10#endif
11    ;
12
13void *
14thd_start(void *arg)
15{
16	unsigned thread_ind = (unsigned)(uintptr_t)arg;
17	unsigned arena_ind;
18	void *p;
19	size_t sz;
20
21	sz = sizeof(arena_ind);
22	assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
23	    0, "Error in arenas.create");
24
25	if (thread_ind % 4 != 3) {
26		size_t mib[3];
27		size_t miblen = sizeof(mib) / sizeof(size_t);
28		const char *dss_precs[] = {"disabled", "primary", "secondary"};
29		unsigned prec_ind = thread_ind %
30		    (sizeof(dss_precs)/sizeof(char*));
31		const char *dss = dss_precs[prec_ind];
32		int expected_err = (have_dss || prec_ind == 0) ? 0 : EFAULT;
33		assert_d_eq(mallctlnametomib("arena.0.dss", mib, &miblen), 0,
34		    "Error in mallctlnametomib()");
35		mib[1] = arena_ind;
36		assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss,
37		    sizeof(const char *)), expected_err,
38		    "Error in mallctlbymib()");
39	}
40
41	p = mallocx(1, MALLOCX_ARENA(arena_ind));
42	assert_ptr_not_null(p, "Unexpected mallocx() error");
43	dallocx(p, 0);
44
45	return (NULL);
46}
47
48TEST_BEGIN(test_MALLOCX_ARENA)
49{
50	thd_t thds[NTHREADS];
51	unsigned i;
52
53	for (i = 0; i < NTHREADS; i++) {
54		thd_create(&thds[i], thd_start,
55		    (void *)(uintptr_t)i);
56	}
57
58	for (i = 0; i < NTHREADS; i++)
59		thd_join(thds[i], NULL);
60}
61TEST_END
62
63int
64main(void)
65{
66	return (test(
67	    test_MALLOCX_ARENA));
68}
69