realloc.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "malloc.h"
  16. #include "heap.h"
  17. void *
  18. realloc (void *mem, size_t new_size)
  19. {
  20. if (! mem)
  21. return malloc (new_size);
  22. else
  23. {
  24. char *base_mem = MALLOC_BASE (mem);
  25. size_t size = MALLOC_SIZE (mem);
  26. /* Include extra space to record the size of the allocated block.
  27. Also make sure that we're dealing in a multiple of the heap
  28. allocation unit (SIZE is already guaranteed to be so).*/
  29. new_size = HEAP_ADJUST_SIZE (new_size + MALLOC_HEADER_SIZE);
  30. MALLOC_DEBUG ("realloc: 0x%lx, %d (base = 0x%lx, total_size = %d)\n",
  31. (long)mem, new_size, (long)base_mem, size);
  32. if (new_size > size)
  33. /* Grow the block. */
  34. {
  35. size_t extra = new_size - size;
  36. __malloc_lock ();
  37. extra = __heap_alloc_at (&__malloc_heap, base_mem + size, extra);
  38. __malloc_unlock ();
  39. if (extra)
  40. /* Record the changed size. */
  41. MALLOC_SET_SIZE (mem, new_size);
  42. else
  43. /* Our attempts to extend MEM in place failed, just
  44. allocate-and-copy. */
  45. {
  46. void *new_mem = malloc (new_size - MALLOC_HEADER_SIZE);
  47. if (new_mem)
  48. {
  49. memcpy (new_mem, mem, size - MALLOC_HEADER_SIZE);
  50. free (mem);
  51. }
  52. mem = new_mem;
  53. }
  54. }
  55. else if (new_size + MALLOC_REALLOC_MIN_FREE_SIZE <= size)
  56. /* Shrink the block. */
  57. {
  58. __malloc_lock ();
  59. __heap_free (&__malloc_heap, base_mem + new_size, size - new_size);
  60. __malloc_unlock ();
  61. MALLOC_SET_SIZE (mem, new_size);
  62. }
  63. if (mem)
  64. MALLOC_DEBUG (" realloc: returning 0x%lx"
  65. " (base:0x%lx, total_size:%d)\n",
  66. (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
  67. return mem;
  68. }
  69. }