diff --git a/src/mem/allocconfig.h b/src/mem/allocconfig.h index fd0edc24..6238af32 100644 --- a/src/mem/allocconfig.h +++ b/src/mem/allocconfig.h @@ -110,8 +110,9 @@ namespace snmalloc #endif // Minimum allocation size is space for two pointers. - static constexpr size_t MIN_ALLOC_BITS = bits::is64() ? 4 : 3; - static constexpr size_t MIN_ALLOC_SIZE = 1 << MIN_ALLOC_BITS; + static_assert(bits::next_pow2_const(sizeof(void*)) == sizeof(void*)); + static constexpr size_t MIN_ALLOC_SIZE = 2 * sizeof(void*); + static constexpr size_t MIN_ALLOC_BITS = bits::ctz_const(MIN_ALLOC_SIZE); // Slabs are 64 KiB unless constrained to 16 KiB. static constexpr size_t SLAB_BITS = ADDRESS_SPACE_CONSTRAINED ? 14 : 16; diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index 990a3236..0f959cba 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -142,12 +142,12 @@ namespace snmalloc static Slab* get_slab(void* p) { - return pointer_cast(address_cast(p) & SLAB_MASK); + return pointer_align_down(p); } static bool is_short(Slab* p) { - return (address_cast(p) & SUPERSLAB_MASK) == address_cast(p); + return pointer_align_down(p) == p; } /** diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index 8526c094..ce2aa5e9 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -191,7 +191,7 @@ namespace snmalloc return std::pair(nullptr, 0); shift -= BITS_PER_INDEX_LEVEL; - ix = (addr >> shift) & ENTRIES_MASK; + ix = (static_cast(addr) >> shift) & ENTRIES_MASK; e = &value->entries[ix]; if constexpr (INDEX_LEVELS == 1) @@ -211,7 +211,7 @@ namespace snmalloc return std::pair(nullptr, 0); shift -= BITS_FOR_LEAF; - ix = (addr >> shift) & LEAF_MASK; + ix = (static_cast(addr) >> shift) & LEAF_MASK; return std::pair(leaf, ix); } @@ -387,7 +387,7 @@ namespace snmalloc */ size_t index_for_address(uintptr_t p) { - return reinterpret_cast((p >> SHIFT) & (OS_PAGE_SIZE - 1)); + return bits::align_down(static_cast(p) >> SHIFT, OS_PAGE_SIZE); } /**