malloc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. block = mmap (0, block_size, PROT_READ | PROT_WRITE,
  85. MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  86. #endif /* MALLOC_USE_SBRK */
  87. if (likely (block != (void *)-1))
  88. {
  89. #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  90. struct malloc_mmb *mmb, *prev_mmb, *new_mmb;
  91. #endif
  92. MALLOC_DEBUG (1, "adding system memroy to heap: 0x%lx - 0x%lx (%d bytes)",
  93. (long)block, (long)block + block_size, block_size);
  94. /* Get back the heap lock. */
  95. __heap_lock (heap);
  96. /* Put BLOCK into the heap. */
  97. __heap_free (heap, block, block_size);
  98. MALLOC_DEBUG_INDENT (-1);
  99. /* Try again to allocate. */
  100. mem = __heap_alloc (heap, &size);
  101. __heap_unlock (heap);
  102. #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
  103. /* Insert a record of BLOCK in sorted order into the
  104. __malloc_mmapped_blocks list. */
  105. for (prev_mmb = 0, mmb = __malloc_mmapped_blocks;
  106. mmb;
  107. prev_mmb = mmb, mmb = mmb->next)
  108. if (block < mmb->mem)
  109. break;
  110. new_mmb = malloc_from_heap (sizeof *new_mmb, &__malloc_mmb_heap);
  111. new_mmb->next = mmb;
  112. new_mmb->mem = block;
  113. new_mmb->size = block_size;
  114. if (prev_mmb)
  115. prev_mmb->next = new_mmb;
  116. else
  117. __malloc_mmapped_blocks = new_mmb;
  118. MALLOC_MMB_DEBUG (0, "new mmb at 0x%x: 0x%x[%d]",
  119. (unsigned)new_mmb,
  120. (unsigned)new_mmb->mem, block_size);
  121. #endif /* !MALLOC_USE_SBRK && __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
  122. }
  123. }
  124. if (likely (mem))
  125. /* Record the size of the block and get the user address. */
  126. {
  127. mem = MALLOC_SETUP (mem, size);
  128. MALLOC_DEBUG (-1, "malloc: returning 0x%lx (base:0x%lx, total_size:%ld)",
  129. (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
  130. }
  131. else
  132. MALLOC_DEBUG (-1, "malloc: returning 0");
  133. return mem;
  134. }
  135. void *
  136. malloc (size_t size)
  137. {
  138. void *mem;
  139. #ifdef MALLOC_DEBUGGING
  140. static int debugging_initialized = 0;
  141. if (! debugging_initialized)
  142. {
  143. debugging_initialized = 1;
  144. __malloc_debug_init ();
  145. }
  146. if (__malloc_check)
  147. __heap_check (&__malloc_heap, "malloc");
  148. #endif
  149. #ifdef __MALLOC_GLIBC_COMPAT__
  150. if (unlikely (size == 0))
  151. size++;
  152. #else
  153. /* Some programs will call malloc (0). Lets be strict and return NULL */
  154. if (unlikely (size == 0))
  155. return 0;
  156. #endif
  157. /* Check if they are doing something dumb like malloc(-1) */
  158. if (unlikely(((unsigned long)size > (unsigned long)(MALLOC_HEADER_SIZE*-2))))
  159. goto oom;
  160. mem = malloc_from_heap (size, &__malloc_heap);
  161. if (unlikely (!mem))
  162. {
  163. oom:
  164. __set_errno (ENOMEM);
  165. return 0;
  166. }
  167. return mem;
  168. }