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
index 37f301b..f4bd97a 100644 (file)
@@ -1,7 +1,6 @@
 /* Initialization for access to a mmap'd malloc managed region. */
 
-/* Copyright (c) 2012-2022. 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. */
@@ -68,7 +67,6 @@ 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. */
 
@@ -89,28 +87,12 @@ xbt_mheap_t xbt_mheap_new(void* baseaddr, int options)
     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.
 
@@ -132,7 +114,6 @@ void *xbt_mheap_destroy(xbt_mheap_t mdp)
 
     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
@@ -153,39 +134,6 @@ void *xbt_mheap_destroy(xbt_mheap_t mdp)
  * 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.
  *
  * There is no malloc_postexit() destroying the default mdp, because it would break ldl trying to free its memory
@@ -193,23 +141,10 @@ static void mmalloc_fork_child(void)
 xbt_mheap_t mmalloc_preinit(void)
 {
   if (__mmalloc_default_mdp == NULL) {
-    xbt_mheap_t (*other)(void) = dlsym(RTLD_NEXT, "mmalloc_preinit");
-    if (other != NULL) { // This is the second time that this module is loaded, let's use the other one
-      __mmalloc_default_mdp = other();
-      //      fprintf(stderr, "Reuse the other mmalloc_init\n");
-    } else {
-      //      fprintf(stderr, "New mmalloc_init\n");
-
-      if (!mmalloc_pagesize)
-        mmalloc_pagesize = getpagesize();
-      unsigned long mask    = ~((unsigned long)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);
-      mmalloc_assert(res == 0, "pthread_atfork() failed: return value %d", res);
-    }
+    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);
   }
   mmalloc_assert(__mmalloc_default_mdp != NULL, "__mmalloc_default_mdp cannot be NULL");