malloc.c 3.6 KB

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