00001 /* @(#)root/clib:$Id: sbrksup.c 20882 2007-11-19 11:31:26Z rdm $ */ 00002 /* Author: */ 00003 00004 /* Support for sbrk() regions. 00005 Copyright 1992 Free Software Foundation, Inc. 00006 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com 00007 00008 This file is part of the GNU C Library. 00009 00010 The GNU C Library is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU Library General Public License as 00012 published by the Free Software Foundation; either version 2 of the 00013 License, or (at your option) any later version. 00014 00015 The GNU C Library is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 Library General Public License for more details. 00019 00020 You should have received a copy of the GNU Library General Public 00021 License along with the GNU C Library; see the file COPYING.LIB. If 00022 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00023 Boston, MA 02111-1307, USA. */ 00024 00025 #include <string.h> /* Prototypes for memcpy, memmove, memset, etc */ 00026 00027 #include "mmprivate.h" 00028 00029 #ifndef NO_SBRK_MALLOC 00030 00031 extern PTR sbrk (); 00032 00033 /* The mmalloc() package can use a single implicit malloc descriptor 00034 for mmalloc/mrealloc/mfree operations which do not supply an explicit 00035 descriptor. For these operations, sbrk() is used to obtain more core 00036 from the system, or return core. This allows mmalloc() to provide 00037 backwards compatibility with the non-mmap'd version. */ 00038 00039 struct mdesc *__mmalloc_default_mdp; 00040 00041 /* Use sbrk() to get more core. */ 00042 00043 static PTR 00044 sbrk_morecore (mdp, size) 00045 struct mdesc *mdp; 00046 int size; 00047 { 00048 PTR result; 00049 00050 if ((result = sbrk (size)) == (PTR) -1) 00051 { 00052 result = NULL; 00053 } 00054 else 00055 { 00056 mdp -> breakval += size; 00057 mdp -> top += size; 00058 } 00059 return (result); 00060 } 00061 00062 /* Initialize the default malloc descriptor if this is the first time 00063 a request has been made to use the default sbrk'd region. 00064 00065 Since no alignment guarantees are made about the initial value returned 00066 by sbrk, test the initial value and (if necessary) sbrk enough additional 00067 memory to start off with alignment to BLOCKSIZE. We actually only need 00068 it aligned to an alignment suitable for any object, so this is overkill. 00069 But at most it wastes just part of one BLOCKSIZE chunk of memory and 00070 minimizes portability problems by avoiding us having to figure out 00071 what the actual minimal alignment is. The rest of the malloc code 00072 avoids this as well, by always aligning to the minimum of the requested 00073 size rounded up to a power of two, or to BLOCKSIZE. 00074 00075 Note that we are going to use some memory starting at this initial sbrk 00076 address for the sbrk region malloc descriptor, which is a struct, so the 00077 base address must be suitably aligned. */ 00078 00079 struct mdesc * 00080 __mmalloc_sbrk_init () 00081 { 00082 PTR base; 00083 unsigned int adj; 00084 00085 base = sbrk (0); 00086 adj = RESIDUAL (base, BLOCKSIZE); 00087 if (adj != 0) 00088 { 00089 sbrk (BLOCKSIZE - adj); 00090 base = sbrk (0); 00091 } 00092 __mmalloc_default_mdp = (struct mdesc *) sbrk (sizeof (struct mdesc)); 00093 memset ((char *) __mmalloc_default_mdp, 0, sizeof (struct mdesc)); 00094 __mmalloc_default_mdp -> morecore = sbrk_morecore; 00095 __mmalloc_default_mdp -> base = base; 00096 __mmalloc_default_mdp -> breakval = __mmalloc_default_mdp -> top = sbrk (0); 00097 __mmalloc_default_mdp -> fd = -1; 00098 return (__mmalloc_default_mdp); 00099 } 00100 00101 #endif /* NO_SBRK_MALLOC */