alloc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* alloc.c
  2. *
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /*
  8. * Parts of the memalign code were stolen from malloc-930716.
  9. */
  10. #include <features.h>
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <sys/mman.h>
  17. /* Experimentally off - libc_hidden_proto(memcpy) */
  18. /*libc_hidden_proto(memset)*/
  19. libc_hidden_proto(mmap)
  20. libc_hidden_proto(munmap)
  21. #ifdef L_malloc
  22. void *malloc(size_t size)
  23. {
  24. void *result;
  25. if (unlikely(size == 0)) {
  26. #if defined(__MALLOC_GLIBC_COMPAT__)
  27. size++;
  28. #else
  29. /* Some programs will call malloc (0). Lets be strict and return NULL */
  30. __set_errno(ENOMEM);
  31. return NULL;
  32. #endif
  33. }
  34. #ifdef __ARCH_USE_MMU__
  35. # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
  36. #else
  37. # define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS
  38. #endif
  39. result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
  40. MMAP_FLAGS, 0, 0);
  41. if (result == MAP_FAILED)
  42. return 0;
  43. * (size_t *) result = size;
  44. return(result + sizeof(size_t));
  45. }
  46. #endif
  47. #ifdef L_calloc
  48. void * calloc(size_t nmemb, size_t lsize)
  49. {
  50. void *result;
  51. size_t size=lsize * nmemb;
  52. /* guard vs integer overflow, but allow nmemb
  53. * to fall through and call malloc(0) */
  54. if (nmemb && lsize != (size / nmemb)) {
  55. __set_errno(ENOMEM);
  56. return NULL;
  57. }
  58. result=malloc(size);
  59. #if 0
  60. /* Standard unix mmap using /dev/zero clears memory so calloc
  61. * doesn't need to actually zero anything....
  62. */
  63. if (result != NULL) {
  64. memset(result, 0, size);
  65. }
  66. #endif
  67. return result;
  68. }
  69. #endif
  70. #ifdef L_realloc
  71. void *realloc(void *ptr, size_t size)
  72. {
  73. void *newptr = NULL;
  74. if (!ptr)
  75. return malloc(size);
  76. if (!size) {
  77. free(ptr);
  78. return malloc(0);
  79. }
  80. newptr = malloc(size);
  81. if (newptr) {
  82. size_t old_size = *((size_t *) (ptr - sizeof(size_t)));
  83. memcpy(newptr, ptr, (old_size < size ? old_size : size));
  84. free(ptr);
  85. }
  86. return newptr;
  87. }
  88. #endif
  89. #ifdef L_free
  90. extern int weak_function __libc_free_aligned(void *ptr);
  91. void free(void *ptr)
  92. {
  93. if (unlikely(ptr == NULL))
  94. return;
  95. if (unlikely(__libc_free_aligned != NULL)) {
  96. if (__libc_free_aligned(ptr))
  97. return;
  98. }
  99. ptr -= sizeof(size_t);
  100. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  101. }
  102. #endif
  103. #ifdef L_memalign
  104. #include <bits/uClibc_mutex.h>
  105. __UCLIBC_MUTEX_STATIC(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  106. #define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
  107. #define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
  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. __MALLOC_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. __MALLOC_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 - (char *) NULL)) % alignment;
  144. if (adj != 0) {
  145. struct alignlist *l;
  146. __MALLOC_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. result = NULL;
  156. goto DONE;
  157. }
  158. l->next = _aligned_blocks;
  159. _aligned_blocks = l;
  160. }
  161. l->exact = result;
  162. result = l->aligned = (char *) result + alignment - adj;
  163. DONE:
  164. __MALLOC_UNLOCK;
  165. }
  166. return result;
  167. }
  168. #endif