Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5c7db2a6d487d2916b4640d2aa360e2c89dbc1dc
[simgrid.git] / src / xbt / mmalloc / mmap-sup.c
1 /* Support for an sbrk-like function that uses mmap.
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 #if defined(HAVE_MMAP)
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>     /* Prototypes for lseek */
27 #endif
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31
32 #ifndef SEEK_SET
33 #define SEEK_SET 0
34 #endif
35
36 #include "mmprivate.h"
37
38 /* Cache the pagesize for the current host machine.  Note that if the host
39    does not readily provide a getpagesize() function, we need to emulate it
40    elsewhere, not clutter up this file with lots of kluges to try to figure
41    it out. */
42
43 static size_t pagesize;
44 #if NEED_DECLARATION_GETPAGESIZE
45 extern int getpagesize PARAMS ((void));
46 #endif
47
48 #define PAGE_ALIGN(addr) (PTR) (((long)(addr) + pagesize - 1) & \
49                                     ~(pagesize - 1))
50
51 /* Return MAP_PRIVATE if MDP represents /dev/zero.  Otherwise, return
52    MAP_SHARED.  */
53
54 #define MAP_PRIVATE_OR_SHARED(MDP) ((MDP -> flags & MMALLOC_DEVZERO) \
55                                     ? MAP_PRIVATE \
56                                     : MAP_SHARED)
57
58 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
59
60 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
61                               ? MAP_ANONYMOUS \
62                               : 0)
63
64 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
65 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
66                               ? -1 \
67                                               : (MDP) -> fd)
68
69 /*  Get core for the memory region specified by MDP, using SIZE as the
70     amount to either add to or subtract from the existing region.  Works
71     like sbrk(), but using mmap(). */
72
73 PTR
74 __mmalloc_mmap_morecore (mdp, size)
75   struct mdesc *mdp;
76   int size;
77 {
78   PTR result = NULL;
79   off_t foffset;        /* File offset at which new mapping will start */
80   size_t mapbytes;      /* Number of bytes to map */
81   PTR moveto;   /* Address where we wish to move "break value" to */
82   PTR mapto;    /* Address we actually mapped to */
83   char buf = 0;         /* Single byte to write to extend mapped file */
84
85   if (pagesize == 0)
86     pagesize = getpagesize();
87
88   if (size == 0)
89   {
90     /* Just return the current "break" value. */
91     result = mdp -> breakval;
92   }
93   else if (size < 0)
94   {
95     /* We are deallocating memory.  If the amount requested would cause
96              us to try to deallocate back past the base of the mmap'd region
97              then do nothing, and return NULL.  Otherwise, deallocate the
98              memory and return the old break value. */
99     if (mdp -> breakval + size >= mdp -> base)
100           {
101             result = (PTR) mdp -> breakval;
102             mdp -> breakval += size;
103             moveto = PAGE_ALIGN (mdp -> breakval);
104             munmap (moveto, (size_t) (mdp -> top - moveto) - 1);
105             mdp -> top = moveto;
106           }
107   }
108   else
109   {
110     /* We are allocating memory. Make sure we have an open file
111              descriptor if not working with anonymous memory. */
112     if ( !(mdp->flags & MMALLOC_ANONYMOUS) && mdp -> fd < 0)
113           {
114             result = NULL;
115           }
116     else if (mdp -> breakval + size > mdp -> top)
117           {
118           /* The request would move us past the end of the currently
119              mapped memory, so map in enough more memory to satisfy
120              the request.  This means we also have to grow the mapped-to
121              file by an appropriate amount, since mmap cannot be used
122              to extend a file. */
123             moveto = PAGE_ALIGN (mdp -> breakval + size);
124             mapbytes = moveto - mdp -> top;
125             foffset = mdp -> top - mdp -> base;
126
127       if( mdp -> fd > 0){
128           /* FIXME:  Test results of lseek() and write() */
129         lseek (mdp -> fd, foffset + mapbytes - 1, SEEK_SET);
130               write (mdp -> fd, &buf, 1);
131       }
132             
133             /* Let's call mmap. Note that it is possible that mdp->top
134                is 0. In this case mmap will choose the address for us */
135       mapto = mmap (mdp->top, mapbytes, PROT_READ | PROT_WRITE,
136         MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) | MAP_FIXED,
137         MAP_ANON_OR_FD(mdp), foffset);
138
139       if (mapto != (PTR) -1){
140             
141         if(mdp -> top == 0)
142           mdp -> base = mdp -> breakval = mapto;
143         
144         mdp -> top = PAGE_ALIGN (mdp -> breakval + size);
145         result = (PTR) mdp -> breakval;
146         mdp -> breakval += size;
147       }
148           }
149     else
150           {
151             result = (PTR) mdp -> breakval;
152             mdp -> breakval += size;
153           }
154   }
155   return (result);
156 }
157
158 PTR
159 __mmalloc_remap_core (mdp)
160   struct mdesc *mdp;
161 {
162   PTR base;
163
164   /* FIXME:  Quick hack, needs error checking and other attention. */
165
166   base = mmap (mdp -> base, mdp -> top - mdp -> base,
167                PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE_OR_SHARED (mdp) | MAP_FIXED,
168                mdp -> fd, 0);
169   return ((PTR) base);
170 }
171
172 PTR
173 mmalloc_findbase (size)
174   int size;
175 {
176   int fd;
177   int flags;
178   PTR base = NULL;
179
180 #ifdef MAP_ANONYMOUS
181   flags = MAP_PRIVATE | MAP_ANONYMOUS;
182   fd = -1;
183 #else
184 #ifdef MAP_FILE
185   flags = MAP_PRIVATE | MAP_FILE;
186 #else
187   flags = MAP_PRIVATE;
188 #endif
189   fd = open ("/dev/zero", O_RDWR);
190   if (fd != -1)
191     {
192       return ((PTR) NULL);
193     }
194 #endif
195   base = mmap (0, size, PROT_READ | PROT_WRITE, flags, fd, 0);
196   if (base != (PTR) -1)
197     {
198       munmap (base, (size_t) size);
199     }
200   if (fd != -1)
201     {
202       close (fd);
203     }
204   if (base == 0)
205     {
206       /* Don't allow mapping at address zero.  We use that value
207          to signal an error return, and besides, it is useful to
208          catch NULL pointers if it is unmapped.  Instead start
209          at the next page boundary. */
210       base = (PTR) getpagesize ();
211     }
212   else if (base == (PTR) -1)
213     {
214       base = NULL;
215     }
216   return ((PTR) base);
217 }
218
219 #else   /* defined(HAVE_MMAP) */
220 /* Prevent "empty translation unit" warnings from the idiots at X3J11. */
221 //static char ansi_c_idiots = 69;
222 #endif  /* defined(HAVE_MMAP) */