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