Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Align address on a page boundary.
[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 #include "xbt/ex.h"
32
33 #ifndef SEEK_SET
34 #define SEEK_SET 0
35 #endif
36
37
38 /* Forward declarations/prototypes for local functions */
39
40 static struct mdesc *reuse(int fd);
41
42 /* Initialize access to a mmalloc managed region.
43
44    If FD is a valid file descriptor for an open file then data for the
45    mmalloc managed region is mapped to that file, otherwise an anonymous
46    map is used if supported by the underlying OS. In case of running in
47    an OS without support of anonymous mappings then "/dev/zero" is used 
48    and in both cases the data will not exist in any filesystem object.
49
50    If the open file corresponding to FD is from a previous use of
51    mmalloc and passes some basic sanity checks to ensure that it is
52    compatible with the current mmalloc package, then it's data is
53    mapped in and is immediately accessible at the same addresses in
54    the current process as the process that created the file (ignoring
55    the BASEADDR parameter).
56
57    For non valid FDs or empty files ones the mapping is established 
58    starting at the specified address BASEADDR in the process address 
59    space.
60
61    The provided BASEADDR should be choosed carefully in order to avoid
62    bumping into existing mapped regions or future mapped regions.
63
64    On success, returns a "malloc descriptor" which is used in subsequent
65    calls to other mmalloc package functions.  It is explicitly "void *"
66    ("char *" for systems that don't fully support void) so that users
67    of the package don't have to worry about the actual implementation
68    details.
69
70    On failure returns NULL. */
71
72 void *mmalloc_attach(int fd, void *baseaddr)
73 {
74   struct mdesc mtemp;
75   struct mdesc *mdp;
76   void *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     if (fstat(fd, &sbuf) < 0)
88       return (NULL);
89
90     else if (sbuf.st_size > 0)
91       return ((void *) reuse(fd));
92   }
93
94   /* If the user provided NULL BASEADDR then fail */
95   if (baseaddr == NULL)
96     return (NULL);
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;
104   memset((char *) mdp, 0, sizeof(mtemp));
105   strncpy(mdp->magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
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   mdp->next_mdesc = NULL;
112   mdp->refcount = 1;
113   
114   /* If we have not been passed a valid open file descriptor for the file
115      to map to, then we go for an anonymous map */
116
117   if (mdp->fd < 0){
118     mdp->flags |= MMALLOC_ANONYMOUS;
119     sem_init(&mdp->sem, 0, 1);
120   }else{
121     sem_init(&mdp->sem, 1, 1);
122   }
123   
124   /* If we have not been passed a valid open file descriptor for the file
125      to map to, then open /dev/zero and use that to map to. */
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     memcpy(mbase, mdp, sizeof(mtemp));
134   } else {
135     THROWF(system_error,0,"morecore failed to get some memory!");
136   }
137
138   /* Add the new heap to the linked list of heaps attached by mmalloc */  
139   if(__mmalloc_default_mdp){
140     mdp = __mmalloc_default_mdp;
141     while(mdp->next_mdesc)
142       mdp = mdp->next_mdesc;
143
144     LOCK(mdp);
145       mdp->next_mdesc = (struct mdesc *)mbase;
146     UNLOCK(mdp);
147   }
148   
149   return ((void *) mbase);
150 }
151
152 /* Given an valid file descriptor on an open file, test to see if that file
153    is a valid mmalloc produced file, and if so, attempt to remap it into the
154    current process at the same address to which it was previously mapped.
155
156    Note that we have to update the file descriptor number in the malloc-
157    descriptor read from the file to match the current valid one, before
158    trying to map the file in, and again after a successful mapping and
159    after we've switched over to using the mapped in malloc descriptor 
160    rather than the temporary one on the stack.
161
162    Once we've switched over to using the mapped in malloc descriptor, we
163    have to update the pointer to the morecore function, since it almost
164    certainly will be at a different address if the process reusing the
165    mapped region is from a different executable.
166
167    Also note that if the heap being remapped previously used the mmcheckf()
168    routines, we need to update the hooks since their target functions
169    will have certainly moved if the executable has changed in any way.
170    We do this by calling mmcheckf() internally.
171
172    Returns a pointer to the malloc descriptor if successful, or NULL if
173    unsuccessful for some reason. */
174
175 static struct mdesc *reuse(int fd)
176 {
177   struct mdesc mtemp;
178   struct mdesc *mdp = NULL, *mdptemp = NULL;
179
180   if (lseek(fd, 0L, SEEK_SET) != 0)
181     return NULL;
182   if (read(fd, (char *) &mtemp, sizeof(mtemp)) != sizeof(mtemp))
183     return NULL;
184   if (mtemp.headersize != sizeof(mtemp))
185     return NULL;
186   if (strcmp(mtemp.magic, MMALLOC_MAGIC) != 0)
187     return NULL;
188   if (mtemp.version > MMALLOC_VERSION)
189     return NULL;
190
191   mtemp.fd = fd;
192   if (__mmalloc_remap_core(&mtemp) == mtemp.base) {
193     mdp = (struct mdesc *) mtemp.base;
194     mdp->fd = fd;
195     mdp->morecore = __mmalloc_mmap_morecore;
196     if(!mdp->refcount){
197       sem_init(&mdp->sem, 1, 1);
198       mdp->refcount++;
199     }
200     if (mdp->mfree_hook != NULL) {
201       mmcheckf((void *) mdp, (void (*)(void)) NULL, 1);
202     }
203   }
204   
205   /* Add the new heap to the linked list of heaps attached by mmalloc */  
206   mdptemp = __mmalloc_default_mdp;
207   while(mdptemp->next_mdesc)
208     mdptemp = mdptemp->next_mdesc;
209
210   LOCK(mdptemp);
211     mdptemp->next_mdesc = mdp;
212   UNLOCK(mdptemp);
213   
214   return (mdp);
215 }