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