memalign.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. This is a version (aka dlmalloc) of malloc/free/realloc written by
  3. Doug Lea and released to the public domain. Use, modify, and
  4. redistribute this code without permission or acknowledgement in any
  5. way you wish. Send questions, comments, complaints, performance
  6. data, etc to dl@cs.oswego.edu
  7. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  8. Note: There may be an updated version of this malloc obtainable at
  9. ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  10. Check before installing!
  11. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  12. */
  13. #include <features.h>
  14. #include <stddef.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include "malloc.h"
  19. /* ------------------------------ memalign ------------------------------ */
  20. void* memalign(size_t alignment, size_t bytes)
  21. {
  22. size_t nb; /* padded request size */
  23. char* m; /* memory returned by malloc call */
  24. mchunkptr p; /* corresponding chunk */
  25. char* _brk; /* alignment point within p */
  26. mchunkptr newp; /* chunk to return */
  27. size_t newsize; /* its size */
  28. size_t leadsize; /* leading space before alignment point */
  29. mchunkptr remainder; /* spare room at end to split off */
  30. unsigned long remainder_size; /* its size */
  31. size_t size;
  32. /* If need less alignment than we give anyway, just relay to malloc */
  33. if (alignment <= MALLOC_ALIGNMENT) return malloc(bytes);
  34. /* Otherwise, ensure that it is at least a minimum chunk size */
  35. if (alignment < MINSIZE) alignment = MINSIZE;
  36. /* Make sure alignment is power of 2 (in case MINSIZE is not). */
  37. if ((alignment & (alignment - 1)) != 0) {
  38. size_t a = MALLOC_ALIGNMENT * 2;
  39. while ((unsigned long)a < (unsigned long)alignment) a <<= 1;
  40. alignment = a;
  41. }
  42. LOCK;
  43. checked_request2size(bytes, nb);
  44. /* Strategy: find a spot within that chunk that meets the alignment
  45. * request, and then possibly free the leading and trailing space. */
  46. /* Call malloc with worst case padding to hit alignment. */
  47. m = (char*)(malloc(nb + alignment + MINSIZE));
  48. if (m == 0) {
  49. UNLOCK;
  50. return 0; /* propagate failure */
  51. }
  52. p = mem2chunk(m);
  53. if ((((unsigned long)(m)) % alignment) != 0) { /* misaligned */
  54. /*
  55. Find an aligned spot inside chunk. Since we need to give back
  56. leading space in a chunk of at least MINSIZE, if the first
  57. calculation places us at a spot with less than MINSIZE leader,
  58. we can move to the next aligned spot -- we've allocated enough
  59. total room so that this is always possible.
  60. */
  61. _brk = (char*)mem2chunk((unsigned long)(((unsigned long)(m + alignment - 1)) &
  62. -((signed long) alignment)));
  63. if ((unsigned long)(_brk - (char*)(p)) < MINSIZE)
  64. _brk += alignment;
  65. newp = (mchunkptr)_brk;
  66. leadsize = _brk - (char*)(p);
  67. newsize = chunksize(p) - leadsize;
  68. /* For mmapped chunks, just adjust offset */
  69. if (chunk_is_mmapped(p)) {
  70. newp->prev_size = p->prev_size + leadsize;
  71. set_head(newp, newsize|IS_MMAPPED);
  72. UNLOCK;
  73. return chunk2mem(newp);
  74. }
  75. /* Otherwise, give back leader, use the rest */
  76. set_head(newp, newsize | PREV_INUSE);
  77. set_inuse_bit_at_offset(newp, newsize);
  78. set_head_size(p, leadsize);
  79. free(chunk2mem(p));
  80. p = newp;
  81. assert (newsize >= nb &&
  82. (((unsigned long)(chunk2mem(p))) % alignment) == 0);
  83. }
  84. /* Also give back spare room at the end */
  85. if (!chunk_is_mmapped(p)) {
  86. size = chunksize(p);
  87. if ((unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
  88. remainder_size = size - nb;
  89. remainder = chunk_at_offset(p, nb);
  90. set_head(remainder, remainder_size | PREV_INUSE);
  91. set_head_size(p, nb);
  92. free(chunk2mem(remainder));
  93. }
  94. }
  95. check_inuse_chunk(p);
  96. UNLOCK;
  97. return chunk2mem(p);
  98. }