size_classes.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. expect_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
  9. 0), 0, "Unexpected mallctl() error");
  10. miblen = sizeof(mib) / sizeof(size_t);
  11. expect_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. expect_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. expect_true(index < max_index,
  28. "Loop conditionals should be equivalent; index=%u, "
  29. "size_class=%zu (%#zx)", index, size_class, size_class);
  30. expect_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. expect_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. expect_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. expect_u_eq(index+1, sz_size2index(size_class+1),
  45. "Next size_class does not round up properly");
  46. expect_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. expect_zu_eq(size_class, sz_s2u(size_class-1),
  50. "sz_s2u() does not round up to size class");
  51. expect_zu_eq(size_class, sz_s2u(size_class),
  52. "sz_s2u() does not compute same size class");
  53. expect_zu_eq(sz_s2u(size_class+1), sz_index2size(index+1),
  54. "sz_s2u() does not round up to next size class");
  55. }
  56. expect_u_eq(index, sz_size2index(sz_index2size(index)),
  57. "sz_size2index() does not reverse sz_index2size()");
  58. expect_zu_eq(max_size_class, sz_index2size(
  59. sz_size2index(max_size_class)),
  60. "sz_index2size() does not reverse sz_size2index()");
  61. expect_zu_eq(size_class, sz_s2u(sz_index2size(index-1)+1),
  62. "sz_s2u() does not round up to size class");
  63. expect_zu_eq(size_class, sz_s2u(size_class-1),
  64. "sz_s2u() does not round up to size class");
  65. expect_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. expect_true(pind < max_pind,
  78. "Loop conditionals should be equivalent; pind=%u, "
  79. "size_class=%zu (%#zx)", pind, size_class, size_class);
  80. expect_true(size_class < max_psz,
  81. "Loop conditionals should be equivalent; pind=%u, "
  82. "size_class=%zu (%#zx)", pind, size_class, size_class);
  83. expect_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. expect_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. if (size_class == SC_LARGE_MAXCLASS) {
  94. expect_u_eq(SC_NPSIZES, sz_psz2ind(size_class + 1),
  95. "Next size_class does not round up properly");
  96. } else {
  97. expect_u_eq(pind + 1, sz_psz2ind(size_class + 1),
  98. "Next size_class does not round up properly");
  99. }
  100. expect_zu_eq(size_class, (pind > 0) ?
  101. sz_psz2u(sz_pind2sz(pind-1)+1) : sz_psz2u(1),
  102. "sz_psz2u() does not round up to size class");
  103. expect_zu_eq(size_class, sz_psz2u(size_class-1),
  104. "sz_psz2u() does not round up to size class");
  105. expect_zu_eq(size_class, sz_psz2u(size_class),
  106. "sz_psz2u() does not compute same size class");
  107. expect_zu_eq(sz_psz2u(size_class+1), sz_pind2sz(pind+1),
  108. "sz_psz2u() does not round up to next size class");
  109. }
  110. expect_u_eq(pind, sz_psz2ind(sz_pind2sz(pind)),
  111. "sz_psz2ind() does not reverse sz_pind2sz()");
  112. expect_zu_eq(max_psz, sz_pind2sz(sz_psz2ind(max_psz)),
  113. "sz_pind2sz() does not reverse sz_psz2ind()");
  114. expect_zu_eq(size_class, sz_psz2u(sz_pind2sz(pind-1)+1),
  115. "sz_psz2u() does not round up to size class");
  116. expect_zu_eq(size_class, sz_psz2u(size_class-1),
  117. "sz_psz2u() does not round up to size class");
  118. expect_zu_eq(size_class, sz_psz2u(size_class),
  119. "sz_psz2u() does not compute same size class");
  120. }
  121. TEST_END
  122. TEST_BEGIN(test_overflow) {
  123. size_t max_size_class, max_psz;
  124. max_size_class = get_max_size_class();
  125. max_psz = max_size_class + PAGE;
  126. expect_u_eq(sz_size2index(max_size_class+1), SC_NSIZES,
  127. "sz_size2index() should return NSIZES on overflow");
  128. expect_u_eq(sz_size2index(ZU(PTRDIFF_MAX)+1), SC_NSIZES,
  129. "sz_size2index() should return NSIZES on overflow");
  130. expect_u_eq(sz_size2index(SIZE_T_MAX), SC_NSIZES,
  131. "sz_size2index() should return NSIZES on overflow");
  132. expect_zu_eq(sz_s2u(max_size_class+1), 0,
  133. "sz_s2u() should return 0 for unsupported size");
  134. expect_zu_eq(sz_s2u(ZU(PTRDIFF_MAX)+1), 0,
  135. "sz_s2u() should return 0 for unsupported size");
  136. expect_zu_eq(sz_s2u(SIZE_T_MAX), 0,
  137. "sz_s2u() should return 0 on overflow");
  138. expect_u_eq(sz_psz2ind(max_size_class+1), SC_NPSIZES,
  139. "sz_psz2ind() should return NPSIZES on overflow");
  140. expect_u_eq(sz_psz2ind(ZU(PTRDIFF_MAX)+1), SC_NPSIZES,
  141. "sz_psz2ind() should return NPSIZES on overflow");
  142. expect_u_eq(sz_psz2ind(SIZE_T_MAX), SC_NPSIZES,
  143. "sz_psz2ind() should return NPSIZES on overflow");
  144. expect_zu_eq(sz_psz2u(max_size_class+1), max_psz,
  145. "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported"
  146. " size");
  147. expect_zu_eq(sz_psz2u(ZU(PTRDIFF_MAX)+1), max_psz,
  148. "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported "
  149. "size");
  150. expect_zu_eq(sz_psz2u(SIZE_T_MAX), max_psz,
  151. "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) on overflow");
  152. }
  153. TEST_END
  154. int
  155. main(void) {
  156. return test(
  157. test_size_classes,
  158. test_psize_classes,
  159. test_overflow);
  160. }