+++ /dev/null
-/* 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 <unistd.h> /* close */
-#include <sys/types.h>
-#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);
-}
#include "mmemalign.c"
#include "mrealloc.c"
#include "mmorecore.c"
-#include "attach.c"
-#include "detach.c"
+#include "mm_module.c"
#include "mm_legacy.c"
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;
}
#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){
#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
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);
+}