malloc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifdef MALLOC_USE_LOCKING
  21. /* A lock protecting the malloc heap. */
  22. malloc_mutex_t __malloc_lock;
  23. # ifdef MALLOC_USE_SBRK
  24. /* A lock protecting our use of sbrk. */
  25. malloc_mutex_t __malloc_sbrk_lock;
  26. # endif /* MALLOC_USE_SBRK */
  27. #endif /* MALLOC_USE_LOCKING */
  28. #ifdef MALLOC_DEBUGGING
  29. int __malloc_debug = 0;
  30. #endif
  31. void *
  32. malloc (size_t size)
  33. {
  34. void *mem;
  35. struct heap *heap = &__malloc_heap;
  36. MALLOC_DEBUG ("malloc: %d bytes\n", size);
  37. /* Include extra space to record the size of the allocated block. */
  38. size += MALLOC_HEADER_SIZE;
  39. __malloc_lock ();
  40. /* First try to get memory that's already in our heap. */
  41. mem = __heap_alloc (heap, &size);
  42. if (unlikely (! mem))
  43. /* We couldn't allocate from the heap, so grab some more
  44. from the system, add it to the heap, and try again. */
  45. {
  46. /* If we're trying to allocate a block bigger than the default
  47. MALLOC_HEAP_EXTEND_SIZE, make sure we get enough to hold it. */
  48. void *block;
  49. size_t block_size
  50. = (size < MALLOC_HEAP_EXTEND_SIZE
  51. ? MALLOC_HEAP_EXTEND_SIZE
  52. : MALLOC_ROUND_UP_TO_PAGE_SIZE (size));
  53. #ifdef MALLOC_USE_SBRK
  54. /* Get the sbrk lock while we've still got the main lock. */
  55. __malloc_lock_sbrk ();
  56. #endif
  57. /* Don't hold the main lock during the syscall, so that small
  58. allocations in a different thread may succeed while we're
  59. blocked. */
  60. __malloc_unlock ();
  61. /* Allocate the new heap block. */
  62. #ifdef MALLOC_USE_SBRK
  63. /* Use sbrk we can, as it's faster than mmap, and guarantees
  64. contiguous allocation. */
  65. block = sbrk (block_size);
  66. if (likely (block != (void *)-1))
  67. {
  68. /* Because sbrk can return results of arbitrary
  69. alignment, align the result to a MALLOC_ALIGNMENT boundary. */
  70. long aligned_block = MALLOC_ROUND_UP ((long)block, MALLOC_ALIGNMENT);
  71. if (block != (void *)aligned_block)
  72. /* Have to adjust. We should only have to actually do this
  73. the first time (after which we will have aligned the brk
  74. correctly). */
  75. {
  76. /* Move the brk to reflect the alignment; our next allocation
  77. should start on exactly the right alignment. */
  78. sbrk (aligned_block - (long)block);
  79. block = (void *)aligned_block;
  80. }
  81. }
  82. __malloc_unlock_sbrk ();
  83. #else /* !MALLOC_USE_SBRK */
  84. /* Otherwise, use mmap. */
  85. block = mmap (0, block_size, PROT_READ | PROT_WRITE,
  86. MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  87. #endif /* MALLOC_USE_SBRK */
  88. /* Get back the main lock. */
  89. __malloc_lock ();
  90. if (likely (block != (void *)-1))
  91. {
  92. MALLOC_DEBUG (" adding memory: 0x%lx - 0x%lx (%d bytes)\n",
  93. (long)block, (long)block + block_size, block_size);
  94. /* Put BLOCK into the heap. */
  95. __heap_free (heap, block, block_size);
  96. /* Try again to allocate. */
  97. mem = __heap_alloc (heap, &size);
  98. }
  99. }
  100. __malloc_unlock ();
  101. if (likely (mem))
  102. /* Record the size of the block and get the user address. */
  103. {
  104. mem = MALLOC_SETUP (mem, size);
  105. MALLOC_DEBUG (" malloc: returning 0x%lx (base:0x%lx, total_size:%d)\n",
  106. (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
  107. }
  108. return mem;
  109. }