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