alloc.c 3.7 KB

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