2
0

rallocx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 be 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 (len=%zu) contains %#x "
  52. "rather than %#x at offset %zu", p, len, b, c,
  53. offset+i);
  54. ret = true;
  55. }
  56. }
  57. return (ret);
  58. }
  59. TEST_BEGIN(test_zero)
  60. {
  61. void *p, *q;
  62. size_t psz, qsz, i, j;
  63. size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
  64. #define FILL_BYTE 0xaaU
  65. #define RANGE 2048
  66. for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
  67. size_t start_size = start_sizes[i];
  68. p = mallocx(start_size, MALLOCX_ZERO);
  69. assert_ptr_not_null(p, "Unexpected mallocx() error");
  70. psz = sallocx(p, 0);
  71. assert_false(validate_fill(p, 0, 0, psz),
  72. "Expected zeroed memory");
  73. memset(p, FILL_BYTE, psz);
  74. assert_false(validate_fill(p, FILL_BYTE, 0, psz),
  75. "Expected filled memory");
  76. for (j = 1; j < RANGE; j++) {
  77. q = rallocx(p, start_size+j, MALLOCX_ZERO);
  78. assert_ptr_not_null(q, "Unexpected rallocx() error");
  79. qsz = sallocx(q, 0);
  80. if (q != p || qsz != psz) {
  81. assert_false(validate_fill(q, FILL_BYTE, 0,
  82. psz), "Expected filled memory");
  83. assert_false(validate_fill(q, 0, psz, qsz-psz),
  84. "Expected zeroed memory");
  85. }
  86. if (psz != qsz) {
  87. memset((void *)((uintptr_t)q+psz), FILL_BYTE,
  88. qsz-psz);
  89. psz = qsz;
  90. }
  91. p = q;
  92. }
  93. assert_false(validate_fill(p, FILL_BYTE, 0, psz),
  94. "Expected filled memory");
  95. dallocx(p, 0);
  96. }
  97. #undef FILL_BYTE
  98. }
  99. TEST_END
  100. TEST_BEGIN(test_align)
  101. {
  102. void *p, *q;
  103. size_t align;
  104. #define MAX_ALIGN (ZU(1) << 25)
  105. align = ZU(1);
  106. p = mallocx(1, MALLOCX_ALIGN(align));
  107. assert_ptr_not_null(p, "Unexpected mallocx() error");
  108. for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
  109. q = rallocx(p, 1, MALLOCX_ALIGN(align));
  110. assert_ptr_not_null(q,
  111. "Unexpected rallocx() error for align=%zu", align);
  112. assert_ptr_null(
  113. (void *)((uintptr_t)q & (align-1)),
  114. "%p inadequately aligned for align=%zu",
  115. q, align);
  116. p = q;
  117. }
  118. dallocx(p, 0);
  119. #undef MAX_ALIGN
  120. }
  121. TEST_END
  122. TEST_BEGIN(test_lg_align_and_zero)
  123. {
  124. void *p, *q;
  125. size_t lg_align, sz;
  126. #define MAX_LG_ALIGN 25
  127. #define MAX_VALIDATE (ZU(1) << 22)
  128. lg_align = ZU(0);
  129. p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  130. assert_ptr_not_null(p, "Unexpected mallocx() error");
  131. for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
  132. q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  133. assert_ptr_not_null(q,
  134. "Unexpected rallocx() error for lg_align=%zu", lg_align);
  135. assert_ptr_null(
  136. (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
  137. "%p inadequately aligned for lg_align=%zu",
  138. q, lg_align);
  139. sz = sallocx(q, 0);
  140. if ((sz << 1) <= MAX_VALIDATE) {
  141. assert_false(validate_fill(q, 0, 0, sz),
  142. "Expected zeroed memory");
  143. } else {
  144. assert_false(validate_fill(q, 0, 0, MAX_VALIDATE),
  145. "Expected zeroed memory");
  146. assert_false(validate_fill(
  147. (void *)((uintptr_t)q+sz-MAX_VALIDATE),
  148. 0, 0, MAX_VALIDATE), "Expected zeroed memory");
  149. }
  150. p = q;
  151. }
  152. dallocx(p, 0);
  153. #undef MAX_VALIDATE
  154. #undef MAX_LG_ALIGN
  155. }
  156. TEST_END
  157. int
  158. main(void)
  159. {
  160. return (test(
  161. test_grow_and_shrink,
  162. test_zero,
  163. test_align,
  164. test_lg_align_and_zero));
  165. }