Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve the integration of mmalloc and mc_memory into the mess.
[simgrid.git] / src / xbt / mmalloc / sbrk-sup.c
index 0211031..64e249f 100644 (file)
@@ -1,33 +1,23 @@
 /* Support for sbrk() regions.
    Copyright 1992, 2000 Free Software Foundation, Inc.
-   Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
+   Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com */
 
-This file is part of the GNU C Library.
+/* Copyright (c) 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
 
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-The GNU C Library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include <unistd.h>    /* Prototypes for sbrk (maybe) */
 
 #include <string.h>    /* Prototypes for memcpy, memmove, memset, etc */
 
+#include "xbt.h"
 #include "mmprivate.h"
 
-static PTR sbrk_morecore PARAMS ((struct mdesc *, int));
+static void* sbrk_morecore (struct mdesc *mdp, int size);
 #if NEED_DECLARATION_SBRK
-extern PTR sbrk PARAMS ((int));
+extern void* sbrk (int size);
 #endif
 
 /* The mmalloc() package can use a single implicit malloc descriptor
@@ -40,14 +30,14 @@ struct mdesc *__mmalloc_default_mdp;
 
 /* Use sbrk() to get more core. */
 
-static PTR
+static void*
 sbrk_morecore (mdp, size)
   struct mdesc *mdp;
   int size;
 {
-  PTR result;
+  void* result;
 
-  if ((result = sbrk (size)) == (PTR) -1)
+  if ((result = sbrk (size)) == (void*) -1)
     {
       result = NULL;
     }
@@ -59,43 +49,19 @@ sbrk_morecore (mdp, size)
   return (result);
 }
 
-/* Initialize the default malloc descriptor if this is the first time
-   a request has been made to use the default sbrk'd region.
-
-   Since no alignment guarantees are made about the initial value returned
-   by sbrk, test the initial value and (if necessary) sbrk enough additional
-   memory to start off with alignment to BLOCKSIZE.  We actually only need
-   it aligned to an alignment suitable for any object, so this is overkill.
-   But at most it wastes just part of one BLOCKSIZE chunk of memory and
-   minimizes portability problems by avoiding us having to figure out
-   what the actual minimal alignment is.  The rest of the malloc code
-   avoids this as well, by always aligning to the minimum of the requested
-   size rounded up to a power of two, or to BLOCKSIZE.
-
-   Note that we are going to use some memory starting at this initial sbrk
-   address for the sbrk region malloc descriptor, which is a struct, so the
-   base address must be suitably aligned. */
-
-struct mdesc *
-__mmalloc_sbrk_init ()
-{
-  PTR base;
-  unsigned int adj;
+#define HEAP_OFFSET   20480000    /* Safety gap from the heap's break address */
 
-  base = sbrk (0);
-  adj = RESIDUAL (base, BLOCKSIZE);
-  if (adj != 0)
-    {
-      sbrk (BLOCKSIZE - adj);
-      base = sbrk (0);
-    }
-  __mmalloc_default_mdp = (struct mdesc *) sbrk (sizeof (struct mdesc));
-  memset ((char *) __mmalloc_default_mdp, 0, sizeof (struct mdesc));
-  __mmalloc_default_mdp -> morecore = sbrk_morecore;
-  __mmalloc_default_mdp -> base = base;
-  __mmalloc_default_mdp -> breakval = __mmalloc_default_mdp -> top = sbrk (0);
-  __mmalloc_default_mdp -> fd = -1;
-  return (__mmalloc_default_mdp);
+void *mmalloc_get_default_md(void) {
+  return __mmalloc_default_mdp;
 }
 
-
+/* Initialize the default malloc descriptor. */
+#include "xbt_modinter.h"
+void mmalloc_preinit(void) {
+  __mmalloc_default_mdp = mmalloc_attach(-1, (char *)sbrk(0) + HEAP_OFFSET);
+  xbt_assert(__mmalloc_default_mdp != NULL);
+}
+void mmalloc_postexit(void) {
+  /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
+  //  mmalloc_detach(__mmalloc_default_mdp);
+}