size_classes.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "test/jemalloc_test.h"
  2. static size_t
  3. get_max_size_class(void)
  4. {
  5. unsigned nhchunks;
  6. size_t mib[4];
  7. size_t sz, miblen, max_size_class;
  8. sz = sizeof(unsigned);
  9. assert_d_eq(mallctl("arenas.nhchunks", &nhchunks, &sz, NULL, 0), 0,
  10. "Unexpected mallctl() error");
  11. miblen = sizeof(mib) / sizeof(size_t);
  12. assert_d_eq(mallctlnametomib("arenas.hchunk.0.size", mib, &miblen), 0,
  13. "Unexpected mallctlnametomib() error");
  14. mib[2] = nhchunks - 1;
  15. sz = sizeof(size_t);
  16. assert_d_eq(mallctlbymib(mib, miblen, &max_size_class, &sz, NULL, 0), 0,
  17. "Unexpected mallctlbymib() error");
  18. return (max_size_class);
  19. }
  20. TEST_BEGIN(test_size_classes)
  21. {
  22. size_t size_class, max_size_class;
  23. szind_t index, max_index;
  24. max_size_class = get_max_size_class();
  25. max_index = size2index(max_size_class);
  26. for (index = 0, size_class = index2size(index); index < max_index ||
  27. size_class < max_size_class; index++, size_class =
  28. index2size(index)) {
  29. assert_true(index < max_index,
  30. "Loop conditionals should be equivalent; index=%u, "
  31. "size_class=%zu (%#zx)", index, size_class, size_class);
  32. assert_true(size_class < max_size_class,
  33. "Loop conditionals should be equivalent; index=%u, "
  34. "size_class=%zu (%#zx)", index, size_class, size_class);
  35. assert_u_eq(index, size2index(size_class),
  36. "size2index() does not reverse index2size(): index=%u -->"
  37. " size_class=%zu --> index=%u --> size_class=%zu", index,
  38. size_class, size2index(size_class),
  39. index2size(size2index(size_class)));
  40. assert_zu_eq(size_class, index2size(size2index(size_class)),
  41. "index2size() does not reverse size2index(): index=%u -->"
  42. " size_class=%zu --> index=%u --> size_class=%zu", index,
  43. size_class, size2index(size_class),
  44. index2size(size2index(size_class)));
  45. assert_u_eq(index+1, size2index(size_class+1),
  46. "Next size_class does not round up properly");
  47. assert_zu_eq(size_class, (index > 0) ?
  48. s2u(index2size(index-1)+1) : s2u(1),
  49. "s2u() does not round up to size class");
  50. assert_zu_eq(size_class, s2u(size_class-1),
  51. "s2u() does not round up to size class");
  52. assert_zu_eq(size_class, s2u(size_class),
  53. "s2u() does not compute same size class");
  54. assert_zu_eq(s2u(size_class+1), index2size(index+1),
  55. "s2u() does not round up to next size class");
  56. }
  57. assert_u_eq(index, size2index(index2size(index)),
  58. "size2index() does not reverse index2size()");
  59. assert_zu_eq(max_size_class, index2size(size2index(max_size_class)),
  60. "index2size() does not reverse size2index()");
  61. assert_zu_eq(size_class, s2u(index2size(index-1)+1),
  62. "s2u() does not round up to size class");
  63. assert_zu_eq(size_class, s2u(size_class-1),
  64. "s2u() does not round up to size class");
  65. assert_zu_eq(size_class, s2u(size_class),
  66. "s2u() does not compute same size class");
  67. }
  68. TEST_END
  69. int
  70. main(void)
  71. {
  72. return (test(
  73. test_size_classes));
  74. }