Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use a more efficient power of 2 check. #274

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ds/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace snmalloc
template<size_t alignment>
static inline bool is_aligned_block(void* p, size_t size)
{
static_assert(bits::next_pow2_const(alignment) == alignment);
static_assert(bits::is_pow2(alignment));

return ((address_cast(p) | size) & (alignment - 1)) == 0;
}
Expand All @@ -62,7 +62,7 @@ namespace snmalloc
SNMALLOC_FAST_PATH T* pointer_align_down(void* p)
{
static_assert(alignment > 0);
static_assert(bits::next_pow2_const(alignment) == alignment);
static_assert(bits::is_pow2(alignment));
if constexpr (alignment == 1)
return static_cast<T*>(p);
else
Expand All @@ -84,7 +84,7 @@ namespace snmalloc
inline T* pointer_align_up(void* p)
{
static_assert(alignment > 0);
static_assert(bits::next_pow2_const(alignment) == alignment);
static_assert(bits::is_pow2(alignment));
if constexpr (alignment == 1)
return static_cast<T*>(p);
else
Expand All @@ -106,7 +106,7 @@ namespace snmalloc
SNMALLOC_FAST_PATH T* pointer_align_down(void* p, size_t alignment)
{
SNMALLOC_ASSERT(alignment > 0);
SNMALLOC_ASSERT(bits::next_pow2(alignment) == alignment);
SNMALLOC_ASSERT(bits::is_pow2(alignment));
#if __has_builtin(__builtin_align_down)
return static_cast<T*>(__builtin_align_down(p, alignment));
#else
Expand All @@ -123,7 +123,7 @@ namespace snmalloc
inline T* pointer_align_up(void* p, size_t alignment)
{
SNMALLOC_ASSERT(alignment > 0);
SNMALLOC_ASSERT(bits::next_pow2(alignment) == alignment);
SNMALLOC_ASSERT(bits::is_pow2(alignment));
#if __has_builtin(__builtin_align_up)
return static_cast<T*>(__builtin_align_up(p, alignment));
#else
Expand Down
9 changes: 7 additions & 2 deletions src/ds/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ namespace snmalloc
#endif
}

constexpr SNMALLOC_FAST_PATH bool is_pow2(size_t x)
{
return (x & (x - 1)) == 0;
}

SNMALLOC_FAST_PATH size_t next_pow2(size_t x)
{
// Correct for numbers [0..MAX_SIZE >> 1).
Expand Down Expand Up @@ -244,7 +249,7 @@ namespace snmalloc
constexpr SNMALLOC_FAST_PATH size_t
align_down(size_t value, size_t alignment)
{
SNMALLOC_ASSERT(next_pow2_const(alignment) == alignment);
SNMALLOC_ASSERT(is_pow2(alignment));

size_t align_1 = alignment - 1;
value &= ~align_1;
Expand All @@ -253,7 +258,7 @@ namespace snmalloc

constexpr SNMALLOC_FAST_PATH size_t align_up(size_t value, size_t alignment)
{
SNMALLOC_ASSERT(next_pow2_const(alignment) == alignment);
SNMALLOC_ASSERT(is_pow2(alignment));

size_t align_1 = alignment - 1;
value += align_1;
Expand Down
3 changes: 1 addition & 2 deletions src/ds/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ namespace snmalloc
template<size_t length, typename T>
class Mod
{
static_assert(
length == bits::next_pow2_const(length), "Must be a power of two.");
static_assert(bits::is_pow2(length), "Must be a power of two.");

private:
T value = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/mem/address_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ namespace snmalloc
template<bool committed>
void* reserve(size_t size)
{
SNMALLOC_ASSERT(bits::next_pow2(size) == size);
SNMALLOC_ASSERT(bits::is_pow2(size));
SNMALLOC_ASSERT(size >= sizeof(void*));

if constexpr (pal_supports<AlignedAllocation, PAL>)
Expand Down
2 changes: 1 addition & 1 deletion src/mem/sizeclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ namespace snmalloc
// Client responsible for checking alignment is not zero
SNMALLOC_ASSERT(alignment != 0);
// Client responsible for checking alignment is a power of two
SNMALLOC_ASSERT(bits::next_pow2(alignment) == alignment);
SNMALLOC_ASSERT(bits::is_pow2(alignment));

return ((alignment - 1) | (size - 1)) + 1;
}
Expand Down
3 changes: 1 addition & 2 deletions src/pal/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ namespace snmalloc
static constexpr size_t OS_PAGE_SIZE = Pal::page_size;

static_assert(
bits::next_pow2_const(OS_PAGE_SIZE) == OS_PAGE_SIZE,
"OS_PAGE_SIZE must be a power of two");
bits::is_pow2(OS_PAGE_SIZE), "OS_PAGE_SIZE must be a power of two");
static_assert(
OS_PAGE_SIZE % Aal::smallest_page_size == 0,
"The smallest architectural page size must divide OS_PAGE_SIZE");
Expand Down
2 changes: 1 addition & 1 deletion src/pal/pal_bsd_aligned.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace snmalloc
static void* reserve_aligned(size_t size) noexcept
{
// Alignment must be a power of 2.
SNMALLOC_ASSERT(size == bits::next_pow2(size));
SNMALLOC_ASSERT(bits::is_pow2(size));
SNMALLOC_ASSERT(size >= minimum_alloc_size);

size_t log2align = bits::next_pow2_bits(size);
Expand Down
2 changes: 1 addition & 1 deletion src/pal/pal_freebsd_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace snmalloc
template<bool committed>
static void* reserve_aligned(size_t size) noexcept
{
SNMALLOC_ASSERT(size == bits::next_pow2(size));
SNMALLOC_ASSERT(bits::is_pow2(size));
SNMALLOC_ASSERT(size >= minimum_alloc_size);
size_t align = size;

Expand Down
2 changes: 1 addition & 1 deletion src/pal/pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ namespace snmalloc
*/
static std::pair<void*, size_t> reserve_at_least(size_t size) noexcept
{
SNMALLOC_ASSERT(size == bits::next_pow2(size));
SNMALLOC_ASSERT(bits::is_pow2(size));

// Magic number for over-allocating chosen by the Pal
// These should be further refined based on experiments.
Expand Down
4 changes: 2 additions & 2 deletions src/pal/pal_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace snmalloc
template<bool committed>
static void* reserve_aligned(size_t size) noexcept
{
SNMALLOC_ASSERT(size == bits::next_pow2(size));
SNMALLOC_ASSERT(bits::is_pow2(size));
SNMALLOC_ASSERT(size >= minimum_alloc_size);

DWORD flags = MEM_RESERVE;
Expand Down Expand Up @@ -215,7 +215,7 @@ namespace snmalloc
# else
static std::pair<void*, size_t> reserve_at_least(size_t size) noexcept
{
SNMALLOC_ASSERT(size == bits::next_pow2(size));
SNMALLOC_ASSERT(bits::is_pow2(size));

// Magic number for over-allocating chosen by the Pal
// These should be further refined based on experiments.
Expand Down