Logo AND Algorithmique Numérique Distribuée

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