Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fight for better integration of mmalloc, mc and xbt
[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 by unmapping all memory pages
18    associated with the region, and closing the file descriptor if it is one
19    that we opened.
20
21    Returns NULL on success.
22
23    Returns the malloc descriptor on failure, which can subsequently be used
24    for further action, such as obtaining more information about the nature of
25    the failure by examining the preserved errno value.
26
27    Note that the malloc descriptor that we are using is currently located in
28    region we are about to unmap, so we first make a local copy of it on the
29    stack and use the copy. */
30
31 void*
32 mmalloc_detach (void *md)
33 {
34   struct mdesc mtemp;
35
36   if (md != NULL)
37     {
38
39       mtemp = *(struct mdesc *) md;
40       xbt_os_mutex_destroy(((struct mdesc*)md)->mutex);
41       
42       /* Now unmap all the pages associated with this region by asking for a
43          negative increment equal to the current size of the region. */
44       
45       if ((mtemp.morecore (&mtemp, (char*)mtemp.base - (char*)mtemp.breakval)) == NULL)
46         {
47           /* Deallocating failed.  Update the original malloc descriptor
48              with any changes */
49           *(struct mdesc *) md = mtemp;
50         }
51       else
52         {
53           if (mtemp.flags & MMALLOC_DEVZERO)
54             {
55               close (mtemp.fd);
56             }
57           md = NULL;
58         }
59     }
60
61   return (md);
62 }