From 05f2af19d52d82f0f441bfa9a11c3bfc5478a236 Mon Sep 17 00:00:00 2001 From: Debian Haskell Group Date: Sat, 26 Aug 2017 18:04:27 +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