From 506c9d37d4f03855c37f075b5c88c6efe6958ccb Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Thu, 2 Feb 2012 14:23:23 +0100 Subject: [PATCH] group together all functions speaking of module and objects init/fini --- src/xbt/mmalloc/detach.c | 78 ------------ src/xbt/mmalloc/mm.c | 3 +- src/xbt/mmalloc/mm_legacy.c | 77 ------------ src/xbt/mmalloc/{attach.c => mm_module.c} | 142 ++++++++++++++++++++++ 4 files changed, 143 insertions(+), 157 deletions(-) delete mode 100644 src/xbt/mmalloc/detach.c rename src/xbt/mmalloc/{attach.c => mm_module.c} (66%) diff --git a/src/xbt/mmalloc/detach.c b/src/xbt/mmalloc/detach.c deleted file mode 100644 index 6ed13980ae..0000000000 --- a/src/xbt/mmalloc/detach.c +++ /dev/null @@ -1,78 +0,0 @@ -/* Finish access to a 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. */ - -#include /* close */ -#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. - - Returns NULL on success. - - Returns the malloc descriptor on failure, which can subsequently be used - for further action, such as obtaining more information about the nature of - the failure by examining the preserved errno value. - - Note that the malloc descriptor that we are using is currently located in - region we are about to unmap, so we first make a local copy of it on the - stack and use the copy. */ - -void *mmalloc_detach(xbt_mheap_t mdp) -{ - struct mdesc mtemp, *mdptemp; - - if (mdp != NULL) { - /* Remove the heap from the linked list of heaps attached by mmalloc */ - mdptemp = __mmalloc_default_mdp; - while(mdptemp->next_mdesc != mdp ) - mdptemp = mdptemp->next_mdesc; - - mdptemp->next_mdesc = mdp->next_mdesc; - - 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 ((mmorecore(&mtemp, - (char *) mtemp.base - (char *) mtemp.breakval)) == - NULL) { - /* Deallocating failed. Update the original malloc descriptor - with any changes */ - *mdp = mtemp; - } else { - if (mtemp.flags & MMALLOC_DEVZERO) { - close(mtemp.fd); - } - mdp = NULL; - } - } - - return (mdp); -} diff --git a/src/xbt/mmalloc/mm.c b/src/xbt/mmalloc/mm.c index 4d3bbb819b..137f461ab3 100644 --- a/src/xbt/mmalloc/mm.c +++ b/src/xbt/mmalloc/mm.c @@ -21,6 +21,5 @@ #include "mmemalign.c" #include "mrealloc.c" #include "mmorecore.c" -#include "attach.c" -#include "detach.c" +#include "mm_module.c" #include "mm_legacy.c" diff --git a/src/xbt/mmalloc/mm_legacy.c b/src/xbt/mmalloc/mm_legacy.c index edff857ad7..7f898308cb 100644 --- a/src/xbt/mmalloc/mm_legacy.c +++ b/src/xbt/mmalloc/mm_legacy.c @@ -22,8 +22,6 @@ 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" - xbt_mheap_t mmalloc_get_current_heap(void) { return __mmalloc_current_heap; @@ -79,81 +77,6 @@ void free(void *p) } #endif -/* Make sure it works with md==NULL */ - -/* Safety gap from the heap's break address. - * Try to increase this first if you experience strange errors under - * valgrind. */ -#define HEAP_OFFSET (128UL<<20) - -xbt_mheap_t mmalloc_get_default_md(void) -{ - xbt_assert(__mmalloc_default_mdp); - return __mmalloc_default_mdp; -} - -static void mmalloc_fork_prepare(void) -{ - xbt_mheap_t mdp = NULL; - if ((mdp =__mmalloc_default_mdp)){ - while(mdp){ - LOCK(mdp); - if(mdp->fd >= 0){ - mdp->refcount++; - } - mdp = mdp->next_mdesc; - } - } -} - -static void mmalloc_fork_parent(void) -{ - xbt_mheap_t mdp = NULL; - if ((mdp =__mmalloc_default_mdp)){ - while(mdp){ - if(mdp->fd < 0) - UNLOCK(mdp); - mdp = mdp->next_mdesc; - } - } -} - -static void mmalloc_fork_child(void) -{ - struct mdesc* mdp = NULL; - if ((mdp =__mmalloc_default_mdp)){ - while(mdp){ - UNLOCK(mdp); - mdp = mdp->next_mdesc; - } - } -} - -/* Initialize the default malloc descriptor. */ -void *mmalloc_preinit(void) -{ - int res; - 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); - /* Fixme? only the default mdp in protected against forks */ - res = xbt_os_thread_atfork(mmalloc_fork_prepare, - mmalloc_fork_parent, mmalloc_fork_child); - if (res != 0) - 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_detach_no_free(__mmalloc_default_mdp); -} int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void *std_heap_addr){ diff --git a/src/xbt/mmalloc/attach.c b/src/xbt/mmalloc/mm_module.c similarity index 66% rename from src/xbt/mmalloc/attach.c rename to src/xbt/mmalloc/mm_module.c index 7741a0b090..6d1baa6b61 100644 --- a/src/xbt/mmalloc/attach.c +++ b/src/xbt/mmalloc/mm_module.c @@ -29,6 +29,7 @@ Boston, MA 02111-1307, USA. */ #endif #include "mmprivate.h" #include "xbt/ex.h" +#include "xbt_modinter.h" /* declarations of mmalloc_preinit and friends that live here */ #ifndef SEEK_SET #define SEEK_SET 0 @@ -207,3 +208,144 @@ static struct mdesc *reuse(int fd) return (mdp); } + + +/** 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. + + Returns NULL on success. + + Returns the malloc descriptor on failure, which can subsequently be used + for further action, such as obtaining more information about the nature of + the failure by examining the preserved errno value. + + Note that the malloc descriptor that we are using is currently located in + region we are about to unmap, so we first make a local copy of it on the + stack and use the copy. */ + +void *mmalloc_detach(xbt_mheap_t mdp) +{ + struct mdesc mtemp, *mdptemp; + + if (mdp != NULL) { + /* Remove the heap from the linked list of heaps attached by mmalloc */ + mdptemp = __mmalloc_default_mdp; + while(mdptemp->next_mdesc != mdp ) + mdptemp = mdptemp->next_mdesc; + + mdptemp->next_mdesc = mdp->next_mdesc; + + 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 ((mmorecore(&mtemp, + (char *) mtemp.base - (char *) mtemp.breakval)) == + NULL) { + /* Deallocating failed. Update the original malloc descriptor + with any changes */ + *mdp = mtemp; + } else { + if (mtemp.flags & MMALLOC_DEVZERO) { + close(mtemp.fd); + } + mdp = NULL; + } + } + + return (mdp); +} + +/* Safety gap from the heap's break address. + * Try to increase this first if you experience strange errors under + * valgrind. */ +#define HEAP_OFFSET (128UL<<20) + +xbt_mheap_t mmalloc_get_default_md(void) +{ + xbt_assert(__mmalloc_default_mdp); + return __mmalloc_default_mdp; +} + +static void mmalloc_fork_prepare(void) +{ + xbt_mheap_t mdp = NULL; + if ((mdp =__mmalloc_default_mdp)){ + while(mdp){ + LOCK(mdp); + if(mdp->fd >= 0){ + mdp->refcount++; + } + mdp = mdp->next_mdesc; + } + } +} + +static void mmalloc_fork_parent(void) +{ + xbt_mheap_t mdp = NULL; + if ((mdp =__mmalloc_default_mdp)){ + while(mdp){ + if(mdp->fd < 0) + UNLOCK(mdp); + mdp = mdp->next_mdesc; + } + } +} + +static void mmalloc_fork_child(void) +{ + struct mdesc* mdp = NULL; + if ((mdp =__mmalloc_default_mdp)){ + while(mdp){ + UNLOCK(mdp); + mdp = mdp->next_mdesc; + } + } +} + + + +/* Initialize the default malloc descriptor. */ +void *mmalloc_preinit(void) +{ + int res; + 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); + /* Fixme? only the default mdp in protected against forks */ + res = xbt_os_thread_atfork(mmalloc_fork_prepare, + mmalloc_fork_parent, mmalloc_fork_child); + if (res != 0) + 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_detach_no_free(__mmalloc_default_mdp); +} -- 2.20.1