Home | History | Annotate | Download | only in integration
      1 #include "test/jemalloc_test.h"
      2 
      3 #define	NTHREADS 10
      4 
      5 void *
      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", &arena_ind, &size, &main_arena_ind,
     20 	    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", &arena_ind, &size, NULL, 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 TEST_BEGIN(test_thread_arena)
     41 {
     42 	void *p;
     43 	unsigned arena_ind;
     44 	size_t size;
     45 	int err;
     46 	thd_t thds[NTHREADS];
     47 	unsigned i;
     48 
     49 	p = malloc(1);
     50 	assert_ptr_not_null(p, "Error in malloc()");
     51 
     52 	size = sizeof(arena_ind);
     53 	if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) {
     54 		char buf[BUFERROR_BUF];
     55 
     56 		buferror(err, buf, sizeof(buf));
     57 		test_fail("Error in mallctl(): %s", buf);
     58 	}
     59 
     60 	for (i = 0; i < NTHREADS; i++) {
     61 		thd_create(&thds[i], thd_start,
     62 		    (void *)&arena_ind);
     63 	}
     64 
     65 	for (i = 0; i < NTHREADS; i++) {
     66 		intptr_t join_ret;
     67 		thd_join(thds[i], (void *)&join_ret);
     68 		assert_zd_eq(join_ret, 0, "Unexpected thread join error");
     69 	}
     70 }
     71 TEST_END
     72 
     73 int
     74 main(void)
     75 {
     76 
     77 	return (test(
     78 	    test_thread_arena));
     79 }
     80