Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
99cf8266c3750835c086f74d54f3881a2860a8e9
[simgrid.git] / src / xbt / mmalloc / attach.c
1 /* Initialization for access to a mmap'd malloc managed region.
2    Copyright 1992, 2000 Free Software Foundation, Inc.
3
4    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
5
6 This file is part of the GNU C Library.
7
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.
12
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.
17
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.  */
22
23 #include <sys/types.h>
24 #include <fcntl.h> /* After sys/types.h, at least for dpx/2.  */
25 #include <sys/stat.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>     /* Prototypes for lseek */
29 #endif
30 #include "mmprivate.h"
31
32 #ifndef SEEK_SET
33 #define SEEK_SET 0
34 #endif
35
36
37 #if defined(HAVE_MMAP)
38
39 /* Forward declarations/prototypes for local functions */
40
41 static struct mdesc *reuse (int fd);
42
43 /* Initialize access to a mmalloc managed region.
44
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.
50
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).
57
58    For non valid FDs or empty files ones the mapping is established 
59    starting at the specified address BASEADDR in the process address 
60    space.
61
62    The provided BASEADDR should be choosed carefully in order to avoid
63    bumping into existing mapped regions or future mapped regions.
64
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
69    details.
70
71    On failure returns NULL. */
72
73 void *
74 mmalloc_attach (int fd, void *baseaddr)
75 {
76   struct mdesc mtemp;
77   struct mdesc *mdp;
78   void* mbase;
79   struct stat sbuf;
80
81   /* First check to see if FD is a valid file descriptor, and if so, see
82      if the file has any current contents (size > 0).  If it does, then
83      attempt to reuse the file.  If we can't reuse the file, either
84      because it isn't a valid mmalloc produced file, was produced by an
85      obsolete version, or any other reason, then we fail to attach to
86      this file. */
87
88   if (fd >= 0)
89   {
90     if (fstat (fd, &sbuf) < 0)
91           return (NULL);
92
93     else if (sbuf.st_size > 0)
94           return ((void*) reuse (fd));
95   }
96
97   /* If the user provided NULL BASEADDR then fail */
98   if (baseaddr == NULL)
99     return (NULL);
100
101   /* We start off with the malloc descriptor allocated on the stack, until
102      we build it up enough to call _mmalloc_mmap_morecore() to allocate the
103      first page of the region and copy it there.  Ensure that it is zero'd and
104      then initialize the fields that we know values for. */
105
106   mdp = &mtemp;
107   memset ((char *) mdp, 0, sizeof (mtemp));
108   strncpy (mdp -> magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
109   mdp -> headersize = sizeof (mtemp);
110   mdp -> version = MMALLOC_VERSION;
111   mdp -> morecore = __mmalloc_mmap_morecore;
112   mdp -> fd = fd;
113   mdp -> base = mdp -> breakval = mdp -> top = baseaddr;
114
115   /* If we have not been passed a valid open file descriptor for the file
116      to map to, then we go for an anonymous map */
117
118   if(mdp -> fd < 0)
119     mdp -> flags |= MMALLOC_ANONYMOUS;
120
121   /* If we have not been passed a valid open file descriptor for the file
122      to map to, then open /dev/zero and use that to map to. */
123
124 /*  if (mdp -> fd < 0)*/
125 /*  {*/
126 /*    if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0)*/
127 /*      {*/
128 /*        return (NULL);*/
129 /*      }*/
130 /*    else*/
131 /*      {*/
132 /*        mdp -> flags |= MMALLOC_DEVZERO;*/
133 /*      }*/
134 /*  }*/
135
136   /* Now try to map in the first page, copy the malloc descriptor structure
137      there, and arrange to return a pointer to this new copy.  If the mapping
138      fails, then close the file descriptor if it was opened by us, and arrange
139      to return a NULL. */
140
141   if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL)
142   {
143     memcpy (mbase, mdp, sizeof (mtemp));
144 //    mdp = (struct mdesc *) mbase;
145   }
146   else
147   {
148     abort();
149 //    mdp = NULL;
150   }
151   
152   return ((void*) mbase);
153 }
154
155 /* Given an valid file descriptor on an open file, test to see if that file
156    is a valid mmalloc produced file, and if so, attempt to remap it into the
157    current process at the same address to which it was previously mapped.
158
159    Note that we have to update the file descriptor number in the malloc-
160    descriptor read from the file to match the current valid one, before
161    trying to map the file in, and again after a successful mapping and
162    after we've switched over to using the mapped in malloc descriptor 
163    rather than the temporary one on the stack.
164
165    Once we've switched over to using the mapped in malloc descriptor, we
166    have to update the pointer to the morecore function, since it almost
167    certainly will be at a different address if the process reusing the
168    mapped region is from a different executable.
169
170    Also note that if the heap being remapped previously used the mmcheckf()
171    routines, we need to update the hooks since their target functions
172    will have certainly moved if the executable has changed in any way.
173    We do this by calling mmcheckf() internally.
174
175    Returns a pointer to the malloc descriptor if successful, or NULL if
176    unsuccessful for some reason. */
177
178 static struct mdesc *
179 reuse (int fd)
180 {
181   struct mdesc mtemp;
182   struct mdesc *mdp = NULL;
183
184   if (lseek (fd, 0L, SEEK_SET) != 0)
185     return NULL;
186   if (read (fd, (char *) &mtemp, sizeof (mtemp)) != sizeof (mtemp))
187     return NULL;
188   if (mtemp.headersize != sizeof (mtemp))
189     return NULL;
190   if (strcmp (mtemp.magic, MMALLOC_MAGIC) != 0)
191     return NULL;
192   if (mtemp.version > MMALLOC_VERSION)
193     return NULL;
194
195   mtemp.fd = fd;
196   if (__mmalloc_remap_core (&mtemp) == mtemp.base)
197   {
198     mdp = (struct mdesc *) mtemp.base;
199         mdp -> fd = fd;
200         mdp -> morecore = __mmalloc_mmap_morecore;
201         if (mdp -> mfree_hook != NULL)
202         {
203           mmcheckf ((void*) mdp, (void (*) (void)) NULL, 1);
204         }
205   }
206   return (mdp);
207 }
208
209 #else   /* !defined (HAVE_MMAP) */
210
211 /* For systems without mmap, the library still supplies an entry point
212    to link to, but trying to initialize access to an mmap'd managed region
213    always fails. */
214
215 /* ARGSUSED */
216 void*
217 mmalloc_attach (int fd, void* baseaddr)
218 {
219    return (NULL);
220 }
221
222 #endif  /* defined (HAVE_MMAP) */
223