1 /* Initialization for access to a mmap'd malloc managed region. */
3 /* Copyright (c) 2012-2022. The SimGrid Team.
4 * All rights reserved. */
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. */
9 /* Copyright 1992, 2000 Free Software Foundation, Inc.
11 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
13 This file is part of the GNU C Library.
15 The GNU C Library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Library General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
20 The GNU C Library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Library General Public License for more details.
25 You should have received a copy of the GNU Library General Public
26 License along with the GNU C Library; see the file COPYING.LIB. If
27 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
30 #include "src/internal_config.h"
31 #include <sys/types.h>
32 #include <fcntl.h> /* After sys/types.h, at least for dpx/2. */
35 #include "mmprivate.h"
37 /* Initialize access to a mmalloc managed region.
39 The mapping is established starting at the specified address BASEADDR
40 in the process address space.
42 The provided BASEADDR should be chosen carefully in order to avoid
43 bumping into existing mapped regions or future mapped regions.
45 On success, returns a "malloc descriptor" which is used in subsequent
46 calls to other mmalloc package functions. It is explicitly "void *"
47 so that users of the package don't have to worry about the actual
48 implementation details.
50 On failure, returns NULL. */
52 xbt_mheap_t xbt_mheap_new(void* baseaddr, int options)
54 /* NULL is not a valid baseaddr as we cannot map anything there. C'mon, user. Think! */
58 /* We start off with the malloc descriptor allocated on the stack, until we build it up enough to
59 * call _mmalloc_mmap_morecore() to allocate the first page of the region and copy it there. Ensure that it is
60 * zero'd and then initialize the fields that we know values for. */
63 xbt_mheap_t mdp = &mtemp;
64 memset((char *) mdp, 0, sizeof(mtemp));
65 strncpy(mdp->magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
66 mdp->headersize = sizeof(mtemp);
67 mdp->version = MMALLOC_VERSION;
68 mdp->base = mdp->breakval = mdp->top = baseaddr;
69 mdp->next_mdesc = NULL;
70 mdp->options = options;
72 pthread_mutex_init(&mdp->mutex, NULL);
73 /* If we have not been passed a valid open file descriptor for the file
74 to map to, then open /dev/zero and use that to map to. */
76 /* Now try to map in the first page, copy the malloc descriptor structure there, and arrange to return a pointer to
77 * this new copy. If the mapping fails, then close the file descriptor if it was opened by us, and arrange to return
80 void* mbase = mmorecore(mdp, sizeof(mtemp));
82 fprintf(stderr, "morecore failed to get some more memory!\n");
85 memcpy(mbase, mdp, sizeof(mtemp));
87 /* Add the new heap to the linked list of heaps attached by mmalloc */
88 if(__mmalloc_default_mdp){
89 mdp = __mmalloc_default_mdp;
90 while(mdp->next_mdesc)
91 mdp = mdp->next_mdesc;
94 mdp->next_mdesc = (struct mdesc *)mbase;
103 /** Terminate access to a mmalloc managed region, but do not free its content.
105 * This is for example useful for the base region where ldl stores its data
106 * because it leaves the place after us.
108 void xbt_mheap_destroy_no_free(xbt_mheap_t md)
110 struct mdesc *mdp = md;
112 pthread_mutex_destroy(&mdp->mutex);
115 /** Terminate access to a mmalloc managed region by unmapping all memory pages associated with the region, and closing
116 * the file descriptor if it is one that we opened.
118 Returns NULL on success.
120 Returns the malloc descriptor on failure, which can subsequently be used for further action, such as obtaining more
121 information about the nature of the failure.
123 Note that the malloc descriptor that we are using is currently located in region we are about to unmap, so we first
124 make a local copy of it on the stack and use the copy. */
126 void *xbt_mheap_destroy(xbt_mheap_t mdp)
129 /* Remove the heap from the linked list of heaps attached by mmalloc */
130 struct mdesc* mdptemp = __mmalloc_default_mdp;
131 while(mdptemp->next_mdesc != mdp )
132 mdptemp = mdptemp->next_mdesc;
134 mdptemp->next_mdesc = mdp->next_mdesc;
136 xbt_mheap_destroy_no_free(mdp);
137 struct mdesc mtemp = *mdp;
139 /* Now unmap all the pages associated with this region by asking for a
140 negative increment equal to the current size of the region. */
142 if (mmorecore(&mtemp, (char *)mtemp.base - (char *)mtemp.breakval) == NULL) {
143 /* Deallocating failed. Update the original malloc descriptor with any changes */
153 /* Safety gap from the heap's break address.
154 * Try to increase this first if you experience strange errors under valgrind. */
155 #define HEAP_OFFSET (128UL<<20)
157 static void mmalloc_fork_prepare(void)
159 xbt_mheap_t mdp = NULL;
160 if ((mdp =__mmalloc_default_mdp)){
163 mdp = mdp->next_mdesc;
168 static void mmalloc_fork_parent(void)
170 xbt_mheap_t mdp = NULL;
171 if ((mdp =__mmalloc_default_mdp)){
174 mdp = mdp->next_mdesc;
179 static void mmalloc_fork_child(void)
181 struct mdesc* mdp = NULL;
182 if ((mdp =__mmalloc_default_mdp)){
185 mdp = mdp->next_mdesc;
190 /* Initialize the default malloc descriptor.
192 * There is no malloc_postexit() destroying the default mdp, because it would break ldl trying to free its memory
194 xbt_mheap_t mmalloc_preinit(void)
196 if (__mmalloc_default_mdp == NULL) {
197 if (!mmalloc_pagesize)
198 mmalloc_pagesize = getpagesize();
199 unsigned long mask = ~((unsigned long)mmalloc_pagesize - 1);
200 void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
201 __mmalloc_default_mdp = xbt_mheap_new(addr, XBT_MHEAP_OPTION_MEMSET);
203 // atfork mandated at least on FreeBSD, or simgrid-mc will fail to fork the verified app
204 int res = pthread_atfork(mmalloc_fork_prepare, mmalloc_fork_parent, mmalloc_fork_child);
205 mmalloc_assert(res == 0, "pthread_atfork() failed: return value %d", res);
207 mmalloc_assert(__mmalloc_default_mdp != NULL, "__mmalloc_default_mdp cannot be NULL");
209 return __mmalloc_default_mdp;
212 // This is the underlying implementation of mmalloc_get_bytes_used_remote.
213 // Is it used directly in order to evaluate the bytes used from a different
215 size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapinfo)
218 for (size_t i=0; i < heaplimit; ++i){
219 if (heapinfo[i].type == MMALLOC_TYPE_UNFRAGMENTED){
220 if (heapinfo[i].busy_block.busy_size > 0)
221 bytes += heapinfo[i].busy_block.busy_size;
222 } else if (heapinfo[i].type > 0) {
223 for (size_t j=0; j < (size_t) (BLOCKSIZE >> heapinfo[i].type); j++){
224 if(heapinfo[i].busy_frag.frag_size[j] > 0)
225 bytes += heapinfo[i].busy_frag.frag_size[j];