* Returns true if the allocation will overflow, and false otherwise. Sets * *size to the product either way. */
| 1687 | * *size to the product either way. |
| 1688 | */ |
| 1689 | JEMALLOC_ALWAYS_INLINE bool |
| 1690 | compute_size_with_overflow(bool may_overflow, dynamic_opts_t *dopts, |
| 1691 | size_t *size) { |
| 1692 | /* |
| 1693 | * This function is just num_items * item_size, except that we may have |
| 1694 | * to check for overflow. |
| 1695 | */ |
| 1696 | |
| 1697 | if (!may_overflow) { |
| 1698 | assert(dopts->num_items == 1); |
| 1699 | *size = dopts->item_size; |
| 1700 | return false; |
| 1701 | } |
| 1702 | |
| 1703 | /* A size_t with its high-half bits all set to 1. */ |
| 1704 | const static size_t high_bits = SIZE_T_MAX << (sizeof(size_t) * 8 / 2); |
| 1705 | |
| 1706 | *size = dopts->item_size * dopts->num_items; |
| 1707 | |
| 1708 | if (unlikely(*size == 0)) { |
| 1709 | return (dopts->num_items != 0 && dopts->item_size != 0); |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * We got a non-zero size, but we don't know if we overflowed to get |
| 1714 | * there. To avoid having to do a divide, we'll be clever and note that |
| 1715 | * if both A and B can be represented in N/2 bits, then their product |
| 1716 | * can be represented in N bits (without the possibility of overflow). |
| 1717 | */ |
| 1718 | if (likely((high_bits & (dopts->num_items | dopts->item_size)) == 0)) { |
| 1719 | return false; |
| 1720 | } |
| 1721 | if (likely(*size / dopts->item_size == dopts->num_items)) { |
| 1722 | return false; |
| 1723 | } |
| 1724 | return true; |
| 1725 | } |
| 1726 | |
| 1727 | JEMALLOC_ALWAYS_INLINE int |
| 1728 | imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) { |