rallocx.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_grow_and_shrink)
  3. {
  4. void *p, *q;
  5. size_t tsz;
  6. #define NCYCLES 3
  7. unsigned i, j;
  8. #define NSZS 2500
  9. size_t szs[NSZS];
  10. #define MAXSZ ZU(12 * 1024 * 1024)
  11. p = mallocx(1, 0);
  12. assert_ptr_not_null(p, "Unexpected mallocx() error");
  13. szs[0] = sallocx(p, 0);
  14. for (i = 0; i < NCYCLES; i++) {
  15. for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
  16. q = rallocx(p, szs[j-1]+1, 0);
  17. assert_ptr_not_null(q,
  18. "Unexpected rallocx() error for size=%zu-->%zu",
  19. szs[j-1], szs[j-1]+1);
  20. szs[j] = sallocx(q, 0);
  21. assert_zu_ne(szs[j], szs[j-1]+1,
  22. "Expected size to at least: %zu", szs[j-1]+1);
  23. p = q;
  24. }
  25. for (j--; j > 0; j--) {
  26. q = rallocx(p, szs[j-1], 0);
  27. assert_ptr_not_null(q,
  28. "Unexpected rallocx() error for size=%zu-->%zu",
  29. szs[j], szs[j-1]);
  30. tsz = sallocx(q, 0);
  31. assert_zu_eq(tsz, szs[j-1],
  32. "Expected size=%zu, got size=%zu", szs[j-1], tsz);
  33. p = q;
  34. }
  35. }
  36. dallocx(p, 0);
  37. #undef MAXSZ
  38. #undef NSZS
  39. #undef NCYCLES
  40. }
  41. TEST_END
  42. static bool
  43. validate_fill(const void *p, uint8_t c, size_t offset, size_t len)
  44. {
  45. bool ret = false;
  46. const uint8_t *buf = (const uint8_t *)p;
  47. size_t i;
  48. for (i = 0; i < len; i++) {
  49. uint8_t b = buf[offset+i];
  50. if (b != c) {
  51. test_fail("Allocation at %p contains %#x rather than "
  52. "%#x at offset %zu", p, b, c, offset+i);
  53. ret = true;
  54. }
  55. }
  56. return (ret);
  57. }
  58. TEST_BEGIN(test_zero)
  59. {
  60. void *p, *q;
  61. size_t psz, qsz, i, j;
  62. size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
  63. #define FILL_BYTE 0xaaU
  64. #define RANGE 2048
  65. for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
  66. size_t start_size = start_sizes[i];
  67. p = mallocx(start_size, MALLOCX_ZERO);
  68. assert_ptr_not_null(p, "Unexpected mallocx() error");
  69. psz = sallocx(p, 0);
  70. assert_false(validate_fill(p, 0, 0, psz),
  71. "Expected zeroed memory");
  72. memset(p, FILL_BYTE, psz);
  73. assert_false(validate_fill(p, FILL_BYTE, 0, psz),
  74. "Expected filled memory");
  75. for (j = 1; j < RANGE; j++) {
  76. q = rallocx(p, start_size+j, MALLOCX_ZERO);
  77. assert_ptr_not_null(q, "Unexpected rallocx() error");
  78. qsz = sallocx(q, 0);
  79. if (q != p || qsz != psz) {
  80. assert_false(validate_fill(q, FILL_BYTE, 0,
  81. psz), "Expected filled memory");
  82. assert_false(validate_fill(q, 0, psz, qsz-psz),
  83. "Expected zeroed memory");
  84. }
  85. if (psz != qsz) {
  86. memset(q+psz, FILL_BYTE, qsz-psz);
  87. psz = qsz;
  88. }
  89. p = q;
  90. }
  91. assert_false(validate_fill(p, FILL_BYTE, 0, psz),
  92. "Expected filled memory");
  93. dallocx(p, 0);
  94. }
  95. #undef FILL_BYTE
  96. }
  97. TEST_END
  98. TEST_BEGIN(test_align)
  99. {
  100. void *p, *q;
  101. size_t align;
  102. #define MAX_ALIGN (ZU(1) << 25)
  103. align = ZU(1);
  104. p = mallocx(1, MALLOCX_ALIGN(align));
  105. assert_ptr_not_null(p, "Unexpected mallocx() error");
  106. for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
  107. q = rallocx(p, 1, MALLOCX_ALIGN(align));
  108. assert_ptr_not_null(q,
  109. "Unexpected rallocx() error for align=%zu", align);
  110. assert_ptr_null(
  111. (void *)((uintptr_t)q & (align-1)),
  112. "%p inadequately aligned for align=%zu",
  113. q, align);
  114. p = q;
  115. }
  116. dallocx(p, 0);
  117. #undef MAX_ALIGN
  118. }
  119. TEST_END
  120. TEST_BEGIN(test_lg_align_and_zero)
  121. {
  122. void *p, *q;
  123. size_t lg_align, sz;
  124. #define MAX_LG_ALIGN 25
  125. #define MAX_VALIDATE (ZU(1) << 22)
  126. lg_align = ZU(0);
  127. p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  128. assert_ptr_not_null(p, "Unexpected mallocx() error");
  129. for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
  130. q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  131. assert_ptr_not_null(q,
  132. "Unexpected rallocx() error for lg_align=%zu", lg_align);
  133. assert_ptr_null(
  134. (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
  135. "%p inadequately aligned for lg_align=%zu",
  136. q, lg_align);
  137. sz = sallocx(q, 0);
  138. if ((sz << 1) <= MAX_VALIDATE) {
  139. assert_false(validate_fill(q, 0, 0, sz),
  140. "Expected zeroed memory");
  141. } else {
  142. assert_false(validate_fill(q, 0, 0, MAX_VALIDATE),
  143. "Expected zeroed memory");
  144. assert_false(validate_fill(q+sz-MAX_VALIDATE, 0, 0,
  145. MAX_VALIDATE), "Expected zeroed memory");
  146. }
  147. p = q;
  148. }
  149. dallocx(p, 0);
  150. #undef MAX_VALIDATE
  151. #undef MAX_LG_ALIGN
  152. }
  153. TEST_END
  154. int
  155. main(void)
  156. {
  157. return (test(
  158. test_grow_and_shrink,
  159. test_zero,
  160. test_align,
  161. test_lg_align_and_zero));
  162. }