osdecommitmemory-compat
authorDebian Haskell Group <pkg-haskell-maintainers@lists.alioth.debian.org>
Sat, 17 Dec 2016 02:44:45 +0000 (02:44 +0000)
committerClint Adams <clint@debian.org>
Sat, 17 Dec 2016 02:44:45 +0000 (02:44 +0000)
Gbp-Pq: Name osdecommitmemory-compat.patch

rts/posix/OSMem.c

index 2a3c294fba104ff4bfc33d0609131ccc06eb54eb..0a9690c0469f613cbbf1f6cbf2fc8a0eb7315575 100644 (file)
@@ -506,11 +506,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");
 }