Logo AND Algorithmique Numérique Distribuée

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