memalign.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * libc/stdlib/malloc/memalign.c -- memalign (`aligned 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. /*
  19. ______________________ TOTAL _________________________
  20. / \
  21. +---------------+-------------------------+--------------+
  22. | | | |
  23. +---------------+-------------------------+--------------+
  24. \____ INIT ____/ \______ RETURNED _______/ \____ END ___/
  25. */
  26. void *memalign (size_t alignment, size_t size);
  27. /* XXX shadow outer malloc.h */
  28. libc_hidden_proto(memalign)
  29. void *
  30. memalign (size_t alignment, size_t size)
  31. {
  32. void *mem, *base;
  33. unsigned long tot_addr, tot_end_addr, addr, end_addr;
  34. struct heap_free_area **heap = &__malloc_heap;
  35. /* Make SIZE something we like. */
  36. size = HEAP_ADJUST_SIZE (size);
  37. /* Use malloc to do the initial allocation, since it deals with getting
  38. system memory. We over-allocate enough to be sure that we'll get
  39. enough memory to hold a properly aligned block of size SIZE,
  40. _somewhere_ in the result. */
  41. mem = malloc (size + 2 * alignment);
  42. if (! mem)
  43. /* Allocation failed, we can't do anything. */
  44. return 0;
  45. if (alignment < MALLOC_ALIGNMENT)
  46. return mem;
  47. /* Remember the base-address, of the allocation, although we normally
  48. use the user-address for calculations, since that's where the
  49. alignment matters. */
  50. base = MALLOC_BASE (mem);
  51. /* The bounds of the initial allocation. */
  52. tot_addr = (unsigned long)mem;
  53. tot_end_addr = (unsigned long)base + MALLOC_SIZE (mem);
  54. /* Find a likely place inside MEM with the right alignment. */
  55. addr = MALLOC_ROUND_UP (tot_addr, alignment);
  56. /* Unless TOT_ADDR was already aligned correctly, we need to return the
  57. initial part of MEM to the heap. */
  58. if (addr != tot_addr)
  59. {
  60. size_t init_size = addr - tot_addr;
  61. /* Ensure that memory returned to the heap is large enough. */
  62. if (init_size < HEAP_MIN_SIZE)
  63. {
  64. addr = MALLOC_ROUND_UP (tot_addr + HEAP_MIN_SIZE, alignment);
  65. init_size = addr - tot_addr;
  66. }
  67. __heap_free (heap, base, init_size);
  68. /* Remember that we've freed the initial part of MEM. */
  69. base += init_size;
  70. }
  71. /* Return the end part of MEM to the heap, unless it's too small. */
  72. end_addr = addr + size;
  73. if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr)
  74. __heap_free (heap, (void *)end_addr, tot_end_addr - end_addr);
  75. else
  76. /* We didn't free the end, so include it in the size. */
  77. end_addr = tot_end_addr;
  78. return MALLOC_SETUP (base, end_addr - (unsigned long)base);
  79. }
  80. weak_alias(memalign, aligned_alloc)
  81. libc_hidden_def(memalign)