alloc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 munmap __munmap
  9. #define _GNU_SOURCE
  10. #include <features.h>
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <sys/mman.h>
  18. #ifdef L_malloc
  19. void *malloc(size_t size)
  20. {
  21. void *result;
  22. if (unlikely(size == 0)) {
  23. #if defined(__MALLOC_GLIBC_COMPAT__)
  24. size++;
  25. #else
  26. /* Some programs will call malloc (0). Lets be strict and return NULL */
  27. return 0;
  28. #endif
  29. }
  30. #ifdef __ARCH_HAS_MMU__
  31. # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
  32. #else
  33. # define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS
  34. #endif
  35. result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
  36. MMAP_FLAGS, 0, 0);
  37. if (result == MAP_FAILED)
  38. return 0;
  39. * (size_t *) result = size;
  40. return(result + sizeof(size_t));
  41. }
  42. #endif
  43. #ifdef L_calloc
  44. void * calloc(size_t nmemb, size_t lsize)
  45. {
  46. void *result;
  47. size_t size=lsize * nmemb;
  48. /* guard vs integer overflow, but allow nmemb
  49. * to fall through and call malloc(0) */
  50. if (nmemb && lsize != (size / nmemb)) {
  51. __set_errno(ENOMEM);
  52. return NULL;
  53. }
  54. result=malloc(size);
  55. #if 0
  56. /* Standard unix mmap using /dev/zero clears memory so calloc
  57. * doesn't need to actually zero anything....
  58. */
  59. if (result != NULL) {
  60. __memset(result, 0, size);
  61. }
  62. #endif
  63. return result;
  64. }
  65. #endif
  66. #ifdef L_realloc
  67. void *realloc(void *ptr, size_t size)
  68. {
  69. void *newptr = NULL;
  70. if (!ptr)
  71. return malloc(size);
  72. if (!size) {
  73. free(ptr);
  74. return malloc(0);
  75. }
  76. newptr = malloc(size);
  77. if (newptr) {
  78. __memcpy(newptr, ptr, *((size_t *) (ptr - sizeof(size_t))));
  79. free(ptr);
  80. }
  81. return newptr;
  82. }
  83. #endif
  84. #ifdef L_free
  85. extern int weak_function __libc_free_aligned(void *ptr);
  86. void free(void *ptr)
  87. {
  88. if (unlikely(ptr == NULL))
  89. return;
  90. if (unlikely(__libc_free_aligned != NULL)) {
  91. if (__libc_free_aligned(ptr))
  92. return;
  93. }
  94. ptr -= sizeof(size_t);
  95. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  96. }
  97. #endif
  98. #ifdef L_memalign
  99. #ifdef __UCLIBC_HAS_THREADS__
  100. #include <pthread.h>
  101. pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  102. # define LOCK __pthread_mutex_lock(&__malloc_lock)
  103. # define UNLOCK __pthread_mutex_unlock(&__malloc_lock);
  104. #else
  105. # define LOCK
  106. # define UNLOCK
  107. #endif
  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