Modified Files:
[deliverable/binutils-gdb.git] / mmalloc / mrealloc.c
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., 675 Mass Ave,
18 Cambridge, 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 #include "mmalloc.h"
24
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. */
31
32 PTR
33 mrealloc (md, ptr, size)
34 PTR md;
35 PTR ptr;
36 size_t size;
37 {
38 struct mdesc *mdp;
39 PTR result;
40 int type;
41 size_t block, blocks, oldlimit;
42
43 if (size == 0)
44 {
45 mfree (md, ptr);
46 return (mmalloc (md, 0));
47 }
48 else if (ptr == NULL)
49 return (mmalloc (md, size));
50
51 mdp = MD_TO_MDP (md);
52
53 if (mdp -> mrealloc_hook != NULL)
54 {
55 return ((*mdp -> mrealloc_hook) (md, ptr, size));
56 }
57
58 block = BLOCK(ptr);
59
60 type = mdp -> heapinfo[block].busy.type;
61 switch (type)
62 {
63 case 0:
64 /* Maybe reallocate a large block to a small fragment. */
65 if (size <= BLOCKSIZE / 2)
66 {
67 result = mmalloc (md, size);
68 if (result != NULL)
69 {
70 memcpy (result, ptr, size);
71 mfree (md, ptr);
72 return (result);
73 }
74 }
75
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)
80 {
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));
88 result = ptr;
89 }
90 else if (blocks == mdp -> heapinfo[block].busy.info.size)
91 /* No size change necessary. */
92 result = ptr;
93 else
94 {
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;
102 mfree (md, ptr);
103 mdp -> heaplimit = oldlimit;
104 result = mmalloc (md, size);
105 if (result == NULL)
106 {
107 (void) mmalloc (md, blocks * BLOCKSIZE);
108 return NULL;
109 }
110 if (ptr != result)
111 memmove (result, ptr, blocks * BLOCKSIZE);
112 }
113 break;
114
115 default:
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. */
120 result = ptr;
121 else
122 {
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);
126 if (result == NULL)
127 return (NULL);
128 memcpy (result, ptr, MIN(size, (size_t) 1 << type));
129 mfree (md, ptr);
130 }
131 break;
132 }
133
134 return (result);
135 }
136
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).
142
143 NOTE: Defining our own copy of this breaks ANSI conformance. */
144
145 PTR
146 realloc (ptr, size)
147 PTR ptr;
148 size_t size;
149 {
150 return (mrealloc ((void *) NULL, ptr, size));
151 }
This page took 0.031343 seconds and 4 git commands to generate.