realloc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. This is a version (aka dlmalloc) of malloc/free/realloc written by
  3. Doug Lea and released to the public domain. Use, modify, and
  4. redistribute this code without permission or acknowledgement in any
  5. way you wish. Send questions, comments, complaints, performance
  6. data, etc to dl@cs.oswego.edu
  7. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  8. Note: There may be an updated version of this malloc obtainable at
  9. ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  10. Check before installing!
  11. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  12. */
  13. #include "malloc.h"
  14. /* ------------------------------ realloc ------------------------------ */
  15. void* realloc(void* oldmem, size_t bytes)
  16. {
  17. mstate av;
  18. size_t nb; /* padded request size */
  19. mchunkptr oldp; /* chunk corresponding to oldmem */
  20. size_t oldsize; /* its size */
  21. mchunkptr newp; /* chunk to return */
  22. size_t newsize; /* its size */
  23. void* newmem; /* corresponding user mem */
  24. mchunkptr next; /* next contiguous chunk after oldp */
  25. mchunkptr remainder; /* extra space at end of newp */
  26. unsigned long remainder_size; /* its size */
  27. mchunkptr bck; /* misc temp for linking */
  28. mchunkptr fwd; /* misc temp for linking */
  29. unsigned long copysize; /* bytes to copy */
  30. unsigned int ncopies; /* size_t words to copy */
  31. size_t* s; /* copy source */
  32. size_t* d; /* copy destination */
  33. void *retval;
  34. /* Check for special cases. */
  35. if (! oldmem)
  36. return malloc(bytes);
  37. if (! bytes) {
  38. free (oldmem);
  39. return NULL;
  40. }
  41. checked_request2size(bytes, nb);
  42. __MALLOC_LOCK;
  43. av = get_malloc_state();
  44. oldp = mem2chunk(oldmem);
  45. oldsize = chunksize(oldp);
  46. check_inuse_chunk(oldp);
  47. if (!chunk_is_mmapped(oldp)) {
  48. if ((unsigned long)(oldsize) >= (unsigned long)(nb)) {
  49. /* already big enough; split below */
  50. newp = oldp;
  51. newsize = oldsize;
  52. }
  53. else {
  54. next = chunk_at_offset(oldp, oldsize);
  55. /* Try to expand forward into top */
  56. if (next == av->top &&
  57. (unsigned long)(newsize = oldsize + chunksize(next)) >=
  58. (unsigned long)(nb + MINSIZE)) {
  59. set_head_size(oldp, nb);
  60. av->top = chunk_at_offset(oldp, nb);
  61. set_head(av->top, (newsize - nb) | PREV_INUSE);
  62. retval = chunk2mem(oldp);
  63. goto DONE;
  64. }
  65. /* Try to expand forward into next chunk; split off remainder below */
  66. else if (next != av->top &&
  67. !inuse(next) &&
  68. (unsigned long)(newsize = oldsize + chunksize(next)) >=
  69. (unsigned long)(nb)) {
  70. newp = oldp;
  71. unlink(next, bck, fwd);
  72. }
  73. /* allocate, copy, free */
  74. else {
  75. newmem = malloc(nb - MALLOC_ALIGN_MASK);
  76. if (newmem == 0) {
  77. retval = 0; /* propagate failure */
  78. goto DONE;
  79. }
  80. newp = mem2chunk(newmem);
  81. newsize = chunksize(newp);
  82. /*
  83. Avoid copy if newp is next chunk after oldp.
  84. */
  85. if (newp == next) {
  86. newsize += oldsize;
  87. newp = oldp;
  88. }
  89. else {
  90. /*
  91. Unroll copy of <= 36 bytes (72 if 8byte sizes)
  92. We know that contents have an odd number of
  93. size_t-sized words; minimally 3.
  94. */
  95. copysize = oldsize - (sizeof(size_t));
  96. s = (size_t*)(oldmem);
  97. d = (size_t*)(newmem);
  98. ncopies = copysize / sizeof(size_t);
  99. assert(ncopies >= 3);
  100. if (ncopies > 9)
  101. memcpy(d, s, copysize);
  102. else {
  103. *(d+0) = *(s+0);
  104. *(d+1) = *(s+1);
  105. *(d+2) = *(s+2);
  106. if (ncopies > 4) {
  107. *(d+3) = *(s+3);
  108. *(d+4) = *(s+4);
  109. if (ncopies > 6) {
  110. *(d+5) = *(s+5);
  111. *(d+6) = *(s+6);
  112. if (ncopies > 8) {
  113. *(d+7) = *(s+7);
  114. *(d+8) = *(s+8);
  115. }
  116. }
  117. }
  118. }
  119. free(oldmem);
  120. check_inuse_chunk(newp);
  121. retval = chunk2mem(newp);
  122. goto DONE;
  123. }
  124. }
  125. }
  126. /* If possible, free extra space in old or extended chunk */
  127. assert((unsigned long)(newsize) >= (unsigned long)(nb));
  128. remainder_size = newsize - nb;
  129. if (remainder_size < MINSIZE) { /* not enough extra to split off */
  130. set_head_size(newp, newsize);
  131. set_inuse_bit_at_offset(newp, newsize);
  132. }
  133. else { /* split remainder */
  134. remainder = chunk_at_offset(newp, nb);
  135. set_head_size(newp, nb);
  136. set_head(remainder, remainder_size | PREV_INUSE);
  137. /* Mark remainder as inuse so free() won't complain */
  138. set_inuse_bit_at_offset(remainder, remainder_size);
  139. free(chunk2mem(remainder));
  140. }
  141. check_inuse_chunk(newp);
  142. retval = chunk2mem(newp);
  143. goto DONE;
  144. }
  145. /*
  146. Handle mmap cases
  147. */
  148. else {
  149. size_t offset = oldp->prev_size;
  150. size_t pagemask = av->pagesize - 1;
  151. char *cp;
  152. unsigned long sum;
  153. /* Note the extra (sizeof(size_t)) overhead */
  154. newsize = (nb + offset + (sizeof(size_t)) + pagemask) & ~pagemask;
  155. /* don't need to remap if still within same page */
  156. if (oldsize == newsize - offset) {
  157. retval = oldmem;
  158. goto DONE;
  159. }
  160. cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
  161. if (cp != (char*)MORECORE_FAILURE) {
  162. newp = (mchunkptr)(cp + offset);
  163. set_head(newp, (newsize - offset)|IS_MMAPPED);
  164. assert(aligned_OK(chunk2mem(newp)));
  165. assert((newp->prev_size == offset));
  166. /* update statistics */
  167. sum = av->mmapped_mem += newsize - oldsize;
  168. if (sum > (unsigned long)(av->max_mmapped_mem))
  169. av->max_mmapped_mem = sum;
  170. sum += av->sbrked_mem;
  171. if (sum > (unsigned long)(av->max_total_mem))
  172. av->max_total_mem = sum;
  173. retval = chunk2mem(newp);
  174. goto DONE;
  175. }
  176. /* Note the extra (sizeof(size_t)) overhead. */
  177. if ((unsigned long)(oldsize) >= (unsigned long)(nb + (sizeof(size_t))))
  178. newmem = oldmem; /* do nothing */
  179. else {
  180. /* Must alloc, copy, free. */
  181. newmem = malloc(nb - MALLOC_ALIGN_MASK);
  182. if (newmem != 0) {
  183. memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
  184. free(oldmem);
  185. }
  186. }
  187. retval = newmem;
  188. }
  189. DONE:
  190. __MALLOC_UNLOCK;
  191. return retval;
  192. }
  193. /* glibc compatibilty */
  194. weak_alias(realloc, __libc_realloc)