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(q+psz, FILL_BYTE, qsz-psz); 99 psz = qsz; 100 } 101 p = q; 102 } 103 assert_false(validate_fill(p, FILL_BYTE, 0, psz), 104 "Expected filled memory"); 105 dallocx(p, 0); 106 } 107 #undef FILL_BYTE 108 } 109 TEST_END 110 111 TEST_BEGIN(test_align) 112 { 113 void *p, *q; 114 size_t align; 115 #define MAX_ALIGN (ZU(1) << 25) 116 117 align = ZU(1); 118 p = mallocx(1, MALLOCX_ALIGN(align)); 119 assert_ptr_not_null(p, "Unexpected mallocx() error"); 120 121 for (align <<= 1; align <= MAX_ALIGN; align <<= 1) { 122 q = rallocx(p, 1, MALLOCX_ALIGN(align)); 123 assert_ptr_not_null(q, 124 "Unexpected rallocx() error for align=%zu", align); 125 assert_ptr_null( 126 (void *)((uintptr_t)q & (align-1)), 127 "%p inadequately aligned for align=%zu", 128 q, align); 129 p = q; 130 } 131 dallocx(p, 0); 132 #undef MAX_ALIGN 133 } 134 TEST_END 135 136 TEST_BEGIN(test_lg_align_and_zero) 137 { 138 void *p, *q; 139 size_t lg_align, sz; 140 #define MAX_LG_ALIGN 25 141 #define MAX_VALIDATE (ZU(1) << 22) 142 143 lg_align = ZU(0); 144 p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO); 145 assert_ptr_not_null(p, "Unexpected mallocx() error"); 146 147 for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) { 148 q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO); 149 assert_ptr_not_null(q, 150 "Unexpected rallocx() error for lg_align=%zu", lg_align); 151 assert_ptr_null( 152 (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)), 153 "%p inadequately aligned for lg_align=%zu", 154 q, lg_align); 155 sz = sallocx(q, 0); 156 if ((sz << 1) <= MAX_VALIDATE) { 157 assert_false(validate_fill(q, 0, 0, sz), 158 "Expected zeroed memory"); 159 } else { 160 assert_false(validate_fill(q, 0, 0, MAX_VALIDATE), 161 "Expected zeroed memory"); 162 assert_false(validate_fill(q+sz-MAX_VALIDATE, 0, 0, 163 MAX_VALIDATE), "Expected zeroed memory"); 164 } 165 p = q; 166 } 167 dallocx(p, 0); 168 #undef MAX_VALIDATE 169 #undef MAX_LG_ALIGN 170 } 171 TEST_END 172 173 int 174 main(void) 175 { 176 177 return (test( 178 test_grow_and_shrink, 179 test_zero, 180 test_align, 181 test_lg_align_and_zero)); 182 } 183