memalign.c 3.0 KB

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