alloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* alloc.c
  2. *
  3. * Written by Erik Andersen <andersee@debian.org>
  4. * LGPLv2
  5. *
  6. * Parts of the memalign code were stolen from malloc-930716.
  7. */
  8. #include <features.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/mman.h>
  15. #ifdef L_malloc
  16. void *malloc(size_t size)
  17. {
  18. void *result;
  19. if (unlikely(size == 0)) {
  20. #if defined(__MALLOC_GLIBC_COMPAT__)
  21. size++;
  22. #else
  23. /* Some programs will call malloc (0). Lets be strict and return NULL */
  24. return 0;
  25. #endif
  26. }
  27. #ifdef __UCLIBC_HAS_MMU__
  28. result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
  29. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  30. if (result == MAP_FAILED)
  31. return 0;
  32. * (size_t *) result = size;
  33. return(result + sizeof(size_t));
  34. #else
  35. result = mmap((void *) 0, size, PROT_READ | PROT_WRITE,
  36. MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  37. if (result == MAP_FAILED)
  38. return 0;
  39. return(result);
  40. #endif
  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,
  79. #ifdef __UCLIBC_HAS_MMU__
  80. *((size_t *) (ptr - sizeof(size_t)))
  81. #else
  82. size
  83. #endif
  84. );
  85. free(ptr);
  86. }
  87. return newptr;
  88. }
  89. #endif
  90. #ifdef L_free
  91. extern int weak_function __libc_free_aligned(void *ptr);
  92. void free(void *ptr)
  93. {
  94. if (ptr == NULL)
  95. return;
  96. if (unlikely(__libc_free_aligned!=NULL)) {
  97. if (__libc_free_aligned(ptr)) {
  98. return;
  99. }
  100. }
  101. #ifdef __UCLIBC_HAS_MMU__
  102. ptr -= sizeof(size_t);
  103. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  104. #else
  105. munmap(ptr, 0);
  106. #endif
  107. }
  108. #endif
  109. #ifdef L_memalign
  110. #ifdef __UCLIBC_HAS_THREADS__
  111. #include <pthread.h>
  112. extern pthread_mutex_t __malloclock;
  113. # define LOCK __pthread_mutex_lock(&__malloclock)
  114. # define UNLOCK __pthread_mutex_unlock(&__malloclock);
  115. #else
  116. # define LOCK
  117. # define UNLOCK
  118. #endif
  119. /* List of blocks allocated with memalign or valloc */
  120. struct alignlist
  121. {
  122. struct alignlist *next;
  123. __ptr_t aligned; /* The address that memaligned returned. */
  124. __ptr_t exact; /* The address that malloc returned. */
  125. };
  126. struct alignlist *_aligned_blocks;
  127. /* Return memory to the heap. */
  128. int __libc_free_aligned(void *ptr)
  129. {
  130. struct alignlist *l;
  131. if (ptr == NULL)
  132. return 0;
  133. LOCK;
  134. for (l = _aligned_blocks; l != NULL; l = l->next) {
  135. if (l->aligned == ptr) {
  136. /* Mark the block as free */
  137. l->aligned = NULL;
  138. ptr = l->exact;
  139. #ifdef __UCLIBC_HAS_MMU__
  140. ptr -= sizeof(size_t);
  141. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  142. #else
  143. munmap(ptr, 0);
  144. #endif
  145. return 1;
  146. }
  147. }
  148. UNLOCK;
  149. return 0;
  150. }
  151. void * memalign (size_t alignment, size_t size)
  152. {
  153. void * result;
  154. unsigned long int adj;
  155. result = malloc (size + alignment - 1);
  156. if (result == NULL)
  157. return NULL;
  158. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  159. (char *) NULL)) % alignment;
  160. if (adj != 0)
  161. {
  162. struct alignlist *l;
  163. LOCK;
  164. for (l = _aligned_blocks; l != NULL; l = l->next)
  165. if (l->aligned == NULL)
  166. /* This slot is free. Use it. */
  167. break;
  168. if (l == NULL)
  169. {
  170. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  171. if (l == NULL) {
  172. free(result);
  173. UNLOCK;
  174. return NULL;
  175. }
  176. l->next = _aligned_blocks;
  177. _aligned_blocks = l;
  178. }
  179. l->exact = result;
  180. result = l->aligned = (char *) result + alignment - adj;
  181. UNLOCK;
  182. }
  183. return result;
  184. }
  185. #endif