MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / compute_size_with_overflow

Function compute_size_with_overflow

deps/memkind/src/jemalloc/src/jemalloc.c:1689–1725  ·  view source on GitHub ↗

* Returns true if the allocation will overflow, and false otherwise. Sets * *size to the product either way. */

Source from the content-addressed store, hash-verified

1687 * *size to the product either way.
1688 */
1689JEMALLOC_ALWAYS_INLINE bool
1690compute_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
1727JEMALLOC_ALWAYS_INLINE int
1728imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {

Callers 1

imalloc_bodyFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected