]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for sbrk() regions. |
9365c12c | 2 | Copyright 1992, 2000 Free Software Foundation, Inc. |
c906108c SS |
3 | Contributed by Fred Fish at Cygnus Support. [email protected] |
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 | ||
9365c12c AC |
22 | #ifdef HAVE_UNISTD_H |
23 | #include <unistd.h> /* Prototypes for sbrk (maybe) */ | |
24 | #endif | |
c906108c SS |
25 | #include <string.h> /* Prototypes for memcpy, memmove, memset, etc */ |
26 | ||
27 | #include "mmprivate.h" | |
28 | ||
29 | static PTR sbrk_morecore PARAMS ((struct mdesc *, int)); | |
dfcd3bfb | 30 | #if NEED_DECLARATION_SBRK |
c906108c | 31 | extern PTR sbrk PARAMS ((int)); |
dfcd3bfb | 32 | #endif |
c906108c SS |
33 | |
34 | /* The mmalloc() package can use a single implicit malloc descriptor | |
35 | for mmalloc/mrealloc/mfree operations which do not supply an explicit | |
36 | descriptor. For these operations, sbrk() is used to obtain more core | |
37 | from the system, or return core. This allows mmalloc() to provide | |
38 | backwards compatibility with the non-mmap'd version. */ | |
39 | ||
40 | struct mdesc *__mmalloc_default_mdp; | |
41 | ||
42 | /* Use sbrk() to get more core. */ | |
43 | ||
44 | static PTR | |
45 | sbrk_morecore (mdp, size) | |
46 | struct mdesc *mdp; | |
47 | int size; | |
48 | { | |
49 | PTR result; | |
50 | ||
51 | if ((result = sbrk (size)) == (PTR) -1) | |
52 | { | |
53 | result = NULL; | |
54 | } | |
55 | else | |
56 | { | |
57 | mdp -> breakval += size; | |
58 | mdp -> top += size; | |
59 | } | |
60 | return (result); | |
61 | } | |
62 | ||
63 | /* Initialize the default malloc descriptor if this is the first time | |
64 | a request has been made to use the default sbrk'd region. | |
65 | ||
66 | Since no alignment guarantees are made about the initial value returned | |
67 | by sbrk, test the initial value and (if necessary) sbrk enough additional | |
68 | memory to start off with alignment to BLOCKSIZE. We actually only need | |
69 | it aligned to an alignment suitable for any object, so this is overkill. | |
70 | But at most it wastes just part of one BLOCKSIZE chunk of memory and | |
71 | minimizes portability problems by avoiding us having to figure out | |
72 | what the actual minimal alignment is. The rest of the malloc code | |
73 | avoids this as well, by always aligning to the minimum of the requested | |
74 | size rounded up to a power of two, or to BLOCKSIZE. | |
75 | ||
76 | Note that we are going to use some memory starting at this initial sbrk | |
77 | address for the sbrk region malloc descriptor, which is a struct, so the | |
78 | base address must be suitably aligned. */ | |
79 | ||
80 | struct mdesc * | |
81 | __mmalloc_sbrk_init () | |
82 | { | |
83 | PTR base; | |
84 | unsigned int adj; | |
85 | ||
86 | base = sbrk (0); | |
87 | adj = RESIDUAL (base, BLOCKSIZE); | |
88 | if (adj != 0) | |
89 | { | |
90 | sbrk (BLOCKSIZE - adj); | |
91 | base = sbrk (0); | |
92 | } | |
93 | __mmalloc_default_mdp = (struct mdesc *) sbrk (sizeof (struct mdesc)); | |
94 | memset ((char *) __mmalloc_default_mdp, 0, sizeof (struct mdesc)); | |
95 | __mmalloc_default_mdp -> morecore = sbrk_morecore; | |
96 | __mmalloc_default_mdp -> base = base; | |
97 | __mmalloc_default_mdp -> breakval = __mmalloc_default_mdp -> top = sbrk (0); | |
98 | __mmalloc_default_mdp -> fd = -1; | |
99 | return (__mmalloc_default_mdp); | |
100 | } | |
101 | ||
102 |