1 /* Initialization for access to a mmap'd malloc managed region.
2 Copyright 1992, 2000 Free Software Foundation, Inc.
4 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
6 This file is part of the GNU C Library.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with the GNU C Library; see the file COPYING.LIB. If
20 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
24 #include <fcntl.h> /* After sys/types.h, at least for dpx/2. */
28 #include <unistd.h> /* Prototypes for lseek */
30 #include "mmprivate.h"
37 #if defined(HAVE_MMAP)
39 /* Forward declarations/prototypes for local functions */
41 static struct mdesc *reuse PARAMS ((int));
43 /* Initialize access to a mmalloc managed region.
45 If FD is a valid file descriptor for an open file then data for the
46 mmalloc managed region is mapped to that file, otherwise an anonymous
47 map is used if supported by the underlying OS. In case of running in
48 an OS without support of anonymous mappings then "/dev/zero" is used
49 and in both cases the data will not exist in any filesystem object.
51 If the open file corresponding to FD is from a previous use of
52 mmalloc and passes some basic sanity checks to ensure that it is
53 compatible with the current mmalloc package, then it's data is
54 mapped in and is immediately accessible at the same addresses in
55 the current process as the process that created the file (ignoring
56 the BASEADDR parameter).
58 For non valid FDs or empty files ones the mapping is established
59 starting at the specified address BASEADDR in the process address
62 The provided BASEADDR should be choosed carefully in order to avoid
63 bumping into existing mapped regions or future mapped regions.
65 On success, returns a "malloc descriptor" which is used in subsequent
66 calls to other mmalloc package functions. It is explicitly "void *"
67 ("char *" for systems that don't fully support void) so that users
68 of the package don't have to worry about the actual implementation
71 On failure returns NULL. */
74 mmalloc_attach (fd, baseaddr)
83 /* First check to see if FD is a valid file descriptor, and if so, see
84 if the file has any current contents (size > 0). If it does, then
85 attempt to reuse the file. If we can't reuse the file, either
86 because it isn't a valid mmalloc produced file, was produced by an
87 obsolete version, or any other reason, then we fail to attach to
92 if (fstat (fd, &sbuf) < 0)
95 else if (sbuf.st_size > 0)
96 return ((PTR) reuse (fd));
99 /* If the user provided NULL BASEADDR then fail */
100 if (baseaddr == NULL)
103 /* We start off with the malloc descriptor allocated on the stack, until
104 we build it up enough to call _mmalloc_mmap_morecore() to allocate the
105 first page of the region and copy it there. Ensure that it is zero'd and
106 then initialize the fields that we know values for. */
109 memset ((char *) mdp, 0, sizeof (mtemp));
110 strncpy (mdp -> magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
111 mdp -> headersize = sizeof (mtemp);
112 mdp -> version = MMALLOC_VERSION;
113 mdp -> morecore = __mmalloc_mmap_morecore;
115 mdp -> base = mdp -> breakval = mdp -> top = baseaddr;
117 /* If we have not been passed a valid open file descriptor for the file
118 to map to, then we go for an anonymous map */
121 mdp -> flags |= MMALLOC_ANONYMOUS;
123 /* If we have not been passed a valid open file descriptor for the file
124 to map to, then open /dev/zero and use that to map to. */
126 /* if (mdp -> fd < 0)*/
128 /* if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0)*/
134 /* mdp -> flags |= MMALLOC_DEVZERO;*/
138 /* Now try to map in the first page, copy the malloc descriptor structure
139 there, and arrange to return a pointer to this new copy. If the mapping
140 fails, then close the file descriptor if it was opened by us, and arrange
143 if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL)
145 memcpy (mbase, mdp, sizeof (mtemp));
146 // mdp = (struct mdesc *) mbase;
154 return ((PTR) mbase);
157 /* Given an valid file descriptor on an open file, test to see if that file
158 is a valid mmalloc produced file, and if so, attempt to remap it into the
159 current process at the same address to which it was previously mapped.
161 Note that we have to update the file descriptor number in the malloc-
162 descriptor read from the file to match the current valid one, before
163 trying to map the file in, and again after a successful mapping and
164 after we've switched over to using the mapped in malloc descriptor
165 rather than the temporary one on the stack.
167 Once we've switched over to using the mapped in malloc descriptor, we
168 have to update the pointer to the morecore function, since it almost
169 certainly will be at a different address if the process reusing the
170 mapped region is from a different executable.
172 Also note that if the heap being remapped previously used the mmcheckf()
173 routines, we need to update the hooks since their target functions
174 will have certainly moved if the executable has changed in any way.
175 We do this by calling mmcheckf() internally.
177 Returns a pointer to the malloc descriptor if successful, or NULL if
178 unsuccessful for some reason. */
180 static struct mdesc *
185 struct mdesc *mdp = NULL;
187 if (lseek (fd, 0L, SEEK_SET) != 0)
189 if (read (fd, (char *) &mtemp, sizeof (mtemp)) != sizeof (mtemp))
191 if (mtemp.headersize != sizeof (mtemp))
193 if (strcmp (mtemp.magic, MMALLOC_MAGIC) != 0)
195 if (mtemp.version > MMALLOC_VERSION)
199 if (__mmalloc_remap_core (&mtemp) == mtemp.base)
201 mdp = (struct mdesc *) mtemp.base;
203 mdp -> morecore = __mmalloc_mmap_morecore;
204 if (mdp -> mfree_hook != NULL)
206 mmcheckf ((PTR) mdp, (void (*) PARAMS ((void))) NULL, 1);
212 #else /* !defined (HAVE_MMAP) */
214 /* For systems without mmap, the library still supplies an entry point
215 to link to, but trying to initialize access to an mmap'd managed region
220 mmalloc_attach (fd, baseaddr)
227 #endif /* defined (HAVE_MMAP) */