Logo AND Algorithmique Numérique Distribuée

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