Logo AND Algorithmique Numérique Distribuée

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