• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test/jemalloc_test.h"
2 
3 #define NTHREADS 10
4 
5 void *
thd_start(void * arg)6 thd_start(void *arg) {
7 	unsigned main_arena_ind = *(unsigned *)arg;
8 	void *p;
9 	unsigned arena_ind;
10 	size_t size;
11 	int err;
12 
13 	p = malloc(1);
14 	assert_ptr_not_null(p, "Error in malloc()");
15 	free(p);
16 
17 	size = sizeof(arena_ind);
18 	if ((err = mallctl("thread.arena", (void *)&arena_ind, &size,
19 	    (void *)&main_arena_ind, sizeof(main_arena_ind)))) {
20 		char buf[BUFERROR_BUF];
21 
22 		buferror(err, buf, sizeof(buf));
23 		test_fail("Error in mallctl(): %s", buf);
24 	}
25 
26 	size = sizeof(arena_ind);
27 	if ((err = mallctl("thread.arena", (void *)&arena_ind, &size, NULL,
28 	    0))) {
29 		char buf[BUFERROR_BUF];
30 
31 		buferror(err, buf, sizeof(buf));
32 		test_fail("Error in mallctl(): %s", buf);
33 	}
34 	assert_u_eq(arena_ind, main_arena_ind,
35 	    "Arena index should be same as for main thread");
36 
37 	return NULL;
38 }
39 
40 static void
mallctl_failure(int err)41 mallctl_failure(int err) {
42 	char buf[BUFERROR_BUF];
43 
44 	buferror(err, buf, sizeof(buf));
45 	test_fail("Error in mallctl(): %s", buf);
46 }
47 
TEST_BEGIN(test_thread_arena)48 TEST_BEGIN(test_thread_arena) {
49 	void *p;
50 	int err;
51 	thd_t thds[NTHREADS];
52 	unsigned i;
53 
54 	p = malloc(1);
55 	assert_ptr_not_null(p, "Error in malloc()");
56 
57 	unsigned arena_ind, old_arena_ind;
58 	size_t sz = sizeof(unsigned);
59 	assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
60 	    0, "Arena creation failure");
61 
62 	size_t size = sizeof(arena_ind);
63 	if ((err = mallctl("thread.arena", (void *)&old_arena_ind, &size,
64 	    (void *)&arena_ind, sizeof(arena_ind))) != 0) {
65 		mallctl_failure(err);
66 	}
67 
68 	for (i = 0; i < NTHREADS; i++) {
69 		thd_create(&thds[i], thd_start,
70 		    (void *)&arena_ind);
71 	}
72 
73 	for (i = 0; i < NTHREADS; i++) {
74 		intptr_t join_ret;
75 		thd_join(thds[i], (void *)&join_ret);
76 		assert_zd_eq(join_ret, 0, "Unexpected thread join error");
77 	}
78 	free(p);
79 }
80 TEST_END
81 
82 int
main(void)83 main(void) {
84 	return test(
85 	    test_thread_arena);
86 }
87