Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / xbt / mmalloc / mm_module.c
1 /* Initialization for access to a mmap'd malloc managed region. */
2
3 /* Copyright (c) 2012-2023. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 /* Copyright 1992, 2000 Free Software Foundation, Inc.
9
10    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
11
12    This file is part of the GNU C Library.
13
14    The GNU C Library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Library General Public License as
16    published by the Free Software Foundation; either version 2 of the
17    License, or (at your option) any later version.
18
19    The GNU C Library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Library General Public License for more details.
23
24    You should have received a copy of the GNU Library General Public
25    License along with the GNU C Library; see the file COPYING.LIB.  If
26    not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27    Boston, MA 02111-1307, USA.  */
28
29 #include <sys/types.h>
30 #include <fcntl.h>              /* After sys/types.h, at least for dpx/2.  */
31 #include <sys/stat.h>
32 #include <string.h>
33 #include "mmprivate.h"
34
35 /* Initialize access to a mmalloc managed region.
36
37    The mapping is established starting at the specified address BASEADDR
38    in the process address space.
39
40    The provided BASEADDR should be chosen carefully in order to avoid
41    bumping into existing mapped regions or future mapped regions.
42
43    On success, returns a "malloc descriptor" which is used in subsequent
44    calls to other mmalloc package functions.  It is explicitly "void *"
45    so that users of the package don't have to worry about the actual
46    implementation details.
47
48    On failure, returns NULL. */
49
50 xbt_mheap_t xbt_mheap_new(void* baseaddr, int options)
51 {
52   /* NULL is not a valid baseaddr as we cannot map anything there. C'mon, user. Think! */
53   if (baseaddr == NULL)
54     return NULL;
55
56   /* We start off with the malloc descriptor allocated on the stack, until we build it up enough to
57    * call _mmalloc_mmap_morecore() to allocate the first page of the region and copy it there.  Ensure that it is
58    * zero'd and then initialize the fields that we know values for. */
59
60   struct mdesc mtemp;
61   xbt_mheap_t mdp = &mtemp;
62   memset((char *) mdp, 0, sizeof(mtemp));
63   strncpy(mdp->magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
64   mdp->headersize = sizeof(mtemp);
65   mdp->version = MMALLOC_VERSION;
66   mdp->base = mdp->breakval = mdp->top = baseaddr;
67   mdp->next_mdesc = NULL;
68   mdp->options = options;
69
70   /* If we have not been passed a valid open file descriptor for the file
71      to map to, then open /dev/zero and use that to map to. */
72
73   /* Now try to map in the first page, copy the malloc descriptor structure there, and arrange to return a pointer to
74    * this new copy.  If the mapping fails, then close the file descriptor if it was opened by us, and arrange to return
75    * a NULL. */
76
77   void* mbase = mmorecore(mdp, sizeof(mtemp));
78   if (mbase == NULL) {
79     fprintf(stderr, "morecore failed to get some more memory!\n");
80     abort();
81   }
82   memcpy(mbase, mdp, sizeof(mtemp));
83
84   /* Add the new heap to the linked list of heaps attached by mmalloc */
85   if(__mmalloc_default_mdp){
86     mdp = __mmalloc_default_mdp;
87     while(mdp->next_mdesc)
88       mdp = mdp->next_mdesc;
89
90     mdp->next_mdesc = (struct mdesc *)mbase;
91   }
92
93   return mbase;
94 }
95
96 /** Terminate access to a mmalloc managed region by unmapping all memory pages associated with the region, and closing
97  *  the file descriptor if it is one that we opened.
98
99     Returns NULL on success.
100
101     Returns the malloc descriptor on failure, which can subsequently be used for further action, such as obtaining more
102     information about the nature of the failure.
103
104     Note that the malloc descriptor that we are using is currently located in region we are about to unmap, so we first
105     make a local copy of it on the stack and use the copy. */
106
107 void *xbt_mheap_destroy(xbt_mheap_t mdp)
108 {
109   if (mdp != NULL) {
110     /* Remove the heap from the linked list of heaps attached by mmalloc */
111     struct mdesc* mdptemp = __mmalloc_default_mdp;
112     while(mdptemp->next_mdesc != mdp )
113       mdptemp = mdptemp->next_mdesc;
114
115     mdptemp->next_mdesc = mdp->next_mdesc;
116
117     struct mdesc mtemp = *mdp;
118
119     /* Now unmap all the pages associated with this region by asking for a
120        negative increment equal to the current size of the region. */
121
122     if (mmorecore(&mtemp, (char *)mtemp.base - (char *)mtemp.breakval) == NULL) {
123       /* Deallocating failed.  Update the original malloc descriptor with any changes */
124       *mdp = mtemp;
125     } else {
126       mdp = NULL;
127     }
128   }
129
130   return mdp;
131 }
132
133 /* Safety gap from the heap's break address.
134  * Try to increase this first if you experience strange errors under valgrind. */
135 #define HEAP_OFFSET   (128UL<<20)
136
137 /* Initialize the default malloc descriptor.
138  *
139  * There is no malloc_postexit() destroying the default mdp, because it would break ldl trying to free its memory
140  */
141 xbt_mheap_t mmalloc_preinit(void)
142 {
143   if (__mmalloc_default_mdp == NULL) {
144     unsigned long mmalloc_pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
145     unsigned long mask             = ~(mmalloc_pagesize - 1);
146     void* addr            = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
147     __mmalloc_default_mdp = xbt_mheap_new(addr, XBT_MHEAP_OPTION_MEMSET);
148   }
149   mmalloc_assert(__mmalloc_default_mdp != NULL, "__mmalloc_default_mdp cannot be NULL");
150
151   return __mmalloc_default_mdp;
152 }