Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Disable soft-dirty page tracking by default
[simgrid.git] / src / xbt / mmalloc / mmorecore.c
1 /* Support for an sbrk-like function that uses mmap. */
2
3 /* Copyright (c) 2010-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 /* Copyright 1992, 2000 Free Software Foundation, Inc.
10
11    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com */
12
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>             /* Prototypes for lseek */
15 #endif
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <sys/mman.h>
19 #include <sys/wait.h>
20
21 #include "mmprivate.h"
22
23 #ifndef MAP_ANONYMOUS
24 #define MAP_ANONYMOUS MAP_ANON
25 #endif
26
27 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + xbt_pagesize - 1) &   \
28                                   ~((long)xbt_pagesize - 1))
29
30 /* Return MAP_PRIVATE if MDP represents /dev/zero.  Otherwise, return
31    MAP_SHARED.  */
32 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
33                                     ? MAP_PRIVATE                       \
34                                     : MAP_SHARED)
35
36 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
37 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
38                                ? MAP_ANONYMOUS                      \
39                                : 0)
40
41 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
42 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
43                              ? -1                                 \
44                              : (MDP) -> fd)
45
46 /** @brief Add memoty to this heap
47  *
48  *  Get core for the memory region specified by MDP, using SIZE as the
49  *  amount to either add to or subtract from the existing region.  Works
50  *  like sbrk(), but using mmap().
51  *
52  *  It never returns NULL. Instead, it dies verbosely on errors.
53  *
54  *  @param mdp  The heap
55  *  @param size Bytes to allocate for this heap (or <0 to free memory from this heap)
56  */
57 void *mmorecore(struct mdesc *mdp, ssize_t size)
58 {
59   ssize_t test = 0;
60   void *result; // please keep it uninitialized to track issues
61   off_t foffset;                /* File offset at which new mapping will start */
62   size_t mapbytes;              /* Number of bytes to map */
63   void *moveto;                 /* Address where we wish to move "break value" to */
64   void *mapto;                  /* Address we actually mapped to */
65   char buf = 0;                 /* Single byte to write to extend mapped file */
66
67   if (size == 0) {
68     /* Just return the current "break" value. */
69     result = mdp->breakval;
70
71   } else if (size < 0) {
72     /* We are deallocating memory.  If the amount requested would cause
73        us to try to deallocate back past the base of the mmap'd region
74        then die verbosely.  Otherwise, deallocate the memory and return
75        the old break value. */
76     if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
77       result = (void *) mdp->breakval;
78       mdp->breakval = (char *) mdp->breakval + size;
79       moveto = PAGE_ALIGN(mdp->breakval);
80       munmap(moveto,
81              (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
82       mdp->top = moveto;
83     } else {
84       fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
85       abort();
86     }
87   } else {
88     /* We are allocating memory. Make sure we have an open file
89        descriptor if not working with anonymous memory. */
90     if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
91       fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
92       abort();
93     } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
94       /* The request would move us past the end of the currently
95          mapped memory, so map in enough more memory to satisfy
96          the request.  This means we also have to grow the mapped-to
97          file by an appropriate amount, since mmap cannot be used
98          to extend a file. */
99       moveto = PAGE_ALIGN((char *) mdp->breakval + size);
100       mapbytes = (char *) moveto - (char *) mdp->top;
101       foffset = (char *) mdp->top - (char *) mdp->base;
102
103       if (mdp->fd > 0) {
104         /* FIXME:  Test results of lseek() */
105         lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
106         test = write(mdp->fd, &buf, 1);
107         if (test == -1) {
108           fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
109           abort();
110         }
111       }
112
113       /* Let's call mmap. Note that it is possible that mdp->top
114          is 0. In this case mmap will choose the address for us.
115          This call might very well overwrite an already existing memory mapping
116          (leading to weird bugs).
117        */
118       mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
119                    MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
120                    MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
121
122       if (mapto == MAP_FAILED) {
123         char buff[1024];
124         fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s\n",strerror(errno));
125         sprintf(buff,"cat /proc/%d/maps",getpid());
126         int status = system(buff);
127         if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
128           fprintf(stderr, "Something went wrong when trying to %s\n", buff);
129         sleep(1);
130         abort();
131       }
132
133       if (mdp->top == 0)
134         mdp->base = mdp->breakval = mapto;
135
136       mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
137       result = (void *) mdp->breakval;
138       mdp->breakval = (char *) mdp->breakval + size;
139     } else {
140       /* Memory is already mapped, we only need to increase the breakval: */
141       result = (void *) mdp->breakval;
142       mdp->breakval = (char *) mdp->breakval + size;
143     }
144   }
145   return (result);
146 }
147
148 void *__mmalloc_remap_core(xbt_mheap_t mdp)
149 {
150   /* FIXME:  Quick hack, needs error checking and other attention. */
151
152   return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
153               PROT_READ | PROT_WRITE | PROT_EXEC,
154               MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);
155 }
156