size_classes.c revision 1.1.1.1
1#include "test/jemalloc_test.h" 2 3static size_t 4get_max_size_class(void) { 5 unsigned nlextents; 6 size_t mib[4]; 7 size_t sz, miblen, max_size_class; 8 9 sz = sizeof(unsigned); 10 assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL, 11 0), 0, "Unexpected mallctl() error"); 12 13 miblen = sizeof(mib) / sizeof(size_t); 14 assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0, 15 "Unexpected mallctlnametomib() error"); 16 mib[2] = nlextents - 1; 17 18 sz = sizeof(size_t); 19 assert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz, 20 NULL, 0), 0, "Unexpected mallctlbymib() error"); 21 22 return max_size_class; 23} 24 25TEST_BEGIN(test_size_classes) { 26 size_t size_class, max_size_class; 27 szind_t index, max_index; 28 29 max_size_class = get_max_size_class(); 30 max_index = sz_size2index(max_size_class); 31 32 for (index = 0, size_class = sz_index2size(index); index < max_index || 33 size_class < max_size_class; index++, size_class = 34 sz_index2size(index)) { 35 assert_true(index < max_index, 36 "Loop conditionals should be equivalent; index=%u, " 37 "size_class=%zu (%#zx)", index, size_class, size_class); 38 assert_true(size_class < max_size_class, 39 "Loop conditionals should be equivalent; index=%u, " 40 "size_class=%zu (%#zx)", index, size_class, size_class); 41 42 assert_u_eq(index, sz_size2index(size_class), 43 "sz_size2index() does not reverse sz_index2size(): index=%u" 44 " --> size_class=%zu --> index=%u --> size_class=%zu", 45 index, size_class, sz_size2index(size_class), 46 sz_index2size(sz_size2index(size_class))); 47 assert_zu_eq(size_class, 48 sz_index2size(sz_size2index(size_class)), 49 "sz_index2size() does not reverse sz_size2index(): index=%u" 50 " --> size_class=%zu --> index=%u --> size_class=%zu", 51 index, size_class, sz_size2index(size_class), 52 sz_index2size(sz_size2index(size_class))); 53 54 assert_u_eq(index+1, sz_size2index(size_class+1), 55 "Next size_class does not round up properly"); 56 57 assert_zu_eq(size_class, (index > 0) ? 58 sz_s2u(sz_index2size(index-1)+1) : sz_s2u(1), 59 "sz_s2u() does not round up to size class"); 60 assert_zu_eq(size_class, sz_s2u(size_class-1), 61 "sz_s2u() does not round up to size class"); 62 assert_zu_eq(size_class, sz_s2u(size_class), 63 "sz_s2u() does not compute same size class"); 64 assert_zu_eq(sz_s2u(size_class+1), sz_index2size(index+1), 65 "sz_s2u() does not round up to next size class"); 66 } 67 68 assert_u_eq(index, sz_size2index(sz_index2size(index)), 69 "sz_size2index() does not reverse sz_index2size()"); 70 assert_zu_eq(max_size_class, sz_index2size( 71 sz_size2index(max_size_class)), 72 "sz_index2size() does not reverse sz_size2index()"); 73 74 assert_zu_eq(size_class, sz_s2u(sz_index2size(index-1)+1), 75 "sz_s2u() does not round up to size class"); 76 assert_zu_eq(size_class, sz_s2u(size_class-1), 77 "sz_s2u() does not round up to size class"); 78 assert_zu_eq(size_class, sz_s2u(size_class), 79 "sz_s2u() does not compute same size class"); 80} 81TEST_END 82 83TEST_BEGIN(test_psize_classes) { 84 size_t size_class, max_psz; 85 pszind_t pind, max_pind; 86 87 max_psz = get_max_size_class() + PAGE; 88 max_pind = sz_psz2ind(max_psz); 89 90 for (pind = 0, size_class = sz_pind2sz(pind); 91 pind < max_pind || size_class < max_psz; 92 pind++, size_class = sz_pind2sz(pind)) { 93 assert_true(pind < max_pind, 94 "Loop conditionals should be equivalent; pind=%u, " 95 "size_class=%zu (%#zx)", pind, size_class, size_class); 96 assert_true(size_class < max_psz, 97 "Loop conditionals should be equivalent; pind=%u, " 98 "size_class=%zu (%#zx)", pind, size_class, size_class); 99 100 assert_u_eq(pind, sz_psz2ind(size_class), 101 "sz_psz2ind() does not reverse sz_pind2sz(): pind=%u -->" 102 " size_class=%zu --> pind=%u --> size_class=%zu", pind, 103 size_class, sz_psz2ind(size_class), 104 sz_pind2sz(sz_psz2ind(size_class))); 105 assert_zu_eq(size_class, sz_pind2sz(sz_psz2ind(size_class)), 106 "sz_pind2sz() does not reverse sz_psz2ind(): pind=%u -->" 107 " size_class=%zu --> pind=%u --> size_class=%zu", pind, 108 size_class, sz_psz2ind(size_class), 109 sz_pind2sz(sz_psz2ind(size_class))); 110 111 assert_u_eq(pind+1, sz_psz2ind(size_class+1), 112 "Next size_class does not round up properly"); 113 114 assert_zu_eq(size_class, (pind > 0) ? 115 sz_psz2u(sz_pind2sz(pind-1)+1) : sz_psz2u(1), 116 "sz_psz2u() does not round up to size class"); 117 assert_zu_eq(size_class, sz_psz2u(size_class-1), 118 "sz_psz2u() does not round up to size class"); 119 assert_zu_eq(size_class, sz_psz2u(size_class), 120 "sz_psz2u() does not compute same size class"); 121 assert_zu_eq(sz_psz2u(size_class+1), sz_pind2sz(pind+1), 122 "sz_psz2u() does not round up to next size class"); 123 } 124 125 assert_u_eq(pind, sz_psz2ind(sz_pind2sz(pind)), 126 "sz_psz2ind() does not reverse sz_pind2sz()"); 127 assert_zu_eq(max_psz, sz_pind2sz(sz_psz2ind(max_psz)), 128 "sz_pind2sz() does not reverse sz_psz2ind()"); 129 130 assert_zu_eq(size_class, sz_psz2u(sz_pind2sz(pind-1)+1), 131 "sz_psz2u() does not round up to size class"); 132 assert_zu_eq(size_class, sz_psz2u(size_class-1), 133 "sz_psz2u() does not round up to size class"); 134 assert_zu_eq(size_class, sz_psz2u(size_class), 135 "sz_psz2u() does not compute same size class"); 136} 137TEST_END 138 139TEST_BEGIN(test_overflow) { 140 size_t max_size_class, max_psz; 141 142 max_size_class = get_max_size_class(); 143 max_psz = max_size_class + PAGE; 144 145 assert_u_eq(sz_size2index(max_size_class+1), NSIZES, 146 "sz_size2index() should return NSIZES on overflow"); 147 assert_u_eq(sz_size2index(ZU(PTRDIFF_MAX)+1), NSIZES, 148 "sz_size2index() should return NSIZES on overflow"); 149 assert_u_eq(sz_size2index(SIZE_T_MAX), NSIZES, 150 "sz_size2index() should return NSIZES on overflow"); 151 152 assert_zu_eq(sz_s2u(max_size_class+1), 0, 153 "sz_s2u() should return 0 for unsupported size"); 154 assert_zu_eq(sz_s2u(ZU(PTRDIFF_MAX)+1), 0, 155 "sz_s2u() should return 0 for unsupported size"); 156 assert_zu_eq(sz_s2u(SIZE_T_MAX), 0, 157 "sz_s2u() should return 0 on overflow"); 158 159 assert_u_eq(sz_psz2ind(max_size_class+1), NPSIZES, 160 "sz_psz2ind() should return NPSIZES on overflow"); 161 assert_u_eq(sz_psz2ind(ZU(PTRDIFF_MAX)+1), NPSIZES, 162 "sz_psz2ind() should return NPSIZES on overflow"); 163 assert_u_eq(sz_psz2ind(SIZE_T_MAX), NPSIZES, 164 "sz_psz2ind() should return NPSIZES on overflow"); 165 166 assert_zu_eq(sz_psz2u(max_size_class+1), max_psz, 167 "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported" 168 " size"); 169 assert_zu_eq(sz_psz2u(ZU(PTRDIFF_MAX)+1), max_psz, 170 "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported " 171 "size"); 172 assert_zu_eq(sz_psz2u(SIZE_T_MAX), max_psz, 173 "sz_psz2u() should return (LARGE_MAXCLASS + PAGE) on overflow"); 174} 175TEST_END 176 177int 178main(void) { 179 return test( 180 test_size_classes, 181 test_psize_classes, 182 test_overflow); 183} 184