]> AND Public Git Repository - simgrid.git/blobdiff - src/xbt/mmalloc/mm_module.c
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Another attempt at PRELOADing mmalloc (WIP)
[simgrid.git] / src / xbt / mmalloc / mm_module.c
index 8358d43f2410bc3f5f30a47d8a443b10391ed175..37f301b1d7c9cfe3ea22e244072ee0e54bde8a65 100644 (file)
    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.
 
@@ -49,7 +46,7 @@
    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)
 {
@@ -189,48 +186,32 @@ static void mmalloc_fork_child(void)
   }
 }
 
-/* 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);
-    __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);
-
-  return __mmalloc_default_mdp;
-}
+    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");
 
-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)
-}
+      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);
 
-// 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];
-      }
+      // 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);
     }
   }
-  return bytes;
+  mmalloc_assert(__mmalloc_default_mdp != NULL, "__mmalloc_default_mdp cannot be NULL");
+
+  return __mmalloc_default_mdp;
 }