--- src/os_win32.c	Sun Aug 24 20:07:26 2008
+++ src/os_win32.c.new	Sun Aug 24 20:07:20 2008
@@ -4271,20 +4271,56 @@
 #endif
 }
 
+#ifndef _WIN64
+# define MCH_AVAIL_MEM_MAX 0xffffff00
+#else
+# define MCH_AVAIL_MEM_MAX 0xffffffffffffff00
+#endif
 
 /*
  * How much memory is available?
- * Return sum of available physical and page file memory.
+ * Return sum of available physical and page file memory on success, non-zero
+ * for failure.
+ *
+ * ============================================================================
+ * Use GlobalMemoryStatusEx instead of GlobalMemoryStatus on systems were we
+ * can, since it's more reliable.
+ *
+ * Sometimes adding availPhys + availPageFile caused an overflow on 32-bit
+ * systems, so that's why I added a check to see if it would overflow. In those
+ * cases, MCH_AVAIL_MEM_MAX is returned to indicate there is plenty of memory
+ * available, but we can't tell how much since we return a 32-bit integer on
+ * some platforms.
+ * We can't just "if (availPhys + availPageFile >= MCH_AVAIL_MEM_MAX)" so
+ * that's why I did "if (availPageFile >= (MCH_AVAIL_MEM_MAX - availPhys))".
+ *
+ * -- Jelle Geerts, 20080823
+ * ============================================================================
  */
 /*ARGSUSED*/
     long_u
 mch_avail_mem(int special)
 {
+#if (_WIN32_WINNT >= 0x0500)
+    // If we're on NT >= 0x0500, we will use GlobalMemoryStatusEx, which is
+    // more reliable on systems with >4 GiB of memory.
+    MEMORYSTATUSEX	msex;
+
+    msex.dwLength = sizeof(MEMORYSTATUSEX);
+    if (!GlobalMemoryStatusEx(&msex))
+        return -1;
+    if (msex.ullAvailPhys >= (MCH_AVAIL_MEM_MAX - msex.ullAvailPageFile))
+        return MCH_AVAIL_MEM_MAX;
+    return msex.ullAvailPhys + msex.ullAvailPageFile;
+#else
     MEMORYSTATUS	ms;
 
     ms.dwLength = sizeof(MEMORYSTATUS);
     GlobalMemoryStatus(&ms);
+    if (ms.dwAvailPageFile >= (MCH_AVAIL_MEM_MAX - ms.dwAvailPhys))
+        return MCH_AVAIL_MEM_MAX;
     return (long_u) (ms.dwAvailPhys + ms.dwAvailPageFile);
+#endif
 }
 
 #ifdef FEAT_MBYTE
