alloc.c 3.8 KB

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