Logo AND Algorithmique Numérique Distribuée

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