MALLOCX_ARENA.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "test/jemalloc_test.h"
  2. #define NTHREADS 10
  3. static bool have_dss =
  4. #ifdef JEMALLOC_DSS
  5. true
  6. #else
  7. false
  8. #endif
  9. ;
  10. void *
  11. thd_start(void *arg) {
  12. unsigned thread_ind = (unsigned)(uintptr_t)arg;
  13. unsigned arena_ind;
  14. void *p;
  15. size_t sz;
  16. sz = sizeof(arena_ind);
  17. assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
  18. 0, "Error in arenas.create");
  19. if (thread_ind % 4 != 3) {
  20. size_t mib[3];
  21. size_t miblen = sizeof(mib) / sizeof(size_t);
  22. const char *dss_precs[] = {"disabled", "primary", "secondary"};
  23. unsigned prec_ind = thread_ind %
  24. (sizeof(dss_precs)/sizeof(char*));
  25. const char *dss = dss_precs[prec_ind];
  26. int expected_err = (have_dss || prec_ind == 0) ? 0 : EFAULT;
  27. assert_d_eq(mallctlnametomib("arena.0.dss", mib, &miblen), 0,
  28. "Error in mallctlnametomib()");
  29. mib[1] = arena_ind;
  30. assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss,
  31. sizeof(const char *)), expected_err,
  32. "Error in mallctlbymib()");
  33. }
  34. p = mallocx(1, MALLOCX_ARENA(arena_ind));
  35. assert_ptr_not_null(p, "Unexpected mallocx() error");
  36. dallocx(p, 0);
  37. return NULL;
  38. }
  39. TEST_BEGIN(test_MALLOCX_ARENA) {
  40. thd_t thds[NTHREADS];
  41. unsigned i;
  42. for (i = 0; i < NTHREADS; i++) {
  43. thd_create(&thds[i], thd_start,
  44. (void *)(uintptr_t)i);
  45. }
  46. for (i = 0; i < NTHREADS; i++) {
  47. thd_join(thds[i], NULL);
  48. }
  49. }
  50. TEST_END
  51. int
  52. main(void) {
  53. return test(
  54. test_MALLOCX_ARENA);
  55. }