[MLton-commit] r4594

Stephen Weeks MLton@mlton.org
Wed, 24 May 2006 21:58:20 -0700


Refactored disk-backing code.  The platform-specific code now provides
three functions, diskBack_{read,write,close}.  Hopefully this will
allow the code to be ported to Cygwin/Windows.

Fixed declaration of environ in linux.h.  It has to be conditioned on
_GNU_SOURCE to avoid redeclaration warnings.


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

U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/heap.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/aix.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c
A   mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/hpux.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.h
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/netbsd.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/openbsd.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c
D   mlton/branches/on-20050822-x86_64-branch/runtime/platform/tempFileDes.mkstemp.c
U   mlton/branches/on-20050822-x86_64-branch/runtime/platform.h

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

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/heap.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/heap.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/heap.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -308,17 +308,15 @@
     *curHeapp = newHeap;
   } else {
     /* Write the heap to disk and try again. */
-    int fd;
+    void *data;
     
-    fd = tempFileDes ();
-    write_safe (fd, orig, size);
+    data = diskBack_write (orig, size);
     releaseHeap (s, curHeapp);
     if (createHeap (s, curHeapp, desiredSize, minSize)) {
-      lseek (fd, 0, SEEK_SET);
-      read_safe (fd, curHeapp->start, size);
-      close_safe (fd);
+      diskBack_read (data, curHeapp->start, size);
+      diskBack_close (data);
     } else {
-      close_safe (fd);
+      diskBack_close (data);
       if (s->controls.messages)
         GC_displayMem ();
       die ("Out of memory.  Unable to allocate %s bytes.\n",

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/aix.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/aix.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/aix.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -10,11 +10,11 @@
 #include <sys/procfs.h>
 #include <sys/vminfo.h>
 
+#include "diskBack.unix.c"
 #include "getrusage.c"
 #include "mkdir2.c"
 #include "recv.nonblock.c"
 #include "ssmmap.c"
-#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 int fegetround(void)

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -4,9 +4,9 @@
 
 #include "platform.h"
 
+#include "diskBack.unix.c"
 #include "mkdir2.c"
 #include "mmap-protect.c"
-#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 code_pointer GC_getTextEnd (void) {

Added: mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/diskBack.unix.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -0,0 +1,54 @@
+static int tempFileDes (void) {
+  int fd;
+  char *template;
+  const char *tmpDir;
+  const char *tag = "/TempFileXXXXXXXXXX";
+  mode_t m;
+
+  tmpDir = getenv ("TMP");
+  if (NULL == tmpDir) {
+    tmpDir = getenv ("TMPDIR");
+    if (NULL == tmpDir)
+      tmpDir = "/var/tmp";
+  }
+  template = malloc_safe (strlen(tmpDir) + strlen(tag) + 1);
+  strcpy (template, tmpDir);
+  strcat (template, tag);
+  m = umask(077);
+  fd = mkstemp_safe (template);
+  (void)umask(m);
+  unlink_safe (template);
+  free (template);
+  return fd;
+}
+
+typedef struct {
+  int fd;
+} *WriteToDiskData;
+
+void diskBack_read (void *data, pointer buf, size_t size) {
+  int fd;
+
+  fd = ((WriteToDiskData)data)->fd;
+  lseek (fd, 0, SEEK_SET);
+  read_safe (fd, buf, size);
+}
+
+void diskBack_close (void *data) {
+  int fd;
+
+  fd = ((WriteToDiskData)data)->fd;
+  close_safe (fd);
+  free (data);
+}
+
+void *diskBack_write (pointer buf, size_t size) {
+  int fd;
+  WriteToDiskData d;
+
+  fd = tempFileDes ();
+  write_safe (fd, buf, size);
+  d = (WriteToDiskData)(malloc_safe (sizeof(*d)));
+  d->fd = fd;
+  return d;
+}

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -1,9 +1,9 @@
 #include "platform.h"
 
+#include "diskBack.unix.c"
 #include "getText.c"
 #include "mkdir2.c"
 #include "mmap-protect.c"
-#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 void GC_displayMem () {

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/hpux.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/hpux.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/hpux.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -8,11 +8,11 @@
 
 #define MAP_ANON MAP_ANONYMOUS
 
+#include "diskBack.unix.c"
 #include "mkdir2.c"
 #include "recv.nonblock.c"
 #include "setenv.putenv.c"
 #include "mmap-protect.c"
-#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 extern unsigned char __text_start;

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -2,12 +2,12 @@
 
 #include "platform.h"
 
+#include "diskBack.unix.c"
 #include "getText.c"
 #include "mkdir2.c"
 #include "displayMem.linux.c"
 #include "mmap-protect.c"
 #include "sysconf.c"
-#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 #ifndef EIP

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.h
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.h	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.h	2006-05-25 04:58:16 UTC (rev 4594)
@@ -36,4 +36,7 @@
 
 #define MLton_Platform_OS_host "linux"
 
+// environ is already defined if _GNU_SOURCE is.
+#ifndef _GNU_SOURCE
 extern char **environ; /* for Posix_ProcEnv_environ */
+#endif

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/netbsd.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/netbsd.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/netbsd.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -1,11 +1,11 @@
 #include "platform.h"
 
+#include "diskBack.unix.c"
 #include "getText.c"
 #include "mkdir2.c"
 #include "displayMem.linux.c"
 #include "mmap-protect.c"
 #include "sysctl.c"
-#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 static void catcher (__attribute__ ((unused)) int sig,

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/openbsd.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/openbsd.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/openbsd.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -1,11 +1,11 @@
 #include "platform.h"
 
+#include "diskBack.unix.c"
 #include "getText.c"
 #include "mkdir2.c"
 #include "displayMem.linux.c"
 #include "mmap-protect.c"
 #include "sysctl.c"
-#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 static void catcher (__attribute__ ((unused)) int sig,

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -2,6 +2,7 @@
 
 #include <ieeefp.h>
 
+#include "diskBack.unix.c"
 #include "float-math.c"
 #include "getText.c"
 #include "mkdir2.c"

Deleted: mlton/branches/on-20050822-x86_64-branch/runtime/platform/tempFileDes.mkstemp.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/tempFileDes.mkstemp.c	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/tempFileDes.mkstemp.c	2006-05-25 04:58:16 UTC (rev 4594)
@@ -1,23 +0,0 @@
-int tempFileDes (void) {
-  int fd;
-  char *template;
-  const char *tmpDir;
-  const char *tag = "/TempFileXXXXXXXXXX";
-  mode_t m;
-
-  tmpDir = getenv ("TMP");
-  if (NULL == tmpDir) {
-    tmpDir = getenv ("TMPDIR");
-    if (NULL == tmpDir)
-      tmpDir = "/var/tmp";
-  }
-  template = malloc_safe (strlen(tmpDir) + strlen(tag) + 1);
-  strcpy (template, tmpDir);
-  strcat (template, tag);
-  m = umask(077);
-  fd = mkstemp_safe (template);
-  (void)umask(m);
-  unlink_safe (template);
-  free (template);
-  return fd;
-}

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/platform.h
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform.h	2006-05-25 03:16:41 UTC (rev 4593)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform.h	2006-05-25 04:58:16 UTC (rev 4594)
@@ -129,7 +129,6 @@
 /* ---------------------------------------------------------------- */
 
 int mkdir2 (const char *pathname, mode_t mode);
-int tempFileDes (void);
 
 /* ---------------------------------------------------------------- */
 /*                        Garbage Collector                         */
@@ -157,6 +156,10 @@
 
 void GC_setCygwinUseMmap (bool b);
 
+void diskBack_close (void *data);
+void diskBack_read (void *data, pointer buf, size_t size);
+void *diskBack_write (pointer buf, size_t size);
+
 /* ------------------------------------------------- */
 /*                Text Segment                       */
 /* ------------------------------------------------- */