From: Martin Quinson Date: Wed, 1 Feb 2012 16:36:58 +0000 (+0100) Subject: Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid X-Git-Tag: exp_20120216~97 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/276b2d9b83a3e3afcd449fb53a2dd8d662070036?hp=1380f1a22147312c119055320c6ca87d72837598 Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid --- diff --git a/include/xbt/mmalloc.h b/include/xbt/mmalloc.h index 8ecb836bd0..74643cd3ee 100644 --- a/include/xbt/mmalloc.h +++ b/include/xbt/mmalloc.h @@ -1,3 +1,13 @@ +/* Copyright (C) 1991, 1992 Free Software Foundation, Inc. + This file was then part of the GNU C Library. */ + +/* Copyright (c) 2010-2012. 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. */ + + #ifndef MMALLOC_H #define MMALLOC_H 1 @@ -7,71 +17,53 @@ # include /* for size_t */ # include /* for NULL */ #endif -//#include "./include/ansidecl.h" -/* Allocate SIZE bytes of memory. */ +/* Datatype representing a separate heap. The whole point of the mmalloc module + * is to allow several such heaps in the process. It thus works by redefining + * all the classical memory management functions (malloc and friends) with an + * extra first argument: the heap in which the memory is to be taken. + * + * The heap structure itself is an opaque object that shouldnt be messed with. + */ +typedef struct mdesc *xbt_mheap_t; -extern void *mmalloc(void *md, size_t size); +/* Allocate SIZE bytes of memory. */ +extern void *mmalloc(xbt_mheap_t md, size_t size); /* Re-allocate the previously allocated block in void*, making the new block SIZE bytes long. */ - -extern void *mrealloc(void *md, void *ptr, size_t size); +extern void *mrealloc(xbt_mheap_t md, void *ptr, size_t size); /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */ - -extern void *mcalloc(void *md, size_t nmemb, size_t size); +extern void *mcalloc(xbt_mheap_t md, size_t nmemb, size_t size); /* Free a block allocated by `mmalloc', `mrealloc' or `mcalloc'. */ - -extern void mfree(void *md, void *ptr); +extern void mfree(xbt_mheap_t md, void *ptr); /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */ - -extern void *mmemalign(void *md, size_t alignment, size_t size); +extern void *mmemalign(xbt_mheap_t md, size_t alignment, size_t size); /* Allocate SIZE bytes on a page boundary. */ +extern void *mvalloc(xbt_mheap_t md, size_t size); -extern void *mvalloc(void *md, size_t size); - -/* Activate a standard collection of debugging hooks. */ +extern xbt_mheap_t mmalloc_attach(int fd, void *baseaddr); -extern int mmcheck(void *md, void (*func) (void)); +extern void mmalloc_detach_no_free(xbt_mheap_t md); -extern int mmcheckf(void *md, void (*func) (void), int force); - -/* Pick up the current statistics. (see FIXME elsewhere) */ - -extern struct mstats mmstats(void *md); - -extern void *mmalloc_attach(int fd, void *baseaddr); - -extern void mmalloc_pre_detach(void *md); - -extern void *mmalloc_detach(void *md); - -extern int mmalloc_setkey(void *md, int keynum, void *key); - -extern void *mmalloc_getkey(void *md, int keynum); - -// FIXME: this function is not implemented anymore? -//extern int mmalloc_errno (void* md); +extern void *mmalloc_detach(xbt_mheap_t md); /* return the heap used when NULL is passed as first argument to any mm* function */ -extern void *mmalloc_get_default_md(void); - -extern int mmtrace(void); +extern xbt_mheap_t mmalloc_get_default_md(void); extern void *mmalloc_findbase(int size); -extern int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr); - -extern void mmalloc_display_info_heap(void *h); +extern void mmalloc_display_info_heap(xbt_mheap_t h); /* To change the heap used when using the legacy version malloc/free/realloc and such */ -void mmalloc_set_current_heap(void *new_heap); -void *mmalloc_get_current_heap(void); +void mmalloc_set_current_heap(xbt_mheap_t new_heap); +xbt_mheap_t mmalloc_get_current_heap(void); +int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void *std_heap_addr); #endif /* MMALLOC_H */ diff --git a/src/mc/mc_memory.c b/src/mc/mc_memory.c index 0478629799..71eb7d0593 100644 --- a/src/mc/mc_memory.c +++ b/src/mc/mc_memory.c @@ -18,23 +18,21 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory, mc, /* Pointers to each of the heap regions to use */ void *std_heap = NULL; /* memory erased each time the MC stuff rollbacks to the beginning. Almost everything goes here */ void *raw_heap = NULL; /* memory persistent over the MC rollbacks. Only MC stuff should go there */ -/* int raw_heap_fd; */ /* unsued */ /* Initialize the model-checker memory subsystem */ /* It creates the two heap regions: std_heap and raw_heap */ void MC_memory_init() { -/* Create the first region HEAP_OFFSET bytes after the heap break address */ + /* Create the first region HEAP_OFFSET bytes after the heap break address */ std_heap = mmalloc_get_default_md(); xbt_assert(std_heap != NULL); -/* Create the second region a page after the first one ends + safety gap */ -/* raw_heap_fd = shm_open("raw_heap", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);*/ + /* Create the second region a page after the first one ends + safety gap */ raw_heap = mmalloc_attach(-1, (char*)(std_heap) + STD_HEAP_SIZE + getpagesize()); xbt_assert(raw_heap != NULL); } -/* Finish the memory subsystem */ +/* Finalize the memory subsystem */ #include "xbt_modinter.h" void MC_memory_exit(void) { diff --git a/src/xbt/mmalloc/attach.c b/src/xbt/mmalloc/attach.c index 254182e635..7741a0b090 100644 --- a/src/xbt/mmalloc/attach.c +++ b/src/xbt/mmalloc/attach.c @@ -49,7 +49,7 @@ static struct mdesc *reuse(int fd); If the open file corresponding to FD is from a previous use of mmalloc and passes some basic sanity checks to ensure that it is - compatible with the current mmalloc package, then it's data is + compatible with the current mmalloc package, then its data is mapped in and is immediately accessible at the same addresses in the current process as the process that created the file (ignoring the BASEADDR parameter). @@ -63,16 +63,15 @@ static struct mdesc *reuse(int fd); On success, returns a "malloc descriptor" which is used in subsequent calls to other mmalloc package functions. It is explicitly "void *" - ("char *" for systems that don't fully support void) so that users - of the package don't have to worry about the actual implementation - details. + so that users of the package don't have to worry about the actual + implementation details. On failure returns NULL. */ -void *mmalloc_attach(int fd, void *baseaddr) +xbt_mheap_t mmalloc_attach(int fd, void *baseaddr) { struct mdesc mtemp; - struct mdesc *mdp; + xbt_mheap_t mdp; void *mbase; struct stat sbuf; @@ -105,7 +104,6 @@ void *mmalloc_attach(int fd, void *baseaddr) strncpy(mdp->magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE); mdp->headersize = sizeof(mtemp); mdp->version = MMALLOC_VERSION; - mdp->morecore = __mmalloc_mmap_morecore; mdp->fd = fd; mdp->base = mdp->breakval = mdp->top = baseaddr; mdp->next_mdesc = NULL; @@ -129,7 +127,7 @@ void *mmalloc_attach(int fd, void *baseaddr) fails, then close the file descriptor if it was opened by us, and arrange to return a NULL. */ - if ((mbase = mdp->morecore(mdp, sizeof(mtemp))) != NULL) { + if ((mbase = mmorecore(mdp, sizeof(mtemp))) != NULL) { memcpy(mbase, mdp, sizeof(mtemp)); } else { THROWF(system_error,0,"morecore failed to get some memory!"); @@ -145,8 +143,8 @@ void *mmalloc_attach(int fd, void *baseaddr) mdp->next_mdesc = (struct mdesc *)mbase; UNLOCK(mdp); } - - return ((void *) mbase); + + return mbase; } /* Given an valid file descriptor on an open file, test to see if that file @@ -192,14 +190,10 @@ static struct mdesc *reuse(int fd) if (__mmalloc_remap_core(&mtemp) == mtemp.base) { mdp = (struct mdesc *) mtemp.base; mdp->fd = fd; - mdp->morecore = __mmalloc_mmap_morecore; if(!mdp->refcount){ sem_init(&mdp->sem, 1, 1); mdp->refcount++; } - if (mdp->mfree_hook != NULL) { - mmcheckf((void *) mdp, (void (*)(void)) NULL, 1); - } } /* Add the new heap to the linked list of heaps attached by mmalloc */ diff --git a/src/xbt/mmalloc/detach.c b/src/xbt/mmalloc/detach.c index e1157ed081..6ed13980ae 100644 --- a/src/xbt/mmalloc/detach.c +++ b/src/xbt/mmalloc/detach.c @@ -14,6 +14,20 @@ #include #include "mmprivate.h" +/* 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 mmalloc_detach_no_free(xbt_mheap_t md) +{ + struct mdesc *mdp = md; + + if(--mdp->refcount == 0){ + LOCK(mdp) ; + sem_destroy(&mdp->sem); + } +} + /* 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. @@ -28,19 +42,8 @@ region we are about to unmap, so we first make a local copy of it on the stack and use the copy. */ -void mmalloc_pre_detach(void *md) -{ - struct mdesc *mdp = md; - - if(--mdp->refcount == 0){ - LOCK(mdp) ; - sem_destroy(&mdp->sem); - } -} - -void *mmalloc_detach(void *md) +void *mmalloc_detach(xbt_mheap_t mdp) { - struct mdesc *mdp = (struct mdesc *)md; struct mdesc mtemp, *mdptemp; if (mdp != NULL) { @@ -51,25 +54,25 @@ void *mmalloc_detach(void *md) mdptemp->next_mdesc = mdp->next_mdesc; - mmalloc_pre_detach(md); - mtemp = *(struct mdesc *) md; + mmalloc_detach_no_free(mdp); + mtemp = *mdp; /* Now unmap all the pages associated with this region by asking for a negative increment equal to the current size of the region. */ - if ((mtemp.morecore(&mtemp, + if ((mmorecore(&mtemp, (char *) mtemp.base - (char *) mtemp.breakval)) == NULL) { /* Deallocating failed. Update the original malloc descriptor with any changes */ - *(struct mdesc *) md = mtemp; + *mdp = mtemp; } else { if (mtemp.flags & MMALLOC_DEVZERO) { close(mtemp.fd); } - md = NULL; + mdp = NULL; } } - return (md); + return (mdp); } diff --git a/src/xbt/mmalloc/keys.c b/src/xbt/mmalloc/keys.c deleted file mode 100644 index 7613923c57..0000000000 --- a/src/xbt/mmalloc/keys.c +++ /dev/null @@ -1,52 +0,0 @@ -/* Access for application keys in mmap'd malloc managed region. - Copyright 1992 Free Software Foundation, Inc. - - Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com - This file was then part of the GNU C Library. */ - -/* Copyright (c) 2010. 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. */ - - - - -/* This module provides access to some keys that the application can use to - provide persistent access to locations in the mapped memory section. - The intent is that these keys are to be used sparingly as sort of - persistent global variables which the application can use to reinitialize - access to data in the mapped region. - - For the moment, these keys are simply stored in the malloc descriptor - itself, in an array of fixed length. This should be fixed so that there - can be an unlimited number of keys, possibly using a multilevel access - scheme of some sort. */ - -#include "mmprivate.h" - -int mmalloc_setkey(void *md, int keynum, void *key) -{ - struct mdesc *mdp = (struct mdesc *) md; - int result = 0; - - LOCK(mdp); - if ((mdp != NULL) && (keynum >= 0) && (keynum < MMALLOC_KEYS)) { - mdp->keys[keynum] = key; - result++; - } - UNLOCK(mdp); - return (result); -} - -void *mmalloc_getkey(void *md, int keynum) -{ - struct mdesc *mdp = (struct mdesc *) md; - void *keyval = NULL; - - if ((mdp != NULL) && (keynum >= 0) && (keynum < MMALLOC_KEYS)) { - keyval = mdp->keys[keynum]; - } - return (keyval); -} diff --git a/src/xbt/mmalloc/mcalloc.c b/src/xbt/mmalloc/mcalloc.c index f80953f85a..6c94d46a76 100644 --- a/src/xbt/mmalloc/mcalloc.c +++ b/src/xbt/mmalloc/mcalloc.c @@ -15,7 +15,7 @@ /* Allocate an array of NMEMB elements each SIZE bytes long. The entire array is initialized to zeros. */ -void *mcalloc(void *md, register size_t nmemb, register size_t size) +void *mcalloc(xbt_mheap_t md, register size_t nmemb, register size_t size) { register void *result; diff --git a/src/xbt/mmalloc/mfree.c b/src/xbt/mmalloc/mfree.c index 53e83ce65b..ff89699679 100644 --- a/src/xbt/mmalloc/mfree.c +++ b/src/xbt/mmalloc/mfree.c @@ -33,13 +33,6 @@ void __mmalloc_free(struct mdesc *mdp, void *ptr) type = mdp->heapinfo[block].busy.type; switch (type) { case 0: - /* Get as many statistics as early as we can. */ - mdp->heapstats.chunks_used--; - mdp->heapstats.bytes_used -= - mdp->heapinfo[block].busy.info.block.size * BLOCKSIZE; - mdp->heapstats.bytes_free += - mdp->heapinfo[block].busy.info.block.size * BLOCKSIZE; - /* Find the free cluster previous to this one in the free list. Start searching at the last block referenced; this may benefit programs with locality of allocation. */ @@ -68,7 +61,6 @@ void __mmalloc_free(struct mdesc *mdp, void *ptr) mdp->heapinfo[block].free.prev = i; mdp->heapinfo[i].free.next = block; mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block; - mdp->heapstats.chunks_free++; } /* Now that the block is linked in, see if we can coalesce it @@ -81,7 +73,6 @@ void __mmalloc_free(struct mdesc *mdp, void *ptr) mdp->heapinfo[block].free.next = mdp->heapinfo[mdp->heapinfo[block].free.next].free.next; mdp->heapinfo[mdp->heapinfo[block].free.next].free.prev = block; - mdp->heapstats.chunks_free--; } /* Now see if we can return stuff to the system. */ @@ -106,12 +97,6 @@ void __mmalloc_free(struct mdesc *mdp, void *ptr) break; default: - /* Do some of the statistics. */ - mdp->heapstats.chunks_used--; - mdp->heapstats.bytes_used -= 1 << type; - mdp->heapstats.chunks_free++; - mdp->heapstats.bytes_free += 1 << type; - /* Get the address of the first free fragment in this block. */ prev = (struct list *) ((char *) ADDRESS(block) + @@ -133,12 +118,6 @@ void __mmalloc_free(struct mdesc *mdp, void *ptr) mdp->heapinfo[block].busy.info.block.size = 1; mdp->heapinfo[block].busy.info.block.busy_size = 0; - /* Keep the statistics accurate. */ - mdp->heapstats.chunks_used++; - mdp->heapstats.bytes_used += BLOCKSIZE; - mdp->heapstats.chunks_free -= BLOCKSIZE >> type; - mdp->heapstats.bytes_free -= BLOCKSIZE; - mfree((void *) mdp, (void *) ADDRESS(block)); } else if (mdp->heapinfo[block].busy.info.frag.nfree != 0) { /* If some fragments of this block are free, link this @@ -173,13 +152,11 @@ void __mmalloc_free(struct mdesc *mdp, void *ptr) /* Return memory to the heap. */ -void mfree(void *md, void *ptr) +void mfree(xbt_mheap_t mdp, void *ptr) { - struct mdesc *mdp; register struct alignlist *l; if (ptr != NULL) { - mdp = MD_TO_MDP(md); for (l = mdp->aligned_blocks; l != NULL; l = l->next) { if (l->aligned == ptr) { l->aligned = NULL; /* Mark the slot in the list as free. */ @@ -187,10 +164,6 @@ void mfree(void *md, void *ptr) break; } } - if (mdp->mfree_hook != NULL) { - mdp->mfree_hook(mdp, ptr); - } else { - __mmalloc_free(mdp, ptr); - } + __mmalloc_free(mdp, ptr); } } diff --git a/src/xbt/mmalloc/mm.c b/src/xbt/mmalloc/mm.c index 8c9b3b8792..adc31bb646 100644 --- a/src/xbt/mmalloc/mm.c +++ b/src/xbt/mmalloc/mm.c @@ -18,15 +18,10 @@ #include "mcalloc.c" #include "mfree.c" #include "mmalloc.c" -#include "mmcheck.c" #include "mmemalign.c" -#include "mmstats.c" -#include "mmtrace.c" #include "mrealloc.c" #include "mvalloc.c" -#include "mmap-sup.c" +#include "mmorecore.c" #include "attach.c" #include "detach.c" -#include "keys.c" -#include "sbrk-sup.c" #include "mm_legacy.c" diff --git a/src/xbt/mmalloc/mm_legacy.c b/src/xbt/mmalloc/mm_legacy.c index f520762bac..0de0dd2534 100644 --- a/src/xbt/mmalloc/mm_legacy.c +++ b/src/xbt/mmalloc/mm_legacy.c @@ -13,16 +13,23 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt, "Logging specific to mm_legacy in mmalloc"); -static void *__mmalloc_current_heap = NULL; /* The heap we are currently using. */ +/* The mmalloc() package can use a single implicit malloc descriptor + for mmalloc/mrealloc/mfree operations which do not supply an explicit + descriptor. This allows mmalloc() to provide + backwards compatibility with the non-mmap'd version. */ +xbt_mheap_t __mmalloc_default_mdp = NULL; + + +static xbt_mheap_t __mmalloc_current_heap = NULL; /* The heap we are currently using. */ #include "xbt_modinter.h" -void *mmalloc_get_current_heap(void) +xbt_mheap_t mmalloc_get_current_heap(void) { return __mmalloc_current_heap; } -void mmalloc_set_current_heap(void *new_heap) +void mmalloc_set_current_heap(xbt_mheap_t new_heap) { __mmalloc_current_heap = new_heap; } @@ -30,11 +37,8 @@ void mmalloc_set_current_heap(void *new_heap) #ifdef MMALLOC_WANT_OVERIDE_LEGACY void *malloc(size_t n) { - void *mdp = __mmalloc_current_heap; -#ifdef HAVE_MMAP - if (!mdp) - mmalloc_preinit(); -#endif + xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); + LOCK(mdp); void *ret = mmalloc(mdp, n); UNLOCK(mdp); @@ -45,11 +49,8 @@ void *malloc(size_t n) void *calloc(size_t nmemb, size_t size) { size_t total_size = nmemb * size; - void *mdp = __mmalloc_current_heap; -#ifdef HAVE_MMAP - if (!mdp) - mmalloc_preinit(); -#endif + xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); + LOCK(mdp); void *ret = mmalloc(mdp, total_size); UNLOCK(mdp); @@ -63,11 +64,8 @@ void *calloc(size_t nmemb, size_t size) void *realloc(void *p, size_t s) { void *ret = NULL; - void *mdp = __mmalloc_current_heap; -#ifdef HAVE_MMAP - if (!mdp) - mmalloc_preinit(); -#endif + xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); + LOCK(mdp); if (s) { if (p) @@ -84,10 +82,8 @@ void *realloc(void *p, size_t s) void free(void *p) { - void *mdp = __mmalloc_current_heap; -#ifdef HAVE_GTNETS - if(!mdp) return; -#endif + xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); + LOCK(mdp); mfree(mdp, p); UNLOCK(mdp); @@ -101,7 +97,7 @@ void free(void *p) * valgrind. */ #define HEAP_OFFSET (128UL<<20) -void *mmalloc_get_default_md(void) +xbt_mheap_t mmalloc_get_default_md(void) { xbt_assert(__mmalloc_default_mdp); return __mmalloc_default_mdp; @@ -109,7 +105,7 @@ void *mmalloc_get_default_md(void) static void mmalloc_fork_prepare(void) { - struct mdesc* mdp = NULL; + xbt_mheap_t mdp = NULL; if ((mdp =__mmalloc_default_mdp)){ while(mdp){ LOCK(mdp); @@ -123,7 +119,7 @@ static void mmalloc_fork_prepare(void) static void mmalloc_fork_parent(void) { - struct mdesc* mdp = NULL; + xbt_mheap_t mdp = NULL; if ((mdp =__mmalloc_default_mdp)){ while(mdp){ if(mdp->fd < 0) @@ -145,10 +141,10 @@ static void mmalloc_fork_child(void) } /* Initialize the default malloc descriptor. */ -void mmalloc_preinit(void) +void *mmalloc_preinit(void) { int res; - if (!__mmalloc_default_mdp) { + if (__mmalloc_default_mdp == NULL) { unsigned long mask = ~((unsigned long)getpagesize() - 1); void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask); __mmalloc_default_mdp = mmalloc_attach(-1, addr); @@ -159,72 +155,29 @@ void mmalloc_preinit(void) THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res); } xbt_assert(__mmalloc_default_mdp != NULL); + + return __mmalloc_default_mdp; } void mmalloc_postexit(void) { /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */ // mmalloc_detach(__mmalloc_default_mdp); - mmalloc_pre_detach(__mmalloc_default_mdp); + mmalloc_detach_no_free(__mmalloc_default_mdp); } -int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr){ +int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void *std_heap_addr){ - if(h1 == NULL && h2 == NULL){ + if(mdp1 == NULL && mdp2 == NULL){ XBT_DEBUG("Malloc descriptors null"); return 0; } /* Heapstats */ - int errors = 0; + int errors = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr); - struct mstats ms1 = mmstats(h1); - struct mstats ms2 = mmstats(h2); - - if(ms1.chunks_used != ms2.chunks_used){ - if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){ - XBT_DEBUG("Different chunks allocated by the user : %zu - %zu", ms1.chunks_used, ms2.chunks_used); - errors++; - }else{ - return 1; - } - } - - if(ms1.bytes_used != ms2.bytes_used){ - if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){ - XBT_DEBUG("Different byte total of user-allocated chunks : %zu - %zu", ms1.bytes_used, ms2.bytes_used); - errors++; - }else{ - return 1; - } - } - - if(ms1.bytes_free != ms2.bytes_free){ - if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){ - XBT_DEBUG("Different byte total of chunks in the free list : %zu - %zu", ms1.bytes_free, ms2.bytes_free); - errors++; - }else{ - return 1; - } - } - - if(ms1.chunks_free != ms2.chunks_free){ - if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){ - XBT_DEBUG("Different chunks in the free list : %zu - %zu", ms1.chunks_free, ms2.chunks_free); - errors++; - }else{ - return 1; - } - } - - struct mdesc *mdp1, *mdp2; - mdp1 = MD_TO_MDP(h1); - mdp2 = MD_TO_MDP(h2); - - int res = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr); - - return ((errors + res ) > 0); + return (errors > 0); } @@ -345,16 +298,7 @@ int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap } } - if(mdp1->saved_errno != mdp2->saved_errno){ - if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){ - XBT_DEBUG("Different errno"); - errors++; - }else{ - return 1; - } - } - - if(mdp1->version != mdp2->version){ + if(mdp1->version != mdp2->version){ if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){ XBT_DEBUG("Different version of the mmalloc package"); errors++; @@ -601,8 +545,17 @@ int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap } -void mmalloc_display_info_heap(void *h){ +void mmalloc_display_info_heap(xbt_mheap_t h){ } + +/* Useless prototype to make gcc happy */ +void *valloc(size_t size); + +void *valloc(size_t size) +{ //FIXME: won't work + return mvalloc(NULL, size); +} + diff --git a/src/xbt/mmalloc/mmalloc.c b/src/xbt/mmalloc/mmalloc.c index 0af8597cfd..a5a0192026 100644 --- a/src/xbt/mmalloc/mmalloc.c +++ b/src/xbt/mmalloc/mmalloc.c @@ -27,11 +27,11 @@ static void *align(struct mdesc *mdp, size_t size) void *result; unsigned long int adj; - result = mdp->morecore(mdp, size); + result = mmorecore(mdp, size); adj = RESIDUAL(result, BLOCKSIZE); if (adj != 0) { adj = BLOCKSIZE - adj; - mdp->morecore(mdp, adj); + mmorecore(mdp, adj); result = (char *) result + adj; } return (result); @@ -78,7 +78,7 @@ static void *morecore(struct mdesc *mdp, size_t size) } newinfo = (malloc_info *) align(mdp, newsize * sizeof(malloc_info)); if (newinfo == NULL) { - mdp->morecore(mdp, -size); + mmorecore(mdp, -size); return (NULL); } memset((void *) newinfo, 0, newsize * sizeof(malloc_info)); @@ -100,9 +100,8 @@ static void *morecore(struct mdesc *mdp, size_t size) /* Allocate memory from the heap. */ -void *mmalloc(void *md, size_t size) +void *mmalloc(xbt_mheap_t mdp, size_t size) { - struct mdesc *mdp; void *result; size_t block, blocks, lastblocks, start; register size_t i; @@ -115,13 +114,8 @@ void *mmalloc(void *md, size_t size) if (size == 0) size = 1; - mdp = MD_TO_MDP(md); // printf("(%s) Mallocing %d bytes on %p (default: %p)...",xbt_thread_self_name(),size,mdp,__mmalloc_default_mdp);fflush(stdout); - if (mdp->mmalloc_hook != NULL) { - return mdp->mmalloc_hook(md, size); - } - if (!(mdp->flags & MMALLOC_INITIALIZED)) { if (!initialize(mdp)) { return (NULL); @@ -160,16 +154,11 @@ void *mmalloc(void *md, size_t size) RESIDUAL(next->next, BLOCKSIZE) >> log; } - /* Update the statistics. */ - mdp->heapstats.chunks_used++; - mdp->heapstats.bytes_used += 1 << log; - mdp->heapstats.chunks_free--; - mdp->heapstats.bytes_free -= 1 << log; } else { /* No free fragments of the desired size, so get a new block and break it into fragments, returning the first. */ //printf("(%s) No free fragment...",xbt_thread_self_name()); - result = mmalloc(md, BLOCKSIZE); + result = mmalloc(mdp, BLOCKSIZE); //printf("(%s) Fragment: %p...",xbt_thread_self_name(),result); if (result == NULL) { return (NULL); @@ -191,10 +180,6 @@ void *mmalloc(void *md, size_t size) mdp->heapinfo[block].busy.type = log; mdp->heapinfo[block].busy.info.frag.nfree = i - 1; mdp->heapinfo[block].busy.info.frag.first = i - 1; - - mdp->heapstats.chunks_free += (BLOCKSIZE >> log) - 1; - mdp->heapstats.bytes_free += BLOCKSIZE - (1 << log); - mdp->heapstats.bytes_used -= BLOCKSIZE - (1 << log); } } else { /* Large allocation to receive one or more blocks. @@ -213,7 +198,7 @@ void *mmalloc(void *md, size_t size) lastblocks = mdp->heapinfo[block].free.size; if (mdp->heaplimit != 0 && block + lastblocks == mdp->heaplimit && - mdp->morecore(mdp, 0) == ADDRESS(block + lastblocks) && + mmorecore(mdp, 0) == ADDRESS(block + lastblocks) && (morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) { /* Which block we are extending (the `final free block' referred to above) might have changed, if @@ -221,7 +206,6 @@ void *mmalloc(void *md, size_t size) block = mdp->heapinfo[0].free.prev; mdp->heapinfo[block].free.size += (blocks - lastblocks); - mdp->heapstats.bytes_free += (blocks - lastblocks) * BLOCKSIZE; continue; } result = morecore(mdp, blocks * BLOCKSIZE); @@ -232,8 +216,6 @@ void *mmalloc(void *md, size_t size) mdp->heapinfo[block].busy.type = 0; mdp->heapinfo[block].busy.info.block.size = blocks; mdp->heapinfo[block].busy.info.block.busy_size = size; - mdp->heapstats.chunks_used++; - mdp->heapstats.bytes_used += blocks * BLOCKSIZE; return (result); } } @@ -260,15 +242,11 @@ void *mmalloc(void *md, size_t size) = mdp->heapinfo[block].free.prev; mdp->heapinfo[mdp->heapinfo[block].free.prev].free.next = mdp->heapindex = mdp->heapinfo[block].free.next; - mdp->heapstats.chunks_free--; } mdp->heapinfo[block].busy.type = 0; mdp->heapinfo[block].busy.info.block.size = blocks; mdp->heapinfo[block].busy.info.block.busy_size = size; - mdp->heapstats.chunks_used++; - mdp->heapstats.bytes_used += blocks * BLOCKSIZE; - mdp->heapstats.bytes_free -= blocks * BLOCKSIZE; } //printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout); return (result); diff --git a/src/xbt/mmalloc/mmcheck.c b/src/xbt/mmalloc/mmcheck.c deleted file mode 100644 index 8e0dc549e3..0000000000 --- a/src/xbt/mmalloc/mmcheck.c +++ /dev/null @@ -1,183 +0,0 @@ -/* Standard debugging hooks for `mmalloc'. - Copyright 1990, 1991, 1992 Free Software Foundation - - Written May 1989 by Mike Haertel. - Heavily modified Mar 1992 by Fred Fish (fnf@cygnus.com) */ - -/* Copyright (c) 2010. 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. */ - -#include "mmprivate.h" - -/* Default function to call when something awful happens. The application - can specify an alternate function to be called instead (and probably will - want to). */ - -extern void abort(void); - -/* Arbitrary magical numbers. */ - -#define MAGICWORD (unsigned int) 0xfedabeeb /* Active chunk */ -#define MAGICWORDFREE (unsigned int) 0xdeadbeef /* Inactive chunk */ -#define MAGICBYTE ((char) 0xd7) - -/* Each memory allocation is bounded by a header structure and a trailer - byte. I.E. - - - - The pointer returned to the user points to the first byte in the - user's allocation area. The magic word can be tested to detect - buffer underruns and the magic byte can be tested to detect overruns. */ - -struct hdr { - size_t size; /* Exact size requested by user. */ - unsigned long int magic; /* Magic number to check header integrity. */ -}; - -static void checkhdr(struct mdesc *mdp, const struct hdr *hdr); -static void mfree_check(void *md, void *ptr); -static void *mmalloc_check(void *md, size_t size); -static void *mrealloc_check(void *md, void *ptr, size_t size); - -/* Check the magicword and magicbyte, and if either is corrupted then - call the emergency abort function specified for the heap in use. */ - -static void checkhdr(struct mdesc *mdp, const struct hdr *hdr) -{ - if (hdr->magic != MAGICWORD || - ((char *) &hdr[1])[hdr->size] != MAGICBYTE) { - mdp->abortfunc(); - } -} - -static void mfree_check(void *md, void *ptr) -{ - struct hdr *hdr = ((struct hdr *) ptr) - 1; - struct mdesc *mdp; - - mdp = MD_TO_MDP(md); - checkhdr(mdp, hdr); - hdr->magic = MAGICWORDFREE; - mdp->mfree_hook = NULL; - mfree(md, (void *) hdr); - mdp->mfree_hook = mfree_check; -} - -static void *mmalloc_check(void *md, size_t size) -{ - struct hdr *hdr; - struct mdesc *mdp; - size_t nbytes; - - mdp = MD_TO_MDP(md); - mdp->mmalloc_hook = NULL; - nbytes = sizeof(struct hdr) + size + 1; - hdr = (struct hdr *) mmalloc(md, nbytes); - mdp->mmalloc_hook = mmalloc_check; - if (hdr != NULL) { - hdr->size = size; - hdr->magic = MAGICWORD; - hdr++; - *((char *) hdr + size) = MAGICBYTE; - } - return ((void *) hdr); -} - -static void *mrealloc_check(void *md, void *ptr, size_t size) -{ - struct hdr *hdr = ((struct hdr *) ptr) - 1; - struct mdesc *mdp; - size_t nbytes; - - mdp = MD_TO_MDP(md); - checkhdr(mdp, hdr); - mdp->mfree_hook = NULL; - mdp->mmalloc_hook = NULL; - mdp->mrealloc_hook = NULL; - nbytes = sizeof(struct hdr) + size + 1; - hdr = (struct hdr *) mrealloc(md, (void *) hdr, nbytes); - mdp->mfree_hook = mfree_check; - mdp->mmalloc_hook = mmalloc_check; - mdp->mrealloc_hook = mrealloc_check; - if (hdr != NULL) { - hdr->size = size; - hdr++; - *((char *) hdr + size) = MAGICBYTE; - } - return ((void *) hdr); -} - -/* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified - by MD. If FUNC is non-NULL, it is a pointer to the function to call - to abort whenever memory corruption is detected. By default, this is the - standard library function abort(). - - Note that we disallow installation of initial checking hooks if mmalloc - has been called at any time for this particular heap, since if any region - that is allocated prior to installation of the hooks is subsequently - reallocated or freed after installation of the hooks, it is guaranteed - to trigger a memory corruption error. We do this by checking the state - of the MMALLOC_INITIALIZED flag. If the FORCE argument is non-zero, this - checking is disabled and it is allowed to install the checking hooks at any - time. This is useful on systems where the C runtime makes one or more - malloc calls before the user code had a chance to call mmcheck or mmcheckf, - but never calls free with these values. Thus if we are certain that only - values obtained from mallocs after an mmcheck/mmcheckf will ever be passed - to free(), we can go ahead and force installation of the useful checking - hooks. - - However, we can call this function at any time after the initial call, - to update the function pointers to the checking routines and to the - user defined corruption handler routine, as long as these function pointers - have been previously extablished by the initial call. Note that we - do this automatically when remapping a previously used heap, to ensure - that the hooks get updated to the correct values, although the corruption - handler pointer gets set back to the default. The application can then - call mmcheck to use a different corruption handler if desired. - - Returns non-zero if checking is successfully enabled, zero otherwise. */ - -int mmcheckf(void *md, void (*func) (void), int force) -{ - struct mdesc *mdp; - int rtnval; - - mdp = MD_TO_MDP(md); - - /* We can safely set or update the abort function at any time, regardless - of whether or not we successfully do anything else. */ - - mdp->abortfunc = (func != NULL ? func : abort); - - /* If we haven't yet called mmalloc the first time for this heap, or if we - have hooks that were previously installed, then allow the hooks to be - initialized or updated. */ - - if (force || - !(mdp->flags & MMALLOC_INITIALIZED) || (mdp->mfree_hook != NULL)) { - mdp->mfree_hook = mfree_check; - mdp->mmalloc_hook = mmalloc_check; - mdp->mrealloc_hook = mrealloc_check; - mdp->flags |= MMALLOC_MMCHECK_USED; - rtnval = 1; - } else { - rtnval = 0; - } - - return (rtnval); -} - -/* This routine is for backwards compatibility only, in case there are - still callers to the original mmcheck function. */ - -int mmcheck(void *md, void (*func) (void)) -{ - int rtnval; - - rtnval = mmcheckf(md, func, 0); - return (rtnval); -} diff --git a/src/xbt/mmalloc/mmemalign.c b/src/xbt/mmalloc/mmemalign.c index f475076d49..5c7fb24cdd 100644 --- a/src/xbt/mmalloc/mmemalign.c +++ b/src/xbt/mmalloc/mmemalign.c @@ -9,17 +9,15 @@ #include "mmprivate.h" -void *mmemalign(void *md, size_t alignment, size_t size) +void *mmemalign(xbt_mheap_t mdp, size_t alignment, size_t size) { void *result; unsigned long int adj; struct alignlist *l; - struct mdesc *mdp; - if ((result = mmalloc(md, size + alignment - 1)) != NULL) { + if ((result = mmalloc(mdp, size + alignment - 1)) != NULL) { adj = RESIDUAL(result, alignment); if (adj != 0) { - mdp = MD_TO_MDP(md); for (l = mdp->aligned_blocks; l != NULL; l = l->next) { if (l->aligned == NULL) { /* This slot is free. Use it. */ @@ -27,9 +25,9 @@ void *mmemalign(void *md, size_t alignment, size_t size) } } if (l == NULL) { - l = (struct alignlist *) mmalloc(md, sizeof(struct alignlist)); + l = (struct alignlist *) mmalloc(mdp, sizeof(struct alignlist)); if (l == NULL) { - mfree(md, result); + mfree(mdp, result); return (NULL); } l->next = mdp->aligned_blocks; diff --git a/src/xbt/mmalloc/mmap-sup.c b/src/xbt/mmalloc/mmorecore.c similarity index 97% rename from src/xbt/mmalloc/mmap-sup.c rename to src/xbt/mmalloc/mmorecore.c index 28f89bc386..2c68d503e4 100644 --- a/src/xbt/mmalloc/mmap-sup.c +++ b/src/xbt/mmalloc/mmorecore.c @@ -20,10 +20,6 @@ #include #include -#ifndef SEEK_SET -#define SEEK_SET 0 -#endif - #include "mmprivate.h" #include "xbt/ex.h" @@ -33,22 +29,17 @@ it out. */ static size_t pagesize; -#if NEED_DECLARATION_GETPAGESIZE -extern int getpagesize(void); -#endif #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \ ~(pagesize - 1)) /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return MAP_SHARED. */ - #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \ ? MAP_PRIVATE \ : MAP_SHARED) /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */ - #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \ ? MAP_ANONYMOUS \ : 0) @@ -62,7 +53,7 @@ extern int getpagesize(void); amount to either add to or subtract from the existing region. Works like sbrk(), but using mmap(). */ -void *__mmalloc_mmap_morecore(struct mdesc *mdp, int size) +void *mmorecore(struct mdesc *mdp, int size) { ssize_t test = 0; void *result = NULL; diff --git a/src/xbt/mmalloc/mmprivate.h b/src/xbt/mmalloc/mmprivate.h index d2e2331792..123b6278cd 100644 --- a/src/xbt/mmalloc/mmprivate.h +++ b/src/xbt/mmalloc/mmprivate.h @@ -26,14 +26,9 @@ # endif #endif -#ifndef MIN -# define MIN(A, B) ((A) < (B) ? (A) : (B)) -#endif - #define MMALLOC_MAGIC "mmalloc" /* Mapped file magic number */ #define MMALLOC_MAGIC_SIZE 8 /* Size of magic number buf */ #define MMALLOC_VERSION 1 /* Current mmalloc version */ -#define MMALLOC_KEYS 16 /* Keys for application use */ /* The allocator divides the heap into blocks of fixed size; large requests receive one or more whole blocks, and small requests @@ -123,20 +118,6 @@ struct list { struct list *prev; }; -/* Statistics available to the user. - FIXME: By design, the internals of the malloc package are no longer - exported to the user via an include file, so access to this data needs - to be via some other mechanism, such as mmstat_ where the - return value is the the user is interested in. */ - -struct mstats { - size_t bytes_total; /* Total size of the heap. */ - size_t chunks_used; /* Chunks allocated by the user. */ - size_t bytes_used; /* Byte total of user-allocated chunks. */ - size_t chunks_free; /* Chunks in the free list. */ - size_t bytes_free; /* Byte total of chunks in the free list. */ -}; - /* Internal structure that defines the format of the malloc-descriptor. This gets written to the base address of the region that mmalloc is managing, and thus also becomes the file header for the mapped file, @@ -166,47 +147,6 @@ struct mdesc { /* Some flag bits to keep track of various internal things. */ unsigned int flags; - /* If a system call made by the mmalloc package fails, the errno is - preserved for future examination. */ - int saved_errno; - - /* Pointer to the function that is used to get more core, or return core - to the system, for requests using this malloc descriptor. For memory - mapped regions, this is the mmap() based routine. There may also be - a single malloc descriptor that points to an sbrk() based routine - for systems without mmap() or for applications that call the mmalloc() - package with a NULL malloc descriptor. - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void *(*morecore) (struct mdesc * mdp, int size); - - /* Pointer to the function that causes an abort when the memory checking - features are activated. By default this is set to abort(), but can - be set to another function by the application using mmalloc(). - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void (*abortfunc) (void); - - /* Debugging hook for free. - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void (*mfree_hook) (void *mdp, void *ptr); - - /* Debugging hook for `malloc'. - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void *(*mmalloc_hook) (void *mdp, size_t size); - - /* Debugging hook for realloc. - - FIXME: For mapped regions shared by more than one process, this - needs to be maintained on a per-process basis. */ - void *(*mrealloc_hook) (void *mdp, void *ptr, size_t size); - /* Number of info entries. */ size_t heapsize; @@ -226,10 +166,6 @@ struct mdesc { malloc_info *heapinfo; - /* Instrumentation. */ - - struct mstats heapstats; - /* Free list headers for each fragment size. */ /* Free lists for each fragment size. */ @@ -262,15 +198,8 @@ struct mdesc { int fd; - /* An array of keys to data within the mapped region, for use by the - application. */ - - void *keys[MMALLOC_KEYS]; - }; -int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr); - int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr); void mmalloc_display_info(void *h); @@ -280,7 +209,6 @@ void mmalloc_display_info(void *h); #define MMALLOC_DEVZERO (1 << 0) /* Have mapped to /dev/zero */ #define MMALLOC_ANONYMOUS (1 << 1) /* Use anonymous mapping */ #define MMALLOC_INITIALIZED (1 << 2) /* Initialized mmalloc */ -#define MMALLOC_MMCHECK_USED (1 << 3) /* mmcheckf() called already */ /* Internal version of `mfree' used in `morecore'. */ @@ -295,38 +223,20 @@ extern struct mdesc *__mmalloc_default_mdp; extern struct mdesc *__mmalloc_create_default_mdp(void); -/* Grow or shrink a contiguous mapped region using mmap(). - Works much like sbrk(), only faster */ - -extern void *__mmalloc_mmap_morecore(struct mdesc *mdp, int size); - - /* Remap a mmalloc region that was previously mapped. */ extern void *__mmalloc_remap_core(struct mdesc *mdp); -/* Macro to convert from a user supplied malloc descriptor to pointer to the - internal malloc descriptor. If the user supplied descriptor is NULL, then - use the default internal version, initializing it if necessary. Otherwise - just cast the user supplied version (which is void *) to the proper type - (struct mdesc *). */ - -#define MD_TO_MDP(md) \ - ((md) == NULL \ - ? __mmalloc_default_mdp \ - : (struct mdesc *) (md)) - -/* Thread-safety (if the sem is already created)*/ -#define LOCK(md) \ - do {\ - struct mdesc *lock_local_mdp = MD_TO_MDP(md); \ - sem_wait(&lock_local_mdp->sem); \ - } while (0) - -#define UNLOCK(md) \ - do { \ - struct mdesc *unlock_local_mdp = MD_TO_MDP(md); \ - sem_post(&unlock_local_mdp->sem); \ - } while (0) +/* Get core for the memory region specified by MDP, using SIZE as the + amount to either add to or subtract from the existing region. Works + like sbrk(), but using mmap(). */ +extern void *mmorecore(struct mdesc *mdp, int size); + +/* Thread-safety (if the sem is already created) FIXME: KILLIT*/ +#define LOCK(mdp) \ + sem_wait(&mdp->sem) + +#define UNLOCK(mdp) \ + sem_post(&mdp->sem) #endif /* __MMPRIVATE_H */ diff --git a/src/xbt/mmalloc/mmstats.c b/src/xbt/mmalloc/mmstats.c deleted file mode 100644 index 4342d191f9..0000000000 --- a/src/xbt/mmalloc/mmstats.c +++ /dev/null @@ -1,31 +0,0 @@ -/* Access the statistics maintained by `mmalloc'. - Copyright 1990, 1991, 1992 Free Software Foundation - - Written May 1989 by Mike Haertel. - Modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */ - -/* Copyright (c) 2010. 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. */ - -#include "mmprivate.h" - -/* FIXME: See the comment in mmprivate.h where struct mstats is defined. - None of the internal mmalloc structures should be externally visible - outside the library. */ - -struct mstats mmstats(void *md) -{ - struct mstats result; - struct mdesc *mdp; - - mdp = MD_TO_MDP(md); - result.bytes_total = (char *) mdp->top - (char *) mdp; - result.chunks_used = mdp->heapstats.chunks_used; - result.bytes_used = mdp->heapstats.bytes_used; - result.chunks_free = mdp->heapstats.chunks_free; - result.bytes_free = mdp->heapstats.bytes_free; - return (result); -} diff --git a/src/xbt/mmalloc/mmtrace.c b/src/xbt/mmalloc/mmtrace.c deleted file mode 100644 index 38219f3fb2..0000000000 --- a/src/xbt/mmalloc/mmtrace.c +++ /dev/null @@ -1,144 +0,0 @@ -/* More debugging hooks for `mmalloc'. - Copyright 1991, 1992, 1994 Free Software Foundation - - Written April 2, 1991 by John Gilmore of Cygnus Support - Based on mcheck.c by Mike Haertel. - Modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */ - -/* Copyright (c) 2010. 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. */ - -#include -#include "mmprivate.h" - -static void tr_break(void); -static void tr_freehook(void *md, void *ptr); -static void *tr_mallochook(void *md, size_t size); -static void *tr_reallochook(void *md, void *ptr, size_t size); - -#ifndef __GNU_LIBRARY__ -extern char *getenv(); -#endif - -static FILE *mallstream; - -#if 0 /* FIXME: Disabled for now. */ -static char mallenv[] = "MALLOC_TRACE"; -static char mallbuf[BUFSIZ]; /* Buffer for the output. */ -#endif - -/* Address to breakpoint on accesses to... */ -static void *mallwatch; - -/* Old hook values. */ - -static void (*old_mfree_hook) (void *md, void *ptr); -static void *(*old_mmalloc_hook) (void *md, size_t size); -static void *(*old_mrealloc_hook) (void *md, void *ptr, size_t size); - -/* This function is called when the block being alloc'd, realloc'd, or - freed has an address matching the variable "mallwatch". In a debugger, - set "mallwatch" to the address of interest, then put a breakpoint on - tr_break. */ - -static void tr_break(void) -{ -} - -static void tr_freehook(void *md, void *ptr) -{ - struct mdesc *mdp; - - mdp = MD_TO_MDP(md); - /* Be sure to print it first. */ - fprintf(mallstream, "- %08lx\n", (unsigned long) ptr); - if (ptr == mallwatch) - tr_break(); - mdp->mfree_hook = old_mfree_hook; - mfree(md, ptr); - mdp->mfree_hook = tr_freehook; -} - -static void *tr_mallochook(void *md, size_t size) -{ - void *hdr; - struct mdesc *mdp; - - mdp = MD_TO_MDP(md); - mdp->mmalloc_hook = old_mmalloc_hook; - hdr = (void *) mmalloc(md, size); - mdp->mmalloc_hook = tr_mallochook; - - /* We could be printing a NULL here; that's OK. */ - fprintf(mallstream, "+ %p 0x%lx\n", hdr, (unsigned long) size); - - if (hdr == mallwatch) - tr_break(); - - return (hdr); -} - -static void *tr_reallochook(void *md, void *ptr, size_t size) -{ - void *hdr; - struct mdesc *mdp; - - mdp = MD_TO_MDP(md); - - if (ptr == mallwatch) - tr_break(); - - mdp->mfree_hook = old_mfree_hook; - mdp->mmalloc_hook = old_mmalloc_hook; - mdp->mrealloc_hook = old_mrealloc_hook; - hdr = (void *) mrealloc(md, ptr, size); - mdp->mfree_hook = tr_freehook; - mdp->mmalloc_hook = tr_mallochook; - mdp->mrealloc_hook = tr_reallochook; - if (hdr == NULL) - /* Failed realloc. */ - fprintf(mallstream, "! %p 0x%lx\n", ptr, (unsigned long) size); - else - fprintf(mallstream, "< %p\n> %p 0x%lx\n", ptr, - hdr, (unsigned long) size); - - if (hdr == mallwatch) - tr_break(); - - return hdr; -} - -/* We enable tracing if either the environment variable MALLOC_TRACE - is set, or if the variable mallwatch has been patched to an address - that the debugging user wants us to stop on. When patching mallwatch, - don't forget to set a breakpoint on tr_break! */ - -int mmtrace(void) -{ -#if 0 /* FIXME! This is disabled for now until we figure out how to - maintain a stack of hooks per heap, since we might have other - hooks (such as set by mmcheck/mmcheckf) active also. */ - char *mallfile; - - mallfile = getenv(mallenv); - if (mallfile != NULL || mallwatch != NULL) { - mallstream = fopen(mallfile != NULL ? mallfile : "/dev/null", "w"); - if (mallstream != NULL) { - /* Be sure it doesn't mmalloc its buffer! */ - setbuf(mallstream, mallbuf); - fprintf(mallstream, "= Start\n"); - old_mfree_hook = mdp->mfree_hook; - mdp->mfree_hook = tr_freehook; - old_mmalloc_hook = mdp->mmalloc_hook; - mdp->mmalloc_hook = tr_mallochook; - old_mrealloc_hook = mdp->mrealloc_hook; - mdp->mrealloc_hook = tr_reallochook; - } - } -#endif /* 0 */ - - return (1); -} diff --git a/src/xbt/mmalloc/mrealloc.c b/src/xbt/mmalloc/mrealloc.c index 7e83e20876..9d20e03b48 100644 --- a/src/xbt/mmalloc/mrealloc.c +++ b/src/xbt/mmalloc/mrealloc.c @@ -9,6 +9,7 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include /* Prototypes for memcpy, memmove, memset, etc */ +#include /* abort */ #include "mmprivate.h" @@ -19,36 +20,30 @@ new region. This module has incestuous knowledge of the internals of both mfree and mmalloc. */ -void *mrealloc(void *md, void *ptr, size_t size) +void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size) { - struct mdesc *mdp; void *result; int type; size_t block, blocks, oldlimit; if (size == 0) { - mfree(md, ptr); - return (mmalloc(md, 0)); + mfree(mdp, ptr); + return (mmalloc(mdp, 0)); } else if (ptr == NULL) { - return (mmalloc(md, size)); + return (mmalloc(mdp, size)); } - mdp = MD_TO_MDP(md); //printf("(%s)realloc %p to %d...",xbt_thread_self_name(),ptr,(int)size); if ((char *) ptr < (char *) mdp->heapbase || BLOCK(ptr) > mdp->heapsize) { printf - ("FIXME. Ouch, this pointer is not mine. I will malloc it instead of reallocing it. (please report this bug)\n"); - result = mmalloc(md, size); + ("FIXME. Ouch, this pointer is not mine, refusing to proceed (another solution would be to malloc it instead of reallocing it, see source code)\n"); + result = mmalloc(mdp, size); abort(); return result; } - if (mdp->mrealloc_hook != NULL) { - return mdp->mrealloc_hook(md, ptr, size); - } - block = BLOCK(ptr); type = mdp->heapinfo[block].busy.type; @@ -57,10 +52,10 @@ void *mrealloc(void *md, void *ptr, size_t size) /* Maybe reallocate a large block to a small fragment. */ if (size <= BLOCKSIZE / 2) { //printf("(%s) alloc large block...",xbt_thread_self_name()); - result = mmalloc(md, size); + result = mmalloc(mdp, size); if (result != NULL) { memcpy(result, ptr, size); - mfree(md, ptr); + mfree(mdp, ptr); return (result); } } @@ -76,7 +71,7 @@ void *mrealloc(void *md, void *ptr, size_t size) = mdp->heapinfo[block].busy.info.block.size - blocks; mdp->heapinfo[block].busy.info.block.size = blocks; mdp->heapinfo[block].busy.info.block.busy_size = size; - mfree(md, ADDRESS(block + blocks)); + mfree(mdp, ADDRESS(block + blocks)); result = ptr; } else if (blocks == mdp->heapinfo[block].busy.info.block.size) { /* No size change necessary. */ @@ -89,11 +84,11 @@ void *mrealloc(void *md, void *ptr, size_t size) /* Prevent free from actually returning memory to the system. */ oldlimit = mdp->heaplimit; mdp->heaplimit = 0; - mfree(md, ptr); + mfree(mdp, ptr); mdp->heaplimit = oldlimit; - result = mmalloc(md, size); + result = mmalloc(mdp, size); if (result == NULL) { - mmalloc(md, blocks * BLOCKSIZE); + mmalloc(mdp, blocks * BLOCKSIZE); return (NULL); } if (ptr != result) @@ -113,12 +108,12 @@ void *mrealloc(void *md, void *ptr, size_t size) and copy the lesser of the new size and the old. */ //printf("(%s) new size is different...",xbt_thread_self_name()); - result = mmalloc(md, size); + result = mmalloc(mdp, size); if (result == NULL) return (NULL); memcpy(result, ptr, MIN(size, (size_t) 1 << type)); - mfree(md, ptr); + mfree(mdp, ptr); } break; } diff --git a/src/xbt/mmalloc/mvalloc.c b/src/xbt/mmalloc/mvalloc.c index 93caf739dc..c940ee9f69 100644 --- a/src/xbt/mmalloc/mvalloc.c +++ b/src/xbt/mmalloc/mvalloc.c @@ -21,19 +21,12 @@ static size_t cache_pagesize; extern int getpagesize PARAMS((void)); #endif -void *mvalloc(void *md, size_t size) +void *mvalloc(xbt_mheap_t mdp, size_t size) { if (cache_pagesize == 0) { cache_pagesize = getpagesize(); } - return (mmemalign(md, cache_pagesize, size)); + return (mmemalign(mdp, cache_pagesize, size)); } -/* Useless prototype to make gcc happy */ -void *valloc(size_t size); - -void *valloc(size_t size) -{ - return mvalloc(NULL, size); -} diff --git a/src/xbt/mmalloc/sbrk-sup.c b/src/xbt/mmalloc/sbrk-sup.c deleted file mode 100644 index 522cbfcab2..0000000000 --- a/src/xbt/mmalloc/sbrk-sup.c +++ /dev/null @@ -1,46 +0,0 @@ -/* Support for sbrk() regions. - Copyright 1992, 2000 Free Software Foundation, Inc. - Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com */ - -/* Copyright (c) 2010. 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. */ - -#include /* Prototypes for sbrk (maybe) */ - -#include /* Prototypes for memcpy, memmove, memset, etc */ - -#include "xbt.h" -#include "mmprivate.h" - -static void *sbrk_morecore(struct mdesc *mdp, int size); -#if NEED_DECLARATION_SBRK -extern void *sbrk(int size); -#endif - -/* The mmalloc() package can use a single implicit malloc descriptor - for mmalloc/mrealloc/mfree operations which do not supply an explicit - descriptor. For these operations, sbrk() is used to obtain more core - from the system, or return core. This allows mmalloc() to provide - backwards compatibility with the non-mmap'd version. */ - -struct mdesc *__mmalloc_default_mdp; - -/* Use sbrk() to get more core. */ - -static void *sbrk_morecore(mdp, size) -struct mdesc *mdp; -int size; -{ - void *result; - - if ((result = sbrk(size)) == (void *) -1) { - result = NULL; - } else { - mdp->breakval = (char *) mdp->breakval + size; - mdp->top = (char *) mdp->top + size; - } - return (result); -} diff --git a/src/xbt_modinter.h b/src/xbt_modinter.h index 2e2d829fc2..9e0892fa4c 100644 --- a/src/xbt_modinter.h +++ b/src/xbt_modinter.h @@ -31,7 +31,7 @@ void xbt_trp_postexit(void); void xbt_datadesc_preinit(void); void xbt_datadesc_postexit(void); -void mmalloc_preinit(void); +void *mmalloc_preinit(void); void mmalloc_postexit(void);