Logo AND Algorithmique Numérique Distribuée

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