realloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* malloc.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. #define _GNU_SOURCE
  11. #include <features.h>
  12. #include <limits.h>
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include "malloc.h"
  19. #ifdef __UCLIBC_HAS_THREADS__
  20. #include <pthread.h>
  21. extern pthread_mutex_t __malloclock;
  22. # define LOCK pthread_mutex_lock(&__malloclock)
  23. # define UNLOCK pthread_mutex_unlock(&__malloclock);
  24. #else
  25. # define LOCK
  26. # define UNLOCK
  27. #endif
  28. /* Resize the given region to the new size, returning a pointer
  29. to the (possibly moved) region. This is optimized for speed;
  30. some benchmarks seem to indicate that greater compactness is
  31. achieved by unconditionally allocating and copying to a
  32. new region. */
  33. void * realloc (void *ptr, size_t size)
  34. {
  35. void *result, *previous;
  36. size_t block, blocks, type;
  37. size_t oldlimit;
  38. if (!ptr)
  39. return malloc(size);
  40. if (!size) {
  41. LOCK;
  42. __free_unlocked(ptr);
  43. result = __malloc_unlocked(0);
  44. goto alldone;
  45. }
  46. LOCK;
  47. block = BLOCK(ptr);
  48. switch (type = _heapinfo[block].busy.type) {
  49. case 0:
  50. /* Maybe reallocate a large block to a small fragment. */
  51. if (size <= BLOCKSIZE / 2) {
  52. if ((result = __malloc_unlocked(size)) != NULL) {
  53. memcpy(result, ptr, size);
  54. __free_unlocked(ptr);
  55. }
  56. goto alldone;
  57. }
  58. /* The new size is a large allocation as well; see if
  59. we can hold it in place. */
  60. blocks = BLOCKIFY(size);
  61. if (blocks < _heapinfo[block].busy.info.size) {
  62. /* The new size is smaller; return excess memory
  63. to the free list. */
  64. _heapinfo[block + blocks].busy.type = 0;
  65. _heapinfo[block + blocks].busy.info.size
  66. = _heapinfo[block].busy.info.size - blocks;
  67. _heapinfo[block].busy.info.size = blocks;
  68. __free_unlocked(ADDRESS(block + blocks));
  69. result = ptr;
  70. goto alldone;
  71. } else if (blocks == _heapinfo[block].busy.info.size) {
  72. /* No size change necessary. */
  73. result = ptr;
  74. goto alldone;
  75. } else {
  76. /* Won't fit, so allocate a new region that will. Free
  77. the old region first in case there is sufficient adjacent
  78. free space to grow without moving. */
  79. blocks = _heapinfo[block].busy.info.size;
  80. /* Prevent free from actually returning memory to the system. */
  81. oldlimit = _heaplimit;
  82. _heaplimit = 0;
  83. __free_unlocked(ptr);
  84. _heaplimit = oldlimit;
  85. result = __malloc_unlocked(size);
  86. if (!result) {
  87. /* Now we're really in trouble. We have to unfree
  88. the thing we just freed. Unfortunately it might
  89. have been coalesced with its neighbors. */
  90. if (_heapindex == block)
  91. __malloc_unlocked(blocks * BLOCKSIZE);
  92. else {
  93. previous = __malloc_unlocked((block - _heapindex) * BLOCKSIZE);
  94. __malloc_unlocked(blocks * BLOCKSIZE);
  95. __free_unlocked(previous);
  96. }
  97. goto oom;
  98. }
  99. if (ptr != result)
  100. memmove(result, ptr, blocks * BLOCKSIZE);
  101. goto alldone;
  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. result = ptr;
  110. goto alldone;
  111. }
  112. else {
  113. /* New size is different; allocate a new space, and copy
  114. the lesser of the new size and the old. */
  115. result = __malloc_unlocked(size);
  116. if (!result) {
  117. goto oom;
  118. }
  119. memcpy(result, ptr, MIN(size, (size_t)(1 << type)));
  120. __free_unlocked(ptr);
  121. goto alldone;
  122. }
  123. break;
  124. }
  125. alldone:
  126. UNLOCK;
  127. return result;
  128. oom:
  129. UNLOCK;
  130. __set_errno(ENOMEM);
  131. return NULL;
  132. }