alloc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* alloc.c
  2. *
  3. * Written by Erik Andersen <andersee@codepoet.org>
  4. * LGPLv2
  5. *
  6. * Parts of the memalign code were stolen from malloc-930716.
  7. */
  8. #define _GNU_SOURCE
  9. #include <features.h>
  10. #include <unistd.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <errno.h>
  16. #include <sys/mman.h>
  17. libc_hidden_proto(memcpy)
  18. /*libc_hidden_proto(memset)*/
  19. libc_hidden_proto(mmap)
  20. libc_hidden_proto(munmap)
  21. #ifdef L_malloc
  22. void *malloc(size_t size)
  23. {
  24. void *result;
  25. if (unlikely(size == 0)) {
  26. #if defined(__MALLOC_GLIBC_COMPAT__)
  27. size++;
  28. #else
  29. /* Some programs will call malloc (0). Lets be strict and return NULL */
  30. return 0;
  31. #endif
  32. }
  33. #ifdef __ARCH_HAS_MMU__
  34. # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
  35. #else
  36. # define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS
  37. #endif
  38. result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
  39. MMAP_FLAGS, 0, 0);
  40. if (result == MAP_FAILED)
  41. return 0;
  42. * (size_t *) result = size;
  43. return(result + sizeof(size_t));
  44. }
  45. #endif
  46. #ifdef L_calloc
  47. void * calloc(size_t nmemb, size_t lsize)
  48. {
  49. void *result;
  50. size_t size=lsize * nmemb;
  51. /* guard vs integer overflow, but allow nmemb
  52. * to fall through and call malloc(0) */
  53. if (nmemb && lsize != (size / nmemb)) {
  54. __set_errno(ENOMEM);
  55. return NULL;
  56. }
  57. result=malloc(size);
  58. #if 0
  59. /* Standard unix mmap using /dev/zero clears memory so calloc
  60. * doesn't need to actually zero anything....
  61. */
  62. if (result != NULL) {
  63. memset(result, 0, size);
  64. }
  65. #endif
  66. return result;
  67. }
  68. #endif
  69. #ifdef L_realloc
  70. void *realloc(void *ptr, size_t size)
  71. {
  72. void *newptr = NULL;
  73. if (!ptr)
  74. return malloc(size);
  75. if (!size) {
  76. free(ptr);
  77. return malloc(0);
  78. }
  79. newptr = malloc(size);
  80. if (newptr) {
  81. memcpy(newptr, ptr, *((size_t *) (ptr - sizeof(size_t))));
  82. free(ptr);
  83. }
  84. return newptr;
  85. }
  86. #endif
  87. #ifdef L_free
  88. extern int weak_function __libc_free_aligned(void *ptr);
  89. void free(void *ptr)
  90. {
  91. if (unlikely(ptr == NULL))
  92. return;
  93. if (unlikely(__libc_free_aligned != NULL)) {
  94. if (__libc_free_aligned(ptr))
  95. return;
  96. }
  97. ptr -= sizeof(size_t);
  98. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  99. }
  100. #endif
  101. #ifdef L_memalign
  102. #ifdef __UCLIBC_HAS_THREADS__
  103. # include <pthread.h>
  104. pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  105. #endif
  106. #define LOCK __pthread_mutex_lock(&__malloc_lock)
  107. #define UNLOCK __pthread_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. 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. 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. UNLOCK;
  134. return 0;
  135. }
  136. void * memalign (size_t alignment, size_t size)
  137. {
  138. void * result;
  139. unsigned long int adj;
  140. result = malloc (size + alignment - 1);
  141. if (result == NULL)
  142. return NULL;
  143. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  144. (char *) NULL)) % alignment;
  145. if (adj != 0) {
  146. struct alignlist *l;
  147. LOCK;
  148. for (l = _aligned_blocks; l != NULL; l = l->next)
  149. if (l->aligned == NULL)
  150. /* This slot is free. Use it. */
  151. break;
  152. if (l == NULL) {
  153. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  154. if (l == NULL) {
  155. free(result);
  156. UNLOCK;
  157. return NULL;
  158. }
  159. l->next = _aligned_blocks;
  160. _aligned_blocks = l;
  161. }
  162. l->exact = result;
  163. result = l->aligned = (char *) result + alignment - adj;
  164. UNLOCK;
  165. }
  166. return result;
  167. }
  168. #endif