1 /* Change the size of a block allocated by `mmalloc'.
2 Copyright 1990, 1991 Free Software Foundation
3 Written May 1989 by Mike Haertel.
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.
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.
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., 675 Mass Ave,
18 Cambridge, MA 02139, USA.
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. */
25 /* Resize the given region to the new size, returning a pointer
26 to the (possibly moved) region. This is optimized for speed;
27 some benchmarks seem to indicate that greater compactness is
28 achieved by unconditionally allocating and copying to a
29 new region. This module has incestuous knowledge of the
30 internals of both mfree and mmalloc. */
33 mrealloc (md, ptr, size)
41 size_t block, blocks, oldlimit;
46 return (mmalloc (md, 0));
49 return (mmalloc (md, size));
53 if (mdp -> mrealloc_hook != NULL)
55 return ((*mdp -> mrealloc_hook) (md, ptr, size));
60 type = mdp -> heapinfo[block].busy.type;
64 /* Maybe reallocate a large block to a small fragment. */
65 if (size <= BLOCKSIZE / 2)
67 result = mmalloc (md, size);
70 memcpy (result, ptr, size);
76 /* The new size is a large allocation as well;
77 see if we can hold it in place. */
78 blocks = BLOCKIFY(size);
79 if (blocks < mdp -> heapinfo[block].busy.info.size)
81 /* The new size is smaller; return
82 excess memory to the free list. */
83 mdp -> heapinfo[block + blocks].busy.type = 0;
84 mdp -> heapinfo[block + blocks].busy.info.size
85 = mdp -> heapinfo[block].busy.info.size - blocks;
86 mdp -> heapinfo[block].busy.info.size = blocks;
87 mfree (md, ADDRESS(block + blocks));
90 else if (blocks == mdp -> heapinfo[block].busy.info.size)
91 /* No size change necessary. */
95 /* Won't fit, so allocate a new region that will.
96 Free the old region first in case there is sufficient
97 adjacent free space to grow without moving. */
98 blocks = mdp -> heapinfo[block].busy.info.size;
99 /* Prevent free from actually returning memory to the system. */
100 oldlimit = mdp -> heaplimit;
101 mdp -> heaplimit = 0;
103 mdp -> heaplimit = oldlimit;
104 result = mmalloc (md, size);
107 (void) mmalloc (md, blocks * BLOCKSIZE);
111 memmove (result, ptr, blocks * BLOCKSIZE);
116 /* Old size is a fragment; type is logarithm
117 to base two of the fragment size. */
118 if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
119 /* The new size is the same kind of fragment. */
123 /* The new size is different; allocate a new space,
124 and copy the lesser of the new size and the old. */
125 result = mmalloc (md, size);
128 memcpy (result, ptr, MIN(size, (size_t) 1 << type));
137 /* When using this package, provide a version of malloc/realloc/free built
138 on top of it, so that if we use the default sbrk() region we will not
139 collide with another malloc package trying to do the same thing, if
140 the application contains any "hidden" calls to malloc/realloc/free (such
141 as inside a system library).
143 NOTE: Defining our own copy of this breaks ANSI conformance. */
150 return (mrealloc ((void *) NULL, ptr, size));