Add DEBUG constexpr
Enable checking use of a constexpr rather than ifdef for checking if in DEBUG.
This commit is contained in:
committed by
Matthew Parkinson
parent
f1be609cdb
commit
3d1b973480
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -586,21 +586,22 @@ namespace snmalloc
|
||||
ChunkAllocator::register_local_state<SharedStateHandle>(
|
||||
get_backend_local_state(), chunk_local_state);
|
||||
|
||||
#ifndef NDEBUG
|
||||
for (smallsizeclass_t i = 0; i < NUM_SMALL_SIZECLASSES; i++)
|
||||
if constexpr (DEBUG)
|
||||
{
|
||||
size_t size = sizeclass_to_size(i);
|
||||
smallsizeclass_t sc1 = size_to_sizeclass(size);
|
||||
smallsizeclass_t sc2 = size_to_sizeclass_const(size);
|
||||
size_t size1 = sizeclass_to_size(sc1);
|
||||
size_t size2 = sizeclass_to_size(sc2);
|
||||
for (smallsizeclass_t i = 0; i < NUM_SMALL_SIZECLASSES; i++)
|
||||
{
|
||||
size_t size = sizeclass_to_size(i);
|
||||
smallsizeclass_t sc1 = size_to_sizeclass(size);
|
||||
smallsizeclass_t sc2 = size_to_sizeclass_const(size);
|
||||
size_t size1 = sizeclass_to_size(sc1);
|
||||
size_t size2 = sizeclass_to_size(sc2);
|
||||
|
||||
SNMALLOC_ASSERT(sc1 == i);
|
||||
SNMALLOC_ASSERT(sc1 == sc2);
|
||||
SNMALLOC_ASSERT(size1 == size);
|
||||
SNMALLOC_ASSERT(size1 == size2);
|
||||
SNMALLOC_CHECK(sc1 == i);
|
||||
SNMALLOC_CHECK(sc1 == sc2);
|
||||
SNMALLOC_CHECK(size1 == size);
|
||||
SNMALLOC_CHECK(size1 == size2);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -30,11 +30,7 @@ namespace
|
||||
#ifdef SNMALLOC_CHECK_LOADS
|
||||
SNMALLOC_CHECK_LOADS
|
||||
#else
|
||||
# ifdef NDEBUG
|
||||
false
|
||||
# else
|
||||
true
|
||||
# endif
|
||||
DEBUG
|
||||
#endif
|
||||
;
|
||||
|
||||
@@ -50,11 +46,7 @@ namespace
|
||||
#ifdef SNMALLOC_FAIL_FAST
|
||||
SNMALLOC_FAIL_FAST
|
||||
#else
|
||||
# ifdef NDEBUG
|
||||
true
|
||||
# else
|
||||
false
|
||||
# endif
|
||||
!DEBUG
|
||||
#endif
|
||||
;
|
||||
|
||||
|
||||
@@ -113,9 +113,8 @@ namespace snmalloc
|
||||
{
|
||||
SNMALLOC_ASSERT(is_aligned_block<page_size>(p, size));
|
||||
|
||||
# if !defined(NDEBUG)
|
||||
memset(p, 0x5a, size);
|
||||
# endif
|
||||
if constexpr (DEBUG)
|
||||
memset(p, 0x5a, size);
|
||||
|
||||
// `MADV_FREE_REUSABLE` can only be applied to writable pages,
|
||||
// otherwise it's an error.
|
||||
|
||||
@@ -35,9 +35,9 @@ namespace snmalloc
|
||||
{
|
||||
SNMALLOC_ASSERT(is_aligned_block<OS::page_size>(p, size));
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
memset(p, 0x5a, size);
|
||||
#endif
|
||||
if constexpr (DEBUG)
|
||||
memset(p, 0x5a, size);
|
||||
|
||||
madvise(p, size, MADV_FREE);
|
||||
|
||||
if constexpr (PalEnforceAccess)
|
||||
|
||||
@@ -70,11 +70,11 @@ namespace snmalloc
|
||||
|
||||
if constexpr (PalEnforceAccess)
|
||||
{
|
||||
# if !defined(NDEBUG)
|
||||
// Fill memory so that when we switch the pages back on we don't make
|
||||
// assumptions on the content.
|
||||
memset(p, 0x5a, size);
|
||||
# endif
|
||||
if constexpr (DEBUG)
|
||||
memset(p, 0x5a, size);
|
||||
|
||||
madvise(p, size, MADV_FREE);
|
||||
mprotect(p, size, PROT_NONE);
|
||||
}
|
||||
|
||||
@@ -189,11 +189,11 @@ namespace snmalloc
|
||||
|
||||
if constexpr (PalEnforceAccess)
|
||||
{
|
||||
#if !defined(NDEBUG)
|
||||
// Fill memory so that when we switch the pages back on we don't make
|
||||
// assumptions on the content.
|
||||
memset(p, 0x5a, size);
|
||||
#endif
|
||||
if constexpr (DEBUG)
|
||||
memset(p, 0x5a, size);
|
||||
|
||||
mprotect(p, size, PROT_NONE);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -95,11 +95,8 @@ int main(int, char**)
|
||||
setup();
|
||||
|
||||
xoroshiro::p128r64 r;
|
||||
# ifdef NDEBUG
|
||||
size_t nn = 30;
|
||||
# else
|
||||
size_t nn = 3;
|
||||
# endif
|
||||
|
||||
size_t nn = snmalloc::DEBUG ? 30 : 3;
|
||||
|
||||
for (size_t n = 0; n < nn; n++)
|
||||
test::test_external_pointer(r);
|
||||
|
||||
Reference in New Issue
Block a user