import gdb-2000-02-04 snapshot
[external/binutils.git] / mmalloc / sbrk-sup.c
1 /* Support for sbrk() regions.
2    Copyright 1992 Free Software Foundation, Inc.
3    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
4
5 This file is part of the GNU C Library.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB.  If
19 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include <string.h>     /* Prototypes for memcpy, memmove, memset, etc */
23
24 #include "mmprivate.h"
25
26 static PTR sbrk_morecore PARAMS ((struct mdesc *, int));
27 #if NEED_DECLARATION_SBRK
28 extern PTR sbrk PARAMS ((int));
29 #endif
30
31 /* The mmalloc() package can use a single implicit malloc descriptor
32    for mmalloc/mrealloc/mfree operations which do not supply an explicit
33    descriptor.  For these operations, sbrk() is used to obtain more core
34    from the system, or return core.  This allows mmalloc() to provide
35    backwards compatibility with the non-mmap'd version. */
36
37 struct mdesc *__mmalloc_default_mdp;
38
39 /* Use sbrk() to get more core. */
40
41 static PTR
42 sbrk_morecore (mdp, size)
43   struct mdesc *mdp;
44   int size;
45 {
46   PTR result;
47
48   if ((result = sbrk (size)) == (PTR) -1)
49     {
50       result = NULL;
51     }
52   else
53     {
54       mdp -> breakval += size;
55       mdp -> top += size;
56     }
57   return (result);
58 }
59
60 /* Initialize the default malloc descriptor if this is the first time
61    a request has been made to use the default sbrk'd region.
62
63    Since no alignment guarantees are made about the initial value returned
64    by sbrk, test the initial value and (if necessary) sbrk enough additional
65    memory to start off with alignment to BLOCKSIZE.  We actually only need
66    it aligned to an alignment suitable for any object, so this is overkill.
67    But at most it wastes just part of one BLOCKSIZE chunk of memory and
68    minimizes portability problems by avoiding us having to figure out
69    what the actual minimal alignment is.  The rest of the malloc code
70    avoids this as well, by always aligning to the minimum of the requested
71    size rounded up to a power of two, or to BLOCKSIZE.
72
73    Note that we are going to use some memory starting at this initial sbrk
74    address for the sbrk region malloc descriptor, which is a struct, so the
75    base address must be suitably aligned. */
76
77 struct mdesc *
78 __mmalloc_sbrk_init ()
79 {
80   PTR base;
81   unsigned int adj;
82
83   base = sbrk (0);
84   adj = RESIDUAL (base, BLOCKSIZE);
85   if (adj != 0)
86     {
87       sbrk (BLOCKSIZE - adj);
88       base = sbrk (0);
89     }
90   __mmalloc_default_mdp = (struct mdesc *) sbrk (sizeof (struct mdesc));
91   memset ((char *) __mmalloc_default_mdp, 0, sizeof (struct mdesc));
92   __mmalloc_default_mdp -> morecore = sbrk_morecore;
93   __mmalloc_default_mdp -> base = base;
94   __mmalloc_default_mdp -> breakval = __mmalloc_default_mdp -> top = sbrk (0);
95   __mmalloc_default_mdp -> fd = -1;
96   return (__mmalloc_default_mdp);
97 }
98
99