20000-05-19 H.J. Lu (hjl@gnu.org)
[binutils-gdb.git] / mmalloc / attach.c
index 44fff4f0707d729754b39e9e99df9f7f5dca0656..2bd70bfdf05f3723f9aed4d8fca13c71942f0dda 100644 (file)
@@ -1,32 +1,41 @@
 /* Initialization for access to a mmap'd malloc managed region.
-   Copyright 1992 Free Software Foundation, Inc.
+   Copyright 1992, 2000 Free Software Foundation, Inc.
 
    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+This file is part of the GNU C Library.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
 
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
-#include <fcntl.h>
 #include <sys/types.h>
+#include <fcntl.h> /* After sys/types.h, at least for dpx/2.  */
 #include <sys/stat.h>
-#include "mmalloc.h"
+#include <string.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>    /* Prototypes for lseek */
+#endif
+#include "mmprivate.h"
 
 #ifndef SEEK_SET
 #define SEEK_SET 0
 #endif
 
+
+#if defined(HAVE_MMAP)
+
 /* Forward declarations/prototypes for local functions */
 
 static struct mdesc *reuse PARAMS ((int));
@@ -62,9 +71,6 @@ static struct mdesc *reuse PARAMS ((int));
 
    On failure returns NULL. */
 
-
-#if defined(HAVE_MMAP)
-
 PTR
 mmalloc_attach (fd, baseaddr)
   int fd;
@@ -155,10 +161,15 @@ mmalloc_attach (fd, baseaddr)
    after we've switched over to using the mapped in malloc descriptor 
    rather than the temporary one on the stack.
 
-   Also note that if the heap being remapped previously used the mmcheck()
+   Once we've switched over to using the mapped in malloc descriptor, we
+   have to update the pointer to the morecore function, since it almost
+   certainly will be at a different address if the process reusing the
+   mapped region is from a different executable.
+
+   Also note that if the heap being remapped previously used the mmcheckf()
    routines, we need to update the hooks since their target functions
    will have certainly moved if the executable has changed in any way.
-   We do this by calling mmcheck() internally.
+   We do this by calling mmcheckf() internally.
 
    Returns a pointer to the malloc descriptor if successful, or NULL if
    unsuccessful for some reason. */
@@ -181,9 +192,10 @@ reuse (fd)
        {
          mdp = (struct mdesc *) mtemp.base;
          mdp -> fd = fd;
+         mdp -> morecore = __mmalloc_mmap_morecore;
          if (mdp -> mfree_hook != NULL)
            {
-             (void) mmcheck ((PTR) mdp, (void (*) PARAMS ((void))) NULL);
+             mmcheckf ((PTR) mdp, (void (*) PARAMS ((void))) NULL, 1);
            }
        }
     }
@@ -196,6 +208,7 @@ reuse (fd)
    to link to, but trying to initialize access to an mmap'd managed region
    always fails. */
 
+/* ARGSUSED */
 PTR
 mmalloc_attach (fd, baseaddr)
   int fd;