Skip to content

Commit 6b0bda0

Browse files
authored
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
1 parent 3e72ef6 commit 6b0bda0

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ endif()
88

99
include(CheckCXXCompilerFlag)
1010
include(CheckCXXSourceCompiles)
11+
include(CheckIncludeFileCXX)
1112
include(CMakeDependentOption)
1213

1314
option(SNMALLOC_HEADER_ONLY_LIBRARY "Use snmalloc has a header-only library" OFF)
@@ -105,6 +106,14 @@ int main() {
105106
return res;
106107
}
107108
" SNMALLOC_PLATFORM_HAS_GETENTROPY)
109+
110+
# check if linux/random.h is available
111+
# older libcs might not have sys/random.h
112+
# but some might provide the necessary flags via linux/random.h
113+
# the __has_include macro isn't working properly on all platforms for that header
114+
# this is why we check its existence here
115+
CHECK_INCLUDE_FILE_CXX(linux/random.h SNMALLOC_HAS_LINUX_RANDOM_H)
116+
108117
# Provide as function so other projects can reuse
109118
# FIXME: This modifies some variables that may or may not be the ones that
110119
# provide flags and so is broken by design. It should be removed once Verona
@@ -203,6 +212,7 @@ add_as_define(SNMALLOC_QEMU_WORKAROUND)
203212
add_as_define(SNMALLOC_TRACING)
204213
add_as_define(SNMALLOC_CI_BUILD)
205214
add_as_define(SNMALLOC_PLATFORM_HAS_GETENTROPY)
215+
add_as_define(SNMALLOC_HAS_LINUX_RANDOM_H)
206216
if (SNMALLOC_NO_REALLOCARRAY)
207217
add_as_define(SNMALLOC_NO_REALLOCARRAY)
208218
endif()

src/snmalloc/pal/pal_linux.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
# include <sys/mman.h>
99
# include <sys/prctl.h>
1010
# include <syscall.h>
11+
// __has_include does not reliably determine if we actually have linux/random.h
12+
// available
13+
# if defined(SNMALLOC_HAS_LINUX_RANDOM_H)
14+
# include <linux/random.h>
15+
# endif
1116

1217
extern "C" int puts(const char* str);
1318

@@ -35,6 +40,19 @@ namespace snmalloc
3540
*/
3641
static constexpr int default_mmap_flags = MAP_NORESERVE;
3742

43+
/**
44+
* MADV_FREE is only available since Linux 4.5.
45+
*
46+
* Fallback to MADV_DONTNEED on older kernels
47+
*/
48+
static constexpr int madvise_free_flags =
49+
# ifdef SNMALLOC_HAS_LINUX_RANDOM_H
50+
MADV_FREE
51+
# else
52+
MADV_DONTNEED
53+
# endif
54+
;
55+
3856
static void* reserve(size_t size) noexcept
3957
{
4058
void* p = PALPOSIX<PALLinux>::reserve(size);
@@ -108,7 +126,7 @@ namespace snmalloc
108126
memset(p, 0x5a, size);
109127

110128
madvise(p, size, MADV_DONTDUMP);
111-
madvise(p, size, MADV_FREE);
129+
madvise(p, size, madvise_free_flags);
112130

113131
if constexpr (PalEnforceAccess)
114132
{

0 commit comments

Comments
 (0)