malloc.c 6.0 KB

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