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