Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
64e249fe15124876359348b285f4c229efe94692
[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 "xbt.h"
16 #include "mmprivate.h"
17
18 static void* sbrk_morecore (struct mdesc *mdp, int size);
19 #if NEED_DECLARATION_SBRK
20 extern void* sbrk (int size);
21 #endif
22
23 /* The mmalloc() package can use a single implicit malloc descriptor
24    for mmalloc/mrealloc/mfree operations which do not supply an explicit
25    descriptor.  For these operations, sbrk() is used to obtain more core
26    from the system, or return core.  This allows mmalloc() to provide
27    backwards compatibility with the non-mmap'd version. */
28
29 struct mdesc *__mmalloc_default_mdp;
30
31 /* Use sbrk() to get more core. */
32
33 static void*
34 sbrk_morecore (mdp, size)
35   struct mdesc *mdp;
36   int size;
37 {
38   void* result;
39
40   if ((result = sbrk (size)) == (void*) -1)
41     {
42       result = NULL;
43     }
44   else
45     {
46       mdp -> breakval = (char*)mdp -> breakval + size;
47       mdp -> top      = (char*)mdp -> top + size;
48     }
49   return (result);
50 }
51
52 #define HEAP_OFFSET   20480000    /* Safety gap from the heap's break address */
53
54 void *mmalloc_get_default_md(void) {
55   return __mmalloc_default_mdp;
56 }
57
58 /* Initialize the default malloc descriptor. */
59 #include "xbt_modinter.h"
60 void mmalloc_preinit(void) {
61   __mmalloc_default_mdp = mmalloc_attach(-1, (char *)sbrk(0) + HEAP_OFFSET);
62   xbt_assert(__mmalloc_default_mdp != NULL);
63 }
64 void mmalloc_postexit(void) {
65   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
66   //  mmalloc_detach(__mmalloc_default_mdp);
67 }