From 0ff4494500586f14c9e9fe1e24aa6ca8422300dc Mon Sep 17 00:00:00 2001 From: Debian Haskell Group Date: Tue, 27 Jun 2017 12:50:03 +0100 Subject: [PATCH] osdecommitmemory-compat Gbp-Pq: Name osdecommitmemory-compat.patch --- rts/posix/OSMem.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/rts/posix/OSMem.c b/rts/posix/OSMem.c index 6bf7e319..3e515c29 100644 --- a/rts/posix/OSMem.c +++ b/rts/posix/OSMem.c @@ -522,11 +522,24 @@ void osDecommitMemory(void *at, W_ size) #ifdef MADV_FREE // Try MADV_FREE first, FreeBSD has both and MADV_DONTNEED - // just swaps memory out + // just swaps memory out. Linux >= 4.5 has both DONTNEED and FREE; either + // will work as they both allow the system to free anonymous pages. + // It is important that we try both methods as the kernel which we were + // built on may differ from the kernel we are now running on. r = madvise(at, size, MADV_FREE); -#else - r = madvise(at, size, MADV_DONTNEED); + if(r < 0) { + if (errno == EINVAL) { + // Perhaps the system doesn't support MADV_FREE; fall-through and + // try MADV_DONTNEED. + } else { + sysErrorBelch("unable to decommit memory"); + } + } else { + return; + } #endif + + r = madvise(at, size, MADV_DONTNEED); if(r < 0) sysErrorBelch("unable to decommit memory"); } -- 2.30.2