home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / mmalloc / sbrk-sup.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  3KB  |  97 lines

  1. /* Support for sbrk() regions.
  2.    Copyright 1992 Free Software Foundation, Inc.
  3.    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
  4.  
  5. This file is part of the GNU C Library.
  6.  
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11.  
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Library General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public
  18. License along with the GNU C Library; see the file COPYING.LIB.  If
  19. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  20. Cambridge, MA 02139, USA.  */
  21.  
  22. #include <string.h>    /* Prototypes for memcpy, memmove, memset, etc */
  23.  
  24. #include "mmalloc.h"
  25.  
  26. extern PTR sbrk ();
  27.  
  28. /* The mmalloc() package can use a single implicit malloc descriptor
  29.    for mmalloc/mrealloc/mfree operations which do not supply an explicit
  30.    descriptor.  For these operations, sbrk() is used to obtain more core
  31.    from the system, or return core.  This allows mmalloc() to provide
  32.    backwards compatibility with the non-mmap'd version. */
  33.  
  34. struct mdesc *__mmalloc_default_mdp;
  35.  
  36. /* Use sbrk() to get more core. */
  37.  
  38. static PTR
  39. sbrk_morecore (mdp, size)
  40.   struct mdesc *mdp;
  41.   int size;
  42. {
  43.   PTR result;
  44.  
  45.   if ((result = sbrk (size)) == (PTR) -1)
  46.     {
  47.       result = NULL;
  48.     }
  49.   else
  50.     {
  51.       mdp -> breakval += size;
  52.       mdp -> top += size;
  53.     }
  54.   return (result);
  55. }
  56.  
  57. /* Initialize the default malloc descriptor if this is the first time
  58.    a request has been made to use the default sbrk'd region.
  59.  
  60.    Since no alignment guarantees are made about the initial value returned
  61.    by sbrk, test the initial value and (if necessary) sbrk enough additional
  62.    memory to start off with alignment to BLOCKSIZE.  We actually only need
  63.    it aligned to an alignment suitable for any object, so this is overkill.
  64.    But at most it wastes just part of one BLOCKSIZE chunk of memory and
  65.    minimizes portability problems by avoiding us having to figure out
  66.    what the actual minimal alignment is.  The rest of the malloc code
  67.    avoids this as well, by always aligning to the minimum of the requested
  68.    size rounded up to a power of two, or to BLOCKSIZE.
  69.  
  70.    Note that we are going to use some memory starting at this initial sbrk
  71.    address for the sbrk region malloc descriptor, which is a struct, so the
  72.    base address must be suitably aligned. */
  73.  
  74. struct mdesc *
  75. __mmalloc_sbrk_init ()
  76. {
  77.   PTR base;
  78.   unsigned int adj;
  79.  
  80.   base = sbrk (0);
  81.   adj = RESIDUAL (base, BLOCKSIZE);
  82.   if (adj != 0)
  83.     {
  84.       sbrk (BLOCKSIZE - adj);
  85.       base = sbrk (0);
  86.     }
  87.   __mmalloc_default_mdp = (struct mdesc *) sbrk (sizeof (struct mdesc));
  88.   memset ((char *) __mmalloc_default_mdp, 0, sizeof (struct mdesc));
  89.   __mmalloc_default_mdp -> morecore = sbrk_morecore;
  90.   __mmalloc_default_mdp -> base = base;
  91.   __mmalloc_default_mdp -> breakval = __mmalloc_default_mdp -> top = sbrk (0);
  92.   __mmalloc_default_mdp -> fd = -1;
  93.   return (__mmalloc_default_mdp);
  94. }
  95.  
  96.  
  97.