malloc.c 6.0 KB

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