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,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