realloc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* realloc.c - C standard library routine.
  2. Copyright (c) 1989, 1993 Michael J. Haertel
  3. You may redistribute this library under the terms of the
  4. GNU Library General Public License (version 2 or any later
  5. version) as published by the Free Software Foundation.
  6. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED
  7. WARRANTY. IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR
  8. WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
  9. SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */
  10. #include <limits.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "malloc.h"
  15. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  16. /* Resize the given region to the new size, returning a pointer
  17. to the (possibly moved) region. This is optimized for speed;
  18. some benchmarks seem to indicate that greater compactness is
  19. achieved by unconditionally allocating and copying to a
  20. new region. */
  21. void *
  22. realloc (void *ptr, size_t size)
  23. {
  24. void *result, *previous;
  25. int block, blocks, type;
  26. int oldlimit;
  27. if (!ptr)
  28. return malloc(size);
  29. if (!size) {
  30. free(ptr);
  31. return malloc(0);
  32. }
  33. block = BLOCK(ptr);
  34. switch (type = _heapinfo[block].busy.type) {
  35. case 0:
  36. /* Maybe reallocate a large block to a small fragment. */
  37. if (size <= BLOCKSIZE / 2) {
  38. if ((result = malloc(size)) != NULL) {
  39. memcpy(result, ptr, size);
  40. #if 1
  41. free(ptr);
  42. #else
  43. _free_internal(ptr);
  44. #endif
  45. }
  46. return result;
  47. }
  48. /* The new size is a large allocation as well; see if
  49. we can hold it in place. */
  50. blocks = BLOCKIFY(size);
  51. if (blocks < _heapinfo[block].busy.info.size) {
  52. /* The new size is smaller; return excess memory
  53. to the free list. */
  54. _heapinfo[block + blocks].busy.type = 0;
  55. _heapinfo[block + blocks].busy.info.size
  56. = _heapinfo[block].busy.info.size - blocks;
  57. _heapinfo[block].busy.info.size = blocks;
  58. #if 1
  59. free(ADDRESS(block + blocks));
  60. #else
  61. _free_internal(ADDRESS(block + blocks));
  62. #endif
  63. return ptr;
  64. } else if (blocks == _heapinfo[block].busy.info.size)
  65. /* No size change necessary. */
  66. return ptr;
  67. else {
  68. /* Won't fit, so allocate a new region that will. Free
  69. the old region first in case there is sufficient adjacent
  70. free space to grow without moving. */
  71. blocks = _heapinfo[block].busy.info.size;
  72. /* Prevent free from actually returning memory to the system. */
  73. oldlimit = _heaplimit;
  74. _heaplimit = 0;
  75. #if 1
  76. free(ptr);
  77. #else
  78. _free_internal(ptr);
  79. #endif
  80. _heaplimit = oldlimit;
  81. result = malloc(size);
  82. if (!result) {
  83. /* Now we're really in trouble. We have to unfree
  84. the thing we just freed. Unfortunately it might
  85. have been coalesced with its neighbors. */
  86. if (_heapindex == block)
  87. malloc(blocks * BLOCKSIZE);
  88. else {
  89. previous = malloc((block - _heapindex) * BLOCKSIZE);
  90. malloc(blocks * BLOCKSIZE);
  91. #if 1
  92. free(previous);
  93. #else
  94. _free_internal(previous);
  95. #endif
  96. }
  97. return NULL;
  98. }
  99. if (ptr != result)
  100. memmove(result, ptr, blocks * BLOCKSIZE);
  101. return result;
  102. }
  103. break;
  104. default:
  105. /* Old size is a fragment; type is logarithm to base two of
  106. the fragment size. */
  107. if ((size > 1 << (type - 1)) && (size <= 1 << type))
  108. /* New size is the same kind of fragment. */
  109. return ptr;
  110. else {
  111. /* New size is different; allocate a new space, and copy
  112. the lesser of the new size and the old. */
  113. result = malloc(size);
  114. if (!result)
  115. return NULL;
  116. memcpy(result, ptr, MIN(size, 1 << type));
  117. free(ptr);
  118. return result;
  119. }
  120. break;
  121. }
  122. }