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