realloc.c 2.6 KB

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