alloc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, 0, 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, 0, 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 num, size_t size)
  45. {
  46. void *ptr = malloc(num * size);
  47. if (ptr)
  48. memset(ptr, 0, num * size);
  49. return ptr;
  50. }
  51. #endif
  52. #ifdef L_realloc
  53. void *realloc(void *ptr, size_t size)
  54. {
  55. void *newptr = NULL;
  56. if (!ptr)
  57. return malloc(size);
  58. if (!size) {
  59. free(ptr);
  60. return malloc(0);
  61. }
  62. newptr = malloc(size);
  63. if (newptr) {
  64. memcpy(newptr, ptr,
  65. #ifdef __UCLIBC_HAS_MMU__
  66. *((size_t *) (ptr - sizeof(size_t)))
  67. #else
  68. size
  69. #endif
  70. );
  71. free(ptr);
  72. }
  73. return newptr;
  74. }
  75. #endif
  76. #ifdef L_free
  77. extern int weak_function __libc_free_aligned(void *ptr);
  78. void free(void *ptr)
  79. {
  80. if (ptr == NULL)
  81. return;
  82. if (unlikely(__libc_free_aligned!=NULL)) {
  83. if (__libc_free_aligned(ptr)) {
  84. return;
  85. }
  86. }
  87. #ifdef __UCLIBC_HAS_MMU__
  88. ptr -= sizeof(size_t);
  89. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  90. #else
  91. munmap(ptr, 0);
  92. #endif
  93. }
  94. #endif
  95. #ifdef L_memalign
  96. #ifdef __UCLIBC_HAS_THREADS__
  97. #include <pthread.h>
  98. extern pthread_mutex_t __malloclock;
  99. # define LOCK __pthread_mutex_lock(&__malloclock)
  100. # define UNLOCK __pthread_mutex_unlock(&__malloclock);
  101. #else
  102. # define LOCK
  103. # define UNLOCK
  104. #endif
  105. /* List of blocks allocated with memalign or valloc */
  106. struct alignlist
  107. {
  108. struct alignlist *next;
  109. __ptr_t aligned; /* The address that memaligned returned. */
  110. __ptr_t exact; /* The address that malloc returned. */
  111. };
  112. struct alignlist *_aligned_blocks;
  113. /* Return memory to the heap. */
  114. int __libc_free_aligned(void *ptr)
  115. {
  116. struct alignlist *l;
  117. if (ptr == NULL)
  118. return 0;
  119. LOCK;
  120. for (l = _aligned_blocks; l != NULL; l = l->next) {
  121. if (l->aligned == ptr) {
  122. /* Mark the block as free */
  123. l->aligned = NULL;
  124. ptr = l->exact;
  125. #ifdef __UCLIBC_HAS_MMU__
  126. ptr -= sizeof(size_t);
  127. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  128. #else
  129. munmap(ptr, 0);
  130. #endif
  131. return 1;
  132. }
  133. }
  134. UNLOCK;
  135. return 0;
  136. }
  137. void * memalign (size_t alignment, size_t size)
  138. {
  139. void * result;
  140. unsigned long int adj;
  141. result = malloc (size + alignment - 1);
  142. if (result == NULL)
  143. return NULL;
  144. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  145. (char *) NULL)) % alignment;
  146. if (adj != 0)
  147. {
  148. struct alignlist *l;
  149. LOCK;
  150. for (l = _aligned_blocks; l != NULL; l = l->next)
  151. if (l->aligned == NULL)
  152. /* This slot is free. Use it. */
  153. break;
  154. if (l == NULL)
  155. {
  156. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  157. if (l == NULL) {
  158. free(result);
  159. UNLOCK;
  160. return NULL;
  161. }
  162. l->next = _aligned_blocks;
  163. _aligned_blocks = l;
  164. }
  165. l->exact = result;
  166. result = l->aligned = (char *) result + alignment - adj;
  167. UNLOCK;
  168. }
  169. return result;
  170. }
  171. #endif