Skip to content

Commit

Permalink
ds_core/bits: add mask_bits; convert one_at_bit-s
Browse files Browse the repository at this point in the history
  • Loading branch information
nwf-msr committed Jun 13, 2024
1 parent c1e0cce commit 5c31127
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/snmalloc/backend_helpers/largebuddyrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ namespace snmalloc
SNMALLOC_ASSERT(size >= MIN_CHUNK_SIZE);
SNMALLOC_ASSERT(bits::is_pow2(size));

if (size >= (bits::one_at_bit(MAX_SIZE_BITS) - 1))
if (size >= bits::mask_bits(MAX_SIZE_BITS))
{
if (ParentRange::Aligned)
return parent.alloc_range(size);
Expand All @@ -378,7 +378,7 @@ namespace snmalloc

if constexpr (MAX_SIZE_BITS != (bits::BITS - 1))
{
if (size >= (bits::one_at_bit(MAX_SIZE_BITS) - 1))
if (size >= bits::mask_bits(MAX_SIZE_BITS))
{
parent_dealloc_range(base, size);
return;
Expand Down
17 changes: 15 additions & 2 deletions src/snmalloc/ds_core/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ namespace snmalloc
return (static_cast<T>(1)) << shift;
}

/**
* Returns a value of type T that has its n LSBs all set.
*
* S is a template parameter because callers use either `int` or `size_t`
* and either is valid to represent a number in the range 0-63 (or 0-127 if
* we want to use `__uint128_t` as `T`).
*/
template<typename T = size_t, typename S>
constexpr T mask_bits(S n)
{
return one_at_bit<T>(n) - 1;
}

inline SNMALLOC_FAST_PATH size_t clz(size_t x)
{
SNMALLOC_ASSERT(x != 0); // Calling with 0 is UB on some implementations
Expand Down Expand Up @@ -326,7 +339,7 @@ namespace snmalloc
constexpr size_t to_exp_mant_const(size_t value)
{
constexpr size_t LEADING_BIT = one_at_bit(MANTISSA_BITS + LOW_BITS) >> 1;
constexpr size_t MANTISSA_MASK = one_at_bit(MANTISSA_BITS) - 1;
constexpr size_t MANTISSA_MASK = mask_bits(MANTISSA_BITS);

value = value - 1;

Expand All @@ -344,7 +357,7 @@ namespace snmalloc
if (MANTISSA_BITS > 0)
{
m_e = m_e + 1;
constexpr size_t MANTISSA_MASK = one_at_bit(MANTISSA_BITS) - 1;
constexpr size_t MANTISSA_MASK = mask_bits(MANTISSA_BITS);
size_t m = m_e & MANTISSA_MASK;
size_t e = m_e >> MANTISSA_BITS;
size_t b = e == 0 ? 0 : 1;
Expand Down
3 changes: 1 addition & 2 deletions src/snmalloc/mem/entropy.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ namespace snmalloc
fresh_bits = get_next();
count = 64;
}
uint16_t result =
static_cast<uint16_t>(fresh_bits & (bits::one_at_bit(n) - 1));
uint16_t result = static_cast<uint16_t>(fresh_bits & bits::mask_bits(n));
fresh_bits >>= n;
count -= n;
return result;
Expand Down
5 changes: 2 additions & 3 deletions src/snmalloc/mem/sizeclasstable.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace snmalloc
size_t slab_bits = bits::max(
bits::next_pow2_bits_const(MIN_OBJECT_COUNT * rsize), MIN_CHUNK_BITS);

meta.slab_mask = bits::one_at_bit(slab_bits) - 1;
meta.slab_mask = bits::mask_bits(slab_bits);

auto& meta_slow = slow(sizeclass_t::from_small_class(sizeclass));
meta_slow.capacity =
Expand All @@ -245,8 +245,7 @@ namespace snmalloc
{
// Calculate reciprocal division constant.
auto& meta = fast_small(sizeclass);
meta.div_mult =
((bits::one_at_bit(DIV_MULT_SHIFT) - 1) / meta.size) + 1;
meta.div_mult = (bits::mask_bits(DIV_MULT_SHIFT) / meta.size) + 1;

size_t zero = 0;
meta.mod_zero_mult = (~zero / meta.size) + 1;
Expand Down

0 comments on commit 5c31127

Please sign in to comment.