* Returns true if the allocation will overflow, and false otherwise. Sets * *size to the product either way. */
| 1987 | * *size to the product either way. |
| 1988 | */ |
| 1989 | JEMALLOC_ALWAYS_INLINE bool |
| 1990 | compute_size_with_overflow(bool may_overflow, dynamic_opts_t *dopts, |
| 1991 | size_t *size) { |
| 1992 | /* |
| 1993 | * This function is just num_items * item_size, except that we may have |
| 1994 | * to check for overflow. |
| 1995 | */ |
| 1996 | |
| 1997 | if (!may_overflow) { |
| 1998 | assert(dopts->num_items == 1); |
| 1999 | *size = dopts->item_size; |
| 2000 | return false; |
| 2001 | } |
| 2002 | |
| 2003 | /* A size_t with its high-half bits all set to 1. */ |
| 2004 | static const size_t high_bits = SIZE_T_MAX << (sizeof(size_t) * 8 / 2); |
| 2005 | |
| 2006 | *size = dopts->item_size * dopts->num_items; |
| 2007 | |
| 2008 | if (unlikely(*size == 0)) { |
| 2009 | return (dopts->num_items != 0 && dopts->item_size != 0); |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * We got a non-zero size, but we don't know if we overflowed to get |
| 2014 | * there. To avoid having to do a divide, we'll be clever and note that |
| 2015 | * if both A and B can be represented in N/2 bits, then their product |
| 2016 | * can be represented in N bits (without the possibility of overflow). |
| 2017 | */ |
| 2018 | if (likely((high_bits & (dopts->num_items | dopts->item_size)) == 0)) { |
| 2019 | return false; |
| 2020 | } |
| 2021 | if (likely(*size / dopts->item_size == dopts->num_items)) { |
| 2022 | return false; |
| 2023 | } |
| 2024 | return true; |
| 2025 | } |
| 2026 | |
| 2027 | JEMALLOC_ALWAYS_INLINE int |
| 2028 | imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) { |