malloc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. We provide a bit of initial static space so that
  19. programs can do a little mallocing without mmaping in more space. */
  20. HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
  21. struct heap __malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
  22. #if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_SBRK)
  23. /* A lock protecting our use of sbrk. */
  24. malloc_mutex_t __malloc_sbrk_lock;
  25. #endif /* MALLOC_USE_LOCKING && MALLOC_USE_SBRK */
  26. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  27. /* A list of all malloc_mmb structures describing blocsk that
  28. malloc has mmapped, ordered by the block address. */
  29. struct malloc_mmb *__malloc_mmapped_blocks = 0;
  30. /* A heap used for allocating malloc_mmb structures. We could allocate
  31. them from the main heap, but that tends to cause heap fragmentation in
  32. annoying ways. */
  33. HEAP_DECLARE_STATIC_FREE_AREA (initial_mmb_fa, 48); /* enough for 3 mmbs */
  34. struct heap __malloc_mmb_heap = HEAP_INIT_WITH_FA (initial_mmb_fa);
  35. #endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  36. static void *
  37. malloc_from_heap (size_t size, struct heap *heap)
  38. {
  39. void *mem;
  40. MALLOC_DEBUG (1, "malloc: %d bytes", size);
  41. /* Include extra space to record the size of the allocated block. */
  42. size += MALLOC_HEADER_SIZE;
  43. __heap_lock (heap);
  44. /* First try to get memory that's already in our heap. */
  45. mem = __heap_alloc (heap, &size);
  46. __heap_unlock (heap);
  47. if (unlikely (! mem))
  48. /* We couldn't allocate from the heap, so grab some more
  49. from the system, add it to the heap, and try again. */
  50. {
  51. /* If we're trying to allocate a block bigger than the default
  52. MALLOC_HEAP_EXTEND_SIZE, make sure we get enough to hold it. */
  53. void *block;
  54. size_t block_size
  55. = (size < MALLOC_HEAP_EXTEND_SIZE
  56. ? MALLOC_HEAP_EXTEND_SIZE
  57. : MALLOC_ROUND_UP_TO_PAGE_SIZE (size));
  58. /* Allocate the new heap block. */
  59. #ifdef MALLOC_USE_SBRK
  60. __malloc_lock_sbrk ();
  61. /* Use sbrk we can, as it's faster than mmap, and guarantees
  62. contiguous allocation. */
  63. block = sbrk (block_size);
  64. if (likely (block != (void *)-1))
  65. {
  66. /* Because sbrk can return results of arbitrary
  67. alignment, align the result to a MALLOC_ALIGNMENT boundary. */
  68. long aligned_block = MALLOC_ROUND_UP ((long)block, MALLOC_ALIGNMENT);
  69. if (block != (void *)aligned_block)
  70. /* Have to adjust. We should only have to actually do this
  71. the first time (after which we will have aligned the brk
  72. correctly). */
  73. {
  74. /* Move the brk to reflect the alignment; our next allocation
  75. should start on exactly the right alignment. */
  76. sbrk (aligned_block - (long)block);
  77. block = (void *)aligned_block;
  78. }
  79. }
  80. __malloc_unlock_sbrk ();
  81. #else /* !MALLOC_USE_SBRK */
  82. /* Otherwise, use mmap. */
  83. block = mmap (0, block_size, PROT_READ | PROT_WRITE,
  84. MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  85. #endif /* MALLOC_USE_SBRK */
  86. if (likely (block != (void *)-1))
  87. {
  88. #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  89. struct malloc_mmb *mmb, *prev_mmb, *new_mmb;
  90. #endif
  91. MALLOC_DEBUG (1, "adding system memroy to heap: 0x%lx - 0x%lx (%d bytes)",
  92. (long)block, (long)block + block_size, block_size);
  93. /* Get back the heap lock. */
  94. __heap_lock (heap);
  95. /* Put BLOCK into the heap. */
  96. __heap_free (heap, block, block_size);
  97. MALLOC_DEBUG_INDENT (-1);
  98. /* Try again to allocate. */
  99. mem = __heap_alloc (heap, &size);
  100. __heap_unlock (heap);
  101. #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  102. /* Insert a record of BLOCK in sorted order into the
  103. __malloc_mmapped_blocks list. */
  104. for (prev_mmb = 0, mmb = __malloc_mmapped_blocks;
  105. mmb;
  106. prev_mmb = mmb, mmb = mmb->next)
  107. if (block < mmb->mem)
  108. break;
  109. new_mmb = malloc_from_heap (sizeof *new_mmb, &__malloc_mmb_heap);
  110. new_mmb->next = mmb;
  111. new_mmb->mem = block;
  112. new_mmb->size = block_size;
  113. if (prev_mmb)
  114. prev_mmb->next = new_mmb;
  115. else
  116. __malloc_mmapped_blocks = new_mmb;
  117. MALLOC_MMB_DEBUG (0, "new mmb at 0x%x: 0x%x[%d]",
  118. (unsigned)new_mmb,
  119. (unsigned)new_mmb->mem, block_size);
  120. #endif /* !MALLOC_USE_SBRK && __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  121. }
  122. }
  123. if (likely (mem))
  124. /* Record the size of the block and get the user address. */
  125. {
  126. mem = MALLOC_SETUP (mem, size);
  127. MALLOC_DEBUG (-1, "malloc: returning 0x%lx (base:0x%lx, total_size:%ld)",
  128. (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
  129. }
  130. else
  131. MALLOC_DEBUG (-1, "malloc: returning 0");
  132. return mem;
  133. }
  134. void *
  135. malloc (size_t size)
  136. {
  137. #ifdef MALLOC_DEBUGGING
  138. static int debugging_initialized = 0;
  139. if (! debugging_initialized)
  140. {
  141. debugging_initialized = 1;
  142. __malloc_debug_init ();
  143. }
  144. if (__malloc_check)
  145. __heap_check (&__malloc_heap, "malloc");
  146. #endif
  147. return malloc_from_heap (size, &__malloc_heap);
  148. }