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>
32 #if defined(HAVE_MMAP)
34 /* Forward declarations/prototypes for local functions */
36 static struct mdesc
*reuse
PARAMS ((int));
38 /* Initialize access to a mmalloc managed region.
40 If FD is a valid file descriptor for an open file then data for the
41 mmalloc managed region is mapped to that file, otherwise "/dev/zero"
42 is used and the data will not exist in any filesystem object.
44 If the open file corresponding to FD is from a previous use of
45 mmalloc and passes some basic sanity checks to ensure that it is
46 compatible with the current mmalloc package, then it's data is
47 mapped in and is immediately accessible at the same addresses in
48 the current process as the process that created the file.
50 If BASEADDR is not NULL, the mapping is established starting at the
51 specified address in the process address space. If BASEADDR is NULL,
52 the mmalloc package chooses a suitable address at which to start the
53 mapped region, which will be the value of the previous mapping if
54 opening an existing file which was previously built by mmalloc, or
55 for new files will be a value chosen by mmap.
57 Specifying BASEADDR provides more control over where the regions
58 start and how big they can be before bumping into existing mapped
59 regions or future mapped regions.
61 On success, returns a "malloc descriptor" which is used in subsequent
62 calls to other mmalloc package functions. It is explicitly "void *"
63 ("char *" for systems that don't fully support void) so that users
64 of the package don't have to worry about the actual implementation
67 On failure returns NULL. */
70 mmalloc_attach (fd
, baseaddr
)
79 /* First check to see if FD is a valid file descriptor, and if so, see
80 if the file has any current contents (size > 0). If it does, then
81 attempt to reuse the file. If we can't reuse the file, either
82 because it isn't a valid mmalloc produced file, was produced by an
83 obsolete version, or any other reason, then we fail to attach to
88 if (fstat (fd
, &sbuf
) < 0)
92 else if (sbuf
.st_size
> 0)
94 return ((PTR
) reuse (fd
));
98 /* We start off with the malloc descriptor allocated on the stack, until
99 we build it up enough to call _mmalloc_mmap_morecore() to allocate the
100 first page of the region and copy it there. Ensure that it is zero'd and
101 then initialize the fields that we know values for. */
104 (void) memset ((char *) mdp
, 0, sizeof (mtemp
));
105 (void) strncpy (mdp
-> magic
, MMALLOC_MAGIC
, MMALLOC_MAGIC_SIZE
);
106 mdp
-> headersize
= sizeof (mtemp
);
107 mdp
-> version
= MMALLOC_VERSION
;
108 mdp
-> morecore
= __mmalloc_mmap_morecore
;
110 mdp
-> base
= mdp
-> breakval
= mdp
-> top
= baseaddr
;
112 /* If we have not been passed a valid open file descriptor for the file
113 to map to, then open /dev/zero and use that to map to. */
117 if ((mdp
-> fd
= open ("/dev/zero", O_RDWR
)) < 0)
123 mdp
-> flags
|= MMALLOC_DEVZERO
;
127 /* Now try to map in the first page, copy the malloc descriptor structure
128 there, and arrange to return a pointer to this new copy. If the mapping
129 fails, then close the file descriptor if it was opened by us, and arrange
132 if ((mbase
= mdp
-> morecore (mdp
, sizeof (mtemp
))) != NULL
)
134 (void) memcpy (mbase
, mdp
, sizeof (mtemp
));
135 mdp
= (struct mdesc
*) mbase
;
139 if (mdp
-> flags
& MMALLOC_DEVZERO
)
149 /* Given an valid file descriptor on an open file, test to see if that file
150 is a valid mmalloc produced file, and if so, attempt to remap it into the
151 current process at the same address to which it was previously mapped.
153 Note that we have to update the file descriptor number in the malloc-
154 descriptor read from the file to match the current valid one, before
155 trying to map the file in, and again after a successful mapping and
156 after we've switched over to using the mapped in malloc descriptor
157 rather than the temporary one on the stack.
159 Once we've switched over to using the mapped in malloc descriptor, we
160 have to update the pointer to the morecore function, since it almost
161 certainly will be at a different address if the process reusing the
162 mapped region is from a different executable.
164 Also note that if the heap being remapped previously used the mmcheck()
165 routines, we need to update the hooks since their target functions
166 will have certainly moved if the executable has changed in any way.
167 We do this by calling mmcheck() internally.
169 Returns a pointer to the malloc descriptor if successful, or NULL if
170 unsuccessful for some reason. */
172 static struct mdesc
*
177 struct mdesc
*mdp
= NULL
;
179 if ((lseek (fd
, 0L, SEEK_SET
) == 0) &&
180 (read (fd
, (char *) &mtemp
, sizeof (mtemp
)) == sizeof (mtemp
)) &&
181 (mtemp
.headersize
== sizeof (mtemp
)) &&
182 (strcmp (mtemp
.magic
, MMALLOC_MAGIC
) == 0) &&
183 (mtemp
.version
<= MMALLOC_VERSION
))
186 if (__mmalloc_remap_core (&mtemp
) == mtemp
.base
)
188 mdp
= (struct mdesc
*) mtemp
.base
;
190 mdp
-> morecore
= __mmalloc_mmap_morecore
;
191 if (mdp
-> mfree_hook
!= NULL
)
193 (void) mmcheck ((PTR
) mdp
, (void (*) PARAMS ((void))) NULL
);
200 #else /* !defined (HAVE_MMAP) */
202 /* For systems without mmap, the library still supplies an entry point
203 to link to, but trying to initialize access to an mmap'd managed region
208 mmalloc_attach (fd
, baseaddr
)
215 #endif /* defined (HAVE_MMAP) */
This page took 0.033345 seconds and 4 git commands to generate.