Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge two files (I'll ignore both of these functions anyway)
[simgrid.git] / src / xbt / mmalloc / detach.c
1 /* Finish access to a mmap'd malloc managed region.
2    Copyright 1992 Free Software Foundation, Inc.
3
4    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
5    This file was then part of the GNU C Library. */
6
7 /* Copyright (c) 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
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. */
12
13 #include <unistd.h>             /* close */
14 #include <sys/types.h>
15 #include "mmprivate.h"
16
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.
20  */
21 void mmalloc_detach_no_free(xbt_mheap_t md)
22 {
23   struct mdesc *mdp = md;
24
25   if(--mdp->refcount == 0){
26     LOCK(mdp) ;
27     sem_destroy(&mdp->sem);
28   }
29 }
30
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
33    that we opened.
34
35    Returns NULL on success.
36
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.
40
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. */
44
45 void *mmalloc_detach(xbt_mheap_t mdp)
46 {
47   struct mdesc mtemp, *mdptemp;
48
49   if (mdp != NULL) {
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;
54
55     mdptemp->next_mdesc = mdp->next_mdesc;
56
57     mmalloc_detach_no_free(mdp);
58     mtemp = *mdp;
59
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. */
62
63     if ((mmorecore(&mtemp,
64                         (char *) mtemp.base - (char *) mtemp.breakval)) ==
65         NULL) {
66       /* Deallocating failed.  Update the original malloc descriptor
67          with any changes */
68       *mdp = mtemp;
69     } else {
70       if (mtemp.flags & MMALLOC_DEVZERO) {
71         close(mtemp.fd);
72       }
73       mdp = NULL;
74     }
75   }
76
77   return (mdp);
78 }