[MLton-commit] r5923

Matthew Fluet fluet at mlton.org
Tue Aug 21 21:52:18 PDT 2007


Fix physical memory computation on Cygwin; presumably the behavior of sysconf(_SC_PAGESIZE) has changed on Cygwin since 20051202
----------------------------------------------------------------------

U   mlton/trunk/runtime/platform/cygwin.c

----------------------------------------------------------------------

Modified: mlton/trunk/runtime/platform/cygwin.c
===================================================================
--- mlton/trunk/runtime/platform/cygwin.c	2007-08-22 00:29:29 UTC (rev 5922)
+++ mlton/trunk/runtime/platform/cygwin.c	2007-08-22 04:52:17 UTC (rev 5923)
@@ -5,17 +5,60 @@
 #include "mkdir2.c"
 #include "mmap.c"
 #include "recv.nonblock.c"
-#include "sysconf.c"
 #include "windows.c"
 
-HANDLE fileDesHandle (int fd) {
-  // The temporary prevents a "cast does not match function type" warning.
-  long t;
+/* 
+ * The sysconf(_SC_PAGESIZE) is the necessary alignment for using
+ * mmap.  Windows has another notion of page size (that corresponds to
+ * physical page size?).  Just to be safe, we take the least common
+ * multiple of the sysconf and Windows notions of page size.
+ *
+ * Since sysconf(_SC_PAGESIZE) might not correspond to the physical
+ * page size, we can't use sysconf(_SC_PHYS_PAGES) to get physical
+ * memory.  So, use the Windows function.
+ * 
+ * See: http://cygwin.com/ml/cygwin/2006-06/msg00341.html
+ */ 
+static size_t GC_pageSize_sysconf (void) {
+  long int pageSize;
 
-  t = get_osfhandle (fd);
-  return (HANDLE)t;
+  pageSize = sysconf (_SC_PAGESIZE);
+  return (size_t)pageSize;
 }
 
+static size_t GC_pageSize_windows (void) {
+  SYSTEM_INFO sysinfo;
+  GetSystemInfo(&sysinfo);
+  return (size_t)sysinfo.dwPageSize;
+}
+
+size_t GC_pageSize (void) {
+  size_t pageSize_sysconf = GC_pageSize_sysconf ();
+  size_t pageSize_windows = GC_pageSize_windows ();
+
+  size_t a = pageSize_sysconf;
+  size_t b = pageSize_windows;
+  size_t t;
+  while (b != 0) {
+    t = b;
+    b = a % b;
+    a = t;
+  }
+  size_t gcd = a;
+
+  size_t lcm = (pageSize_sysconf / gcd) * pageSize_windows;
+  
+  return lcm;
+}
+
+uintmax_t GC_physMem (void) {
+  MEMORYSTATUS memstat;
+
+  memstat.dwLength = sizeof(memstat);
+  GlobalMemoryStatus(&memstat);
+  return (uintmax_t)memstat.dwTotalPhys;
+}
+
 void GC_decommit (void *base, size_t length) {
         if (MLton_Platform_CygwinUseMmap)
                 munmap_safe (base, length);
@@ -37,6 +80,15 @@
                 Windows_release (base);
 }
 
+
+HANDLE fileDesHandle (int fd) {
+  // The temporary prevents a "cast does not match function type" warning.
+  long t;
+
+  t = get_osfhandle (fd);
+  return (HANDLE)t;
+}
+
 /* ------------------------------------------------- */
 /*                      Cygwin                       */
 /* ------------------------------------------------- */




More information about the MLton-commit mailing list