You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
6.9KB

  1. /*
  2. * default memory allocator for libavutil
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * default memory allocator for libavutil
  24. */
  25. #define _XOPEN_SOURCE 600
  26. #include "config.h"
  27. #include <limits.h>
  28. #include <stdint.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #if HAVE_MALLOC_H
  32. #include <malloc.h>
  33. #endif
  34. #include "avutil.h"
  35. #include "intreadwrite.h"
  36. #include "mem.h"
  37. /* here we can use OS-dependent allocation functions */
  38. #undef free
  39. #undef malloc
  40. #undef realloc
  41. #ifdef MALLOC_PREFIX
  42. #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
  43. #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
  44. #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
  45. #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
  46. #define free AV_JOIN(MALLOC_PREFIX, free)
  47. void *malloc(size_t size);
  48. void *memalign(size_t align, size_t size);
  49. int posix_memalign(void **ptr, size_t align, size_t size);
  50. void *realloc(void *ptr, size_t size);
  51. void free(void *ptr);
  52. #endif /* MALLOC_PREFIX */
  53. #define ALIGN (HAVE_AVX ? 32 : 16)
  54. /* NOTE: if you want to override these functions with your own
  55. * implementations (not recommended) you have to link libav* as
  56. * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
  57. * Note that this will cost performance. */
  58. static size_t max_alloc_size= INT_MAX;
  59. void av_max_alloc(size_t max){
  60. max_alloc_size = max;
  61. }
  62. void *av_malloc(size_t size)
  63. {
  64. void *ptr = NULL;
  65. #if CONFIG_MEMALIGN_HACK
  66. long diff;
  67. #endif
  68. /* let's disallow possible ambiguous cases */
  69. if (size > (max_alloc_size - 32))
  70. return NULL;
  71. #if CONFIG_MEMALIGN_HACK
  72. ptr = malloc(size + ALIGN);
  73. if (!ptr)
  74. return ptr;
  75. diff = ((-(long)ptr - 1)&(ALIGN - 1)) + 1;
  76. ptr = (char *)ptr + diff;
  77. ((char *)ptr)[-1] = diff;
  78. #elif HAVE_POSIX_MEMALIGN
  79. if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
  80. if (posix_memalign(&ptr, ALIGN, size))
  81. ptr = NULL;
  82. #elif HAVE_ALIGNED_MALLOC
  83. ptr = _aligned_malloc(size, ALIGN);
  84. #elif HAVE_MEMALIGN
  85. ptr = memalign(ALIGN, size);
  86. /* Why 64?
  87. * Indeed, we should align it:
  88. * on 4 for 386
  89. * on 16 for 486
  90. * on 32 for 586, PPro - K6-III
  91. * on 64 for K7 (maybe for P3 too).
  92. * Because L1 and L2 caches are aligned on those values.
  93. * But I don't want to code such logic here!
  94. */
  95. /* Why 32?
  96. * For AVX ASM. SSE / NEON needs only 16.
  97. * Why not larger? Because I did not see a difference in benchmarks ...
  98. */
  99. /* benchmarks with P3
  100. * memalign(64) + 1 3071, 3051, 3032
  101. * memalign(64) + 2 3051, 3032, 3041
  102. * memalign(64) + 4 2911, 2896, 2915
  103. * memalign(64) + 8 2545, 2554, 2550
  104. * memalign(64) + 16 2543, 2572, 2563
  105. * memalign(64) + 32 2546, 2545, 2571
  106. * memalign(64) + 64 2570, 2533, 2558
  107. *
  108. * BTW, malloc seems to do 8-byte alignment by default here.
  109. */
  110. #else
  111. ptr = malloc(size);
  112. #endif
  113. if(!ptr && !size) {
  114. size = 1;
  115. ptr= av_malloc(1);
  116. }
  117. #if CONFIG_MEMORY_POISONING
  118. if (ptr)
  119. memset(ptr, 0x2a, size);
  120. #endif
  121. return ptr;
  122. }
  123. void *av_realloc(void *ptr, size_t size)
  124. {
  125. #if CONFIG_MEMALIGN_HACK
  126. int diff;
  127. #endif
  128. /* let's disallow possible ambiguous cases */
  129. if (size > (max_alloc_size - 32))
  130. return NULL;
  131. #if CONFIG_MEMALIGN_HACK
  132. //FIXME this isn't aligned correctly, though it probably isn't needed
  133. if (!ptr)
  134. return av_malloc(size);
  135. diff = ((char *)ptr)[-1];
  136. ptr = realloc((char *)ptr - diff, size + diff);
  137. if (ptr)
  138. ptr = (char *)ptr + diff;
  139. return ptr;
  140. #elif HAVE_ALIGNED_MALLOC
  141. return _aligned_realloc(ptr, size + !size, ALIGN);
  142. #else
  143. return realloc(ptr, size + !size);
  144. #endif
  145. }
  146. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
  147. {
  148. size_t size;
  149. void *r;
  150. if (av_size_mult(elsize, nelem, &size)) {
  151. av_free(ptr);
  152. return NULL;
  153. }
  154. r = av_realloc(ptr, size);
  155. if (!r && size)
  156. av_free(ptr);
  157. return r;
  158. }
  159. void av_free(void *ptr)
  160. {
  161. #if CONFIG_MEMALIGN_HACK
  162. if (ptr)
  163. free((char *)ptr - ((char *)ptr)[-1]);
  164. #elif HAVE_ALIGNED_MALLOC
  165. _aligned_free(ptr);
  166. #else
  167. free(ptr);
  168. #endif
  169. }
  170. void av_freep(void *arg)
  171. {
  172. void **ptr = (void **)arg;
  173. av_free(*ptr);
  174. *ptr = NULL;
  175. }
  176. void *av_mallocz(size_t size)
  177. {
  178. void *ptr = av_malloc(size);
  179. if (ptr)
  180. memset(ptr, 0, size);
  181. return ptr;
  182. }
  183. void *av_calloc(size_t nmemb, size_t size)
  184. {
  185. if (size <= 0 || nmemb >= INT_MAX / size)
  186. return NULL;
  187. return av_mallocz(nmemb * size);
  188. }
  189. char *av_strdup(const char *s)
  190. {
  191. char *ptr = NULL;
  192. if (s) {
  193. int len = strlen(s) + 1;
  194. ptr = av_malloc(len);
  195. if (ptr)
  196. memcpy(ptr, s, len);
  197. }
  198. return ptr;
  199. }
  200. /* add one element to a dynamic array */
  201. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
  202. {
  203. /* see similar ffmpeg.c:grow_array() */
  204. int nb, nb_alloc;
  205. intptr_t *tab;
  206. nb = *nb_ptr;
  207. tab = *(intptr_t**)tab_ptr;
  208. if ((nb & (nb - 1)) == 0) {
  209. if (nb == 0)
  210. nb_alloc = 1;
  211. else
  212. nb_alloc = nb * 2;
  213. tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
  214. *(intptr_t**)tab_ptr = tab;
  215. }
  216. tab[nb++] = (intptr_t)elem;
  217. *nb_ptr = nb;
  218. }
  219. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  220. {
  221. const uint8_t *src = &dst[-back];
  222. if (back <= 1) {
  223. memset(dst, *src, cnt);
  224. } else {
  225. if (cnt >= 4) {
  226. AV_COPY16U(dst, src);
  227. AV_COPY16U(dst + 2, src + 2);
  228. src += 4;
  229. dst += 4;
  230. cnt -= 4;
  231. }
  232. if (cnt >= 8) {
  233. AV_COPY16U(dst, src);
  234. AV_COPY16U(dst + 2, src + 2);
  235. AV_COPY16U(dst + 4, src + 4);
  236. AV_COPY16U(dst + 6, src + 6);
  237. src += 8;
  238. dst += 8;
  239. cnt -= 8;
  240. }
  241. if (cnt > 0) {
  242. int blocklen = back;
  243. while (cnt > blocklen) {
  244. memcpy(dst, src, blocklen);
  245. dst += blocklen;
  246. cnt -= blocklen;
  247. blocklen <<= 1;
  248. }
  249. memcpy(dst, src, cnt);
  250. }
  251. }
  252. }