malloc.c 6.0 KB

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