Skip to content

Commit

Permalink
Add assertion for initialisation to the pagemap (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjp41 authored Sep 21, 2022
1 parent 38d4483 commit fb85216
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/snmalloc/backend_helpers/pagemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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!");

Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/snmalloc/override/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fb85216

Please sign in to comment.