Logo AND Algorithmique Numérique Distribuée

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