1 #ifndef JEMALLOC_INTERNAL_SIZE_H
2 #define JEMALLOC_INTERNAL_SIZE_H
4 #include "jemalloc/internal/bit_util.h"
5 #include "jemalloc/internal/pages.h"
6 #include "jemalloc/internal/size_classes.h"
7 #include "jemalloc/internal/util.h"
10 * sz module: Size computations.
12 * Some abbreviations used here:
19 * These are not always used completely consistently, but should be enough to
20 * interpret function names. E.g. sz_psz2ind converts page size to page size
21 * index; sz_sa2u converts a (size, alignment) allocation request to the usable
22 * size that would result from such an allocation.
26 * sz_pind2sz_tab encodes the same information as could be computed by
27 * sz_pind2sz_compute().
29 extern size_t const sz_pind2sz_tab[NPSIZES+1];
31 * sz_index2size_tab encodes the same information as could be computed (at
32 * unacceptable cost in some code paths) by sz_index2size_compute().
34 extern size_t const sz_index2size_tab[NSIZES];
36 * sz_size2index_tab is a compact lookup table that rounds request sizes up to
37 * size classes. In order to reduce cache footprint, the table is compressed,
38 * and all accesses are via sz_size2index().
40 extern uint8_t const sz_size2index_tab[];
42 static const size_t sz_large_pad =
43 #ifdef JEMALLOC_CACHE_OBLIVIOUS
50 JEMALLOC_ALWAYS_INLINE pszind_t
51 sz_psz2ind(size_t psz) {
52 if (unlikely(psz > LARGE_MAXCLASS)) {
56 pszind_t x = lg_floor((psz<<1)-1);
57 pszind_t shift = (x < LG_SIZE_CLASS_GROUP + LG_PAGE) ? 0 : x -
58 (LG_SIZE_CLASS_GROUP + LG_PAGE);
59 pszind_t grp = shift << LG_SIZE_CLASS_GROUP;
61 pszind_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_PAGE + 1) ?
62 LG_PAGE : x - LG_SIZE_CLASS_GROUP - 1;
64 size_t delta_inverse_mask = ZU(-1) << lg_delta;
65 pszind_t mod = ((((psz-1) & delta_inverse_mask) >> lg_delta)) &
66 ((ZU(1) << LG_SIZE_CLASS_GROUP) - 1);
68 pszind_t ind = grp + mod;
74 sz_pind2sz_compute(pszind_t pind) {
75 if (unlikely(pind == NPSIZES)) {
76 return LARGE_MAXCLASS + PAGE;
79 size_t grp = pind >> LG_SIZE_CLASS_GROUP;
80 size_t mod = pind & ((ZU(1) << LG_SIZE_CLASS_GROUP) - 1);
82 size_t grp_size_mask = ~((!!grp)-1);
83 size_t grp_size = ((ZU(1) << (LG_PAGE +
84 (LG_SIZE_CLASS_GROUP-1))) << grp) & grp_size_mask;
86 size_t shift = (grp == 0) ? 1 : grp;
87 size_t lg_delta = shift + (LG_PAGE-1);
88 size_t mod_size = (mod+1) << lg_delta;
90 size_t sz = grp_size + mod_size;
96 sz_pind2sz_lookup(pszind_t pind) {
97 size_t ret = (size_t)sz_pind2sz_tab[pind];
98 assert(ret == sz_pind2sz_compute(pind));
103 sz_pind2sz(pszind_t pind) {
104 assert(pind < NPSIZES+1);
105 return sz_pind2sz_lookup(pind);
109 sz_psz2u(size_t psz) {
110 if (unlikely(psz > LARGE_MAXCLASS)) {
111 return LARGE_MAXCLASS + PAGE;
114 size_t x = lg_floor((psz<<1)-1);
115 size_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_PAGE + 1) ?
116 LG_PAGE : x - LG_SIZE_CLASS_GROUP - 1;
117 size_t delta = ZU(1) << lg_delta;
118 size_t delta_mask = delta - 1;
119 size_t usize = (psz + delta_mask) & ~delta_mask;
124 static inline szind_t
125 sz_size2index_compute(size_t size) {
126 if (unlikely(size > LARGE_MAXCLASS)) {
130 if (size <= (ZU(1) << LG_TINY_MAXCLASS)) {
131 szind_t lg_tmin = LG_TINY_MAXCLASS - NTBINS + 1;
132 szind_t lg_ceil = lg_floor(pow2_ceil_zu(size));
133 return (lg_ceil < lg_tmin ? 0 : lg_ceil - lg_tmin);
137 szind_t x = lg_floor((size<<1)-1);
138 szind_t shift = (x < LG_SIZE_CLASS_GROUP + LG_QUANTUM) ? 0 :
139 x - (LG_SIZE_CLASS_GROUP + LG_QUANTUM);
140 szind_t grp = shift << LG_SIZE_CLASS_GROUP;
142 szind_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_QUANTUM + 1)
143 ? LG_QUANTUM : x - LG_SIZE_CLASS_GROUP - 1;
145 size_t delta_inverse_mask = ZU(-1) << lg_delta;
146 szind_t mod = ((((size-1) & delta_inverse_mask) >> lg_delta)) &
147 ((ZU(1) << LG_SIZE_CLASS_GROUP) - 1);
149 szind_t index = NTBINS + grp + mod;
154 JEMALLOC_ALWAYS_INLINE szind_t
155 sz_size2index_lookup(size_t size) {
156 assert(size <= LOOKUP_MAXCLASS);
158 szind_t ret = (sz_size2index_tab[(size-1) >> LG_TINY_MIN]);
159 assert(ret == sz_size2index_compute(size));
164 JEMALLOC_ALWAYS_INLINE szind_t
165 sz_size2index(size_t size) {
167 if (likely(size <= LOOKUP_MAXCLASS)) {
168 return sz_size2index_lookup(size);
170 return sz_size2index_compute(size);
174 sz_index2size_compute(szind_t index) {
176 if (index < NTBINS) {
177 return (ZU(1) << (LG_TINY_MAXCLASS - NTBINS + 1 + index));
181 size_t reduced_index = index - NTBINS;
182 size_t grp = reduced_index >> LG_SIZE_CLASS_GROUP;
183 size_t mod = reduced_index & ((ZU(1) << LG_SIZE_CLASS_GROUP) -
186 size_t grp_size_mask = ~((!!grp)-1);
187 size_t grp_size = ((ZU(1) << (LG_QUANTUM +
188 (LG_SIZE_CLASS_GROUP-1))) << grp) & grp_size_mask;
190 size_t shift = (grp == 0) ? 1 : grp;
191 size_t lg_delta = shift + (LG_QUANTUM-1);
192 size_t mod_size = (mod+1) << lg_delta;
194 size_t usize = grp_size + mod_size;
199 JEMALLOC_ALWAYS_INLINE size_t
200 sz_index2size_lookup(szind_t index) {
201 size_t ret = (size_t)sz_index2size_tab[index];
202 assert(ret == sz_index2size_compute(index));
206 JEMALLOC_ALWAYS_INLINE size_t
207 sz_index2size(szind_t index) {
208 assert(index < NSIZES);
209 return sz_index2size_lookup(index);
212 JEMALLOC_ALWAYS_INLINE size_t
213 sz_s2u_compute(size_t size) {
214 if (unlikely(size > LARGE_MAXCLASS)) {
218 if (size <= (ZU(1) << LG_TINY_MAXCLASS)) {
219 size_t lg_tmin = LG_TINY_MAXCLASS - NTBINS + 1;
220 size_t lg_ceil = lg_floor(pow2_ceil_zu(size));
221 return (lg_ceil < lg_tmin ? (ZU(1) << lg_tmin) :
226 size_t x = lg_floor((size<<1)-1);
227 size_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_QUANTUM + 1)
228 ? LG_QUANTUM : x - LG_SIZE_CLASS_GROUP - 1;
229 size_t delta = ZU(1) << lg_delta;
230 size_t delta_mask = delta - 1;
231 size_t usize = (size + delta_mask) & ~delta_mask;
236 JEMALLOC_ALWAYS_INLINE size_t
237 sz_s2u_lookup(size_t size) {
238 size_t ret = sz_index2size_lookup(sz_size2index_lookup(size));
240 assert(ret == sz_s2u_compute(size));
245 * Compute usable size that would result from allocating an object with the
248 JEMALLOC_ALWAYS_INLINE size_t
249 sz_s2u(size_t size) {
251 if (likely(size <= LOOKUP_MAXCLASS)) {
252 return sz_s2u_lookup(size);
254 return sz_s2u_compute(size);
258 * Compute usable size that would result from allocating an object with the
259 * specified size and alignment.
261 JEMALLOC_ALWAYS_INLINE size_t
262 sz_sa2u(size_t size, size_t alignment) {
265 assert(alignment != 0 && ((alignment - 1) & alignment) == 0);
267 /* Try for a small size class. */
268 if (size <= SMALL_MAXCLASS && alignment < PAGE) {
270 * Round size up to the nearest multiple of alignment.
272 * This done, we can take advantage of the fact that for each
273 * small size class, every object is aligned at the smallest
274 * power of two that is non-zero in the base two representation
275 * of the size. For example:
277 * Size | Base 2 | Minimum alignment
278 * -----+----------+------------------
280 * 144 | 10100000 | 32
281 * 192 | 11000000 | 64
283 usize = sz_s2u(ALIGNMENT_CEILING(size, alignment));
284 if (usize < LARGE_MINCLASS) {
289 /* Large size class. Beware of overflow. */
291 if (unlikely(alignment > LARGE_MAXCLASS)) {
295 /* Make sure result is a large size class. */
296 if (size <= LARGE_MINCLASS) {
297 usize = LARGE_MINCLASS;
299 usize = sz_s2u(size);
301 /* size_t overflow. */
307 * Calculate the multi-page mapping that large_palloc() would need in
308 * order to guarantee the alignment.
310 if (usize + sz_large_pad + PAGE_CEILING(alignment) - PAGE < usize) {
311 /* size_t overflow. */
317 #endif /* JEMALLOC_INTERNAL_SIZE_H */