Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't ignore return value of 'system'.
[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-2012. The SimGrid Team. All rights reserved.          */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #ifndef MAP_ANONYMOUS
12 #define MAP_ANONYMOUS MAP_ANON
13 #endif
14
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>             /* Prototypes for lseek */
17 #endif
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <sys/mman.h>
21
22 #include "mmprivate.h"
23
24 /* Cache the pagesize for the current host machine.  Note that if the host
25    does not readily provide a getpagesize() function, we need to emulate it
26    elsewhere, not clutter up this file with lots of kluges to try to figure
27    it out. */
28
29 static size_t pagesize;
30
31 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \
32                                   ~(pagesize - 1))
33
34 /* Return MAP_PRIVATE if MDP represents /dev/zero.  Otherwise, return
35    MAP_SHARED.  */
36 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
37                                     ? MAP_PRIVATE                       \
38                                     : MAP_SHARED)
39
40 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
41 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
42                                ? MAP_ANONYMOUS                      \
43                                : 0)
44
45 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
46 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
47                              ? -1                                 \
48                              : (MDP) -> fd)
49
50 /*  Get core for the memory region specified by MDP, using SIZE as the
51     amount to either add to or subtract from the existing region.  Works
52     like sbrk(), but using mmap().
53
54     It never returns NULL. Instead, it dies verbosely on errors. */
55
56 void *mmorecore(struct mdesc *mdp, int size)
57 {
58   ssize_t test = 0;
59   void *result; // please keep it uninitialized to track issues
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 //  fprintf(stderr,"increase %p by %u\n",mdp,size);
67   if (pagesize == 0)
68     pagesize = getpagesize();
69
70   if (size == 0) {
71     /* Just return the current "break" value. */
72     result = mdp->breakval;
73
74   } else if (size < 0) {
75     /* We are deallocating memory.  If the amount requested would cause
76        us to try to deallocate back past the base of the mmap'd region
77        then die verbosely.  Otherwise, deallocate the memory and return
78        the old break value. */
79     if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
80       result = (void *) mdp->breakval;
81       mdp->breakval = (char *) mdp->breakval + size;
82       moveto = PAGE_ALIGN(mdp->breakval);
83       munmap(moveto,
84              (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
85       mdp->top = moveto;
86     } else {
87       fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
88       abort();
89     }
90   } else {
91     /* We are allocating memory. Make sure we have an open file
92        descriptor if not working with anonymous memory. */
93     if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
94       fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
95       abort();
96     } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
97       /* The request would move us past the end of the currently
98          mapped memory, so map in enough more memory to satisfy
99          the request.  This means we also have to grow the mapped-to
100          file by an appropriate amount, since mmap cannot be used
101          to extend a file. */
102       moveto = PAGE_ALIGN((char *) mdp->breakval + size);
103       mapbytes = (char *) moveto - (char *) mdp->top;
104       foffset = (char *) mdp->top - (char *) mdp->base;
105
106       if (mdp->fd > 0) {
107         /* FIXME:  Test results of lseek() */
108         lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
109         test = write(mdp->fd, &buf, 1);
110         if (test == -1) {
111           fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
112           abort();
113         }
114       }
115
116       /* Let's call mmap. Note that it is possible that mdp->top
117          is 0. In this case mmap will choose the address for us */
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 == (void *) -1/* That's 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       result = (void *) mdp->breakval;
141       mdp->breakval = (char *) mdp->breakval + size;
142     }
143   }
144   return (result);
145 }
146
147 void *__mmalloc_remap_core(xbt_mheap_t mdp)
148 {
149   /* FIXME:  Quick hack, needs error checking and other attention. */
150
151   return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
152               PROT_READ | PROT_WRITE | PROT_EXEC,
153               MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);
154 }
155