From c1062e629eaa4d270ec1834d19e38bf6587a5c11 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Wed, 20 Oct 2021 16:54:45 +0100 Subject: [PATCH] Add Madvise free to Linux layer --- src/pal/pal_linux.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/pal/pal_linux.h b/src/pal/pal_linux.h index 8104b33..f597ff1 100644 --- a/src/pal/pal_linux.h +++ b/src/pal/pal_linux.h @@ -65,6 +65,26 @@ namespace snmalloc ::memset(p, 0, size); } } + + static void notify_not_using(void* p, size_t size) noexcept + { + SNMALLOC_ASSERT(is_aligned_block(p, size)); + + 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 + madvise(p, size, MADV_FREE); + mprotect(p, size, PROT_NONE); + } + else + { + madvise(p, size, MADV_FREE); + } + } }; } // namespace snmalloc #endif