home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / mmalloc / mrealloc.c < prev    next >
C/C++ Source or Header  |  1995-08-01  |  5KB  |  161 lines

  1. /* Change the size of a block allocated by `mmalloc'.
  2.    Copyright 1990, 1991 Free Software Foundation
  3.           Written May 1989 by Mike Haertel.
  4.  
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with the GNU C Library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. Boston, MA 02111-1307, USA.
  19.  
  20.    The author may be reached (Email) at the address mike@ai.mit.edu,
  21.    or (US mail) as Mike Haertel c/o Free Software Foundation. */
  22.  
  23. #include <string.h>    /* Prototypes for memcpy, memmove, memset, etc */
  24.  
  25. #include "mmprivate.h"
  26.  
  27. /* Resize the given region to the new size, returning a pointer
  28.    to the (possibly moved) region.  This is optimized for speed;
  29.    some benchmarks seem to indicate that greater compactness is
  30.    achieved by unconditionally allocating and copying to a
  31.    new region.  This module has incestuous knowledge of the
  32.    internals of both mfree and mmalloc. */
  33.  
  34. PTR
  35. mrealloc (md, ptr, size)
  36.   PTR md;
  37.   PTR ptr;
  38.   size_t size;
  39. {
  40.   struct mdesc *mdp;
  41.   PTR result;
  42.   int type;
  43.   size_t block, blocks, oldlimit;
  44.  
  45.   if (size == 0)
  46.     {
  47.       mfree (md, ptr);
  48.       return (mmalloc (md, 0));
  49.     }
  50.   else if (ptr == NULL)
  51.     {
  52.       return (mmalloc (md, size));
  53.     }
  54.  
  55.   mdp = MD_TO_MDP (md);
  56.  
  57.   if (mdp -> mrealloc_hook != NULL)
  58.     {
  59.       return ((*mdp -> mrealloc_hook) (md, ptr, size));
  60.     }
  61.  
  62.   block = BLOCK (ptr);
  63.  
  64.   type = mdp -> heapinfo[block].busy.type;
  65.   switch (type)
  66.     {
  67.     case 0:
  68.       /* Maybe reallocate a large block to a small fragment.  */
  69.       if (size <= BLOCKSIZE / 2)
  70.     {
  71.       result = mmalloc (md, size);
  72.       if (result != NULL)
  73.         {
  74.           memcpy (result, ptr, size);
  75.           mfree (md, ptr);
  76.           return (result);
  77.         }
  78.     }
  79.  
  80.       /* The new size is a large allocation as well;
  81.      see if we can hold it in place. */
  82.       blocks = BLOCKIFY (size);
  83.       if (blocks < mdp -> heapinfo[block].busy.info.size)
  84.     {
  85.       /* The new size is smaller; return excess memory to the free list. */
  86.       mdp -> heapinfo[block + blocks].busy.type = 0;
  87.       mdp -> heapinfo[block + blocks].busy.info.size
  88.         = mdp -> heapinfo[block].busy.info.size - blocks;
  89.       mdp -> heapinfo[block].busy.info.size = blocks;
  90.       mfree (md, ADDRESS (block + blocks));
  91.       result = ptr;
  92.     }
  93.       else if (blocks == mdp -> heapinfo[block].busy.info.size)
  94.     {
  95.       /* No size change necessary.  */
  96.       result = ptr;
  97.     }
  98.       else
  99.     {
  100.       /* Won't fit, so allocate a new region that will.
  101.          Free the old region first in case there is sufficient
  102.          adjacent free space to grow without moving. */
  103.       blocks = mdp -> heapinfo[block].busy.info.size;
  104.       /* Prevent free from actually returning memory to the system.  */
  105.       oldlimit = mdp -> heaplimit;
  106.       mdp -> heaplimit = 0;
  107.       mfree (md, ptr);
  108.       mdp -> heaplimit = oldlimit;
  109.       result = mmalloc (md, size);
  110.       if (result == NULL)
  111.         {
  112.           mmalloc (md, blocks * BLOCKSIZE);
  113.           return (NULL);
  114.         }
  115.       if (ptr != result)
  116.         {
  117.           memmove (result, ptr, blocks * BLOCKSIZE);
  118.         }
  119.     }
  120.       break;
  121.  
  122.     default:
  123.       /* Old size is a fragment; type is logarithm
  124.      to base two of the fragment size.  */
  125.       if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
  126.     {
  127.       /* The new size is the same kind of fragment.  */
  128.       result = ptr;
  129.     }
  130.       else
  131.     {
  132.       /* The new size is different; allocate a new space,
  133.          and copy the lesser of the new size and the old. */
  134.       result = mmalloc (md, size);
  135.       if (result == NULL)
  136.         {
  137.           return (NULL);
  138.         }
  139.       memcpy (result, ptr, MIN (size, (size_t) 1 << type));
  140.       mfree (md, ptr);
  141.     }
  142.       break;
  143.     }
  144.  
  145.   return (result);
  146. }
  147.  
  148. /* When using this package, provide a version of malloc/realloc/free built
  149.    on top of it, so that if we use the default sbrk() region we will not
  150.    collide with another malloc package trying to do the same thing, if
  151.    the application contains any "hidden" calls to malloc/realloc/free (such
  152.    as inside a system library). */
  153.  
  154. PTR
  155. realloc (ptr, size)
  156.   PTR ptr;
  157.   size_t size;
  158. {
  159.   return (mrealloc ((PTR) NULL, ptr, size));
  160. }
  161.