From 6b0bda01c7ab2323ad7ac5e4e850751f50345c4a Mon Sep 17 00:00:00 2001 From: Matthias Wahl Date: Fri, 17 Jun 2022 07:16:32 +0200 Subject: [PATCH] Support older linux systems (#545) * Fix pal_linux.h for older linux systems Where MADV_FREE is not defined - replaced with MADV_DONTNEED Where GRND_NONBLOCK is not defined in but in * Check for linux/random.h in CMake as __has_include seems to not be reliable * Use CMake module CheckIncludeFilesCXX as C language isn't enabled by default everywhere * Move madvise flag ifdefs into constexpr for cleaner code --- CMakeLists.txt | 10 ++++++++++ src/snmalloc/pal/pal_linux.h | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37fb8cc..6a43a43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ endif() include(CheckCXXCompilerFlag) include(CheckCXXSourceCompiles) +include(CheckIncludeFileCXX) include(CMakeDependentOption) option(SNMALLOC_HEADER_ONLY_LIBRARY "Use snmalloc has a header-only library" OFF) @@ -105,6 +106,14 @@ int main() { return res; } " SNMALLOC_PLATFORM_HAS_GETENTROPY) + +# check if linux/random.h is available +# older libcs might not have sys/random.h +# but some might provide the necessary flags via linux/random.h +# the __has_include macro isn't working properly on all platforms for that header +# this is why we check its existence here +CHECK_INCLUDE_FILE_CXX(linux/random.h SNMALLOC_HAS_LINUX_RANDOM_H) + # Provide as function so other projects can reuse # FIXME: This modifies some variables that may or may not be the ones that # provide flags and so is broken by design. It should be removed once Verona @@ -203,6 +212,7 @@ add_as_define(SNMALLOC_QEMU_WORKAROUND) add_as_define(SNMALLOC_TRACING) add_as_define(SNMALLOC_CI_BUILD) add_as_define(SNMALLOC_PLATFORM_HAS_GETENTROPY) +add_as_define(SNMALLOC_HAS_LINUX_RANDOM_H) if (SNMALLOC_NO_REALLOCARRAY) add_as_define(SNMALLOC_NO_REALLOCARRAY) endif() diff --git a/src/snmalloc/pal/pal_linux.h b/src/snmalloc/pal/pal_linux.h index 3a06bf8..ffc02ec 100644 --- a/src/snmalloc/pal/pal_linux.h +++ b/src/snmalloc/pal/pal_linux.h @@ -8,6 +8,11 @@ # include # include # include +// __has_include does not reliably determine if we actually have linux/random.h +// available +# if defined(SNMALLOC_HAS_LINUX_RANDOM_H) +# include +# endif extern "C" int puts(const char* str); @@ -35,6 +40,19 @@ namespace snmalloc */ static constexpr int default_mmap_flags = MAP_NORESERVE; + /** + * MADV_FREE is only available since Linux 4.5. + * + * Fallback to MADV_DONTNEED on older kernels + */ + static constexpr int madvise_free_flags = +# ifdef SNMALLOC_HAS_LINUX_RANDOM_H + MADV_FREE +# else + MADV_DONTNEED +# endif + ; + static void* reserve(size_t size) noexcept { void* p = PALPOSIX::reserve(size); @@ -108,7 +126,7 @@ namespace snmalloc memset(p, 0x5a, size); madvise(p, size, MADV_DONTDUMP); - madvise(p, size, MADV_FREE); + madvise(p, size, madvise_free_flags); if constexpr (PalEnforceAccess) {