From e5302335e3d2d05db342b67533725f631f086ea3 Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Fri, 16 May 2014 15:45:16 +0200 Subject: [PATCH] [mc] Avoid memsetting twice --- include/xbt/mmalloc.h | 4 ++++ src/mc/mc_checkpoint.c | 1 - src/mc/mc_dwarf_expression.c | 6 ------ src/mc/mc_memory.c | 2 +- src/xbt/mmalloc/mm_legacy.c | 23 ++++++++++++++++------- src/xbt/mmalloc/mm_module.c | 8 +++++++- src/xbt/mmalloc/mmalloc.c | 5 +++-- src/xbt/mmalloc/mmprivate.h | 2 ++ 8 files changed, 33 insertions(+), 18 deletions(-) diff --git a/include/xbt/mmalloc.h b/include/xbt/mmalloc.h index 353249ea8d..f7c6f4ac41 100644 --- a/include/xbt/mmalloc.h +++ b/include/xbt/mmalloc.h @@ -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); diff --git a/src/mc/mc_checkpoint.c b/src/mc/mc_checkpoint.c index eb4cfd348e..170a10e9cf 100644 --- a/src/mc/mc_checkpoint.c +++ b/src/mc/mc_checkpoint.c @@ -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); diff --git a/src/mc/mc_dwarf_expression.c b/src/mc/mc_dwarf_expression.c index e92da2acdc..e06ad82e61 100644 --- a/src/mc/mc_dwarf_expression.c +++ b/src/mc/mc_dwarf_expression.c @@ -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); diff --git a/src/mc/mc_memory.c b/src/mc/mc_memory.c index f734ec70ad..f4f06e1810 100644 --- a/src/mc/mc_memory.c +++ b/src/mc/mc_memory.c @@ -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 } diff --git a/src/xbt/mmalloc/mm_legacy.c b/src/xbt/mmalloc/mm_legacy.c index 5fbdb042f7..94aa3d1ac9 100644 --- a/src/xbt/mmalloc/mm_legacy.c +++ b/src/xbt/mmalloc/mm_legacy.c @@ -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) diff --git a/src/xbt/mmalloc/mm_module.c b/src/xbt/mmalloc/mm_module.c index 8ee23eb6b3..4839cfe887 100644 --- a/src/xbt/mmalloc/mm_module.c +++ b/src/xbt/mmalloc/mm_module.c @@ -72,6 +72,11 @@ 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 diff --git a/src/xbt/mmalloc/mmalloc.c b/src/xbt/mmalloc/mmalloc.c index 7376ca6874..72ca5a0cd6 100644 --- a/src/xbt/mmalloc/mmalloc.c +++ b/src/xbt/mmalloc/mmalloc.c @@ -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 diff --git a/src/xbt/mmalloc/mmprivate.h b/src/xbt/mmalloc/mmprivate.h index 37e4ea718d..6f457bc3c3 100644 --- a/src/xbt/mmalloc/mmprivate.h +++ b/src/xbt/mmalloc/mmprivate.h @@ -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; -- 2.20.1