1/* Change the size of a block allocated by `malloc'.
2   Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3		     Written May 1989 by Mike Haertel.
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with this library; see the file COPYING.LIB.  If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, 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#ifndef	_MALLOC_INTERNAL
24#define _MALLOC_INTERNAL
25#include <malloc.h>
26#endif
27
28#define min(A, B) ((A) < (B) ? (A) : (B))
29
30/* Debugging hook for realloc.  */
31__ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
32
33/* Resize the given region to the new size, returning a pointer
34   to the (possibly moved) region.  This is optimized for speed;
35   some benchmarks seem to indicate that greater compactness is
36   achieved by unconditionally allocating and copying to a
37   new region.  This module has incestuous knowledge of the
38   internals of both free and malloc. */
39__ptr_t
40realloc (ptr, size)
41     __ptr_t ptr;
42     size_t size;
43{
44  __ptr_t result;
45  int type;
46  size_t block, blocks, oldlimit;
47
48  if (size == 0)
49    {
50      free (ptr);
51      return malloc (0);
52    }
53  else if (ptr == NULL)
54    return malloc (size);
55
56  if (__realloc_hook != NULL)
57    return (*__realloc_hook) (ptr, size);
58
59  block = BLOCK (ptr);
60
61  type = _heapinfo[block].busy.type;
62  switch (type)
63    {
64    case 0:
65      /* Maybe reallocate a large block to a small fragment.  */
66      if (size <= BLOCKSIZE / 2)
67	{
68	  result = malloc (size);
69	  if (result != NULL)
70	    {
71	      memcpy (result, ptr, size);
72	      free (ptr);
73	      return result;
74	    }
75	}
76
77      /* The new size is a large allocation as well;
78	 see if we can hold it in place. */
79      blocks = BLOCKIFY (size);
80      if (blocks < _heapinfo[block].busy.info.size)
81	{
82	  /* The new size is smaller; return
83	     excess memory to the free list. */
84	  _heapinfo[block + blocks].busy.type = 0;
85	  _heapinfo[block + blocks].busy.info.size
86	    = _heapinfo[block].busy.info.size - blocks;
87	  _heapinfo[block].busy.info.size = blocks;
88	  free (ADDRESS (block + blocks));
89	  result = ptr;
90	}
91      else if (blocks == _heapinfo[block].busy.info.size)
92	/* No size change necessary.  */
93	result = ptr;
94      else
95	{
96	  /* Won't fit, so allocate a new region that will.
97	     Free the old region first in case there is sufficient
98	     adjacent free space to grow without moving. */
99	  blocks = _heapinfo[block].busy.info.size;
100	  /* Prevent free from actually returning memory to the system.  */
101	  oldlimit = _heaplimit;
102	  _heaplimit = 0;
103	  free (ptr);
104	  _heaplimit = oldlimit;
105	  result = malloc (size);
106	  if (result == NULL)
107	    {
108	      /* Now we're really in trouble.  We have to unfree
109		 the thing we just freed.  Unfortunately it might
110		 have been coalesced with its neighbors.  */
111	      if (_heapindex == block)
112	        (void) malloc (blocks * BLOCKSIZE);
113	      else
114		{
115		  __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
116		  (void) malloc (blocks * BLOCKSIZE);
117		  free (previous);
118		}
119	      return NULL;
120	    }
121	  if (ptr != result)
122	    memmove (result, ptr, blocks * BLOCKSIZE);
123	}
124      break;
125
126    default:
127      /* Old size is a fragment; type is logarithm
128	 to base two of the fragment size.  */
129      if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
130	/* The new size is the same kind of fragment.  */
131	result = ptr;
132      else
133	{
134	  /* The new size is different; allocate a new space,
135	     and copy the lesser of the new size and the old. */
136	  result = malloc (size);
137	  if (result == NULL)
138	    return NULL;
139	  memcpy (result, ptr, min (size, (size_t) 1 << type));
140	  free (ptr);
141	}
142      break;
143    }
144
145  return result;
146}
147