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 <sys/random.h> but in <linux/random.h> * 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
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
# include <sys/mman.h>
|
||||
# include <sys/prctl.h>
|
||||
# include <syscall.h>
|
||||
// __has_include does not reliably determine if we actually have linux/random.h
|
||||
// available
|
||||
# if defined(SNMALLOC_HAS_LINUX_RANDOM_H)
|
||||
# include <linux/random.h>
|
||||
# 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<PALLinux>::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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user