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 (md
, ptr
, size
)
43 size_t block
, blocks
, oldlimit
;
48 return (mmalloc (md
, 0));
52 return (mmalloc (md
, size
));
57 if (mdp
-> mrealloc_hook
!= NULL
)
59 return ((*mdp
-> mrealloc_hook
) (md
, ptr
, size
));
64 type
= mdp
-> heapinfo
[block
].busy
.type
;
68 /* Maybe reallocate a large block to a small fragment. */
69 if (size
<= BLOCKSIZE
/ 2)
71 result
= mmalloc (md
, size
);
74 memcpy (result
, ptr
, size
);
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
)
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
));
93 else if (blocks
== mdp
-> heapinfo
[block
].busy
.info
.size
)
95 /* No size change necessary. */
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;
108 mdp
-> heaplimit
= oldlimit
;
109 result
= mmalloc (md
, size
);
112 mmalloc (md
, blocks
* BLOCKSIZE
);
117 memmove (result
, ptr
, blocks
* BLOCKSIZE
);
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
))
127 /* The new size is the same kind of fragment. */
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
);
139 memcpy (result
, ptr
, MIN (size
, (size_t) 1 << type
));
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). */
161 result
= mrealloc ((PTR
) NULL
, ptr
, size
);
This page took 0.03226 seconds and 4 git commands to generate.