malloc.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * libc/stdlib/malloc/malloc.c -- malloc 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 <unistd.h>
  15. #include <sys/mman.h>
  16. #include "malloc.h"
  17. #include "heap.h"
  18. /* The malloc heap. */
  19. struct heap __malloc_heap = HEAP_INIT;
  20. void *malloc (size_t size)
  21. {
  22. void *mem;
  23. MALLOC_DEBUG ("malloc: %d bytes\n", size);
  24. /* Include extra space to record the size of the allocated block. */
  25. size += MALLOC_ROUND_UP (sizeof (size_t), MALLOC_ALIGNMENT);
  26. mem = __heap_alloc (&__malloc_heap, &size);
  27. if (! mem)
  28. /* We couldn't allocate from the heap, so get some more memory
  29. from the system, add it to the heap, and try again. */
  30. {
  31. /* If we're trying to allocate a block bigger than the default
  32. MALLOC_HEAP_EXTEND_SIZE, make sure we get enough to hold it. */
  33. size_t block_size
  34. = (size < MALLOC_HEAP_EXTEND_SIZE
  35. ? MALLOC_HEAP_EXTEND_SIZE
  36. : MALLOC_ROUND_UP_TO_PAGE_SIZE (size));
  37. /* Allocate the new heap block. */
  38. #ifdef MALLOC_USE_SBRK
  39. /* Use sbrk we can, as it's faster than mmap, and guarantees
  40. contiguous allocation. */
  41. void *block = sbrk (block_size);
  42. #else
  43. /* Otherwise, use mmap. */
  44. void *block = mmap (0, block_size, PROT_READ | PROT_WRITE,
  45. MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  46. #endif
  47. if (block != (void *)-1)
  48. {
  49. #ifdef MALLOC_USE_SBRK
  50. /* Because sbrk can return results of arbitrary
  51. alignment, align the result to a MALLOC_ALIGNMENT boundary. */
  52. long aligned_block = MALLOC_ROUND_UP ((long)block, MALLOC_ALIGNMENT);
  53. if (block != (void *)aligned_block)
  54. /* Have to adjust. We should only have to actually do this
  55. the first time (after which we will have aligned the brk
  56. correctly). */
  57. {
  58. /* Move the brk to reflect the alignment; our next allocation
  59. should start on exactly the right alignment. */
  60. sbrk (aligned_block - (long)block);
  61. block = (void *)aligned_block;
  62. }
  63. #endif /* MALLOC_USE_SBRK */
  64. MALLOC_DEBUG (" adding memory: 0x%lx - 0x%lx (%d bytes)\n",
  65. (long)block, (long)block + block_size, block_size);
  66. /* Put BLOCK into the heap. */
  67. __heap_free (&__malloc_heap, block, block_size);
  68. /* Try again to allocate. */
  69. mem = __heap_alloc (&__malloc_heap, &size);
  70. }
  71. }
  72. if (mem)
  73. /* Record the size of this block just before the returned address. */
  74. {
  75. *(size_t *)mem = size;
  76. mem += MALLOC_ALIGNMENT;
  77. MALLOC_DEBUG (" malloc: returning 0x%lx (base:0x%lx, total_size:%d)\n",
  78. (long)mem, (long)mem - MALLOC_ALIGNMENT, size);
  79. }
  80. return mem;
  81. }