alloc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include <malloc.h>
  18. #ifdef L_malloc
  19. void *malloc(size_t size)
  20. {
  21. void *result;
  22. if (unlikely(size == 0)) {
  23. #if defined(__MALLOC_GLIBC_COMPAT__)
  24. size++;
  25. #else
  26. /* Some programs will call malloc (0). Lets be strict and return NULL */
  27. __set_errno(ENOMEM);
  28. return NULL;
  29. #endif
  30. }
  31. #ifdef __ARCH_USE_MMU__
  32. # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
  33. #else
  34. # define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS | MAP_UNINITIALIZE
  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. size_t old_size = *((size_t *) (ptr - sizeof(size_t)));
  80. memcpy(newptr, ptr, (old_size < size ? old_size : size));
  81. free(ptr);
  82. }
  83. return newptr;
  84. }
  85. #endif
  86. #ifdef L_free
  87. extern int weak_function __libc_free_aligned(void *ptr);
  88. void free(void *ptr)
  89. {
  90. if (unlikely(ptr == NULL))
  91. return;
  92. if (unlikely(__libc_free_aligned != NULL)) {
  93. if (__libc_free_aligned(ptr))
  94. return;
  95. }
  96. ptr -= sizeof(size_t);
  97. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  98. }
  99. #endif
  100. #ifdef L_memalign
  101. #include <bits/uClibc_mutex.h>
  102. __UCLIBC_MUTEX_INIT(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  103. #define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
  104. #define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
  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. __MALLOC_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. ptr -= sizeof(size_t);
  126. munmap(ptr, * (size_t *) ptr + sizeof(size_t));
  127. return 1;
  128. }
  129. }
  130. __MALLOC_UNLOCK;
  131. return 0;
  132. }
  133. void * memalign (size_t alignment, size_t size)
  134. {
  135. void * result;
  136. unsigned long int adj;
  137. result = malloc (size + alignment - 1);
  138. if (result == NULL)
  139. return NULL;
  140. adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
  141. if (adj != 0) {
  142. struct alignlist *l;
  143. __MALLOC_LOCK;
  144. for (l = _aligned_blocks; l != NULL; l = l->next)
  145. if (l->aligned == NULL)
  146. /* This slot is free. Use it. */
  147. break;
  148. if (l == NULL) {
  149. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  150. if (l == NULL) {
  151. free(result);
  152. result = NULL;
  153. goto DONE;
  154. }
  155. l->next = _aligned_blocks;
  156. _aligned_blocks = l;
  157. }
  158. l->exact = result;
  159. result = l->aligned = (char *) result + alignment - adj;
  160. DONE:
  161. __MALLOC_UNLOCK;
  162. }
  163. return result;
  164. }
  165. #endif