Add DEBUG constexpr

Enable checking use of a constexpr rather than ifdef for checking if in
DEBUG.
This commit is contained in:
Matthew Parkinson
2022-02-01 09:11:35 +00:00
committed by Matthew Parkinson
parent f1be609cdb
commit 3d1b973480
9 changed files with 83 additions and 87 deletions

View File

@@ -95,6 +95,12 @@
namespace snmalloc
{
#ifdef NDEBUG
static constexpr bool DEBUG = false;
#else
static constexpr bool DEBUG = true;
#endif
// Forwards reference so that the platform can define how to handle errors.
[[noreturn]] SNMALLOC_COLD void error(const char* const str);
} // namespace snmalloc
@@ -161,12 +167,15 @@ namespace snmalloc
{
if (SNMALLOC_UNLIKELY(!test))
{
#ifdef NDEBUG
UNUSED(str);
SNMALLOC_FAST_FAIL();
#else
check_client_error(str);
#endif
if constexpr (DEBUG)
{
UNUSED(str);
SNMALLOC_FAST_FAIL();
}
else
{
check_client_error(str);
}
}
}

View File

@@ -92,9 +92,7 @@ namespace snmalloc
void insert(Ptr<T> item)
{
#ifndef NDEBUG
debug_check_not_contains(item);
#endif
item->next = head;
item->prev = Terminator();
@@ -105,16 +103,14 @@ namespace snmalloc
tail = item;
head = item;
#ifndef NDEBUG
debug_check();
#endif
if constexpr (DEBUG)
debug_check();
}
void insert_back(Ptr<T> item)
{
#ifndef NDEBUG
debug_check_not_contains(item);
#endif
item->prev = tail;
item->next = Terminator();
@@ -125,16 +121,13 @@ namespace snmalloc
head = item;
tail = item;
#ifndef NDEBUG
debug_check();
#endif
}
SNMALLOC_FAST_PATH void remove(Ptr<T> item)
{
#ifndef NDEBUG
debug_check_contains(item);
#endif
if (item->next != Terminator())
item->next->prev = item->prev;
@@ -146,9 +139,7 @@ namespace snmalloc
else
head = item->next;
#ifndef NDEBUG
debug_check();
#endif
}
void clear()
@@ -163,49 +154,56 @@ namespace snmalloc
void debug_check_contains(Ptr<T> item)
{
#ifndef NDEBUG
debug_check();
Ptr<T> curr = head;
while (curr != item)
if constexpr (DEBUG)
{
SNMALLOC_ASSERT(curr != Terminator());
curr = curr->next;
debug_check();
Ptr<T> curr = head;
while (curr != item)
{
SNMALLOC_ASSERT(curr != Terminator());
curr = curr->next;
}
}
else
{
UNUSED(item);
}
#else
UNUSED(item);
#endif
}
void debug_check_not_contains(Ptr<T> item)
{
#ifndef NDEBUG
debug_check();
Ptr<T> curr = head;
while (curr != Terminator())
if constexpr (DEBUG)
{
SNMALLOC_ASSERT(curr != item);
curr = curr->next;
debug_check();
Ptr<T> curr = head;
while (curr != Terminator())
{
SNMALLOC_ASSERT(curr != item);
curr = curr->next;
}
}
else
{
UNUSED(item);
}
#else
UNUSED(item);
#endif
}
void debug_check()
{
#ifndef NDEBUG
Ptr<T> item = head;
Ptr<T> prev = Terminator();
while (item != Terminator())
if constexpr (DEBUG)
{
SNMALLOC_ASSERT(item->prev == prev);
prev = item;
item = item->next;
Ptr<T> item = head;
Ptr<T> prev = Terminator();
while (item != Terminator())
{
SNMALLOC_ASSERT(item->prev == prev);
prev = item;
item = item->next;
}
}
#endif
}
};
} // namespace snmalloc