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. #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. #ifndef __ARCH_USE_MMU__
  57. /* mmap'd with MAP_UNINITIALIZE, we have to blank memory ourselves */
  58. if (result != NULL) {
  59. memset(result, 0, size);
  60. }
  61. #endif
  62. return result;
  63. }
  64. #endif
  65. #ifdef L_realloc
  66. void *realloc(void *ptr, size_t size)
  67. {
  68. void *newptr = NULL;
  69. if (!ptr)
  70. return malloc(size);
  71. if (!size) {
  72. free(ptr);
  73. return malloc(0);
  74. }
  75. newptr = malloc(size);
  76. if (newptr) {
  77. size_t old_size = *((size_t *) (ptr - sizeof(size_t)));
  78. memcpy(newptr, ptr, (old_size < size ? old_size : size));
  79. free(ptr);
  80. }
  81. return newptr;
  82. }
  83. #endif
  84. #ifdef L_free
  85. extern int weak_function __libc_free_aligned(void *ptr);
  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. 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