1 /* Finish access to a mmap'd malloc managed region.
2 Copyright 1992 Free Software Foundation, Inc.
4 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
5 This file was then part of the GNU C Library. */
7 /* Copyright (c) 2010. The SimGrid Team.
8 * All rights reserved. */
10 /* This program is free software; you can redistribute it and/or modify it
11 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <unistd.h> /* close */
14 #include <sys/types.h>
15 #include "mmprivate.h"
17 /* Terminate access to a mmalloc managed region, but do not free its content.
18 * This is for example useful for the base region where ldl stores its data
19 * because it leaves the place after us.
21 void mmalloc_detach_no_free(xbt_mheap_t md)
23 struct mdesc *mdp = md;
25 if(--mdp->refcount == 0){
27 sem_destroy(&mdp->sem);
31 /* Terminate access to a mmalloc managed region by unmapping all memory pages
32 associated with the region, and closing the file descriptor if it is one
35 Returns NULL on success.
37 Returns the malloc descriptor on failure, which can subsequently be used
38 for further action, such as obtaining more information about the nature of
39 the failure by examining the preserved errno value.
41 Note that the malloc descriptor that we are using is currently located in
42 region we are about to unmap, so we first make a local copy of it on the
43 stack and use the copy. */
45 void *mmalloc_detach(xbt_mheap_t mdp)
47 struct mdesc mtemp, *mdptemp;
50 /* Remove the heap from the linked list of heaps attached by mmalloc */
51 mdptemp = __mmalloc_default_mdp;
52 while(mdptemp->next_mdesc != mdp )
53 mdptemp = mdptemp->next_mdesc;
55 mdptemp->next_mdesc = mdp->next_mdesc;
57 mmalloc_detach_no_free(mdp);
60 /* Now unmap all the pages associated with this region by asking for a
61 negative increment equal to the current size of the region. */
63 if ((mmorecore(&mtemp,
64 (char *) mtemp.base - (char *) mtemp.breakval)) ==
66 /* Deallocating failed. Update the original malloc descriptor
70 if (mtemp.flags & MMALLOC_DEVZERO) {