2000-02-27 Eli Zaretskii <eliz@is.elta.co.il>
[deliverable/binutils-gdb.git] / mmalloc / mmap-sup.c
index e085f95f7c441eae1d7e0df5f78da202d4885698..56146e295228229bd0861d5e871cbb70b3d53a08 100644 (file)
@@ -1,5 +1,5 @@
 /* Support for an sbrk-like function that uses mmap.
-   Copyright 1992 Free Software Foundation, Inc.
+   Copyright 1992, 2000 Free Software Foundation, Inc.
 
    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
 
@@ -22,6 +22,9 @@ Boston, MA 02111-1307, USA.  */
 
 #if defined(HAVE_MMAP)
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>    /* Prototypes for lseek */
+#endif
 #include <stdio.h>
 #include <fcntl.h>
 #include <sys/mman.h>
@@ -38,7 +41,9 @@ Boston, MA 02111-1307, USA.  */
    it out. */
 
 static size_t pagesize;
+#if NEED_DECLARATION_GETPAGESIZE
 extern int getpagesize PARAMS ((void));
+#endif
 
 #define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + pagesize - 1) & \
                                    ~(pagesize - 1))
@@ -157,29 +162,44 @@ mmalloc_findbase (size)
   int size;
 {
   int fd;
+  int flags;
   caddr_t base = NULL;
 
+#ifdef MAP_ANONYMOUS
+  flags = MAP_SHARED | MAP_ANONYMOUS;
+  fd = -1;
+#else
+#ifdef MAP_FILE
+  flags = MAP_SHARED | MAP_FILE;
+#else
+  flags = MAP_SHARED;
+#endif
   fd = open ("/dev/zero", O_RDWR);
   if (fd != -1)
     {
-      base = mmap (0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-      if (base != (caddr_t) -1)
-       {
-         munmap (base, (size_t) size);
-       }
+      return ((PTR) NULL);
+    }
+#endif
+  base = mmap (0, size, PROT_READ | PROT_WRITE, flags, fd, 0);
+  if (base != (caddr_t) -1)
+    {
+      munmap (base, (size_t) size);
+    }
+  if (fd != -1)
+    {
       close (fd);
-      if (base == 0)
-       {
-         /* Don't allow mapping at address zero.  We use that value
-            to signal an error return, and besides, it is useful to
-            catch NULL pointers if it is unmapped.  Instead start
-            at the next page boundary. */
-         base = (caddr_t) getpagesize ();
-       }
-      else if (base == (caddr_t) -1)
-       {
-         base = NULL;
-       }
+    }
+  if (base == 0)
+    {
+      /* Don't allow mapping at address zero.  We use that value
+        to signal an error return, and besides, it is useful to
+        catch NULL pointers if it is unmapped.  Instead start
+        at the next page boundary. */
+      base = (caddr_t) getpagesize ();
+    }
+  else if (base == (caddr_t) -1)
+    {
+      base = NULL;
     }
   return ((PTR) base);
 }
This page took 0.094894 seconds and 4 git commands to generate.