]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - mmalloc/sbrk-sup.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.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 extern PTR sbrk PARAMS ((int));
28
29 /* The mmalloc() package can use a single implicit malloc descriptor
30 for mmalloc/mrealloc/mfree operations which do not supply an explicit
31 descriptor. For these operations, sbrk() is used to obtain more core
32 from the system, or return core. This allows mmalloc() to provide
33 backwards compatibility with the non-mmap'd version. */
34
35 struct mdesc *__mmalloc_default_mdp;
36
37 /* Use sbrk() to get more core. */
38
39 static PTR
40 sbrk_morecore (mdp, size)
41 struct mdesc *mdp;
42 int size;
43 {
44 PTR result;
45
46 if ((result = sbrk (size)) == (PTR) -1)
47 {
48 result = NULL;
49 }
50 else
51 {
52 mdp -> breakval += size;
53 mdp -> top += size;
54 }
55 return (result);
56 }
57
58 /* Initialize the default malloc descriptor if this is the first time
59 a request has been made to use the default sbrk'd region.
60
61 Since no alignment guarantees are made about the initial value returned
62 by sbrk, test the initial value and (if necessary) sbrk enough additional
63 memory to start off with alignment to BLOCKSIZE. We actually only need
64 it aligned to an alignment suitable for any object, so this is overkill.
65 But at most it wastes just part of one BLOCKSIZE chunk of memory and
66 minimizes portability problems by avoiding us having to figure out
67 what the actual minimal alignment is. The rest of the malloc code
68 avoids this as well, by always aligning to the minimum of the requested
69 size rounded up to a power of two, or to BLOCKSIZE.
70
71 Note that we are going to use some memory starting at this initial sbrk
72 address for the sbrk region malloc descriptor, which is a struct, so the
73 base address must be suitably aligned. */
74
75 struct mdesc *
76 __mmalloc_sbrk_init ()
77 {
78 PTR base;
79 unsigned int adj;
80
81 base = sbrk (0);
82 adj = RESIDUAL (base, BLOCKSIZE);
83 if (adj != 0)
84 {
85 sbrk (BLOCKSIZE - adj);
86 base = sbrk (0);
87 }
88 __mmalloc_default_mdp = (struct mdesc *) sbrk (sizeof (struct mdesc));
89 memset ((char *) __mmalloc_default_mdp, 0, sizeof (struct mdesc));
90 __mmalloc_default_mdp -> morecore = sbrk_morecore;
91 __mmalloc_default_mdp -> base = base;
92 __mmalloc_default_mdp -> breakval = __mmalloc_default_mdp -> top = sbrk (0);
93 __mmalloc_default_mdp -> fd = -1;
94 return (__mmalloc_default_mdp);
95 }
96
97