/* Initialization for access to a mmap'd malloc managed region. */
-/* Copyright (c) 2012-2021. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2012-2023. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
-#include "src/internal_config.h"
#include <sys/types.h>
#include <fcntl.h> /* After sys/types.h, at least for dpx/2. */
#include <sys/stat.h>
#include <string.h>
#include "mmprivate.h"
-#include "xbt/ex.h"
-#include "xbt/xbt_modinter.h" /* declarations of mmalloc_preinit and friends that live here */
/* Initialize access to a mmalloc managed region.
so that users of the package don't have to worry about the actual
implementation details.
- On failure returns NULL. */
+ On failure, returns NULL. */
xbt_mheap_t xbt_mheap_new(void* baseaddr, int options)
{
mdp->next_mdesc = NULL;
mdp->options = options;
- pthread_mutex_init(&mdp->mutex, NULL);
/* If we have not been passed a valid open file descriptor for the file
to map to, then open /dev/zero and use that to map to. */
while(mdp->next_mdesc)
mdp = mdp->next_mdesc;
- LOCK(mdp);
mdp->next_mdesc = (struct mdesc *)mbase;
- UNLOCK(mdp);
}
return mbase;
}
-
-
-/** Terminate access to a mmalloc managed region, but do not free its content.
- *
- * This is for example useful for the base region where ldl stores its data
- * because it leaves the place after us.
- */
-void xbt_mheap_destroy_no_free(xbt_mheap_t md)
-{
- struct mdesc *mdp = md;
-
- pthread_mutex_destroy(&mdp->mutex);
-}
-
/** Terminate access to a mmalloc managed region by unmapping all memory pages associated with the region, and closing
* the file descriptor if it is one that we opened.
mdptemp->next_mdesc = mdp->next_mdesc;
- xbt_mheap_destroy_no_free(mdp);
struct mdesc mtemp = *mdp;
/* Now unmap all the pages associated with this region by asking for a
* Try to increase this first if you experience strange errors under valgrind. */
#define HEAP_OFFSET (128UL<<20)
-static void mmalloc_fork_prepare(void)
-{
- xbt_mheap_t mdp = NULL;
- if ((mdp =__mmalloc_default_mdp)){
- while(mdp){
- LOCK(mdp);
- mdp = mdp->next_mdesc;
- }
- }
-}
-
-static void mmalloc_fork_parent(void)
-{
- xbt_mheap_t mdp = NULL;
- if ((mdp =__mmalloc_default_mdp)){
- while(mdp){
- UNLOCK(mdp);
- mdp = mdp->next_mdesc;
- }
- }
-}
-
-static void mmalloc_fork_child(void)
-{
- struct mdesc* mdp = NULL;
- if ((mdp =__mmalloc_default_mdp)){
- while(mdp){
- UNLOCK(mdp);
- mdp = mdp->next_mdesc;
- }
- }
-}
-
-/* Initialize the default malloc descriptor. */
+/* Initialize the default malloc descriptor.
+ *
+ * There is no malloc_postexit() destroying the default mdp, because it would break ldl trying to free its memory
+ */
xbt_mheap_t mmalloc_preinit(void)
{
if (__mmalloc_default_mdp == NULL) {
- if(!xbt_pagesize)
- xbt_pagesize = getpagesize();
- unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
- void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
+ unsigned long mmalloc_pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
+ unsigned long mask = ~(mmalloc_pagesize - 1);
+ void* addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
__mmalloc_default_mdp = xbt_mheap_new(addr, XBT_MHEAP_OPTION_MEMSET);
-
- // atfork mandated at least on FreeBSD, or simgrid-mc will fail to fork the verified app
- int res = pthread_atfork(mmalloc_fork_prepare, mmalloc_fork_parent, mmalloc_fork_child);
- xbt_assert(res == 0, "pthread_atfork() failed: return value %d", res);
}
- xbt_assert(__mmalloc_default_mdp != NULL);
+ mmalloc_assert(__mmalloc_default_mdp != NULL, "__mmalloc_default_mdp cannot be NULL");
return __mmalloc_default_mdp;
}
-
-void mmalloc_postexit(void)
-{
- /* Do not destroy the default mdp or ldl won't be able to free the memory it
- * allocated since we're in memory */
- // xbt_mheap_destroy_no_free(__mmalloc_default_mdp)
-}
-
-// This is the underlying implementation of mmalloc_get_bytes_used_remote.
-// Is it used directly in order to evaluate the bytes used from a different
-// process.
-size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapinfo)
-{
- int bytes = 0;
- for (size_t i=0; i < heaplimit; ++i){
- if (heapinfo[i].type == MMALLOC_TYPE_UNFRAGMENTED){
- if (heapinfo[i].busy_block.busy_size > 0)
- bytes += heapinfo[i].busy_block.busy_size;
- } else if (heapinfo[i].type > 0) {
- for (size_t j=0; j < (size_t) (BLOCKSIZE >> heapinfo[i].type); j++){
- if(heapinfo[i].busy_frag.frag_size[j] > 0)
- bytes += heapinfo[i].busy_frag.frag_size[j];
- }
- }
- }
- return bytes;
-}