00001 /* @(#)root/clib:$Id: detach.c 20882 2007-11-19 11:31:26Z rdm $ */ 00002 /* Author: */ 00003 00004 /* Finish access to a mmap'd malloc managed region. 00005 Copyright 1992 Free Software Foundation, Inc. 00006 00007 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com 00008 00009 This file is part of the GNU C Library. 00010 00011 The GNU C Library is free software; you can redistribute it and/or 00012 modify it under the terms of the GNU Library General Public License as 00013 published by the Free Software Foundation; either version 2 of the 00014 License, or (at your option) any later version. 00015 00016 The GNU C Library is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 Library General Public License for more details. 00020 00021 You should have received a copy of the GNU Library General Public 00022 License along with the GNU C Library; see the file COPYING.LIB. If 00023 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00024 Boston, MA 02111-1307, USA. */ 00025 00026 #include <sys/types.h> 00027 #include <fcntl.h> /* After sys/types.h, at least for dpx/2. */ 00028 #include "mmprivate.h" 00029 00030 /* Terminate access to a mmalloc managed region by unmapping all memory pages 00031 associated with the region, and closing the file descriptor if it is one 00032 that we opened. 00033 00034 Returns NULL on success. 00035 00036 Returns the malloc descriptor on failure, which can subsequently be used 00037 for further action, such as obtaining more information about the nature of 00038 the failure by examining the preserved errno value. 00039 00040 Note that the malloc descriptor that we are using is currently located in 00041 region we are about to unmap, so we first make a local copy of it on the 00042 stack and use the copy. */ 00043 00044 PTR 00045 mmalloc_detach (md) 00046 PTR md; 00047 { 00048 struct mdesc mtemp; 00049 00050 if (md != NULL) 00051 { 00052 00053 mtemp = *(struct mdesc *) md; 00054 00055 /* Now unmap all the pages associated with this region by asking for a 00056 negative increment equal to the current size of the region. */ 00057 00058 if ((mtemp.morecore (&mtemp, mtemp.base - mtemp.breakval)) == NULL) 00059 { 00060 /* Update the original malloc descriptor with any changes */ 00061 /* *(struct mdesc *) md = mtemp; don't update, just unmapped (rdm) */ 00062 ; 00063 } 00064 else 00065 { 00066 if (mtemp.flags & MMALLOC_DEVZERO) 00067 { 00068 #ifndef WIN32 00069 close (mtemp.fd); 00070 #else 00071 CloseHandle(mtemp.fd); 00072 #endif 00073 } 00074 md = NULL; 00075 } 00076 } 00077 00078 return (md); 00079 }