alloc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 mmap __mmap
  9. #define munmap __munmap
  10. #define _GNU_SOURCE
  11. #include <features.h>
  12. #include <unistd.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <sys/mman.h>
  19. #ifdef L_malloc
  20. void *malloc(size_t size)
  21. {
  22. void *result;
  23. if (unlikely(size == 0)) {
  24. #if defined(__MALLOC_GLIBC_COMPAT__)
  25. size++;
  26. #else
  27. /* Some programs will call malloc (0). Lets be strict and return NULL */
  28. return 0;
  29. #endif
  30. }
  31. #ifdef __ARCH_HAS_MMU__
  32. # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
  33. #else
  34. # define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS
  35. #endif
  36. result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
  37. MMAP_FLAGS, 0, 0);
  38. if (result == MAP_FAILED)
  39. return 0;
  40. * (size_t *) result = size;
  41. return(result + sizeof(size_t));
  42. }
  43. #endif
  44. #ifdef L_calloc
  45. void * calloc(size_t nmemb, size_t lsize)
  46. {
  47. void *result;
  48. size_t size=lsize * nmemb;
  49. /* guard vs integer overflow, but allow nmemb
  50. * to fall through and call malloc(0) */
  51. if (nmemb && lsize != (size / nmemb)) {
  52. __set_errno(ENOMEM);
  53. return NULL;
  54. }
  55. result=malloc(size);
  56. #if 0
  57. /* Standard unix mmap using /dev/zero clears memory so calloc
  58. * doesn't need to actually zero anything....
  59. */
  60. if (result != NULL) {
  61. __memset(result, 0, size);
  62. }
  63. #endif
  64. return result;
  65. }
  66. #endif
  67. #ifdef L_realloc
  68. void *realloc(void *ptr, size_t size)
  69. {
  70. void *newptr = NULL;
  71. if (!ptr)
  72. return malloc(size);
  73. if (!size) {
  74. free(ptr);
  75. return malloc(0);
  76. }
  77. newptr = malloc(size);
  78. if (newptr) {
  79. __memcpy(newptr, ptr, *((size_t *) (ptr - sizeof(size_t))));
  80. free(ptr);
  81. }
  82. return newptr;
  83. }
  84. #endif
  85. #ifdef L_free
  86. extern int weak_function __libc_free_aligned(void *ptr);
  87. void free(void *ptr)
  88. {
  89. if (unlikely(ptr == NULL))
  90. return;
  91. if (unlikely(__libc_free_aligned != NULL)) {
  92. if (__libc_free_aligned(ptr))
  93. return;
  94. }
  95. ptr -= sizeof(size_t);
  96. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  97. }
  98. #endif
  99. #ifdef L_memalign
  100. #ifdef __UCLIBC_HAS_THREADS__
  101. # include <pthread.h>
  102. pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  103. #endif
  104. #define LOCK __pthread_mutex_lock(&__malloc_lock)
  105. #define UNLOCK __pthread_mutex_unlock(&__malloc_lock)
  106. /* List of blocks allocated with memalign or valloc */
  107. struct alignlist
  108. {
  109. struct alignlist *next;
  110. __ptr_t aligned; /* The address that memaligned returned. */
  111. __ptr_t exact; /* The address that malloc returned. */
  112. };
  113. struct alignlist *_aligned_blocks;
  114. /* Return memory to the heap. */
  115. int __libc_free_aligned(void *ptr)
  116. {
  117. struct alignlist *l;
  118. if (ptr == NULL)
  119. return 0;
  120. LOCK;
  121. for (l = _aligned_blocks; l != NULL; l = l->next) {
  122. if (l->aligned == ptr) {
  123. /* Mark the block as free */
  124. l->aligned = NULL;
  125. ptr = l->exact;
  126. ptr -= sizeof(size_t);
  127. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  128. return 1;
  129. }
  130. }
  131. UNLOCK;
  132. return 0;
  133. }
  134. void * memalign (size_t alignment, size_t size)
  135. {
  136. void * result;
  137. unsigned long int adj;
  138. result = malloc (size + alignment - 1);
  139. if (result == NULL)
  140. return NULL;
  141. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  142. (char *) NULL)) % alignment;
  143. if (adj != 0) {
  144. struct alignlist *l;
  145. LOCK;
  146. for (l = _aligned_blocks; l != NULL; l = l->next)
  147. if (l->aligned == NULL)
  148. /* This slot is free. Use it. */
  149. break;
  150. if (l == NULL) {
  151. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  152. if (l == NULL) {
  153. free(result);
  154. UNLOCK;
  155. return NULL;
  156. }
  157. l->next = _aligned_blocks;
  158. _aligned_blocks = l;
  159. }
  160. l->exact = result;
  161. result = l->aligned = (char *) result + alignment - adj;
  162. UNLOCK;
  163. }
  164. return result;
  165. }
  166. #endif