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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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. */
23 #include <string.h> /* Prototypes for memcpy, memmove, memset, etc */
25 #include "mmprivate.h"
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. */
35 mrealloc (void *md, void *ptr, size_t size)
40 size_t block, blocks, oldlimit;
45 return (mmalloc (md, 0));
49 return (mmalloc (md, size));
54 if (mdp -> mrealloc_hook != NULL)
56 return ((*mdp -> mrealloc_hook) (md, ptr, size));
61 type = mdp -> heapinfo[block].busy.type;
65 /* Maybe reallocate a large block to a small fragment. */
66 if (size <= BLOCKSIZE / 2)
68 result = mmalloc (md, size);
71 memcpy (result, ptr, size);
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 < mdp -> heapinfo[block].busy.info.size)
82 /* The new size is smaller; return 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)
92 /* No size change necessary. */
97 /* Won't fit, so allocate a new region that will.
98 Free the old region first in case there is sufficient
99 adjacent free space to grow without moving. */
100 blocks = mdp -> heapinfo[block].busy.info.size;
101 /* Prevent free from actually returning memory to the system. */
102 oldlimit = mdp -> heaplimit;
103 mdp -> heaplimit = 0;
105 mdp -> heaplimit = oldlimit;
106 result = mmalloc (md, size);
109 mmalloc (md, blocks * BLOCKSIZE);
114 memmove (result, ptr, blocks * BLOCKSIZE);
120 /* Old size is a fragment; type is logarithm
121 to base two of the fragment size. */
122 if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
124 /* The new size is the same kind of fragment. */
129 /* The new size is different; allocate a new space,
130 and copy the lesser of the new size and the old. */
131 result = mmalloc (md, size);
136 memcpy (result, ptr, MIN (size, (size_t) 1 << type));
145 /* Useless prototype to make gcc happy */
146 void *realloc (void *ptr, size_t size);
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). */
155 realloc (void *ptr, size_t size)
159 result = mrealloc (NULL, ptr, size);