Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc++'
[simgrid.git] / src / xbt / mmalloc / mmorecore.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-2014. 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 #include <sys/wait.h>
23
24 #include "mmprivate.h"
25
26 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + xbt_pagesize - 1) &   \
27                                   ~((long)xbt_pagesize - 1))
28
29 /* Return MAP_PRIVATE if MDP represents /dev/zero.  Otherwise, return
30    MAP_SHARED.  */
31 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
32                                     ? MAP_PRIVATE                       \
33                                     : MAP_SHARED)
34
35 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
36 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
37                                ? MAP_ANONYMOUS                      \
38                                : 0)
39
40 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
41 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
42                              ? -1                                 \
43                              : (MDP) -> fd)
44
45 /*  Get core for the memory region specified by MDP, using SIZE as the
46     amount to either add to or subtract from the existing region.  Works
47     like sbrk(), but using mmap().
48
49     It never returns NULL. Instead, it dies verbosely on errors. */
50
51 void *mmorecore(struct mdesc *mdp, ssize_t size)
52 {
53   ssize_t test = 0;
54   void *result; // please keep it uninitialized to track issues
55   off_t foffset;                /* File offset at which new mapping will start */
56   size_t mapbytes;              /* Number of bytes to map */
57   void *moveto;                 /* Address where we wish to move "break value" to */
58   void *mapto;                  /* Address we actually mapped to */
59   char buf = 0;                 /* Single byte to write to extend mapped file */
60
61 //  fprintf(stderr,"increase %p by %u\n",mdp,size);
62
63   if (size == 0) {
64     /* Just return the current "break" value. */
65     result = mdp->breakval;
66
67   } else if (size < 0) {
68     /* We are deallocating memory.  If the amount requested would cause
69        us to try to deallocate back past the base of the mmap'd region
70        then die verbosely.  Otherwise, deallocate the memory and return
71        the old break value. */
72     if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
73       result = (void *) mdp->breakval;
74       mdp->breakval = (char *) mdp->breakval + size;
75       moveto = PAGE_ALIGN(mdp->breakval);
76       munmap(moveto,
77              (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
78       mdp->top = moveto;
79     } else {
80       fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
81       abort();
82     }
83   } else {
84     /* We are allocating memory. Make sure we have an open file
85        descriptor if not working with anonymous memory. */
86     if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
87       fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
88       abort();
89     } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
90       /* The request would move us past the end of the currently
91          mapped memory, so map in enough more memory to satisfy
92          the request.  This means we also have to grow the mapped-to
93          file by an appropriate amount, since mmap cannot be used
94          to extend a file. */
95       moveto = PAGE_ALIGN((char *) mdp->breakval + size);
96       mapbytes = (char *) moveto - (char *) mdp->top;
97       foffset = (char *) mdp->top - (char *) mdp->base;
98
99       if (mdp->fd > 0) {
100         /* FIXME:  Test results of lseek() */
101         lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
102         test = write(mdp->fd, &buf, 1);
103         if (test == -1) {
104           fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
105           abort();
106         }
107       }
108
109       /* Let's call mmap. Note that it is possible that mdp->top
110          is 0. In this case mmap will choose the address for us */
111       mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
112                    MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
113                    MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
114
115       if (mapto == (void *) -1/* That's MAP_FAILED */) {
116         char buff[1024];
117         fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s\n",strerror(errno));
118         sprintf(buff,"cat /proc/%d/maps",getpid());
119         int status = system(buff);
120         if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
121           fprintf(stderr, "Something went wrong when trying to %s\n", buff);
122         sleep(1);
123         abort();
124       }
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     } else {
133       result = (void *) mdp->breakval;
134       mdp->breakval = (char *) mdp->breakval + size;
135     }
136   }
137   return (result);
138 }
139
140 void *__mmalloc_remap_core(xbt_mheap_t mdp)
141 {
142   /* FIXME:  Quick hack, needs error checking and other attention. */
143
144   return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
145               PROT_READ | PROT_WRITE | PROT_EXEC,
146               MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);
147 }
148