1 1.1 christos #ifndef JEMALLOC_INTERNAL_SC_H 2 1.1 christos #define JEMALLOC_INTERNAL_SC_H 3 1.1 christos 4 1.1 christos #include "jemalloc/internal/jemalloc_internal_types.h" 5 1.1 christos 6 1.1 christos /* 7 1.1 christos * Size class computations: 8 1.1 christos * 9 1.1 christos * These are a little tricky; we'll first start by describing how things 10 1.1 christos * generally work, and then describe some of the details. 11 1.1 christos * 12 1.1 christos * Ignore the first few size classes for a moment. We can then split all the 13 1.1 christos * remaining size classes into groups. The size classes in a group are spaced 14 1.1 christos * such that they cover allocation request sizes in a power-of-2 range. The 15 1.1 christos * power of two is called the base of the group, and the size classes in it 16 1.1 christos * satisfy allocations in the half-open range (base, base * 2]. There are 17 1.1 christos * SC_NGROUP size classes in each group, equally spaced in the range, so that 18 1.1 christos * each one covers allocations for base / SC_NGROUP possible allocation sizes. 19 1.1 christos * We call that value (base / SC_NGROUP) the delta of the group. Each size class 20 1.1 christos * is delta larger than the one before it (including the initial size class in a 21 1.1 christos * group, which is delta larger than base, the largest size class in the 22 1.1 christos * previous group). 23 1.1 christos * To make the math all work out nicely, we require that SC_NGROUP is a power of 24 1.1 christos * two, and define it in terms of SC_LG_NGROUP. We'll often talk in terms of 25 1.1 christos * lg_base and lg_delta. For each of these groups then, we have that 26 1.1 christos * lg_delta == lg_base - SC_LG_NGROUP. 27 1.1 christos * The size classes in a group with a given lg_base and lg_delta (which, recall, 28 1.1 christos * can be computed from lg_base for these groups) are therefore: 29 1.1 christos * base + 1 * delta 30 1.1 christos * which covers allocations in (base, base + 1 * delta] 31 1.1 christos * base + 2 * delta 32 1.1 christos * which covers allocations in (base + 1 * delta, base + 2 * delta]. 33 1.1 christos * base + 3 * delta 34 1.1 christos * which covers allocations in (base + 2 * delta, base + 3 * delta]. 35 1.1 christos * ... 36 1.1 christos * base + SC_NGROUP * delta ( == 2 * base) 37 1.1 christos * which covers allocations in (base + (SC_NGROUP - 1) * delta, 2 * base]. 38 1.1 christos * (Note that currently SC_NGROUP is always 4, so the "..." is empty in 39 1.1 christos * practice.) 40 1.1 christos * Note that the last size class in the group is the next power of two (after 41 1.1 christos * base), so that we've set up the induction correctly for the next group's 42 1.1 christos * selection of delta. 43 1.1 christos * 44 1.1 christos * Now, let's start considering the first few size classes. Two extra constants 45 1.1 christos * come into play here: LG_QUANTUM and SC_LG_TINY_MIN. LG_QUANTUM ensures 46 1.1 christos * correct platform alignment; all objects of size (1 << LG_QUANTUM) or larger 47 1.1 christos * are at least (1 << LG_QUANTUM) aligned; this can be used to ensure that we 48 1.1 christos * never return improperly aligned memory, by making (1 << LG_QUANTUM) equal the 49 1.1 christos * highest required alignment of a platform. For allocation sizes smaller than 50 1.1 christos * (1 << LG_QUANTUM) though, we can be more relaxed (since we don't support 51 1.1 christos * platforms with types with alignment larger than their size). To allow such 52 1.1 christos * allocations (without wasting space unnecessarily), we introduce tiny size 53 1.1 christos * classes; one per power of two, up until we hit the quantum size. There are 54 1.1 christos * therefore LG_QUANTUM - SC_LG_TINY_MIN such size classes. 55 1.1 christos * 56 1.1 christos * Next, we have a size class of size (1 << LG_QUANTUM). This can't be the 57 1.1 christos * start of a group in the sense we described above (covering a power of two 58 1.1 christos * range) since, if we divided into it to pick a value of delta, we'd get a 59 1.1 christos * delta smaller than (1 << LG_QUANTUM) for sizes >= (1 << LG_QUANTUM), which 60 1.1 christos * is against the rules. 61 1.1 christos * 62 1.1 christos * The first base we can divide by SC_NGROUP while still being at least 63 1.1 christos * (1 << LG_QUANTUM) is SC_NGROUP * (1 << LG_QUANTUM). We can get there by 64 1.1 christos * having SC_NGROUP size classes, spaced (1 << LG_QUANTUM) apart. These size 65 1.1 christos * classes are: 66 1.1 christos * 1 * (1 << LG_QUANTUM) 67 1.1 christos * 2 * (1 << LG_QUANTUM) 68 1.1 christos * 3 * (1 << LG_QUANTUM) 69 1.1 christos * ... (although, as above, this "..." is empty in practice) 70 1.1 christos * SC_NGROUP * (1 << LG_QUANTUM). 71 1.1 christos * 72 1.1 christos * There are SC_NGROUP of these size classes, so we can regard it as a sort of 73 1.1 christos * pseudo-group, even though it spans multiple powers of 2, is divided 74 1.1 christos * differently, and both starts and ends on a power of 2 (as opposed to just 75 1.1 christos * ending). SC_NGROUP is itself a power of two, so the first group after the 76 1.1 christos * pseudo-group has the power-of-two base SC_NGROUP * (1 << LG_QUANTUM), for a 77 1.1 christos * lg_base of LG_QUANTUM + SC_LG_NGROUP. We can divide this base into SC_NGROUP 78 1.1 christos * sizes without violating our LG_QUANTUM requirements, so we can safely set 79 1.1 christos * lg_delta = lg_base - SC_LG_GROUP (== LG_QUANTUM). 80 1.1 christos * 81 1.1 christos * So, in order, the size classes are: 82 1.1 christos * 83 1.1 christos * Tiny size classes: 84 1.1 christos * - Count: LG_QUANTUM - SC_LG_TINY_MIN. 85 1.1 christos * - Sizes: 86 1.1 christos * 1 << SC_LG_TINY_MIN 87 1.1 christos * 1 << (SC_LG_TINY_MIN + 1) 88 1.1 christos * 1 << (SC_LG_TINY_MIN + 2) 89 1.1 christos * ... 90 1.1 christos * 1 << (LG_QUANTUM - 1) 91 1.1 christos * 92 1.1 christos * Initial pseudo-group: 93 1.1 christos * - Count: SC_NGROUP 94 1.1 christos * - Sizes: 95 1.1 christos * 1 * (1 << LG_QUANTUM) 96 1.1 christos * 2 * (1 << LG_QUANTUM) 97 1.1 christos * 3 * (1 << LG_QUANTUM) 98 1.1 christos * ... 99 1.1 christos * SC_NGROUP * (1 << LG_QUANTUM) 100 1.1 christos * 101 1.1 christos * Regular group 0: 102 1.1 christos * - Count: SC_NGROUP 103 1.1 christos * - Sizes: 104 1.1 christos * (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP and lg_delta of 105 1.1 christos * lg_base - SC_LG_NGROUP) 106 1.1 christos * (1 << lg_base) + 1 * (1 << lg_delta) 107 1.1 christos * (1 << lg_base) + 2 * (1 << lg_delta) 108 1.1 christos * (1 << lg_base) + 3 * (1 << lg_delta) 109 1.1 christos * ... 110 1.1 christos * (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ] 111 1.1 christos * 112 1.1 christos * Regular group 1: 113 1.1 christos * - Count: SC_NGROUP 114 1.1 christos * - Sizes: 115 1.1 christos * (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP + 1 and lg_delta of 116 1.1 christos * lg_base - SC_LG_NGROUP) 117 1.1 christos * (1 << lg_base) + 1 * (1 << lg_delta) 118 1.1 christos * (1 << lg_base) + 2 * (1 << lg_delta) 119 1.1 christos * (1 << lg_base) + 3 * (1 << lg_delta) 120 1.1 christos * ... 121 1.1 christos * (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ] 122 1.1 christos * 123 1.1 christos * ... 124 1.1 christos * 125 1.1 christos * Regular group N: 126 1.1 christos * - Count: SC_NGROUP 127 1.1 christos * - Sizes: 128 1.1 christos * (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP + N and lg_delta of 129 1.1 christos * lg_base - SC_LG_NGROUP) 130 1.1 christos * (1 << lg_base) + 1 * (1 << lg_delta) 131 1.1 christos * (1 << lg_base) + 2 * (1 << lg_delta) 132 1.1 christos * (1 << lg_base) + 3 * (1 << lg_delta) 133 1.1 christos * ... 134 1.1 christos * (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ] 135 1.1 christos * 136 1.1 christos * 137 1.1 christos * Representation of metadata: 138 1.1 christos * To make the math easy, we'll mostly work in lg quantities. We record lg_base, 139 1.1 christos * lg_delta, and ndelta (i.e. number of deltas above the base) on a 140 1.1 christos * per-size-class basis, and maintain the invariant that, across all size 141 1.1 christos * classes, size == (1 << lg_base) + ndelta * (1 << lg_delta). 142 1.1 christos * 143 1.1 christos * For regular groups (i.e. those with lg_base >= LG_QUANTUM + SC_LG_NGROUP), 144 1.1 christos * lg_delta is lg_base - SC_LG_NGROUP, and ndelta goes from 1 to SC_NGROUP. 145 1.1 christos * 146 1.1 christos * For the initial tiny size classes (if any), lg_base is lg(size class size). 147 1.1 christos * lg_delta is lg_base for the first size class, and lg_base - 1 for all 148 1.1 christos * subsequent ones. ndelta is always 0. 149 1.1 christos * 150 1.1 christos * For the pseudo-group, if there are no tiny size classes, then we set 151 1.1 christos * lg_base == LG_QUANTUM, lg_delta == LG_QUANTUM, and have ndelta range from 0 152 1.1 christos * to SC_NGROUP - 1. (Note that delta == base, so base + (SC_NGROUP - 1) * delta 153 1.1 christos * is just SC_NGROUP * base, or (1 << (SC_LG_NGROUP + LG_QUANTUM)), so we do 154 1.1 christos * indeed get a power of two that way). If there *are* tiny size classes, then 155 1.1 christos * the first size class needs to have lg_delta relative to the largest tiny size 156 1.1 christos * class. We therefore set lg_base == LG_QUANTUM - 1, 157 1.1 christos * lg_delta == LG_QUANTUM - 1, and ndelta == 1, keeping the rest of the 158 1.1 christos * pseudo-group the same. 159 1.1 christos * 160 1.1 christos * 161 1.1 christos * Other terminology: 162 1.1 christos * "Small" size classes mean those that are allocated out of bins, which is the 163 1.1 christos * same as those that are slab allocated. 164 1.1 christos * "Large" size classes are those that are not small. The cutoff for counting as 165 1.1 christos * large is page size * group size. 166 1.1 christos */ 167 1.1 christos 168 1.1 christos /* 169 1.1 christos * Size class N + (1 << SC_LG_NGROUP) twice the size of size class N. 170 1.1 christos */ 171 1.1 christos #define SC_LG_NGROUP 2 172 1.1 christos #define SC_LG_TINY_MIN 3 173 1.1 christos 174 1.1 christos #if SC_LG_TINY_MIN == 0 175 1.1 christos /* The div module doesn't support division by 1, which this would require. */ 176 1.1 christos #error "Unsupported LG_TINY_MIN" 177 1.1 christos #endif 178 1.1 christos 179 1.1 christos /* 180 1.1 christos * The definitions below are all determined by the above settings and system 181 1.1 christos * characteristics. 182 1.1 christos */ 183 1.1 christos #define SC_NGROUP (1ULL << SC_LG_NGROUP) 184 1.1 christos #define SC_PTR_BITS ((1ULL << LG_SIZEOF_PTR) * 8) 185 1.1 christos #define SC_NTINY (LG_QUANTUM - SC_LG_TINY_MIN) 186 1.1 christos #define SC_LG_TINY_MAXCLASS (LG_QUANTUM > SC_LG_TINY_MIN ? LG_QUANTUM - 1 : -1) 187 1.1 christos #define SC_NPSEUDO SC_NGROUP 188 1.1 christos #define SC_LG_FIRST_REGULAR_BASE (LG_QUANTUM + SC_LG_NGROUP) 189 1.1 christos /* 190 1.1 christos * We cap allocations to be less than 2 ** (ptr_bits - 1), so the highest base 191 1.1 christos * we need is 2 ** (ptr_bits - 2). (This also means that the last group is 1 192 1.1 christos * size class shorter than the others). 193 1.1 christos * We could probably save some space in arenas by capping this at LG_VADDR size. 194 1.1 christos */ 195 1.1 christos #define SC_LG_BASE_MAX (SC_PTR_BITS - 2) 196 1.1 christos #define SC_NREGULAR (SC_NGROUP * \ 197 1.1 christos (SC_LG_BASE_MAX - SC_LG_FIRST_REGULAR_BASE + 1) - 1) 198 1.1 christos #define SC_NSIZES (SC_NTINY + SC_NPSEUDO + SC_NREGULAR) 199 1.1 christos 200 1.1 christos /* 201 1.1 christos * The number of size classes that are a multiple of the page size. 202 1.1 christos * 203 1.1 christos * Here are the first few bases that have a page-sized SC. 204 1.1 christos * 205 1.1 christos * lg(base) | base | highest SC | page-multiple SCs 206 1.1 christos * --------------|------------------------------------------ 207 1.1 christos * LG_PAGE - 1 | PAGE / 2 | PAGE | 1 208 1.1 christos * LG_PAGE | PAGE | 2 * PAGE | 1 209 1.1 christos * LG_PAGE + 1 | 2 * PAGE | 4 * PAGE | 2 210 1.1 christos * LG_PAGE + 2 | 4 * PAGE | 8 * PAGE | 4 211 1.1 christos * 212 1.1 christos * The number of page-multiple SCs continues to grow in powers of two, up until 213 1.1 christos * lg_delta == lg_page, which corresponds to setting lg_base to lg_page + 214 1.1 christos * SC_LG_NGROUP. So, then, the number of size classes that are multiples of the 215 1.1 christos * page size whose lg_delta is less than the page size are 216 1.1 christos * is 1 + (2**0 + 2**1 + ... + 2**(lg_ngroup - 1) == 2**lg_ngroup. 217 1.1 christos * 218 1.1 christos * For each base with lg_base in [lg_page + lg_ngroup, lg_base_max), there are 219 1.1 christos * NGROUP page-sized size classes, and when lg_base == lg_base_max, there are 220 1.1 christos * NGROUP - 1. 221 1.1 christos * 222 1.1 christos * This gives us the quantity we seek. 223 1.1 christos */ 224 1.1 christos #define SC_NPSIZES ( \ 225 1.1 christos SC_NGROUP \ 226 1.1 christos + (SC_LG_BASE_MAX - (LG_PAGE + SC_LG_NGROUP)) * SC_NGROUP \ 227 1.1 christos + SC_NGROUP - 1) 228 1.1 christos 229 1.1 christos /* 230 1.1 christos * We declare a size class is binnable if size < page size * group. Or, in other 231 1.1 christos * words, lg(size) < lg(page size) + lg(group size). 232 1.1 christos */ 233 1.1 christos #define SC_NBINS ( \ 234 1.1 christos /* Sub-regular size classes. */ \ 235 1.1 christos SC_NTINY + SC_NPSEUDO \ 236 1.1 christos /* Groups with lg_regular_min_base <= lg_base <= lg_base_max */ \ 237 1.1 christos + SC_NGROUP * (LG_PAGE + SC_LG_NGROUP - SC_LG_FIRST_REGULAR_BASE) \ 238 1.1 christos /* Last SC of the last group hits the bound exactly; exclude it. */ \ 239 1.1 christos - 1) 240 1.1 christos 241 1.1 christos /* 242 1.1 christos * The size2index_tab lookup table uses uint8_t to encode each bin index, so we 243 1.1 christos * cannot support more than 256 small size classes. 244 1.1 christos */ 245 1.1 christos #if (SC_NBINS > 256) 246 1.1 christos # error "Too many small size classes" 247 1.1 christos #endif 248 1.1 christos 249 1.1 christos /* The largest size class in the lookup table, and its binary log. */ 250 1.1 christos #define SC_LG_MAX_LOOKUP 12 251 1.1 christos #define SC_LOOKUP_MAXCLASS (1 << SC_LG_MAX_LOOKUP) 252 1.1 christos 253 1.1 christos /* Internal, only used for the definition of SC_SMALL_MAXCLASS. */ 254 1.1 christos #define SC_SMALL_MAX_BASE (1 << (LG_PAGE + SC_LG_NGROUP - 1)) 255 1.1 christos #define SC_SMALL_MAX_DELTA (1 << (LG_PAGE - 1)) 256 1.1 christos 257 1.1 christos /* The largest size class allocated out of a slab. */ 258 1.1 christos #define SC_SMALL_MAXCLASS (SC_SMALL_MAX_BASE \ 259 1.1 christos + (SC_NGROUP - 1) * SC_SMALL_MAX_DELTA) 260 1.1 christos 261 1.1 christos /* The fastpath assumes all lookup-able sizes are small. */ 262 1.1 christos #if (SC_SMALL_MAXCLASS < SC_LOOKUP_MAXCLASS) 263 1.1 christos # error "Lookup table sizes must be small" 264 1.1 christos #endif 265 1.1 christos 266 1.1 christos /* The smallest size class not allocated out of a slab. */ 267 1.1 christos #define SC_LARGE_MINCLASS ((size_t)1ULL << (LG_PAGE + SC_LG_NGROUP)) 268 1.1 christos #define SC_LG_LARGE_MINCLASS (LG_PAGE + SC_LG_NGROUP) 269 1.1 christos 270 1.1 christos /* Internal; only used for the definition of SC_LARGE_MAXCLASS. */ 271 1.1 christos #define SC_MAX_BASE ((size_t)1 << (SC_PTR_BITS - 2)) 272 1.1 christos #define SC_MAX_DELTA ((size_t)1 << (SC_PTR_BITS - 2 - SC_LG_NGROUP)) 273 1.1 christos 274 1.1 christos /* The largest size class supported. */ 275 1.1 christos #define SC_LARGE_MAXCLASS (SC_MAX_BASE + (SC_NGROUP - 1) * SC_MAX_DELTA) 276 1.1 christos 277 1.1 christos /* Maximum number of regions in one slab. */ 278 1.1 christos #ifndef CONFIG_LG_SLAB_MAXREGS 279 1.1 christos # define SC_LG_SLAB_MAXREGS (LG_PAGE - SC_LG_TINY_MIN) 280 1.1 christos #else 281 1.1 christos # if CONFIG_LG_SLAB_MAXREGS < (LG_PAGE - SC_LG_TINY_MIN) 282 1.1 christos # error "Unsupported SC_LG_SLAB_MAXREGS" 283 1.1 christos # else 284 1.1 christos # define SC_LG_SLAB_MAXREGS CONFIG_LG_SLAB_MAXREGS 285 1.1 christos # endif 286 1.1 christos #endif 287 1.1 christos 288 1.1 christos #define SC_SLAB_MAXREGS (1U << SC_LG_SLAB_MAXREGS) 289 1.1 christos 290 1.1 christos typedef struct sc_s sc_t; 291 1.1 christos struct sc_s { 292 1.1 christos /* Size class index, or -1 if not a valid size class. */ 293 1.1 christos int index; 294 1.1 christos /* Lg group base size (no deltas added). */ 295 1.1 christos int lg_base; 296 1.1 christos /* Lg delta to previous size class. */ 297 1.1 christos int lg_delta; 298 1.1 christos /* Delta multiplier. size == 1<<lg_base + ndelta<<lg_delta */ 299 1.1 christos int ndelta; 300 1.1 christos /* 301 1.1 christos * True if the size class is a multiple of the page size, false 302 1.1 christos * otherwise. 303 1.1 christos */ 304 1.1 christos bool psz; 305 1.1 christos /* 306 1.1 christos * True if the size class is a small, bin, size class. False otherwise. 307 1.1 christos */ 308 1.1 christos bool bin; 309 1.1 christos /* The slab page count if a small bin size class, 0 otherwise. */ 310 1.1 christos int pgs; 311 1.1 christos /* Same as lg_delta if a lookup table size class, 0 otherwise. */ 312 1.1 christos int lg_delta_lookup; 313 1.1 christos }; 314 1.1 christos 315 1.1 christos typedef struct sc_data_s sc_data_t; 316 1.1 christos struct sc_data_s { 317 1.1 christos /* Number of tiny size classes. */ 318 1.1 christos unsigned ntiny; 319 1.1 christos /* Number of bins supported by the lookup table. */ 320 1.1 christos int nlbins; 321 1.1 christos /* Number of small size class bins. */ 322 1.1 christos int nbins; 323 1.1 christos /* Number of size classes. */ 324 1.1 christos int nsizes; 325 1.1 christos /* Number of bits required to store NSIZES. */ 326 1.1 christos int lg_ceil_nsizes; 327 1.1 christos /* Number of size classes that are a multiple of (1U << LG_PAGE). */ 328 1.1 christos unsigned npsizes; 329 1.1 christos /* Lg of maximum tiny size class (or -1, if none). */ 330 1.1 christos int lg_tiny_maxclass; 331 1.1 christos /* Maximum size class included in lookup table. */ 332 1.1 christos size_t lookup_maxclass; 333 1.1 christos /* Maximum small size class. */ 334 1.1 christos size_t small_maxclass; 335 1.1 christos /* Lg of minimum large size class. */ 336 1.1 christos int lg_large_minclass; 337 1.1 christos /* The minimum large size class. */ 338 1.1 christos size_t large_minclass; 339 1.1 christos /* Maximum (large) size class. */ 340 1.1 christos size_t large_maxclass; 341 1.1 christos /* True if the sc_data_t has been initialized (for debugging only). */ 342 1.1 christos bool initialized; 343 1.1 christos 344 1.1 christos sc_t sc[SC_NSIZES]; 345 1.1 christos }; 346 1.1 christos 347 1.1 christos size_t reg_size_compute(int lg_base, int lg_delta, int ndelta); 348 1.1 christos void sc_data_init(sc_data_t *data); 349 1.1 christos /* 350 1.1 christos * Updates slab sizes in [begin, end] to be pgs pages in length, if possible. 351 1.1 christos * Otherwise, does its best to accommodate the request. 352 1.1 christos */ 353 1.1 christos void sc_data_update_slab_size(sc_data_t *data, size_t begin, size_t end, 354 1.1 christos int pgs); 355 1.1 christos void sc_boot(sc_data_t *data); 356 1.1 christos 357 1.1 christos #endif /* JEMALLOC_INTERNAL_SC_H */ 358