[MLton-commit] r4582

Stephen Weeks MLton@mlton.org
Wed, 24 May 2006 15:15:33 -0700


Moved code to create a temporary file descriptor into platform/*,
available via tempFileDes().  The idea is that tempFileDes returns a
file descriptor for a temporary file that will be automatically
deleted upon close.

Put in a Unix implementation of tempFileDes based on mkstemp and umask
followed by unlink.


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

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
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/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
A   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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/heap.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -307,39 +307,18 @@
     newHeap.oldGenSize = size;
     *curHeapp = newHeap;
   } else {
-    /* Write the heap to a file and try again. */
+    /* Write the heap to disk and try again. */
     int fd;
-    char template[80];
-    const char *tmpDefault;
-    char *tmpDir;
-    const char *tmpVar;
     
-#if (defined (__MSVCRT__))
-    tmpVar = "TEMP";
-    tmpDefault = "C:/WINNT/TEMP";
-#else
-    tmpVar = "TMPDIR";
-    tmpDefault = "/tmp";
-#endif
-    tmpDir = getenv (tmpVar);
-    strcpy (template, (NULL == tmpDir) ? tmpDefault : tmpDir);
-    strcat (template, "/FromSpaceXXXXXX");
-    fd = mkstemp_safe (template);
-    close_safe (fd);
-    if (s->controls.messages)
-      fprintf (stderr, "Paging heap from "FMTPTR" to %s.\n", 
-               (uintptr_t)orig, template);
-    fd = open_safe (template, O_WRONLY, S_IRUSR | S_IWUSR);
+    fd = tempFileDes ();
     write_safe (fd, orig, size);
-    close_safe (fd);
     releaseHeap (s, curHeapp);
     if (createHeap (s, curHeapp, desiredSize, minSize)) {
-      fd = open_safe (template, O_RDONLY, S_IRUSR | S_IWUSR);
+      lseek (fd, 0, SEEK_SET);
       read_safe (fd, curHeapp->start, size);
       close_safe (fd);
-      unlink_safe (template);
     } else {
-      unlink_safe (template);
+      close_safe (fd);
       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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/aix.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -14,6 +14,7 @@
 #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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/darwin.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -6,6 +6,7 @@
 
 #include "mkdir2.c"
 #include "mmap-protect.c"
+#include "tempFileDes.mkstemp.c"
 #include "use-mmap.c"
 
 code_pointer GC_getTextEnd (void) {

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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/freebsd.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -3,6 +3,7 @@
 #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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/hpux.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -12,6 +12,7 @@
 #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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/linux.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -7,6 +7,7 @@
 #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/netbsd.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/platform/netbsd.c	2006-05-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/netbsd.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -5,6 +5,7 @@
 #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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/openbsd.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -5,6 +5,7 @@
 #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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/solaris.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -9,6 +9,7 @@
 #include "mmap-protect.c"
 #include "sysconf.c"
 #include "setenv.putenv.c"
+#include "tempFileDes.mkstemp.c"
 
 int fegetround () {
         int mode;

Added: 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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform/tempFileDes.mkstemp.c	2006-05-24 22:15:28 UTC (rev 4582)
@@ -0,0 +1,23 @@
+int tempFileDes (void) {
+  int fd;
+  char *template;
+  const char *tmpDir;
+  const char *tag = "/FromSpaceXXXXXXXXXX";
+  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-24 22:10:31 UTC (rev 4581)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/platform.h	2006-05-24 22:15:28 UTC (rev 4582)
@@ -129,6 +129,7 @@
 /* ---------------------------------------------------------------- */
 
 int mkdir2 (const char *pathname, mode_t mode);
+int tempFileDes (void);
 
 /* ---------------------------------------------------------------- */
 /*                        Garbage Collector                         */