realloc.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /* Check for special cases. */
  24. if (! new_size)
  25. {
  26. free (mem);
  27. return malloc (new_size);
  28. }
  29. if (! mem)
  30. return malloc (new_size);
  31. /* Normal realloc. */
  32. base_mem = MALLOC_BASE (mem);
  33. size = MALLOC_SIZE (mem);
  34. /* Include extra space to record the size of the allocated block.
  35. Also make sure that we're dealing in a multiple of the heap
  36. allocation unit (SIZE is already guaranteed to be so).*/
  37. new_size = HEAP_ADJUST_SIZE (new_size + MALLOC_HEADER_SIZE);
  38. if (new_size < sizeof (struct heap_free_area))
  39. /* Because we sometimes must use a freed block to hold a free-area node,
  40. we must make sure that every allocated block can hold one. */
  41. new_size = HEAP_ADJUST_SIZE (sizeof (struct heap_free_area));
  42. MALLOC_DEBUG (1, "realloc: 0x%lx, %d (base = 0x%lx, total_size = %d)",
  43. (long)mem, new_size, (long)base_mem, size);
  44. if (new_size > size)
  45. /* Grow the block. */
  46. {
  47. size_t extra = new_size - size;
  48. __heap_lock (&__malloc_heap_lock);
  49. extra = __heap_alloc_at (&__malloc_heap, base_mem + size, extra);
  50. __heap_unlock (&__malloc_heap_lock);
  51. if (extra)
  52. /* Record the changed size. */
  53. MALLOC_SET_SIZE (base_mem, size + extra);
  54. else
  55. /* Our attempts to extend MEM in place failed, just
  56. allocate-and-copy. */
  57. {
  58. void *new_mem = malloc (new_size - MALLOC_HEADER_SIZE);
  59. if (new_mem)
  60. {
  61. memcpy (new_mem, mem, size - MALLOC_HEADER_SIZE);
  62. free (mem);
  63. }
  64. mem = new_mem;
  65. }
  66. }
  67. else if (new_size + MALLOC_REALLOC_MIN_FREE_SIZE <= size)
  68. /* Shrink the block. */
  69. {
  70. __heap_lock (&__malloc_heap_lock);
  71. __heap_free (&__malloc_heap, base_mem + new_size, size - new_size);
  72. __heap_unlock (&__malloc_heap_lock);
  73. MALLOC_SET_SIZE (base_mem, new_size);
  74. }
  75. if (mem)
  76. MALLOC_DEBUG (-1, "realloc: returning 0x%lx (base:0x%lx, total_size:%d)",
  77. (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
  78. else
  79. MALLOC_DEBUG (-1, "realloc: returning 0");
  80. return mem;
  81. }