Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Avoid memsetting twice
authorGabriel Corona <gabriel.corona@loria.fr>
Fri, 16 May 2014 13:45:16 +0000 (15:45 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Fri, 16 May 2014 13:45:16 +0000 (15:45 +0200)
include/xbt/mmalloc.h
src/mc/mc_checkpoint.c
src/mc/mc_dwarf_expression.c
src/mc/mc_memory.c
src/xbt/mmalloc/mm_legacy.c
src/xbt/mmalloc/mm_module.c
src/xbt/mmalloc/mmalloc.c
src/xbt/mmalloc/mmprivate.h

index 353249e..f7c6f4a 100644 (file)
@@ -48,6 +48,10 @@ XBT_PUBLIC( void ) mfree(xbt_mheap_t md, void *ptr);
 
 XBT_PUBLIC( xbt_mheap_t ) xbt_mheap_new(int fd, void *baseaddr);
 
+#define XBT_MHEAP_OPTION_MEMSET 1
+
+XBT_PUBLIC( xbt_mheap_t ) xbt_mheap_new_options(int fd, void *baseaddr, int options);
+
 XBT_PUBLIC( void ) xbt_mheap_destroy_no_free(xbt_mheap_t md);
 
 XBT_PUBLIC( void ) *xbt_mheap_destroy(xbt_mheap_t md);
index eb4cfd3..170a10e 100644 (file)
@@ -474,7 +474,6 @@ static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
 
 }
 
-
 mc_snapshot_t MC_take_snapshot(int num_state){
 
   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
index e92da2a..e06ad82 100644 (file)
@@ -366,9 +366,6 @@ void mc_dwarf_location_list_clear(mc_location_list_t list) {
 }
 
 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops) {
-  if(expression->ops) {
-    free(expression->ops);
-  }
   expression->lowpc = NULL;
   expression->highpc = NULL;
   expression->size = len;
@@ -377,9 +374,6 @@ void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op*
 }
 
 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops) {
-  if(target->locations) {
-    mc_dwarf_location_list_clear(target);
-  }
   target->size = 1;
   target->locations = (mc_expression_t) xbt_malloc(sizeof(s_mc_expression_t));
   mc_dwarf_expression_init(target->locations, len, ops);
index f734ec7..f4f06e1 100644 (file)
@@ -30,7 +30,7 @@ void MC_memory_init()
   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 + xbt_pagesize);
+  raw_heap = xbt_mheap_new_options(-1, (char*)(std_heap) + STD_HEAP_SIZE + xbt_pagesize, 0);
   xbt_assert(raw_heap != NULL);
 #endif
 }
index 5fbdb04..94aa3d1 100644 (file)
@@ -61,7 +61,7 @@ static int allocated_junk = 0; /* keep track of many blocks of our little area w
 static char junkareas[MAX_JUNK_AREAS][JUNK_SIZE];
 
 /* This version use mmalloc if there is a current heap, or the legacy implem if not */
-void *malloc(size_t n) {
+static void *malloc_or_calloc(size_t n, int setzero) {
   xbt_mheap_t mdp = __mmalloc_current_heap;
   void *ret;
 #ifdef MM_LEGACY_VERBOSE
@@ -73,14 +73,17 @@ void *malloc(size_t n) {
     LOCK(mdp);
     ret = mmalloc(mdp, n);
     UNLOCK(mdp);
+    // This was already done by mmalloc:
+    if (mdp->options & XBT_MHEAP_OPTION_MEMSET) {
+      setzero = 0;
+    }
 #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) {
+  } else if (!real_malloc) {
       size_t needed_areas = n / JUNK_SIZE;
       if(needed_areas * JUNK_SIZE != n) needed_areas++;
       if (allocated_junk+needed_areas>=MAX_JUNK_AREAS) {
@@ -90,9 +93,10 @@ void *malloc(size_t n) {
       } else {
         size_t i = allocated_junk;
         allocated_junk += needed_areas;
-        return junkareas[i];
+        ret = junkareas[i];
       }
     }
+  else {
 #ifdef MM_LEGACY_VERBOSE
     if (!warned_raw) {
       fprintf(stderr,"Using system malloc after interception; you seem to be currently model-checking\n");
@@ -101,15 +105,20 @@ void *malloc(size_t n) {
 #endif
     ret = real_malloc(n);
   }
+  if (ret && setzero) {
+    memset(ret, 0, n);
+  }
   return ret;
 }
 
+void *malloc(size_t n)
+{
+  return malloc_or_calloc(n, 0);
+}
 
 void *calloc(size_t nmemb, size_t size)
 {
-  void *ret = malloc(nmemb*size);
-  memset(ret, 0, nmemb * size);
-  return ret;
+  return malloc_or_calloc(nmemb*size, 1);
 }
 
 void *realloc(void *p, size_t s)
index 8ee23eb..4839cfe 100644 (file)
    On failure returns NULL. */
 
 xbt_mheap_t xbt_mheap_new(int fd, void *baseaddr)
+{
+  return xbt_mheap_new_options(fd, baseaddr, 0);
+}
+
+xbt_mheap_t xbt_mheap_new_options(int fd, void *baseaddr, int options)
 {
   struct mdesc mtemp;
   xbt_mheap_t mdp;
@@ -169,6 +174,7 @@ xbt_mheap_t xbt_mheap_new(int fd, void *baseaddr)
   mdp->base = mdp->breakval = mdp->top = baseaddr;
   mdp->next_mdesc = NULL;
   mdp->refcount = 1;
+  mdp->options = options;
   
   /* If we have not been passed a valid open file descriptor for the file
      to map to, then we go for an anonymous map */
@@ -326,7 +332,7 @@ void *mmalloc_preinit(void)
   if (__mmalloc_default_mdp == NULL) {
     unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
-    __mmalloc_default_mdp = xbt_mheap_new(-1, addr);
+    __mmalloc_default_mdp = xbt_mheap_new_options(-1, addr, XBT_MHEAP_OPTION_MEMSET);
     /* Fixme? only the default mdp in protected against forks */
     // This is mandated to protect the mmalloced areas through forks. Think of tesh.
     // Nah, removing the mutex isn't a good idea either for tesh
index 7376ca6..72ca5a0 100644 (file)
@@ -137,8 +137,9 @@ static void *register_morecore(struct mdesc *mdp, size_t size)
 /* Allocate memory from the heap.  */
 void *mmalloc(xbt_mheap_t mdp, size_t size) {
   void *res= mmalloc_no_memset(mdp,size);
-//  fprintf(stderr,"malloc(%zu)~>%p\n",size,res);
-  memset(res,0,size);
+  if (mdp->options & XBT_MHEAP_OPTION_MEMSET) {
+    memset(res,0,size);
+  }
   return res;
 }
 /* Spliting mmalloc this way is mandated by a trick in mrealloc, that gives
index 37e4ea7..6f457bc 100644 (file)
@@ -202,6 +202,8 @@ struct mdesc {
   /* The version number of the mmalloc package that created this file. */
   unsigned char version;
 
+  unsigned int options;
+
   /* Some flag bits to keep track of various internal things. */
   unsigned int flags;