1 /* Support for sbrk() regions.
2 Copyright 1992, 2000 Free Software Foundation, Inc.
3 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com */
5 /* Copyright (c) 2010. The SimGrid Team.
6 * All rights reserved. */
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. */
11 #include <unistd.h> /* Prototypes for sbrk (maybe) */
13 #include <string.h> /* Prototypes for memcpy, memmove, memset, etc */
15 #include "mmprivate.h"
17 static void* sbrk_morecore (struct mdesc *mdp, int size);
18 #if NEED_DECLARATION_SBRK
19 extern void* sbrk (int size);
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. */
28 struct mdesc *__mmalloc_default_mdp;
30 /* Use sbrk() to get more core. */
33 sbrk_morecore (mdp, size)
39 if ((result = sbrk (size)) == (void*) -1)
45 mdp -> breakval = (char*)mdp -> breakval + size;
46 mdp -> top = (char*)mdp -> top + size;
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.
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.
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. */
69 __mmalloc_sbrk_init (void)
75 adj = RESIDUAL (base, BLOCKSIZE);
78 sbrk (BLOCKSIZE - adj);
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);