changeset 31515:92dec3ff92a1

(mmap_fd): Remove initializer which can make it read-only in a dumped Emacs. (mmap_fd_1): New variable. (mmap_set_vars): Remove local `fd'. Save mmap_fd in mmap_fd_1, restore it from there. (r_alloc, r_re_alloc, r_alloc_free): Call r_alloc_init unconditionally so that mmap_fd can be initialized there. (r_alloc_init_fd): Open-coded in r_alloc_init; function removed. (r_alloc_init) [REL_ALLOC_MMAP && !MAP_ANON]: Open /dev/zero. (r_alloc_init) [REL_ALLOC_MMAP && MAP_ANON]: Set mmap_fd to -1.
author Gerd Moellmann <gerd@gnu.org>
date Fri, 08 Sep 2000 19:52:38 +0000
parents 2c900086fb10
children 84ba6f2b7d60
files src/ralloc.c
diffstat 1 files changed, 35 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/ralloc.c	Fri Sep 08 16:53:08 2000 +0000
+++ b/src/ralloc.c	Fri Sep 08 19:52:38 2000 +0000
@@ -1228,6 +1228,7 @@
 
 #include <sys/types.h>
 #include <sys/mman.h>
+
 #ifndef MAP_ANON
 #ifdef MAP_ANONYMOUS
 #define MAP_ANON MAP_ANONYMOUS
@@ -1235,12 +1236,15 @@
 #define MAP_ANON 0
 #endif
 #endif
+
 #include <stdio.h>
 #include <errno.h>
-#if !MAP_ANON
+
+#if MAP_ANON == 0
 #include <fcntl.h>
 #endif
 
+
 /* Memory is allocated in regions which are mapped using mmap(2).
    The current implementation lets the system select mapped
    addresses;  we're not using MAP_FIXED in general, except when
@@ -1279,14 +1283,15 @@
 
 static struct mmap_region *mmap_regions;
 
+/* File descriptor for mmap.  If we don't have anonymous mapping,
+   /dev/zero will be opened on it.  */
+
+static int mmap_fd;
+
 /* Temporary storage for mmap_set_vars, see there.  */
 
 static struct mmap_region *mmap_regions_1;
-
-/* File descriptor for mmap.  If we don't have anonymous mapping,
-   /dev/zero will be opened on it.  */
-
-static int mmap_fd = -1;
+static int mmap_fd_1;
 
 /* Value is X rounded up to the next multiple of N.  */
 
@@ -1317,7 +1322,7 @@
 POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
 POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
 void r_alloc_free P_ ((POINTER_TYPE **ptr));
-void r_alloc_init_fd ();
+
 
 /* Return a region overlapping address range START...END, or null if
    none.  END is not including, i.e. the last byte in the range
@@ -1453,14 +1458,13 @@
      int restore_p;
 {
   struct mmap_region *r;
-  static int fd;
 
   if (restore_p)
     {
       mmap_regions = mmap_regions_1;
+      mmap_fd = mmap_fd_1;
       for (r = mmap_regions; r; r = r->next)
 	*r->var = MMAP_USER_AREA (r);
-      mmap_fd = fd;
     }
   else
     {
@@ -1468,6 +1472,7 @@
 	*r->var = NULL;
       mmap_regions_1 = mmap_regions;
       mmap_regions = NULL;
+      mmap_fd_1 = mmap_fd;
       mmap_fd = -1;
     }
 }
@@ -1505,12 +1510,7 @@
   void *p;
   size_t map;
 
-  if (!r_alloc_initialized)
-    r_alloc_init ();
-#if defined (REL_ALLOC_MMAP) && !MAP_ANON
-  if (mmap_fd == -1)
-    r_alloc_init_fd ();
-#endif
+  r_alloc_init ();
 
   map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, page_size);
   p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
@@ -1554,12 +1554,7 @@
 {
   POINTER_TYPE *result;
   
-  if (!r_alloc_initialized)
-    r_alloc_init ();
-#if defined (REL_ALLOC_MMAP) && !MAP_ANON
-  if (mmap_fd == -1)
-    r_alloc_init_fd ();
-#endif
+  r_alloc_init ();
 
   if (*var == NULL)
     result = r_alloc (var, nbytes);
@@ -1627,13 +1622,8 @@
 r_alloc_free (var)
      POINTER_TYPE **var;
 {
-  if (!r_alloc_initialized)
-    r_alloc_init ();
-#if defined (REL_ALLOC_MMAP) && !MAP_ANON
-  if (mmap_fd == -1)
-    r_alloc_init_fd ();
-#endif
-
+  r_alloc_init ();
+  
   if (*var)
     {
       mmap_free (MMAP_REGION (*var));
@@ -1656,30 +1646,31 @@
 extern POINTER (*__morecore) ();
 #endif
 
-/* Set up the file descriptor for non-anonymous mmapping.  */
-
-void
-r_alloc_init_fd ()
-{
-#ifdef REL_ALLOC_MMAP
-#if !MAP_ANON
-  /* No anonymous mmap -- we need the file descriptor.  */
-  mmap_fd = open ("/dev/zero", O_RDONLY);
-  if (mmap_fd < 0)
-    fatal ("cannot open /dev/zero");
-#endif
-#endif
-}
 
 /* Initialize various things for memory allocation.  */
 
 static void
 r_alloc_init ()
 {
+#if defined REL_ALLOC_MMAP && MAP_ANON == 0
+  /* The value of mmap_fd is initially 0 in temacs, and -1
+     in a dumped Emacs.  */
+  if (mmap_fd <= 0)
+    {
+      /* No anonymous mmap -- we need the file descriptor.  */
+      mmap_fd = open ("/dev/zero", O_RDONLY);
+      if (mmap_fd == -1)
+	fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
+    }
+#endif /* REL_ALLOC_MMAP && MAP_ANON == 0 */
+
   if (r_alloc_initialized)
     return;
-
   r_alloc_initialized = 1;
+#if defined REL_ALLOC_MMAP && MAP_ANON != 0
+  mmap_fd = -1;
+#endif
+  
   page_size = PAGE;
 #ifndef SYSTEM_MALLOC
   real_morecore = __morecore;
@@ -1724,6 +1715,6 @@
 	 (char *) first_heap->end - (char *) first_heap->start);
   virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
 #endif
+  
   use_relocatable_buffers = 1;
-  r_alloc_init_fd ();
 }