realloc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * libc/stdlib/malloc/realloc.c -- realloc function
  3. *
  4. * Copyright (C) 2002 NEC Corporation
  5. * Copyright (C) 2002 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU Lesser
  8. * General Public License. See the file COPYING.LIB in the main
  9. * directory of this archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include "malloc.h"
  17. #include "heap.h"
  18. void *
  19. realloc (void *mem, size_t new_size)
  20. {
  21. size_t size;
  22. char *base_mem;
  23. if (! mem)
  24. return malloc (new_size);
  25. /* Check for special cases. */
  26. if (! new_size)
  27. {
  28. free (mem);
  29. return NULL;
  30. }
  31. /* This matches the check in malloc() */
  32. if (unlikely(((unsigned long)new_size > (unsigned long)(MALLOC_HEADER_SIZE*-2))))
  33. return NULL;
  34. /* Normal realloc. */
  35. base_mem = MALLOC_BASE (mem);
  36. size = MALLOC_SIZE (mem);
  37. /* Include extra space to record the size of the allocated block.
  38. Also make sure that we're dealing in a multiple of the heap
  39. allocation unit (SIZE is already guaranteed to be so).*/
  40. new_size = HEAP_ADJUST_SIZE (new_size + MALLOC_HEADER_SIZE);
  41. if (new_size < sizeof (struct heap_free_area))
  42. /* Because we sometimes must use a freed block to hold a free-area node,
  43. we must make sure that every allocated block can hold one. */
  44. new_size = HEAP_ADJUST_SIZE (sizeof (struct heap_free_area));
  45. MALLOC_DEBUG (1, "realloc: 0x%lx, %d (base = 0x%lx, total_size = %d)",
  46. (long)mem, new_size, (long)base_mem, size);
  47. if (new_size > size)
  48. /* Grow the block. */
  49. {
  50. size_t extra = new_size - size;
  51. __heap_lock (&__malloc_heap_lock);
  52. extra = __heap_alloc_at (&__malloc_heap, base_mem + size, extra);
  53. __heap_unlock (&__malloc_heap_lock);
  54. if (extra)
  55. /* Record the changed size. */
  56. MALLOC_SET_SIZE (base_mem, size + extra);
  57. else
  58. /* Our attempts to extend MEM in place failed, just
  59. allocate-and-copy. */
  60. {
  61. void *new_mem = malloc (new_size - MALLOC_HEADER_SIZE);
  62. if (new_mem)
  63. {
  64. memcpy (new_mem, mem, size - MALLOC_HEADER_SIZE);
  65. free (mem);
  66. }
  67. mem = new_mem;
  68. }
  69. }
  70. else if (new_size + MALLOC_REALLOC_MIN_FREE_SIZE <= size)
  71. /* Shrink the block. */
  72. {
  73. __heap_lock (&__malloc_heap_lock);
  74. __heap_free (&__malloc_heap, base_mem + new_size, size - new_size);
  75. __heap_unlock (&__malloc_heap_lock);
  76. MALLOC_SET_SIZE (base_mem, new_size);
  77. }
  78. if (mem)
  79. MALLOC_DEBUG (-1, "realloc: returning 0x%lx (base:0x%lx, total_size:%d)",
  80. (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
  81. else
  82. MALLOC_DEBUG (-1, "realloc: returning 0");
  83. return mem;
  84. }