Logo AND Algorithmique Numérique Distribuée

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