Add Sterling Augustine to previous entry (credit where it's due)
[deliverable/binutils-gdb.git] / mmalloc / sbrk-sup.c
index f6efa1746e0454f493f3714839824597609d82cd..93c078bb2d0b2636be1797c2dd2662d626767f6e 100644 (file)
@@ -1,27 +1,35 @@
 /* Support for sbrk() regions.
-   Copyright 1992 Free Software Foundation, Inc.
+   Copyright 1992, 2000 Free Software Foundation, Inc.
    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+This file is part of the GNU C Library.
 
-This program 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 General Public License for more details.
+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.
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+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.  */
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>    /* Prototypes for sbrk (maybe) */
+#endif
 #include <string.h>    /* Prototypes for memcpy, memmove, memset, etc */
 
-#include "mmalloc.h"
+#include "mmprivate.h"
 
-extern PTR sbrk ();
+static PTR sbrk_morecore PARAMS ((struct mdesc *, int));
+#if NEED_DECLARATION_SBRK
+extern PTR sbrk PARAMS ((int));
+#endif
 
 /* The mmalloc() package can use a single implicit malloc descriptor
    for mmalloc/mrealloc/mfree operations which do not supply an explicit
@@ -40,7 +48,11 @@ sbrk_morecore (mdp, size)
 {
   PTR result;
 
-  if ((result = sbrk (size)) != NULL)
+  if ((result = sbrk (size)) == (PTR) -1)
+    {
+      result = NULL;
+    }
+  else
     {
       mdp -> breakval += size;
       mdp -> top += size;
@@ -49,16 +61,37 @@ sbrk_morecore (mdp, size)
 }
 
 /* Initialize the default malloc descriptor if this is the first time
-   a request has been made to use the default sbrk'd region. */
+   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;
 
   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));
-  (void) memset ((char *) __mmalloc_default_mdp, 0, 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);
This page took 0.024218 seconds and 4 git commands to generate.