1 /* Initialization for access to a mmap'd malloc managed region.
2 Copyright 1992 Free Software Foundation, Inc.
4 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <sys/types.h>
30 /* Forward declarations/prototypes for local functions */
32 static struct mdesc *reuse PARAMS ((int));
34 /* Initialize access to a mmalloc managed region.
36 If FD is a valid file descriptor for an open file then data for the
37 mmalloc managed region is mapped to that file, otherwise "/dev/zero"
38 is used and the data will not exist in any filesystem object.
40 If the open file corresponding to FD is from a previous use of
41 mmalloc and passes some basic sanity checks to ensure that it is
42 compatible with the current mmalloc package, then it's data is
43 mapped in and is immediately accessible at the same addresses in
44 the current process as the process that created the file.
46 If BASEADDR is not NULL, the mapping is established starting at the
47 specified address in the process address space. If BASEADDR is NULL,
48 the mmalloc package chooses a suitable address at which to start the
49 mapped region, which will be the value of the previous mapping if
50 opening an existing file which was previously built by mmalloc, or
51 for new files will be a value chosen by mmap.
53 Specifying BASEADDR provides more control over where the regions
54 start and how big they can be before bumping into existing mapped
55 regions or future mapped regions.
57 On success, returns a "malloc descriptor" which is used in subsequent
58 calls to other mmalloc package functions. It is explicitly "void *"
59 ("char *" for systems that don't fully support void) so that users
60 of the package don't have to worry about the actual implementation
63 On failure returns NULL. */
66 #if defined(HAVE_MMAP)
69 mmalloc_attach (fd, baseaddr)
78 /* First check to see if FD is a valid file descriptor, and if so, see
79 if the file has any current contents (size > 0). If it does, then
80 attempt to reuse the file. If we can't reuse the file, either
81 because it isn't a valid mmalloc produced file, was produced by an
82 obsolete version, or any other reason, then we fail to attach to
87 if (fstat (fd, &sbuf) < 0)
91 else if (sbuf.st_size > 0)
93 return ((PTR) reuse (fd));
97 /* We start off with the malloc descriptor allocated on the stack, until
98 we build it up enough to call _mmalloc_mmap_morecore() to allocate the
99 first page of the region and copy it there. Ensure that it is zero'd and
100 then initialize the fields that we know values for. */
103 memset ((char *) mdp, 0, sizeof (mtemp));
104 strncpy (mdp -> magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
105 mdp -> headersize = sizeof (mtemp);
106 mdp -> version = MMALLOC_VERSION;
107 mdp -> morecore = __mmalloc_mmap_morecore;
109 mdp -> base = mdp -> breakval = mdp -> top = baseaddr;
111 /* If we have not been passed a valid open file descriptor for the file
112 to map to, then open /dev/zero and use that to map to. */
116 if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0)
122 mdp -> flags |= MMALLOC_DEVZERO;
126 /* Now try to map in the first page, copy the malloc descriptor structure
127 there, and arrange to return a pointer to this new copy. If the mapping
128 fails, then close the file descriptor if it was opened by us, and arrange
131 if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL)
133 memcpy (mbase, mdp, sizeof (mtemp));
134 mdp = (struct mdesc *) mbase;
138 if (mdp -> flags & MMALLOC_DEVZERO)
148 /* Given an valid file descriptor on an open file, test to see if that file
149 is a valid mmalloc produced file, and if so, attempt to remap it into the
150 current process at the same address to which it was previously mapped.
152 Note that we have to update the file descriptor number in the malloc-
153 descriptor read from the file to match the current valid one, before
154 trying to map the file in, and again after a successful mapping and
155 after we've switched over to using the mapped in malloc descriptor
156 rather than the temporary one on the stack.
158 Also note that if the heap being remapped previously used the mmcheck()
159 routines, we need to update the hooks since their target functions
160 will have certainly moved if the executable has changed in any way.
161 We do this by calling mmcheck() internally.
163 Returns a pointer to the malloc descriptor if successful, or NULL if
164 unsuccessful for some reason. */
166 static struct mdesc *
171 struct mdesc *mdp = NULL;
173 if ((lseek (fd, 0L, SEEK_SET) == 0) &&
174 (read (fd, (char *) &mtemp, sizeof (mtemp)) == sizeof (mtemp)) &&
175 (mtemp.headersize == sizeof (mtemp)) &&
176 (strcmp (mtemp.magic, MMALLOC_MAGIC) == 0) &&
177 (mtemp.version <= MMALLOC_VERSION))
180 if (__mmalloc_remap_core (&mtemp) == mtemp.base)
182 mdp = (struct mdesc *) mtemp.base;
184 if (mdp -> mfree_hook != NULL)
186 (void) mmcheck ((PTR) mdp, (void (*) PARAMS ((void))) NULL);
193 #else /* !defined (HAVE_MMAP) */
195 /* For systems without mmap, the library still supplies an entry point
196 to link to, but trying to initialize access to an mmap'd managed region
200 mmalloc_attach (fd, baseaddr)
207 #endif /* defined (HAVE_MMAP) */