osdecommitmemory-compat
authorDebian Haskell Group <pkg-haskell-maintainers@lists.alioth.debian.org>
Tue, 27 Jun 2017 11:50:03 +0000 (12:50 +0100)
committerClint Adams <clint@debian.org>
Tue, 27 Jun 2017 11:50:03 +0000 (12:50 +0100)
Gbp-Pq: Name osdecommitmemory-compat.patch

rts/posix/OSMem.c

index 6bf7e319fd0bc4502f22a08ea65d432874b1d26b..3e515c2966f2e677c6410fb18fb27e531d56bc37 100644 (file)
@@ -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");
 }