Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fight for better integration of mmalloc, mc and xbt
[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 /* Forward declarations/prototypes for local functions */
38
39 static struct mdesc *reuse (int fd);
40
41 /* Initialize access to a mmalloc managed region.
42
43    If FD is a valid file descriptor for an open file then data for the
44    mmalloc managed region is mapped to that file, otherwise an anonymous
45    map is used if supported by the underlying OS. In case of running in
46    an OS without support of anonymous mappings then "/dev/zero" is used 
47    and in both cases the data will not exist in any filesystem object.
48
49    If the open file corresponding to FD is from a previous use of
50    mmalloc and passes some basic sanity checks to ensure that it is
51    compatible with the current mmalloc package, then it's data is
52    mapped in and is immediately accessible at the same addresses in
53    the current process as the process that created the file (ignoring
54    the BASEADDR parameter).
55
56    For non valid FDs or empty files ones the mapping is established 
57    starting at the specified address BASEADDR in the process address 
58    space.
59
60    The provided BASEADDR should be choosed carefully in order to avoid
61    bumping into existing mapped regions or future mapped regions.
62
63    On success, returns a "malloc descriptor" which is used in subsequent
64    calls to other mmalloc package functions.  It is explicitly "void *"
65    ("char *" for systems that don't fully support void) so that users
66    of the package don't have to worry about the actual implementation
67    details.
68
69    On failure returns NULL. */
70
71 void * mmalloc_attach (int fd, void *baseaddr) {
72   struct mdesc mtemp;
73   struct mdesc *mdp;
74   void* mbase;
75   struct stat sbuf;
76
77   /* First check to see if FD is a valid file descriptor, and if so, see
78      if the file has any current contents (size > 0).  If it does, then
79      attempt to reuse the file.  If we can't reuse the file, either
80      because it isn't a valid mmalloc produced file, was produced by an
81      obsolete version, or any other reason, then we fail to attach to
82      this file. */
83
84   if (fd >= 0)
85   {
86     if (fstat (fd, &sbuf) < 0)
87       return (NULL);
88
89     else if (sbuf.st_size > 0)
90       return ((void*) reuse (fd));
91   }
92
93   /* If the user provided NULL BASEADDR then fail */
94   if (baseaddr == NULL)
95     return (NULL);
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 we go for an anonymous map */
113
114   if(mdp -> fd < 0)
115     mdp -> flags |= MMALLOC_ANONYMOUS;
116
117   /* If we have not been passed a valid open file descriptor for the file
118      to map to, then open /dev/zero and use that to map to. */
119
120   /* Now try to map in the first page, copy the malloc descriptor structure
121      there, and arrange to return a pointer to this new copy.  If the mapping
122      fails, then close the file descriptor if it was opened by us, and arrange
123      to return a NULL. */
124
125   if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL)
126   {
127     memcpy (mbase, mdp, sizeof (mtemp));
128     //    mdp = (struct mdesc *) mbase;
129   }
130   else
131   {
132     abort();
133     //    mdp = NULL;
134   }
135
136   { /* create the mutex within that heap */
137     void*old_heap=mmalloc_get_current_heap();
138     mmalloc_set_current_heap(mbase);
139     mdp->mutex =xbt_os_mutex_init();
140     mmalloc_set_current_heap(old_heap);
141   }
142
143   return ((void*) mbase);
144 }
145
146 /* Given an valid file descriptor on an open file, test to see if that file
147    is a valid mmalloc produced file, and if so, attempt to remap it into the
148    current process at the same address to which it was previously mapped.
149
150    Note that we have to update the file descriptor number in the malloc-
151    descriptor read from the file to match the current valid one, before
152    trying to map the file in, and again after a successful mapping and
153    after we've switched over to using the mapped in malloc descriptor 
154    rather than the temporary one on the stack.
155
156    Once we've switched over to using the mapped in malloc descriptor, we
157    have to update the pointer to the morecore function, since it almost
158    certainly will be at a different address if the process reusing the
159    mapped region is from a different executable.
160
161    Also note that if the heap being remapped previously used the mmcheckf()
162    routines, we need to update the hooks since their target functions
163    will have certainly moved if the executable has changed in any way.
164    We do this by calling mmcheckf() internally.
165
166    Returns a pointer to the malloc descriptor if successful, or NULL if
167    unsuccessful for some reason. */
168
169 static struct mdesc *
170 reuse (int fd)
171 {
172   struct mdesc mtemp;
173   struct mdesc *mdp = NULL;
174
175   if (lseek (fd, 0L, SEEK_SET) != 0)
176     return NULL;
177   if (read (fd, (char *) &mtemp, sizeof (mtemp)) != sizeof (mtemp))
178     return NULL;
179   if (mtemp.headersize != sizeof (mtemp))
180     return NULL;
181   if (strcmp (mtemp.magic, MMALLOC_MAGIC) != 0)
182     return NULL;
183   if (mtemp.version > MMALLOC_VERSION)
184     return NULL;
185
186   mtemp.fd = fd;
187   if (__mmalloc_remap_core (&mtemp) == mtemp.base)
188   {
189     mdp = (struct mdesc *) mtemp.base;
190     mdp -> fd = fd;
191     mdp -> morecore = __mmalloc_mmap_morecore;
192     mdp->mutex =xbt_os_mutex_init();
193     if (mdp -> mfree_hook != NULL)
194     {
195       mmcheckf ((void*) mdp, (void (*) (void)) NULL, 1);
196     }
197   }
198
199   { /* create the mutex within that heap */
200     void*old_heap=mmalloc_get_current_heap();
201     mmalloc_set_current_heap(mdp);
202     mdp->mutex =xbt_os_mutex_init();
203     mmalloc_set_current_heap(old_heap);
204   }
205
206   return (mdp);
207 }
208