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.
5 This 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 This 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 this 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. */
23 #ifndef _MALLOC_INTERNAL
24 #define _MALLOC_INTERNAL
28 #define min(A, B) ((A) < (B) ? (A) : (B))
30 /* Debugging hook for realloc. */
31 __ptr_t (*__realloc_hook
) __P ((__ptr_t __ptr
, size_t __size
));
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. */
46 size_t block
, blocks
, oldlimit
;
56 if (__realloc_hook
!= NULL
)
57 return (*__realloc_hook
) (ptr
, size
);
61 type
= _heapinfo
[block
].busy
.type
;
65 /* Maybe reallocate a large block to a small fragment. */
66 if (size
<= BLOCKSIZE
/ 2)
68 result
= malloc (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
< _heapinfo
[block
].busy
.info
.size
)
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
));
91 else if (blocks
== _heapinfo
[block
].busy
.info
.size
)
92 /* No size change necessary. */
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
;
104 _heaplimit
= oldlimit
;
105 result
= malloc (size
);
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
);
115 __ptr_t previous
= malloc ((block
- _heapindex
) * BLOCKSIZE
);
116 (void) malloc (blocks
* BLOCKSIZE
);
122 memmove (result
, ptr
, blocks
* BLOCKSIZE
);
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. */
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
);
139 memcpy (result
, ptr
, min (size
, (size_t) 1 << type
));