malloc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * libc/stdlib/malloc/malloc.c -- malloc function
  3. *
  4. * Copyright (C) 2002,03 NEC Electronics Corporation
  5. * Copyright (C) 2002,03 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 <errno.h>
  16. #include <sys/mman.h>
  17. /* libc_hidden_proto(mmap) */
  18. /* libc_hidden_proto(sbrk) */
  19. #include "malloc.h"
  20. #include "heap.h"
  21. /* The malloc heap. We provide a bit of initial static space so that
  22. programs can do a little mallocing without mmaping in more space. */
  23. HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
  24. struct heap_free_area *__malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
  25. #ifdef HEAP_USE_LOCKING
  26. malloc_mutex_t __malloc_heap_lock = PTHREAD_MUTEX_INITIALIZER;
  27. #endif
  28. #if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_SBRK)
  29. /* A lock protecting our use of sbrk. */
  30. malloc_mutex_t __malloc_sbrk_lock;
  31. #endif /* MALLOC_USE_LOCKING && MALLOC_USE_SBRK */
  32. #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
  33. /* A list of all malloc_mmb structures describing blocks that
  34. malloc has mmapped, ordered by the block address. */
  35. struct malloc_mmb *__malloc_mmapped_blocks = 0;
  36. /* A heap used for allocating malloc_mmb structures. We could allocate
  37. them from the main heap, but that tends to cause heap fragmentation in
  38. annoying ways. */
  39. HEAP_DECLARE_STATIC_FREE_AREA (initial_mmb_fa, 48); /* enough for 3 mmbs */
  40. struct heap_free_area *__malloc_mmb_heap = HEAP_INIT_WITH_FA (initial_mmb_fa);
  41. #ifdef HEAP_USE_LOCKING
  42. malloc_mutex_t __malloc_mmb_heap_lock = PTHREAD_MUTEX_INITIALIZER;
  43. #endif
  44. #endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  45. #ifdef HEAP_USE_LOCKING
  46. #define malloc_from_heap(size, heap, lck) __malloc_from_heap(size, heap, lck)
  47. #else
  48. #define malloc_from_heap(size, heap, lck) __malloc_from_heap(size, heap)
  49. #endif
  50. static void *
  51. __malloc_from_heap (size_t size, struct heap_free_area **heap
  52. #ifdef HEAP_USE_LOCKING
  53. , malloc_mutex_t *heap_lock
  54. #endif
  55. )
  56. {
  57. void *mem;
  58. MALLOC_DEBUG (1, "malloc: %d bytes", size);
  59. /* Include extra space to record the size of the allocated block. */
  60. size += MALLOC_HEADER_SIZE;
  61. __heap_lock (heap_lock);
  62. /* First try to get memory that's already in our heap. */
  63. mem = __heap_alloc (heap, &size);
  64. __heap_unlock (heap_lock);
  65. if (unlikely (! mem))
  66. /* We couldn't allocate from the heap, so grab some more
  67. from the system, add it to the heap, and try again. */
  68. {
  69. /* If we're trying to allocate a block bigger than the default
  70. MALLOC_HEAP_EXTEND_SIZE, make sure we get enough to hold it. */
  71. void *block;
  72. size_t block_size
  73. = (size < MALLOC_HEAP_EXTEND_SIZE
  74. ? MALLOC_HEAP_EXTEND_SIZE
  75. : MALLOC_ROUND_UP_TO_PAGE_SIZE (size));
  76. /* Allocate the new heap block. */
  77. #ifdef MALLOC_USE_SBRK
  78. __malloc_lock_sbrk ();
  79. /* Use sbrk we can, as it's faster than mmap, and guarantees
  80. contiguous allocation. */
  81. block = sbrk (block_size);
  82. if (likely (block != (void *)-1))
  83. {
  84. /* Because sbrk can return results of arbitrary
  85. alignment, align the result to a MALLOC_ALIGNMENT boundary. */
  86. long aligned_block = MALLOC_ROUND_UP ((long)block, MALLOC_ALIGNMENT);
  87. if (block != (void *)aligned_block)
  88. /* Have to adjust. We should only have to actually do this
  89. the first time (after which we will have aligned the brk
  90. correctly). */
  91. {
  92. /* Move the brk to reflect the alignment; our next allocation
  93. should start on exactly the right alignment. */
  94. sbrk (aligned_block - (long)block);
  95. block = (void *)aligned_block;
  96. }
  97. }
  98. __malloc_unlock_sbrk ();
  99. #else /* !MALLOC_USE_SBRK */
  100. /* Otherwise, use mmap. */
  101. #ifdef __ARCH_USE_MMU__
  102. block = mmap ((void *)0, block_size, PROT_READ | PROT_WRITE,
  103. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  104. #else
  105. block = mmap ((void *)0, block_size, PROT_READ | PROT_WRITE,
  106. MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  107. #endif
  108. #endif /* MALLOC_USE_SBRK */
  109. if (likely (block != (void *)-1))
  110. {
  111. #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  112. struct malloc_mmb *mmb, *prev_mmb, *new_mmb;
  113. #endif
  114. MALLOC_DEBUG (1, "adding system memory to heap: 0x%lx - 0x%lx (%d bytes)",
  115. (long)block, (long)block + block_size, block_size);
  116. /* Get back the heap lock. */
  117. __heap_lock (heap_lock);
  118. /* Put BLOCK into the heap. */
  119. __heap_free (heap, block, block_size);
  120. MALLOC_DEBUG_INDENT (-1);
  121. /* Try again to allocate. */
  122. mem = __heap_alloc (heap, &size);
  123. __heap_unlock (heap_lock);
  124. #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  125. /* Insert a record of BLOCK in sorted order into the
  126. __malloc_mmapped_blocks list. */
  127. for (prev_mmb = 0, mmb = __malloc_mmapped_blocks;
  128. mmb;
  129. prev_mmb = mmb, mmb = mmb->next)
  130. if (block < mmb->mem)
  131. break;
  132. new_mmb = malloc_from_heap (sizeof *new_mmb, &__malloc_mmb_heap, &__malloc_mmb_heap_lock);
  133. new_mmb->next = mmb;
  134. new_mmb->mem = block;
  135. new_mmb->size = block_size;
  136. if (prev_mmb)
  137. prev_mmb->next = new_mmb;
  138. else
  139. __malloc_mmapped_blocks = new_mmb;
  140. MALLOC_MMB_DEBUG (0, "new mmb at 0x%x: 0x%x[%d]",
  141. (unsigned)new_mmb,
  142. (unsigned)new_mmb->mem, block_size);
  143. #endif /* !MALLOC_USE_SBRK && __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  144. }
  145. }
  146. if (likely (mem))
  147. /* Record the size of the block and get the user address. */
  148. {
  149. mem = MALLOC_SETUP (mem, size);
  150. MALLOC_DEBUG (-1, "malloc: returning 0x%lx (base:0x%lx, total_size:%ld)",
  151. (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
  152. }
  153. else
  154. MALLOC_DEBUG (-1, "malloc: returning 0");
  155. return mem;
  156. }
  157. void *
  158. malloc (size_t size)
  159. {
  160. void *mem;
  161. #ifdef MALLOC_DEBUGGING
  162. static smallint debugging_initialized;
  163. if (! debugging_initialized)
  164. {
  165. debugging_initialized = 1;
  166. __malloc_debug_init ();
  167. }
  168. if (__malloc_check)
  169. __heap_check (__malloc_heap, "malloc");
  170. #endif
  171. #ifdef __MALLOC_GLIBC_COMPAT__
  172. if (unlikely (size == 0))
  173. size++;
  174. #else
  175. /* Some programs will call malloc (0). Lets be strict and return NULL */
  176. if (unlikely (size == 0))
  177. goto oom;
  178. #endif
  179. /* Check if they are doing something dumb like malloc(-1) */
  180. if (unlikely(((unsigned long)size > (unsigned long)(MALLOC_HEADER_SIZE*-2))))
  181. goto oom;
  182. mem = malloc_from_heap (size, &__malloc_heap, &__malloc_heap_lock);
  183. if (unlikely (!mem))
  184. {
  185. oom:
  186. __set_errno (ENOMEM);
  187. return 0;
  188. }
  189. return mem;
  190. }