thread_arena.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #define JEMALLOC_MANGLE
  2. #include "jemalloc_test.h"
  3. #define NTHREADS 10
  4. void *
  5. je_thread_start(void *arg)
  6. {
  7. unsigned main_arena_ind = *(unsigned *)arg;
  8. void *p;
  9. unsigned arena_ind;
  10. size_t size;
  11. int err;
  12. p = malloc(1);
  13. if (p == NULL) {
  14. malloc_printf("%s(): Error in malloc()\n", __func__);
  15. return (void *)1;
  16. }
  17. size = sizeof(arena_ind);
  18. if ((err = mallctl("thread.arena", &arena_ind, &size, &main_arena_ind,
  19. sizeof(main_arena_ind)))) {
  20. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  21. strerror(err));
  22. return (void *)1;
  23. }
  24. size = sizeof(arena_ind);
  25. if ((err = mallctl("thread.arena", &arena_ind, &size, NULL,
  26. 0))) {
  27. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  28. strerror(err));
  29. return (void *)1;
  30. }
  31. assert(arena_ind == main_arena_ind);
  32. return (NULL);
  33. }
  34. int
  35. main(void)
  36. {
  37. int ret = 0;
  38. void *p;
  39. unsigned arena_ind;
  40. size_t size;
  41. int err;
  42. je_thread_t threads[NTHREADS];
  43. unsigned i;
  44. malloc_printf("Test begin\n");
  45. p = malloc(1);
  46. if (p == NULL) {
  47. malloc_printf("%s(): Error in malloc()\n", __func__);
  48. ret = 1;
  49. goto label_return;
  50. }
  51. size = sizeof(arena_ind);
  52. if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) {
  53. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  54. strerror(err));
  55. ret = 1;
  56. goto label_return;
  57. }
  58. for (i = 0; i < NTHREADS; i++) {
  59. je_thread_create(&threads[i], je_thread_start,
  60. (void *)&arena_ind);
  61. }
  62. for (i = 0; i < NTHREADS; i++)
  63. je_thread_join(threads[i], (void *)&ret);
  64. label_return:
  65. malloc_printf("Test end\n");
  66. return (ret);
  67. }