| 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 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include "mmalloc.h" |
| 23 | |
| 24 | /* Terminate access to a mmalloc managed region by unmapping all memory pages |
| 25 | associated with the region, and closing the file descriptor if it is one |
| 26 | that we opened. |
| 27 | |
| 28 | Returns NULL on success. |
| 29 | |
| 30 | Returns the malloc descriptor on failure, which can subsequently be used |
| 31 | for further action, such as obtaining more information about the nature of |
| 32 | the failure by examining the preserved errno value. |
| 33 | |
| 34 | Note that the malloc descriptor that we are using is currently located in |
| 35 | region we are about to unmap, so we first make a local copy of it on the |
| 36 | stack and use the copy. */ |
| 37 | |
| 38 | PTR |
| 39 | mmalloc_detach (md) |
| 40 | void *md; |
| 41 | { |
| 42 | struct mdesc mtemp; |
| 43 | |
| 44 | if (md != NULL) |
| 45 | { |
| 46 | |
| 47 | mtemp = *(struct mdesc *) md; |
| 48 | |
| 49 | /* Now unmap all the pages associated with this region by asking for a |
| 50 | negative increment equal to the current size of the region. */ |
| 51 | |
| 52 | if ((mtemp.morecore (&mtemp, mtemp.base - mtemp.top)) == NULL) |
| 53 | { |
| 54 | /* Update the original malloc descriptor with any changes */ |
| 55 | *(struct mdesc *) md = mtemp; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | if (mtemp.flags & MMALLOC_DEVZERO) |
| 60 | { |
| 61 | (void) close (mtemp.fd); |
| 62 | } |
| 63 | md = NULL; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return (md); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |