thread_arena.c 1.8 KB

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