alloc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* alloc.c
  2. *
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /*
  8. * Parts of the memalign code were stolen from malloc-930716.
  9. */
  10. #include <features.h>
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdint.h>
  16. #include <errno.h>
  17. #include <sys/mman.h>
  18. #include <malloc.h>
  19. extern int weak_function __libc_free_aligned(void *ptr) attribute_hidden;
  20. #ifdef L_malloc
  21. void *malloc(size_t size)
  22. {
  23. void *result;
  24. if (unlikely(size == 0)) {
  25. size++;
  26. }
  27. /* prevent Undefined Behaviour for pointer arithmetic (substract) of too big pointers
  28. * see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303
  29. * No need to check for size + sizeof(size_t) integer overflow since we already check for PTRDIFF_MAX
  30. */
  31. if (unlikely(size > PTRDIFF_MAX)) {
  32. __set_errno(ENOMEM);
  33. return 0;
  34. }
  35. #ifdef __ARCH_USE_MMU__
  36. # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
  37. #else
  38. # define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS | MAP_UNINITIALIZED
  39. #endif
  40. result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
  41. MMAP_FLAGS, 0, 0);
  42. if (result == MAP_FAILED) {
  43. __set_errno(ENOMEM);
  44. return 0;
  45. }
  46. * (size_t *) result = size;
  47. return(result + sizeof(size_t));
  48. }
  49. #endif
  50. #ifdef L_calloc
  51. void * calloc(size_t nmemb, size_t lsize)
  52. {
  53. void *result;
  54. size_t size=lsize * nmemb;
  55. /* guard vs integer overflow, but allow nmemb
  56. * to fall through and call malloc(0) */
  57. if (nmemb && lsize != (size / nmemb)) {
  58. __set_errno(ENOMEM);
  59. return NULL;
  60. }
  61. result = malloc(size);
  62. #ifndef __ARCH_USE_MMU__
  63. /* mmap'd with MAP_UNINITIALIZED, we have to blank memory ourselves */
  64. if (result != NULL) {
  65. memset(result, 0, size);
  66. }
  67. #endif
  68. return result;
  69. }
  70. #endif
  71. #ifdef L_realloc
  72. void *realloc(void *ptr, size_t size)
  73. {
  74. void *newptr = NULL;
  75. if (!ptr)
  76. return malloc(size);
  77. if (!size) {
  78. free(ptr);
  79. return malloc(0);
  80. }
  81. newptr = malloc(size);
  82. if (newptr) {
  83. size_t old_size = *((size_t *) (ptr - sizeof(size_t)));
  84. memcpy(newptr, ptr, (old_size < size ? old_size : size));
  85. free(ptr);
  86. }
  87. return newptr;
  88. }
  89. #endif
  90. #ifdef L_free
  91. void free(void *ptr)
  92. {
  93. if (unlikely(ptr == NULL))
  94. return;
  95. if (unlikely(__libc_free_aligned != NULL)) {
  96. if (__libc_free_aligned(ptr))
  97. return;
  98. }
  99. ptr -= sizeof(size_t);
  100. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  101. }
  102. #endif
  103. #ifdef L_memalign
  104. #include <bits/uClibc_mutex.h>
  105. __UCLIBC_MUTEX_INIT(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  106. #define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
  107. #define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
  108. /* List of blocks allocated with memalign or valloc */
  109. struct alignlist
  110. {
  111. struct alignlist *next;
  112. __ptr_t aligned; /* The address that memaligned returned. */
  113. __ptr_t exact; /* The address that malloc returned. */
  114. };
  115. static struct alignlist *_aligned_blocks;
  116. /* Return memory to the heap. */
  117. int __libc_free_aligned(void *ptr)
  118. {
  119. struct alignlist *l;
  120. if (ptr == NULL)
  121. return 0;
  122. __MALLOC_LOCK;
  123. for (l = _aligned_blocks; l != NULL; l = l->next) {
  124. if (l->aligned == ptr) {
  125. /* Mark the block as free */
  126. l->aligned = NULL;
  127. ptr = l->exact;
  128. ptr -= sizeof(size_t);
  129. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  130. return 1;
  131. }
  132. }
  133. __MALLOC_UNLOCK;
  134. return 0;
  135. }
  136. void * memalign (size_t alignment, size_t size)
  137. {
  138. void * result;
  139. unsigned long int adj;
  140. if (unlikely(size > PTRDIFF_MAX)) {
  141. __set_errno(ENOMEM);
  142. return NULL;
  143. }
  144. if (unlikely((size + alignment - 1 < size) && (alignment != 0))) {
  145. __set_errno(ENOMEM);
  146. return NULL;
  147. }
  148. result = malloc (size + alignment - 1);
  149. if (result == NULL)
  150. return NULL;
  151. adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
  152. if (adj != 0) {
  153. struct alignlist *l;
  154. __MALLOC_LOCK;
  155. for (l = _aligned_blocks; l != NULL; l = l->next)
  156. if (l->aligned == NULL)
  157. /* This slot is free. Use it. */
  158. break;
  159. if (l == NULL) {
  160. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  161. if (l == NULL) {
  162. free(result);
  163. result = NULL;
  164. goto DONE;
  165. }
  166. l->next = _aligned_blocks;
  167. _aligned_blocks = l;
  168. }
  169. l->exact = result;
  170. result = l->aligned = (char *) result + alignment - adj;
  171. DONE:
  172. __MALLOC_UNLOCK;
  173. }
  174. return result;
  175. }
  176. libc_hidden_def(memalign)
  177. #endif