Fri Sep 11 13:58:02 1998 Michael Snyder <msnyder@cleaver.cygnus.com>
[deliverable/binutils-gdb.git] / mmalloc / attach.c
CommitLineData
01fb5bca
FF
1/* Initialization for access to a mmap'd malloc managed region.
2 Copyright 1992 Free Software Foundation, Inc.
3
4 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <fcntl.h>
22#include <sys/types.h>
23#include <sys/stat.h>
bc22f7de 24#include <string.h>
01fb5bca
FF
25#include "mmalloc.h"
26
27#ifndef SEEK_SET
28#define SEEK_SET 0
29#endif
30
bc22f7de
FF
31
32#if defined(HAVE_MMAP)
33
01fb5bca
FF
34/* Forward declarations/prototypes for local functions */
35
36static struct mdesc *reuse PARAMS ((int));
37
38/* Initialize access to a mmalloc managed region.
39
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.
43
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.
49
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.
56
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.
60
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
65 details.
66
67 On failure returns NULL. */
68
01fb5bca
FF
69PTR
70mmalloc_attach (fd, baseaddr)
71 int fd;
72 PTR baseaddr;
73{
74 struct mdesc mtemp;
75 struct mdesc *mdp;
76 PTR mbase;
77 struct stat sbuf;
78
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
84 this file. */
85
86 if (fd >= 0)
87 {
88 if (fstat (fd, &sbuf) < 0)
89 {
90 return (NULL);
91 }
92 else if (sbuf.st_size > 0)
93 {
94 return ((PTR) reuse (fd));
95 }
96 }
97
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. */
102
103 mdp = &mtemp;
bc22f7de
FF
104 (void) memset ((char *) mdp, 0, sizeof (mtemp));
105 (void) strncpy (mdp -> magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
01fb5bca
FF
106 mdp -> headersize = sizeof (mtemp);
107 mdp -> version = MMALLOC_VERSION;
108 mdp -> morecore = __mmalloc_mmap_morecore;
109 mdp -> fd = fd;
110 mdp -> base = mdp -> breakval = mdp -> top = baseaddr;
111
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. */
114
115 if (mdp -> fd < 0)
116 {
117 if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0)
118 {
119 return (NULL);
120 }
121 else
122 {
123 mdp -> flags |= MMALLOC_DEVZERO;
124 }
125 }
126
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
130 to return a NULL. */
131
132 if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL)
133 {
bc22f7de 134 (void) memcpy (mbase, mdp, sizeof (mtemp));
01fb5bca
FF
135 mdp = (struct mdesc *) mbase;
136 }
137 else
138 {
139 if (mdp -> flags & MMALLOC_DEVZERO)
140 {
141 close (mdp -> fd);
142 }
143 mdp = NULL;
144 }
145
146 return ((PTR) mdp);
147}
148
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.
152
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.
158
bc22f7de
FF
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.
163
01fb5bca
FF
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.
168
169 Returns a pointer to the malloc descriptor if successful, or NULL if
170 unsuccessful for some reason. */
171
172static struct mdesc *
173reuse (fd)
174 int fd;
175{
176 struct mdesc mtemp;
177 struct mdesc *mdp = NULL;
178
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))
184 {
185 mtemp.fd = fd;
186 if (__mmalloc_remap_core (&mtemp) == mtemp.base)
187 {
188 mdp = (struct mdesc *) mtemp.base;
189 mdp -> fd = fd;
bc22f7de 190 mdp -> morecore = __mmalloc_mmap_morecore;
01fb5bca
FF
191 if (mdp -> mfree_hook != NULL)
192 {
193 (void) mmcheck ((PTR) mdp, (void (*) PARAMS ((void))) NULL);
194 }
195 }
196 }
197 return (mdp);
198}
199
200#else /* !defined (HAVE_MMAP) */
201
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
204 always fails. */
205
bc22f7de 206/* ARGSUSED */
01fb5bca
FF
207PTR
208mmalloc_attach (fd, baseaddr)
209 int fd;
210 PTR baseaddr;
211{
212 return (NULL);
213}
214
215#endif /* defined (HAVE_MMAP) */
216
This page took 0.208824 seconds and 4 git commands to generate.