memalign.c 3.3 KB

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