realloc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "malloc.h"
  18. #ifdef __UCLIBC_HAS_THREADS__
  19. #include <pthread.h>
  20. extern pthread_mutex_t __malloclock;
  21. # define LOCK pthread_mutex_lock(&__malloclock)
  22. # define UNLOCK pthread_mutex_unlock(&__malloclock);
  23. #else
  24. # define LOCK
  25. # define UNLOCK
  26. #endif
  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. */
  32. void * realloc (void *ptr, size_t size)
  33. {
  34. void *result, *previous;
  35. size_t block, blocks, type;
  36. size_t oldlimit;
  37. if (!ptr)
  38. return malloc(size);
  39. if (!size) {
  40. LOCK;
  41. __free_unlocked(ptr);
  42. result = __malloc_unlocked(0);
  43. UNLOCK;
  44. return(result);
  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. UNLOCK;
  57. return result;
  58. }
  59. /* The new size is a large allocation as well; see if
  60. we can hold it in place. */
  61. blocks = BLOCKIFY(size);
  62. if (blocks < _heapinfo[block].busy.info.size) {
  63. /* The new size is smaller; return excess memory
  64. to the free list. */
  65. _heapinfo[block + blocks].busy.type = 0;
  66. _heapinfo[block + blocks].busy.info.size
  67. = _heapinfo[block].busy.info.size - blocks;
  68. _heapinfo[block].busy.info.size = blocks;
  69. __free_unlocked(ADDRESS(block + blocks));
  70. UNLOCK;
  71. return ptr;
  72. } else if (blocks == _heapinfo[block].busy.info.size) {
  73. /* No size change necessary. */
  74. UNLOCK;
  75. return ptr;
  76. } else {
  77. /* Won't fit, so allocate a new region that will. Free
  78. the old region first in case there is sufficient adjacent
  79. free space to grow without moving. */
  80. blocks = _heapinfo[block].busy.info.size;
  81. /* Prevent free from actually returning memory to the system. */
  82. oldlimit = _heaplimit;
  83. _heaplimit = 0;
  84. __free_unlocked(ptr);
  85. _heaplimit = oldlimit;
  86. result = __malloc_unlocked(size);
  87. if (!result) {
  88. /* Now we're really in trouble. We have to unfree
  89. the thing we just freed. Unfortunately it might
  90. have been coalesced with its neighbors. */
  91. if (_heapindex == block)
  92. __malloc_unlocked(blocks * BLOCKSIZE);
  93. else {
  94. previous = __malloc_unlocked((block - _heapindex) * BLOCKSIZE);
  95. __malloc_unlocked(blocks * BLOCKSIZE);
  96. __free_unlocked(previous);
  97. }
  98. UNLOCK;
  99. return NULL;
  100. }
  101. if (ptr != result)
  102. memmove(result, ptr, blocks * BLOCKSIZE);
  103. UNLOCK;
  104. return result;
  105. }
  106. break;
  107. default:
  108. /* Old size is a fragment; type is logarithm to base two of
  109. the fragment size. */
  110. if ((size > 1 << (type - 1)) && (size <= 1 << type)) {
  111. /* New size is the same kind of fragment. */
  112. UNLOCK;
  113. return ptr;
  114. }
  115. else {
  116. /* New size is different; allocate a new space, and copy
  117. the lesser of the new size and the old. */
  118. result = __malloc_unlocked(size);
  119. if (!result) {
  120. UNLOCK;
  121. return NULL;
  122. }
  123. memcpy(result, ptr, MIN(size, (size_t)(1 << type)));
  124. __free_unlocked(ptr);
  125. UNLOCK;
  126. return result;
  127. }
  128. break;
  129. }
  130. UNLOCK;
  131. }