Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[project-description] Fix extraction of the ns-3 version.
[simgrid.git] / src / xbt / mmalloc / mmorecore.c
1 /* Support for an sbrk-like function that uses mmap. */
2
3 /* Copyright (c) 2010-2022. 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 #include "src/internal_config.h"
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <sys/mman.h>
17 #include <sys/wait.h>
18 #include <errno.h>
19
20 #include "mmprivate.h"
21
22 #ifndef MAP_ANONYMOUS
23 #define MAP_ANONYMOUS MAP_ANON
24 #endif
25
26 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + xbt_pagesize - 1) &   \
27                                   ~((long)xbt_pagesize - 1))
28
29 /** @brief Add memory to this heap
30  *
31  *  Get core for the memory region specified by MDP, using SIZE as the
32  *  amount to either add to or subtract from the existing region.  Works
33  *  like sbrk(), but using mmap().
34  *
35  *  It never returns NULL. Instead, it dies verbosely on errors.
36  *
37  *  @param mdp  The heap
38  *  @param size Bytes to allocate for this heap (or <0 to free memory from this heap)
39  */
40 void *mmorecore(struct mdesc *mdp, ssize_t size)
41 {
42   void* result;                 // please keep it uninitialized to track issues
43   size_t mapbytes;              /* Number of bytes to map */
44   void* moveto;                 /* Address where we wish to move "break value" to */
45   void* mapto;                  /* Address we actually mapped to */
46
47   if (size == 0) {
48     /* Just return the current "break" value. */
49     return mdp->breakval;
50   }
51
52   if (size < 0) {
53     /* We are deallocating memory.  If the amount requested would cause us to try to deallocate back past the base of
54      * the mmap'd region then die verbosely.  Otherwise, deallocate the memory and return the old break value. */
55     if (((char*)mdp->breakval) + size >= (char*)mdp->base) {
56       result        = mdp->breakval;
57       mdp->breakval = (char*)mdp->breakval + size;
58       moveto = PAGE_ALIGN(mdp->breakval);
59       munmap(moveto, (size_t)(((char*)mdp->top) - ((char*)moveto)) - 1);
60       mdp->top = moveto;
61     } else {
62       fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
63       abort();
64     }
65   } else if ((char*)mdp->breakval + size > (char*)mdp->top) {
66     /* The request would move us past the end of the currently mapped memory, so map in enough more memory to satisfy
67        the request.  This means we also have to grow the mapped-to file by an appropriate amount, since mmap cannot
68        be used to extend a file. */
69     moveto   = PAGE_ALIGN((char*)mdp->breakval + size);
70     mapbytes = (char*)moveto - (char*)mdp->top;
71
72     /* Let's call mmap. Note that it is possible that mdp->top is 0. In this case mmap will choose the address for us.
73        This call might very well overwrite an already existing memory mapping (leading to weird bugs).
74     */
75     mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
76
77     if (mapto == MAP_FAILED) {
78       char buff[1024];
79       fprintf(stderr, "Internal error: mmap returned MAP_FAILED! error: %s\n", strerror(errno));
80       snprintf(buff, 1024, "cat /proc/%d/maps", getpid());
81       int status = system(buff);
82       if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
83         fprintf(stderr, "Something went wrong when trying to %s\n", buff);
84       sleep(1);
85       abort();
86     }
87
88     if (mdp->top == 0)
89       mdp->base = mdp->breakval = mapto;
90
91     mdp->top      = PAGE_ALIGN((char*)mdp->breakval + size);
92     result        = mdp->breakval;
93     mdp->breakval = (char*)mdp->breakval + size;
94   } else {
95     /* Memory is already mapped, we only need to increase the breakval: */
96     result        = mdp->breakval;
97     mdp->breakval = (char*)mdp->breakval + size;
98   }
99   return result;
100 }