size_classes.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "test/jemalloc_test.h"
  2. static size_t
  3. get_max_size_class(void) {
  4. unsigned nlextents;
  5. size_t mib[4];
  6. size_t sz, miblen, max_size_class;
  7. sz = sizeof(unsigned);
  8. assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
  9. 0), 0, "Unexpected mallctl() error");
  10. miblen = sizeof(mib) / sizeof(size_t);
  11. assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
  12. "Unexpected mallctlnametomib() error");
  13. mib[2] = nlextents - 1;
  14. sz = sizeof(size_t);
  15. assert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
  16. NULL, 0), 0, "Unexpected mallctlbymib() error");
  17. return max_size_class;
  18. }
  19. TEST_BEGIN(test_size_classes) {
  20. size_t size_class, max_size_class;
  21. szind_t index, max_index;
  22. max_size_class = get_max_size_class();
  23. max_index = sz_size2index(max_size_class);
  24. for (index = 0, size_class = sz_index2size(index); index < max_index ||
  25. size_class < max_size_class; index++, size_class =
  26. sz_index2size(index)) {
  27. assert_true(index < max_index,
  28. "Loop conditionals should be equivalent; index=%u, "
  29. "size_class=%zu (%#zx)", index, size_class, size_class);
  30. assert_true(size_class < max_size_class,
  31. "Loop conditionals should be equivalent; index=%u, "
  32. "size_class=%zu (%#zx)", index, size_class, size_class);
  33. assert_u_eq(index, sz_size2index(size_class),
  34. "sz_size2index() does not reverse sz_index2size(): index=%u"
  35. " --> size_class=%zu --> index=%u --> size_class=%zu",
  36. index, size_class, sz_size2index(size_class),
  37. sz_index2size(sz_size2index(size_class)));
  38. assert_zu_eq(size_class,
  39. sz_index2size(sz_size2index(size_class)),
  40. "sz_index2size() does not reverse sz_size2index(): index=%u"
  41. " --> size_class=%zu --> index=%u --> size_class=%zu",
  42. index, size_class, sz_size2index(size_class),
  43. sz_index2size(sz_size2index(size_class)));
  44. assert_u_eq(index+1, sz_size2index(size_class+1),
  45. "Next size_class does not round up properly");
  46. assert_zu_eq(size_class, (index > 0) ?
  47. sz_s2u(sz_index2size(index-1)+1) : sz_s2u(1),
  48. "sz_s2u() does not round up to size class");
  49. assert_zu_eq(size_class, sz_s2u(size_class-1),
  50. "sz_s2u() does not round up to size class");
  51. assert_zu_eq(size_class, sz_s2u(size_class),
  52. "sz_s2u() does not compute same size class");
  53. assert_zu_eq(sz_s2u(size_class+1), sz_index2size(index+1),
  54. "sz_s2u() does not round up to next size class");
  55. }
  56. assert_u_eq(index, sz_size2index(sz_index2size(index)),
  57. "sz_size2index() does not reverse sz_index2size()");
  58. assert_zu_eq(max_size_class, sz_index2size(
  59. sz_size2index(max_size_class)),
  60. "sz_index2size() does not reverse sz_size2index()");
  61. assert_zu_eq(size_class, sz_s2u(sz_index2size(index-1)+1),
  62. "sz_s2u() does not round up to size class");
  63. assert_zu_eq(size_class, sz_s2u(size_class-1),
  64. "sz_s2u() does not round up to size class");
  65. assert_zu_eq(size_class, sz_s2u(size_class),
  66. "sz_s2u() does not compute same size class");
  67. }
  68. TEST_END
  69. TEST_BEGIN(test_psize_classes) {
  70. size_t size_class, max_psz;
  71. pszind_t pind, max_pind;
  72. max_psz = get_max_size_class() + PAGE;
  73. max_pind = sz_psz2ind(max_psz);
  74. for (pind = 0, size_class = sz_pind2sz(pind);
  75. pind < max_pind || size_class < max_psz;
  76. pind++, size_class = sz_pind2sz(pind)) {
  77. assert_true(pind < max_pind,
  78. "Loop conditionals should be equivalent; pind=%u, "
  79. "size_class=%zu (%#zx)", pind, size_class, size_class);
  80. assert_true(size_class < max_psz,
  81. "Loop conditionals should be equivalent; pind=%u, "
  82. "size_class=%zu (%#zx)", pind, size_class, size_class);
  83. assert_u_eq(pind, sz_psz2ind(size_class),
  84. "sz_psz2ind() does not reverse sz_pind2sz(): pind=%u -->"
  85. " size_class=%zu --> pind=%u --> size_class=%zu", pind,
  86. size_class, sz_psz2ind(size_class),
  87. sz_pind2sz(sz_psz2ind(size_class)));
  88. assert_zu_eq(size_class, sz_pind2sz(sz_psz2ind(size_class)),
  89. "sz_pind2sz() does not reverse sz_psz2ind(): pind=%u -->"
  90. " size_class=%zu --> pind=%u --> size_class=%zu", pind,
  91. size_class, sz_psz2ind(size_class),
  92. sz_pind2sz(sz_psz2ind(size_class)));
  93. assert_u_eq(pind+1, sz_psz2ind(size_class+1),
  94. "Next size_class does not round up properly");
  95. assert_zu_eq(size_class, (pind > 0) ?
  96. sz_psz2u(sz_pind2sz(pind-1)+1) : sz_psz2u(1),
  97. "sz_psz2u() does not round up to size class");
  98. assert_zu_eq(size_class, sz_psz2u(size_class-1),
  99. "sz_psz2u() does not round up to size class");
  100. assert_zu_eq(size_class, sz_psz2u(size_class),
  101. "sz_psz2u() does not compute same size class");
  102. assert_zu_eq(sz_psz2u(size_class+1), sz_pind2sz(pind+1),
  103. "sz_psz2u() does not round up to next size class");
  104. }
  105. assert_u_eq(pind, sz_psz2ind(sz_pind2sz(pind)),
  106. "sz_psz2ind() does not reverse sz_pind2sz()");
  107. assert_zu_eq(max_psz, sz_pind2sz(sz_psz2ind(max_psz)),
  108. "sz_pind2sz() does not reverse sz_psz2ind()");
  109. assert_zu_eq(size_class, sz_psz2u(sz_pind2sz(pind-1)+1),
  110. "sz_psz2u() does not round up to size class");
  111. assert_zu_eq(size_class, sz_psz2u(size_class-1),
  112. "sz_psz2u() does not round up to size class");
  113. assert_zu_eq(size_class, sz_psz2u(size_class),
  114. "sz_psz2u() does not compute same size class");
  115. }
  116. TEST_END
  117. TEST_BEGIN(test_overflow) {
  118. size_t max_size_class, max_psz;
  119. max_size_class = get_max_size_class();
  120. max_psz = max_size_class + PAGE;
  121. assert_u_eq(sz_size2index(max_size_class+1), NSIZES,
  122. "sz_size2index() should return NSIZES on overflow");
  123. assert_u_eq(sz_size2index(ZU(PTRDIFF_MAX)+1), NSIZES,
  124. "sz_size2index() should return NSIZES on overflow");
  125. assert_u_eq(sz_size2index(SIZE_T_MAX), NSIZES,
  126. "sz_size2index() should return NSIZES on overflow");
  127. assert_zu_eq(sz_s2u(max_size_class+1), 0,
  128. "sz_s2u() should return 0 for unsupported size");
  129. assert_zu_eq(sz_s2u(ZU(PTRDIFF_MAX)+1), 0,
  130. "sz_s2u() should return 0 for unsupported size");
  131. assert_zu_eq(sz_s2u(SIZE_T_MAX), 0,
  132. "sz_s2u() should return 0 on overflow");
  133. assert_u_eq(sz_psz2ind(max_size_class+1), NPSIZES,
  134. "sz_psz2ind() should return NPSIZES on overflow");
  135. assert_u_eq(sz_psz2ind(ZU(PTRDIFF_MAX)+1), NPSIZES,
  136. "sz_psz2ind() should return NPSIZES on overflow");
  137. assert_u_eq(sz_psz2ind(SIZE_T_MAX), NPSIZES,
  138. "sz_psz2ind() should return NPSIZES on overflow");
  139. assert_zu_eq(sz_psz2u(max_size_class+1), max_psz,
  140. "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported"
  141. " size");
  142. assert_zu_eq(sz_psz2u(ZU(PTRDIFF_MAX)+1), max_psz,
  143. "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported "
  144. "size");
  145. assert_zu_eq(sz_psz2u(SIZE_T_MAX), max_psz,
  146. "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) on overflow");
  147. }
  148. TEST_END
  149. int
  150. main(void) {
  151. return test(
  152. test_size_classes,
  153. test_psize_classes,
  154. test_overflow);
  155. }