Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use system malloc for the raw heap if GNU ld is used
authorMartin Quinson <martin.quinson@loria.fr>
Wed, 19 Dec 2012 18:31:22 +0000 (19:31 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Wed, 19 Dec 2012 18:31:45 +0000 (19:31 +0100)
buildtools/Cmake/CompleteInFiles.cmake
buildtools/Cmake/src/internal_config.h.in
src/mc/mc_memory.c
src/xbt/mmalloc/mm.c
src/xbt/mmalloc/mm_legacy.c
src/xbt/mmalloc/mm_module.c

index 037d1ed..2cb506e 100644 (file)
@@ -118,7 +118,6 @@ CHECK_INCLUDE_FILE("sys/time.h" HAVE_SYS_TIME_H)
 CHECK_INCLUDE_FILE("sys/param.h" HAVE_SYS_PARAM_H)
 CHECK_INCLUDE_FILE("sys/sysctl.h" HAVE_SYS_SYSCTL_H)
 CHECK_INCLUDE_FILE("time.h" HAVE_TIME_H)
 CHECK_INCLUDE_FILE("sys/param.h" HAVE_SYS_PARAM_H)
 CHECK_INCLUDE_FILE("sys/sysctl.h" HAVE_SYS_SYSCTL_H)
 CHECK_INCLUDE_FILE("time.h" HAVE_TIME_H)
-CHECK_INCLUDE_FILE("dlfcn.h" HAVE_DLFCN_H)
 CHECK_INCLUDE_FILE("inttypes.h" HAVE_INTTYPES_H)
 CHECK_INCLUDE_FILE("memory.h" HAVE_MEMORY_H)
 CHECK_INCLUDE_FILE("stdlib.h" HAVE_STDLIB_H)
 CHECK_INCLUDE_FILE("inttypes.h" HAVE_INTTYPES_H)
 CHECK_INCLUDE_FILE("memory.h" HAVE_MEMORY_H)
 CHECK_INCLUDE_FILE("stdlib.h" HAVE_STDLIB_H)
@@ -215,6 +214,40 @@ endif()
 CHECK_TYPE_SIZE(int SIZEOF_INT)
 CHECK_TYPE_SIZE(void* SIZEOF_VOIDP)
 
 CHECK_TYPE_SIZE(int SIZEOF_INT)
 CHECK_TYPE_SIZE(void* SIZEOF_VOIDP)
 
+#--------------------------------------------------------------------------------------------------
+### Check for GNU dynamic linker
+CHECK_INCLUDE_FILE("dlfcn.h" HAVE_DLFCN_H)
+if (HAVE_DLFCN_H)
+    execute_process(COMMAND ${CMAKE_C_COMPILER} ${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/test_prog/prog_gnu_dynlinker.c -ldl -o test_gnu_ld
+      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+      OUTPUT_VARIABLE HAVE_GNU_LD_compil
+    )
+    if(HAVE_GNU_LD_compil)
+      set(HAVE_GNU_LD 0)
+      message(STATUS "Warning: test program toward GNU ld failed to compile:")
+      message(STATUS "${HAVE_GNU_LD_comp_output}")
+    else()
+
+      execute_process(COMMAND ./test_gnu_ld
+          WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 
+          RESULT_VARIABLE HAVE_GNU_LD_run 
+          OUTPUT_VARIABLE var_exec
+      )
+
+      if(NOT HAVE_GNU_LD_run)
+        set(HAVE_GNU_LD 1)
+        message(STATUS "We are using GNU dynamic linker")
+      else()
+        set(HAVE_GNU_LD 0)
+        message(STATUS "Warning: error while checking for GNU ld:")
+        message(STATUS "Test output: '${var_exec}'")
+       message(STATUS "Exit status: ${HAVE_GNU_LD_run}")
+      endif()
+      file(REMOVE test_gnu_ld)
+    endif()
+endif()
+
+
 #--------------------------------------------------------------------------------------------------
 ### Initialize of CONTEXT THREADS
 
 #--------------------------------------------------------------------------------------------------
 ### Initialize of CONTEXT THREADS
 
@@ -541,7 +574,7 @@ endif()
 
 #AC_PROG_MAKE_SET
 
 
 #AC_PROG_MAKE_SET
 
-#AC_PRINTF_NULL
+#AC_PRINTF_NULL FIXME: this is too ancient to survive!
 try_run(RUN_PRINTF_NULL_VAR COMPILE_PRINTF_NULL_VAR
   ${CMAKE_BINARY_DIR}
   ${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/test_prog/prog_printf_null.c
 try_run(RUN_PRINTF_NULL_VAR COMPILE_PRINTF_NULL_VAR
   ${CMAKE_BINARY_DIR}
   ${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/test_prog/prog_printf_null.c
index efbcacb..bf62388 100644 (file)
 #cmakedefine CMAKE_SYSTEM_PROCESSOR @CMAKE_SYSTEM_PROCESSOR@
 #cmakedefine HAVE_RAWCTX @HAVE_RAWCTX@
 
 #cmakedefine CMAKE_SYSTEM_PROCESSOR @CMAKE_SYSTEM_PROCESSOR@
 #cmakedefine HAVE_RAWCTX @HAVE_RAWCTX@
 
-/* Define to 1 if you have the <dlfcn.h> header file. */
-#cmakedefine HAVE_DLFCN_H @HAVE_DLFCN_H@
+/* Define to 1 if you have the GNU ld library */
+#cmakedefine HAVE_GNU_LD @HAVE_GNU_LD@
 
 /* Define to 1 if you have the <errno.h> header file. */
 #cmakedefine HAVE_ERRNO_H @HAVE_ERRNO_H@
 
 /* Define to 1 if you have the <errno.h> header file. */
 #cmakedefine HAVE_ERRNO_H @HAVE_ERRNO_H@
index f275b3a..64c181c 100644 (file)
@@ -24,9 +24,14 @@ void MC_memory_init()
   std_heap = mmalloc_get_default_md();
   xbt_assert(std_heap != NULL);
 
   std_heap = mmalloc_get_default_md();
   xbt_assert(std_heap != NULL);
 
+#ifdef HAVE_GNU_LD
+  /* use the system malloc for the model-checker data */
+  raw_heap = NULL;
+#else
   /* Create the second region a page after the first one ends + safety gap */
   raw_heap = xbt_mheap_new(-1, (char*)(std_heap) + STD_HEAP_SIZE + getpagesize());
   xbt_assert(raw_heap != NULL);
   /* Create the second region a page after the first one ends + safety gap */
   raw_heap = xbt_mheap_new(-1, (char*)(std_heap) + STD_HEAP_SIZE + getpagesize());
   xbt_assert(raw_heap != NULL);
+#endif
 }
 
 /* Finalize the memory subsystem */
 }
 
 /* Finalize the memory subsystem */
index e435f7f..ade101a 100644 (file)
@@ -19,6 +19,6 @@
 #include "mmalloc.c"
 #include "mrealloc.c"
 #include "mmorecore.c"
 #include "mmalloc.c"
 #include "mrealloc.c"
 #include "mmorecore.c"
-#include "mm_module.c"
 #include "mm_legacy.c"
 #include "mm_legacy.c"
+#include "mm_module.c"
 #include "mm_diff.c"
 #include "mm_diff.c"
index 9ae3771..e92b122 100644 (file)
@@ -10,6 +10,7 @@
 #include "internal_config.h"
 #include <math.h>
 
 #include "internal_config.h"
 #include <math.h>
 
+//#define MM_LEGACY_VERBOSE 1 /* define this to see which version of malloc gets used */
 
 /* The mmalloc() package can use a single implicit malloc descriptor
    for mmalloc/mrealloc/mfree operations which do not supply an explicit
 
 /* The mmalloc() package can use a single implicit malloc descriptor
    for mmalloc/mrealloc/mfree operations which do not supply an explicit
@@ -30,7 +31,124 @@ void mmalloc_set_current_heap(xbt_mheap_t new_heap)
   __mmalloc_current_heap = new_heap;
 }
 
   __mmalloc_current_heap = new_heap;
 }
 
+
 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
+#ifdef HAVE_GNU_LD
+
+#undef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#include <dlfcn.h>
+
+static void * (*real_malloc) (size_t);
+static void * (*real_realloc) (void*,size_t);
+static void * (*real_free) (void*);
+
+static void mm_gnuld_legacy_init(void) { /* This function is called from mmalloc_preinit(); it works even if it's static because all mm is in mm.c */
+  real_malloc = (void * (*) (size_t)) dlsym(RTLD_NEXT, "malloc");
+  real_realloc = (void * (*) (void*,size_t)) dlsym(RTLD_NEXT, "realloc");
+  real_free = (void * (*) (void*)) dlsym(RTLD_NEXT, "free");
+  __mmalloc_current_heap = __mmalloc_default_mdp;
+}
+
+/* Hello pimple!
+ * DL needs some memory while resolving the malloc symbol, that is somehow problematic
+ * To that extend, we have a little area here living in .BSS that we return if asked for memory before the malloc is resolved.
+ */
+int allocated_junk=0; /* keep track of whether our little area was already given to someone */
+char junkarea[512];
+
+/* This version use mmalloc if there is a current heap, or the legacy implem if not */
+void *malloc(size_t n) {
+  xbt_mheap_t mdp = __mmalloc_current_heap;
+  void *ret;
+#ifdef MM_LEGACY_VERBOSE
+  static int warned_raw = 0;
+  static int warned_mmalloc = 0;
+#endif
+
+  if (mdp) {
+    LOCK(mdp);
+    ret = mmalloc(mdp, n);
+    UNLOCK(mdp);
+#ifdef MM_LEGACY_VERBOSE
+    if (!warned_mmalloc) {
+      fprintf(stderr,"Using mmalloc; enabling the model-checker in cmake may have a bad impact on your simulation performance\n");
+      warned_mmalloc = 1;
+    }
+#endif
+  } else {
+    if (!real_malloc) {
+      if (allocated_junk) {
+        fprintf(stderr,
+            "Panic: real malloc symbol not resolved yet, and I already gave my little private memory chunk away. "
+            "Damn LD, we must extend our code to have several such areas.\n");
+        exit(1);
+      } else if (n>512) {
+        fprintf(stderr,
+            "Panic: real malloc symbol not resolved yet, and I need %zu bytes while my little private memory chunk is only 512 bytes wide. "
+            "Damn LD, we must fix our code to extend this area.\n",n);
+        exit(1);
+      } else {
+        allocated_junk = 1;
+        return junkarea;
+      }
+    }
+#ifdef MM_LEGACY_VERBOSE
+    if (!warned_raw) {
+      fprintf(stderr,"Using system malloc after interception; you seem to be currently model-checking\n");
+      warned_raw = 1;
+    }
+#endif
+    ret = real_malloc(n);
+  }
+  return ret;
+}
+
+
+void *calloc(size_t nmemb, size_t size)
+{
+  void *ret = malloc(nmemb*size);
+  memset(ret, 0, nmemb * size);
+  return ret;
+}
+
+void *realloc(void *p, size_t s)
+{
+  xbt_mheap_t mdp = __mmalloc_current_heap;
+  void *ret;
+
+  if (mdp) {
+    LOCK(mdp);
+    ret = mrealloc(mdp, p, s);
+    UNLOCK(mdp);
+  } else {
+    ret = real_realloc(p,s);
+  }
+
+  return ret;
+}
+
+void free(void *p)
+{
+  if (p==NULL)
+    return;
+  if (p!=junkarea) {
+    xbt_mheap_t mdp = __mmalloc_current_heap;
+
+    if (mdp) {
+      LOCK(mdp);
+      mfree(mdp, p);
+      UNLOCK(mdp);
+    } else {
+      real_free(p);
+    }
+  } else {
+    allocated_junk=0;
+  }
+}
+
+
+#else /* NO GNU_LD */
 void *malloc(size_t n)
 {
   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
 void *malloc(size_t n)
 {
   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
@@ -77,6 +195,7 @@ void free(void *p)
     UNLOCK(mdp);
   }
 }
     UNLOCK(mdp);
   }
 }
-#endif
+#endif /* NO GNU_LD */
+#endif /* WANT_MALLOC_OVERRIDE */
 
 
 
 
index de1942a..07cbe7e 100644 (file)
@@ -330,6 +330,10 @@ void *mmalloc_preinit(void)
   }
   xbt_assert(__mmalloc_default_mdp != NULL);
 
   }
   xbt_assert(__mmalloc_default_mdp != NULL);
 
+#if defined(HAVE_GNU_LD) && defined(MMALLOC_WANT_OVERRIDE_LEGACY)
+  mm_gnuld_legacy_init();
+#endif
+
   return __mmalloc_default_mdp;
 }
 
   return __mmalloc_default_mdp;
 }