malloc.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * libc/stdlib/malloc/malloc.c -- malloc function
  3. *
  4. * Copyright (C) 2002 NEC Corporation
  5. * Copyright (C) 2002 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 <sys/mman.h>
  16. #include "malloc.h"
  17. #include "heap.h"
  18. /* The malloc heap. */
  19. struct heap __malloc_heap = HEAP_INIT;
  20. void *
  21. malloc (size_t size)
  22. {
  23. void *mem;
  24. MALLOC_DEBUG ("malloc: %d bytes\n", size);
  25. /* Include extra space to record the size of the allocated block. */
  26. size += MALLOC_ROUND_UP (sizeof (size_t), MALLOC_ALIGNMENT);
  27. mem = __heap_alloc (&__malloc_heap, &size);
  28. if (! mem)
  29. /* We couldn't allocate from the heap, so get some more memory
  30. from the system, add it to the heap, and try again. */
  31. {
  32. /* If we're trying to allocate a block bigger than the default
  33. MALLOC_HEAP_EXTEND_SIZE, make sure we get enough to hold it. */
  34. size_t block_size
  35. = (size < MALLOC_HEAP_EXTEND_SIZE
  36. ? MALLOC_HEAP_EXTEND_SIZE
  37. : MALLOC_ROUND_UP_TO_PAGE_SIZE (size));
  38. /* Allocate the new heap block. */
  39. #ifdef MALLOC_USE_SBRK
  40. /* Use sbrk we can, as it's faster than mmap, and guarantees
  41. contiguous allocation. */
  42. void *block = sbrk (block_size);
  43. #else
  44. /* Otherwise, use mmap. */
  45. void *block = mmap (0, block_size, PROT_READ | PROT_WRITE,
  46. MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  47. #endif
  48. if (block != (void *)-1)
  49. {
  50. #ifdef MALLOC_USE_SBRK
  51. /* Because sbrk can return results of arbitrary
  52. alignment, align the result to a MALLOC_ALIGNMENT boundary. */
  53. long aligned_block = MALLOC_ROUND_UP ((long)block, MALLOC_ALIGNMENT);
  54. if (block != (void *)aligned_block)
  55. /* Have to adjust. We should only have to actually do this
  56. the first time (after which we will have aligned the brk
  57. correctly). */
  58. {
  59. /* Move the brk to reflect the alignment; our next allocation
  60. should start on exactly the right alignment. */
  61. sbrk (aligned_block - (long)block);
  62. block = (void *)aligned_block;
  63. }
  64. #endif /* MALLOC_USE_SBRK */
  65. MALLOC_DEBUG (" adding memory: 0x%lx - 0x%lx (%d bytes)\n",
  66. (long)block, (long)block + block_size, block_size);
  67. /* Put BLOCK into the heap. */
  68. __heap_free (&__malloc_heap, block, block_size);
  69. /* Try again to allocate. */
  70. mem = __heap_alloc (&__malloc_heap, &size);
  71. }
  72. }
  73. if (mem)
  74. /* Record the size of this block just before the returned address. */
  75. {
  76. *(size_t *)mem = size;
  77. mem += MALLOC_ALIGNMENT;
  78. MALLOC_DEBUG (" malloc: returning 0x%lx (base:0x%lx, total_size:%d)\n",
  79. (long)mem, (long)mem - MALLOC_ALIGNMENT, size);
  80. }
  81. return mem;
  82. }