Add assertion for initialisation to the pagemap (#560)

This commit is contained in:
Matthew Parkinson
2022-09-21 11:14:41 +01:00
committed by GitHub
parent 38d4483b27
commit fb85216386
2 changed files with 18 additions and 0 deletions

View File

@@ -58,6 +58,8 @@ namespace snmalloc
*/
void register_range(address_t p, size_t length)
{
SNMALLOC_ASSERT(is_initialised());
// Calculate range in pagemap that is associated to this space.
auto first = &body[p >> SHIFT];
auto last = &body[(p + length + bits::one_at_bit(SHIFT) - 1) >> SHIFT];
@@ -96,6 +98,8 @@ namespace snmalloc
template<bool has_bounds_ = has_bounds>
std::enable_if_t<!has_bounds_> init(T* address)
{
SNMALLOC_ASSERT(!is_initialised());
static_assert(
has_bounds_ == has_bounds, "Don't set SFINAE template parameter!");
body = address;
@@ -111,6 +115,8 @@ namespace snmalloc
std::enable_if_t<has_bounds_, std::pair<void*, size_t>>
init(void* b, size_t s)
{
SNMALLOC_ASSERT(!is_initialised());
static_assert(
has_bounds_ == has_bounds, "Don't set SFINAE template parameter!");
#ifdef SNMALLOC_TRACING
@@ -167,6 +173,8 @@ namespace snmalloc
template<bool has_bounds_ = has_bounds>
std::enable_if_t<!has_bounds_> init()
{
SNMALLOC_ASSERT(!is_initialised());
static_assert(
has_bounds_ == has_bounds, "Don't set SFINAE template parameter!");
static constexpr size_t REQUIRED_SIZE = required_size();
@@ -222,6 +230,8 @@ namespace snmalloc
template<bool has_bounds_ = has_bounds>
std::enable_if_t<has_bounds_, std::pair<address_t, size_t>> get_bounds()
{
SNMALLOC_ASSERT(is_initialised());
static_assert(
has_bounds_ == has_bounds, "Don't set SFINAE template parameter!");
@@ -233,6 +243,8 @@ namespace snmalloc
*/
[[nodiscard]] constexpr size_t num_entries() const
{
SNMALLOC_ASSERT(is_initialised());
if constexpr (has_bounds)
{
return size >> GRANULARITY_BITS;
@@ -258,6 +270,8 @@ namespace snmalloc
return const_cast<T&>(default_value);
}
SNMALLOC_ASSERT(is_initialised() || p == 0);
if constexpr (has_bounds)
{
if (p - base > size)
@@ -318,6 +332,7 @@ namespace snmalloc
*/
[[nodiscard]] address_t get_address(const T& t) const
{
SNMALLOC_ASSERT(is_initialised());
address_t entry_offset = address_cast(&t) - address_cast(body);
address_t entry_index = entry_offset / sizeof(T);
SNMALLOC_ASSERT(
@@ -327,6 +342,7 @@ namespace snmalloc
void set(address_t p, const T& t)
{
SNMALLOC_ASSERT(is_initialised());
#ifdef SNMALLOC_TRACING
message<1024>("Pagemap.Set {}", p);
#endif

View File

@@ -154,6 +154,8 @@ extern "C"
}
sz = bits::min(sz, a.alloc_size(*ptr));
SNMALLOC_ASSUME(*ptr != nullptr || sz == 0);
// Guard memcpy as GCC is assuming not nullptr for ptr after the memcpy
// otherwise.
if (sz != 0)