realloc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Change the size of a block allocated by `malloc'.
  2. Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3. Written May 1989 by Mike Haertel.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. Cambridge, MA 02139, USA.
  16. The author may be reached (Email) at the address mike@ai.mit.edu,
  17. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  18. #ifndef _MALLOC_INTERNAL
  19. #define _MALLOC_INTERNAL
  20. #include <malloc.h>
  21. #endif
  22. #if (defined (MEMMOVE_MISSING) || \
  23. !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
  24. /* Snarfed directly from Emacs src/dispnew.c:
  25. XXX Should use system bcopy if it handles overlap. */
  26. #ifndef emacs
  27. /* Like bcopy except never gets confused by overlap. */
  28. static void
  29. safe_bcopy (from, to, size)
  30. char *from, *to;
  31. int size;
  32. {
  33. if (size <= 0 || from == to)
  34. return;
  35. /* If the source and destination don't overlap, then bcopy can
  36. handle it. If they do overlap, but the destination is lower in
  37. memory than the source, we'll assume bcopy can handle that. */
  38. if (to < from || from + size <= to)
  39. bcopy (from, to, size);
  40. /* Otherwise, we'll copy from the end. */
  41. else
  42. {
  43. register char *endf = from + size;
  44. register char *endt = to + size;
  45. /* If TO - FROM is large, then we should break the copy into
  46. nonoverlapping chunks of TO - FROM bytes each. However, if
  47. TO - FROM is small, then the bcopy function call overhead
  48. makes this not worth it. The crossover point could be about
  49. anywhere. Since I don't think the obvious copy loop is too
  50. bad, I'm trying to err in its favor. */
  51. if (to - from < 64)
  52. {
  53. do
  54. *--endt = *--endf;
  55. while (endf != from);
  56. }
  57. else
  58. {
  59. for (;;)
  60. {
  61. endt -= (to - from);
  62. endf -= (to - from);
  63. if (endt < to)
  64. break;
  65. bcopy (endf, endt, to - from);
  66. }
  67. /* If SIZE wasn't a multiple of TO - FROM, there will be a
  68. little left over. The amount left over is
  69. (endt + (to - from)) - to, which is endt - from. */
  70. bcopy (from, to, endt - from);
  71. }
  72. }
  73. }
  74. #endif /* Not emacs. */
  75. #define memmove(to, from, size) safe_bcopy ((from), (to), (size))
  76. #endif
  77. #define min(A, B) ((A) < (B) ? (A) : (B))
  78. /* Debugging hook for realloc. */
  79. __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
  80. /* Resize the given region to the new size, returning a pointer
  81. to the (possibly moved) region. This is optimized for speed;
  82. some benchmarks seem to indicate that greater compactness is
  83. achieved by unconditionally allocating and copying to a
  84. new region. This module has incestuous knowledge of the
  85. internals of both free and malloc. */
  86. __ptr_t
  87. realloc (ptr, size)
  88. __ptr_t ptr;
  89. __malloc_size_t size;
  90. {
  91. __ptr_t result;
  92. int type;
  93. __malloc_size_t block, blocks, oldlimit;
  94. if (size == 0)
  95. {
  96. free (ptr);
  97. return malloc (0);
  98. }
  99. else if (ptr == NULL)
  100. return malloc (size);
  101. if (__realloc_hook != NULL)
  102. return (*__realloc_hook) (ptr, size);
  103. block = BLOCK (ptr);
  104. type = _heapinfo[block].busy.type;
  105. switch (type)
  106. {
  107. case 0:
  108. /* Maybe reallocate a large block to a small fragment. */
  109. if (size <= BLOCKSIZE / 2)
  110. {
  111. result = malloc (size);
  112. if (result != NULL)
  113. {
  114. memcpy (result, ptr, size);
  115. _free_internal (ptr);
  116. return result;
  117. }
  118. }
  119. /* The new size is a large allocation as well;
  120. see if we can hold it in place. */
  121. blocks = BLOCKIFY (size);
  122. if (blocks < _heapinfo[block].busy.info.size)
  123. {
  124. /* The new size is smaller; return
  125. excess memory to the free list. */
  126. _heapinfo[block + blocks].busy.type = 0;
  127. _heapinfo[block + blocks].busy.info.size
  128. = _heapinfo[block].busy.info.size - blocks;
  129. _heapinfo[block].busy.info.size = blocks;
  130. /* We have just created a new chunk by splitting a chunk in two.
  131. Now we will free this chunk; increment the statistics counter
  132. so it doesn't become wrong when _free_internal decrements it. */
  133. ++_chunks_used;
  134. _free_internal (ADDRESS (block + blocks));
  135. result = ptr;
  136. }
  137. else if (blocks == _heapinfo[block].busy.info.size)
  138. /* No size change necessary. */
  139. result = ptr;
  140. else
  141. {
  142. /* Won't fit, so allocate a new region that will.
  143. Free the old region first in case there is sufficient
  144. adjacent free space to grow without moving. */
  145. blocks = _heapinfo[block].busy.info.size;
  146. /* Prevent free from actually returning memory to the system. */
  147. oldlimit = _heaplimit;
  148. _heaplimit = 0;
  149. _free_internal (ptr);
  150. _heaplimit = oldlimit;
  151. result = malloc (size);
  152. if (result == NULL)
  153. {
  154. /* Now we're really in trouble. We have to unfree
  155. the thing we just freed. Unfortunately it might
  156. have been coalesced with its neighbors. */
  157. if (_heapindex == block)
  158. (void) malloc (blocks * BLOCKSIZE);
  159. else
  160. {
  161. __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
  162. (void) malloc (blocks * BLOCKSIZE);
  163. _free_internal (previous);
  164. }
  165. return NULL;
  166. }
  167. if (ptr != result)
  168. memmove (result, ptr, blocks * BLOCKSIZE);
  169. }
  170. break;
  171. default:
  172. /* Old size is a fragment; type is logarithm
  173. to base two of the fragment size. */
  174. if (size > (__malloc_size_t) (1 << (type - 1)) &&
  175. size <= (__malloc_size_t) (1 << type))
  176. /* The new size is the same kind of fragment. */
  177. result = ptr;
  178. else
  179. {
  180. /* The new size is different; allocate a new space,
  181. and copy the lesser of the new size and the old. */
  182. result = malloc (size);
  183. if (result == NULL)
  184. return NULL;
  185. memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
  186. free (ptr);
  187. }
  188. break;
  189. }
  190. return result;
  191. }