Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8df6e5e8276d9e80076d1c1187bf9c9cb0578948
[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(void *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(void *md)
46 {
47   struct mdesc *mdp = (struct mdesc *)md;
48   struct mdesc mtemp, *mdptemp;
49
50   if (mdp != NULL) {
51     /* Remove the heap from the linked list of heaps attached by mmalloc */
52     mdptemp = __mmalloc_default_mdp;
53     while(mdptemp->next_mdesc != mdp )
54       mdptemp = mdptemp->next_mdesc;
55
56     mdptemp->next_mdesc = mdp->next_mdesc;
57
58     mmalloc_detach_no_free(md);
59     mtemp = *(struct mdesc *) md;
60
61     /* Now unmap all the pages associated with this region by asking for a
62        negative increment equal to the current size of the region. */
63
64     if ((mmorecore(&mtemp,
65                         (char *) mtemp.base - (char *) mtemp.breakval)) ==
66         NULL) {
67       /* Deallocating failed.  Update the original malloc descriptor
68          with any changes */
69       *(struct mdesc *) md = mtemp;
70     } else {
71       if (mtemp.flags & MMALLOC_DEVZERO) {
72         close(mtemp.fd);
73       }
74       md = NULL;
75     }
76   }
77
78   return (md);
79 }