Move bits::is_aligned_block to address.h

And chase consequences
This commit is contained in:
Nathaniel Filardo
2019-11-12 15:12:48 +00:00
parent 261249d9cb
commit 20e804728b
9 changed files with 34 additions and 28 deletions

View File

@@ -1,4 +1,7 @@
#pragma once
#include "bits.h"
#include <cassert>
#include <cstdint>
namespace snmalloc
@@ -39,4 +42,18 @@ namespace snmalloc
{
return reinterpret_cast<T*>(address);
}
/**
* Test if a pointer is aligned to a given size, which must be a power of
* two.
*/
template<size_t alignment>
static inline bool is_aligned_block(void* p, size_t size)
{
static_assert(bits::next_pow2_const(alignment) == alignment);
return ((static_cast<size_t>(address_cast(p)) | size) & (alignment - 1)) ==
0;
}
} // namespace snmalloc

View File

@@ -6,7 +6,6 @@
// #define USE_LZCNT
#include "../aal/aal.h"
#include "address.h"
#include "defines.h"
#include <atomic>
@@ -240,15 +239,6 @@ namespace snmalloc
return value;
}
template<size_t alignment>
static inline bool is_aligned_block(void* p, size_t size)
{
assert(next_pow2(alignment) == alignment);
return ((static_cast<size_t>(address_cast(p)) | size) &
(alignment - 1)) == 0;
}
/************************************************
*
* Map large range of strictly positive integers

View File

@@ -682,7 +682,7 @@ namespace snmalloc
// All medium size classes are page aligned.
if (i > NUM_SMALL_CLASSES)
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(nullptr, size1));
assert(is_aligned_block<OS_PAGE_SIZE>(nullptr, size1));
}
assert(sc1 == i);

View File

@@ -84,7 +84,7 @@ namespace snmalloc
void* p = pointer_offset(this, (static_cast<size_t>(index) << 8));
free--;
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, OS_PAGE_SIZE));
assert(is_aligned_block<OS_PAGE_SIZE>(p, OS_PAGE_SIZE));
size = bits::align_up(size, OS_PAGE_SIZE);
if constexpr (decommit_strategy == DecommitAll)

View File

@@ -32,9 +32,9 @@ namespace snmalloc
template<bool page_aligned = false>
void zero(void* p, size_t size)
{
if (page_aligned || bits::is_aligned_block<OS_PAGE_SIZE>(p, size))
if (page_aligned || is_aligned_block<OS_PAGE_SIZE>(p, size))
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size));
void* r = mmap(
p,
size,

View File

@@ -33,7 +33,7 @@ namespace snmalloc
*/
void notify_not_using(void* p, size_t size) noexcept
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size));
madvise(p, size, MADV_FREE);
}
};

View File

@@ -37,9 +37,9 @@ namespace snmalloc
template<bool page_aligned = false>
void zero(void* p, size_t size) noexcept
{
if (page_aligned || bits::is_aligned_block<OS_PAGE_SIZE>(p, size))
if (page_aligned || is_aligned_block<OS_PAGE_SIZE>(p, size))
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size));
madvise(p, size, MADV_DONTNEED);
}
else

View File

@@ -1,6 +1,6 @@
#pragma once
#include "../ds/bits.h"
#include "../ds/address.h"
#include "../mem/allocconfig.h"
#include <string.h>
@@ -53,7 +53,7 @@ namespace snmalloc
*/
void notify_not_using(void* p, size_t size) noexcept
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size));
UNUSED(p);
UNUSED(size);
}
@@ -68,8 +68,7 @@ namespace snmalloc
template<ZeroMem zero_mem>
void notify_using(void* p, size_t size) noexcept
{
assert(
bits::is_aligned_block<OS_PAGE_SIZE>(p, size) || (zero_mem == NoZero));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size) || (zero_mem == NoZero));
if constexpr (zero_mem == YesZero)
static_cast<OS*>(this)->template zero<true>(p, size);
@@ -94,9 +93,9 @@ namespace snmalloc
template<bool page_aligned = false>
void zero(void* p, size_t size) noexcept
{
if (page_aligned || bits::is_aligned_block<OS_PAGE_SIZE>(p, size))
if (page_aligned || is_aligned_block<OS_PAGE_SIZE>(p, size))
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size));
void* r = mmap(
p,
size,

View File

@@ -1,5 +1,6 @@
#pragma once
#include "../ds/address.h"
#include "../ds/bits.h"
#include "../mem/allocconfig.h"
@@ -107,7 +108,7 @@ namespace snmalloc
/// Notify platform that we will not be using these pages
void notify_not_using(void* p, size_t size) noexcept
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size));
BOOL ok = VirtualFree(p, size, MEM_DECOMMIT);
@@ -119,8 +120,7 @@ namespace snmalloc
template<ZeroMem zero_mem>
void notify_using(void* p, size_t size) noexcept
{
assert(
bits::is_aligned_block<OS_PAGE_SIZE>(p, size) || (zero_mem == NoZero));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size) || (zero_mem == NoZero));
void* r = VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE);
@@ -132,9 +132,9 @@ namespace snmalloc
template<bool page_aligned = false>
void zero(void* p, size_t size) noexcept
{
if (page_aligned || bits::is_aligned_block<OS_PAGE_SIZE>(p, size))
if (page_aligned || is_aligned_block<OS_PAGE_SIZE>(p, size))
{
assert(bits::is_aligned_block<OS_PAGE_SIZE>(p, size));
assert(is_aligned_block<OS_PAGE_SIZE>(p, size));
notify_not_using(p, size);
notify_using<YesZero>(p, size);
}