malloc.c 3.6 KB

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