* Compute usable size that would result from allocating an object with the * specified size and alignment. */
| 260 | * specified size and alignment. |
| 261 | */ |
| 262 | JEMALLOC_ALWAYS_INLINE size_t |
| 263 | sz_sa2u(size_t size, size_t alignment) { |
| 264 | size_t usize; |
| 265 | |
| 266 | assert(alignment != 0 && ((alignment - 1) & alignment) == 0); |
| 267 | |
| 268 | /* Try for a small size class. */ |
| 269 | if (size <= SC_SMALL_MAXCLASS && alignment < PAGE) { |
| 270 | /* |
| 271 | * Round size up to the nearest multiple of alignment. |
| 272 | * |
| 273 | * This done, we can take advantage of the fact that for each |
| 274 | * small size class, every object is aligned at the smallest |
| 275 | * power of two that is non-zero in the base two representation |
| 276 | * of the size. For example: |
| 277 | * |
| 278 | * Size | Base 2 | Minimum alignment |
| 279 | * -----+----------+------------------ |
| 280 | * 96 | 1100000 | 32 |
| 281 | * 144 | 10100000 | 32 |
| 282 | * 192 | 11000000 | 64 |
| 283 | */ |
| 284 | usize = sz_s2u(ALIGNMENT_CEILING(size, alignment)); |
| 285 | if (usize < SC_LARGE_MINCLASS) { |
| 286 | return usize; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* Large size class. Beware of overflow. */ |
| 291 | |
| 292 | if (unlikely(alignment > SC_LARGE_MAXCLASS)) { |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* Make sure result is a large size class. */ |
| 297 | if (size <= SC_LARGE_MINCLASS) { |
| 298 | usize = SC_LARGE_MINCLASS; |
| 299 | } else { |
| 300 | usize = sz_s2u(size); |
| 301 | if (usize < size) { |
| 302 | /* size_t overflow. */ |
| 303 | return 0; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Calculate the multi-page mapping that large_palloc() would need in |
| 309 | * order to guarantee the alignment. |
| 310 | */ |
| 311 | if (usize + sz_large_pad + PAGE_CEILING(alignment) - PAGE < usize) { |
| 312 | /* size_t overflow. */ |
| 313 | return 0; |
| 314 | } |
| 315 | return usize; |
| 316 | } |
| 317 | |
| 318 | #endif /* JEMALLOC_INTERNAL_SIZE_H */ |
no test coverage detected