diff --git a/src/ds/defines.h b/src/ds/defines.h index b21b194..7830433 100644 --- a/src/ds/defines.h +++ b/src/ds/defines.h @@ -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); + } } } diff --git a/src/ds/dllist.h b/src/ds/dllist.h index d4c20e8..95bb8e5 100644 --- a/src/ds/dllist.h +++ b/src/ds/dllist.h @@ -92,9 +92,7 @@ namespace snmalloc void insert(Ptr 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 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 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 item) { -#ifndef NDEBUG - debug_check(); - Ptr curr = head; - - while (curr != item) + if constexpr (DEBUG) { - SNMALLOC_ASSERT(curr != Terminator()); - curr = curr->next; + debug_check(); + Ptr 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 item) { -#ifndef NDEBUG - debug_check(); - Ptr curr = head; - - while (curr != Terminator()) + if constexpr (DEBUG) { - SNMALLOC_ASSERT(curr != item); - curr = curr->next; + debug_check(); + Ptr 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 item = head; - Ptr prev = Terminator(); - - while (item != Terminator()) + if constexpr (DEBUG) { - SNMALLOC_ASSERT(item->prev == prev); - prev = item; - item = item->next; + Ptr item = head; + Ptr prev = Terminator(); + + while (item != Terminator()) + { + SNMALLOC_ASSERT(item->prev == prev); + prev = item; + item = item->next; + } } -#endif } }; } // namespace snmalloc diff --git a/src/mem/corealloc.h b/src/mem/corealloc.h index 9a87d2f..66e6b7b 100644 --- a/src/mem/corealloc.h +++ b/src/mem/corealloc.h @@ -586,21 +586,22 @@ namespace snmalloc ChunkAllocator::register_local_state( 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: diff --git a/src/override/memcpy.cc b/src/override/memcpy.cc index 6b51bc5..acfc6a6 100644 --- a/src/override/memcpy.cc +++ b/src/override/memcpy.cc @@ -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 ; diff --git a/src/pal/pal_apple.h b/src/pal/pal_apple.h index edf511b..69f4e5d 100644 --- a/src/pal/pal_apple.h +++ b/src/pal/pal_apple.h @@ -113,9 +113,8 @@ namespace snmalloc { SNMALLOC_ASSERT(is_aligned_block(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. diff --git a/src/pal/pal_bsd.h b/src/pal/pal_bsd.h index cf64244..a07b7c5 100644 --- a/src/pal/pal_bsd.h +++ b/src/pal/pal_bsd.h @@ -35,9 +35,9 @@ namespace snmalloc { SNMALLOC_ASSERT(is_aligned_block(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) diff --git a/src/pal/pal_linux.h b/src/pal/pal_linux.h index 39d402d..139a6af 100644 --- a/src/pal/pal_linux.h +++ b/src/pal/pal_linux.h @@ -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); } diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index bc05f9f..4689f69 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -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 diff --git a/src/test/perf/external_pointer/externalpointer.cc b/src/test/perf/external_pointer/externalpointer.cc index e2fdb06..b589124 100644 --- a/src/test/perf/external_pointer/externalpointer.cc +++ b/src/test/perf/external_pointer/externalpointer.cc @@ -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);