malloc.c 3.5 KB

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