]>
Commit | Line | Data |
---|---|---|
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. [email protected] | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. */ | |
19 | ||
20 | ||
21 | #include <fcntl.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include "mmalloc.h" | |
25 | ||
26 | #ifndef SEEK_SET | |
27 | #define SEEK_SET 0 | |
28 | #endif | |
29 | ||
30 | /* Forward declarations/prototypes for local functions */ | |
31 | ||
32 | static struct mdesc *reuse PARAMS ((int)); | |
33 | ||
34 | /* Initialize access to a mmalloc managed region. | |
35 | ||
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. | |
39 | ||
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. | |
45 | ||
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. | |
52 | ||
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. | |
56 | ||
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 | |
61 | details. | |
62 | ||
63 | On failure returns NULL. */ | |
64 | ||
65 | ||
66 | #if defined(HAVE_MMAP) | |
67 | ||
68 | PTR | |
69 | mmalloc_attach (fd, baseaddr) | |
70 | int fd; | |
71 | PTR baseaddr; | |
72 | { | |
73 | struct mdesc mtemp; | |
74 | struct mdesc *mdp; | |
75 | PTR mbase; | |
76 | struct stat sbuf; | |
77 | ||
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 | |
83 | this file. */ | |
84 | ||
85 | if (fd >= 0) | |
86 | { | |
87 | if (fstat (fd, &sbuf) < 0) | |
88 | { | |
89 | return (NULL); | |
90 | } | |
91 | else if (sbuf.st_size > 0) | |
92 | { | |
93 | return ((PTR) reuse (fd)); | |
94 | } | |
95 | } | |
96 | ||
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. */ | |
101 | ||
102 | mdp = &mtemp; | |
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; | |
108 | mdp -> fd = fd; | |
109 | mdp -> base = mdp -> breakval = mdp -> top = baseaddr; | |
110 | ||
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. */ | |
113 | ||
114 | if (mdp -> fd < 0) | |
115 | { | |
116 | if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0) | |
117 | { | |
118 | return (NULL); | |
119 | } | |
120 | else | |
121 | { | |
122 | mdp -> flags |= MMALLOC_DEVZERO; | |
123 | } | |
124 | } | |
125 | ||
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 | |
129 | to return a NULL. */ | |
130 | ||
131 | if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL) | |
132 | { | |
133 | memcpy (mbase, mdp, sizeof (mtemp)); | |
134 | mdp = (struct mdesc *) mbase; | |
135 | } | |
136 | else | |
137 | { | |
138 | if (mdp -> flags & MMALLOC_DEVZERO) | |
139 | { | |
140 | close (mdp -> fd); | |
141 | } | |
142 | mdp = NULL; | |
143 | } | |
144 | ||
145 | return ((PTR) mdp); | |
146 | } | |
147 | ||
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. | |
151 | ||
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. | |
157 | ||
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. | |
162 | ||
163 | Returns a pointer to the malloc descriptor if successful, or NULL if | |
164 | unsuccessful for some reason. */ | |
165 | ||
166 | static struct mdesc * | |
167 | reuse (fd) | |
168 | int fd; | |
169 | { | |
170 | struct mdesc mtemp; | |
171 | struct mdesc *mdp = NULL; | |
172 | ||
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)) | |
178 | { | |
179 | mtemp.fd = fd; | |
180 | if (__mmalloc_remap_core (&mtemp) == mtemp.base) | |
181 | { | |
182 | mdp = (struct mdesc *) mtemp.base; | |
183 | mdp -> fd = fd; | |
184 | if (mdp -> mfree_hook != NULL) | |
185 | { | |
186 | (void) mmcheck ((PTR) mdp, (void (*) PARAMS ((void))) NULL); | |
187 | } | |
188 | } | |
189 | } | |
190 | return (mdp); | |
191 | } | |
192 | ||
193 | #else /* !defined (HAVE_MMAP) */ | |
194 | ||
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 | |
197 | always fails. */ | |
198 | ||
199 | PTR | |
200 | mmalloc_attach (fd, baseaddr) | |
201 | int fd; | |
202 | PTR baseaddr; | |
203 | { | |
204 | return (NULL); | |
205 | } | |
206 | ||
207 | #endif /* defined (HAVE_MMAP) */ | |
208 |