Logo AND Algorithmique Numérique Distribuée

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