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.

370 lines
8.4KB

  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 "avassert.h"
  35. #include "avutil.h"
  36. #include "intreadwrite.h"
  37. #include "mem.h"
  38. #ifdef MALLOC_PREFIX
  39. #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
  40. #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
  41. #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
  42. #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
  43. #define free AV_JOIN(MALLOC_PREFIX, free)
  44. void *malloc(size_t size);
  45. void *memalign(size_t align, size_t size);
  46. int posix_memalign(void **ptr, size_t align, size_t size);
  47. void *realloc(void *ptr, size_t size);
  48. void free(void *ptr);
  49. #endif /* MALLOC_PREFIX */
  50. #define ALIGN (HAVE_AVX ? 32 : 16)
  51. /* NOTE: if you want to override these functions with your own
  52. * implementations (not recommended) you have to link libav* as
  53. * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
  54. * Note that this will cost performance. */
  55. static size_t max_alloc_size= INT_MAX;
  56. void av_max_alloc(size_t max){
  57. max_alloc_size = max;
  58. }
  59. void *av_malloc(size_t size)
  60. {
  61. void *ptr = NULL;
  62. #if CONFIG_MEMALIGN_HACK
  63. long diff;
  64. #endif
  65. /* let's disallow possible ambiguous cases */
  66. if (size > (max_alloc_size - 32))
  67. return NULL;
  68. #if CONFIG_MEMALIGN_HACK
  69. ptr = malloc(size + ALIGN);
  70. if (!ptr)
  71. return ptr;
  72. diff = ((~(long)ptr)&(ALIGN - 1)) + 1;
  73. ptr = (char *)ptr + diff;
  74. ((char *)ptr)[-1] = diff;
  75. #elif HAVE_POSIX_MEMALIGN
  76. if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
  77. if (posix_memalign(&ptr, ALIGN, size))
  78. ptr = NULL;
  79. #elif HAVE_ALIGNED_MALLOC
  80. ptr = _aligned_malloc(size, ALIGN);
  81. #elif HAVE_MEMALIGN
  82. ptr = memalign(ALIGN, size);
  83. /* Why 64?
  84. * Indeed, we should align it:
  85. * on 4 for 386
  86. * on 16 for 486
  87. * on 32 for 586, PPro - K6-III
  88. * on 64 for K7 (maybe for P3 too).
  89. * Because L1 and L2 caches are aligned on those values.
  90. * But I don't want to code such logic here!
  91. */
  92. /* Why 32?
  93. * For AVX ASM. SSE / NEON needs only 16.
  94. * Why not larger? Because I did not see a difference in benchmarks ...
  95. */
  96. /* benchmarks with P3
  97. * memalign(64) + 1 3071, 3051, 3032
  98. * memalign(64) + 2 3051, 3032, 3041
  99. * memalign(64) + 4 2911, 2896, 2915
  100. * memalign(64) + 8 2545, 2554, 2550
  101. * memalign(64) + 16 2543, 2572, 2563
  102. * memalign(64) + 32 2546, 2545, 2571
  103. * memalign(64) + 64 2570, 2533, 2558
  104. *
  105. * BTW, malloc seems to do 8-byte alignment by default here.
  106. */
  107. #else
  108. ptr = malloc(size);
  109. #endif
  110. if(!ptr && !size) {
  111. size = 1;
  112. ptr= av_malloc(1);
  113. }
  114. #if CONFIG_MEMORY_POISONING
  115. if (ptr)
  116. memset(ptr, 0x2a, size);
  117. #endif
  118. return ptr;
  119. }
  120. void *av_realloc(void *ptr, size_t size)
  121. {
  122. #if CONFIG_MEMALIGN_HACK
  123. int diff;
  124. #endif
  125. /* let's disallow possible ambiguous cases */
  126. if (size > (max_alloc_size - 32))
  127. return NULL;
  128. #if CONFIG_MEMALIGN_HACK
  129. //FIXME this isn't aligned correctly, though it probably isn't needed
  130. if (!ptr)
  131. return av_malloc(size);
  132. diff = ((char *)ptr)[-1];
  133. av_assert0(diff>0 && diff<=ALIGN);
  134. ptr = realloc((char *)ptr - diff, size + diff);
  135. if (ptr)
  136. ptr = (char *)ptr + diff;
  137. return ptr;
  138. #elif HAVE_ALIGNED_MALLOC
  139. return _aligned_realloc(ptr, size + !size, ALIGN);
  140. #else
  141. return realloc(ptr, size + !size);
  142. #endif
  143. }
  144. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
  145. {
  146. size_t size;
  147. void *r;
  148. if (av_size_mult(elsize, nelem, &size)) {
  149. av_free(ptr);
  150. return NULL;
  151. }
  152. r = av_realloc(ptr, size);
  153. if (!r && size)
  154. av_free(ptr);
  155. return r;
  156. }
  157. void av_free(void *ptr)
  158. {
  159. #if CONFIG_MEMALIGN_HACK
  160. if (ptr) {
  161. int v= ((char *)ptr)[-1];
  162. av_assert0(v>0 && v<=ALIGN);
  163. free((char *)ptr - v);
  164. }
  165. #elif HAVE_ALIGNED_MALLOC
  166. _aligned_free(ptr);
  167. #else
  168. free(ptr);
  169. #endif
  170. }
  171. void av_freep(void *arg)
  172. {
  173. void **ptr = (void **)arg;
  174. av_free(*ptr);
  175. *ptr = NULL;
  176. }
  177. void *av_mallocz(size_t size)
  178. {
  179. void *ptr = av_malloc(size);
  180. if (ptr)
  181. memset(ptr, 0, size);
  182. return ptr;
  183. }
  184. void *av_calloc(size_t nmemb, size_t size)
  185. {
  186. if (size <= 0 || nmemb >= INT_MAX / size)
  187. return NULL;
  188. return av_mallocz(nmemb * size);
  189. }
  190. char *av_strdup(const char *s)
  191. {
  192. char *ptr = NULL;
  193. if (s) {
  194. int len = strlen(s) + 1;
  195. ptr = av_malloc(len);
  196. if (ptr)
  197. memcpy(ptr, s, len);
  198. }
  199. return ptr;
  200. }
  201. /* add one element to a dynamic array */
  202. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
  203. {
  204. /* see similar ffmpeg.c:grow_array() */
  205. int nb, nb_alloc;
  206. intptr_t *tab;
  207. nb = *nb_ptr;
  208. tab = *(intptr_t**)tab_ptr;
  209. if ((nb & (nb - 1)) == 0) {
  210. if (nb == 0)
  211. nb_alloc = 1;
  212. else
  213. nb_alloc = nb * 2;
  214. tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
  215. *(intptr_t**)tab_ptr = tab;
  216. }
  217. tab[nb++] = (intptr_t)elem;
  218. *nb_ptr = nb;
  219. }
  220. static void fill16(uint8_t *dst, int len)
  221. {
  222. uint32_t v = AV_RN16(dst - 2);
  223. v |= v << 16;
  224. while (len >= 4) {
  225. AV_WN32(dst, v);
  226. dst += 4;
  227. len -= 4;
  228. }
  229. while (len--) {
  230. *dst = dst[-2];
  231. dst++;
  232. }
  233. }
  234. static void fill24(uint8_t *dst, int len)
  235. {
  236. #if HAVE_BIGENDIAN
  237. uint32_t v = AV_RB24(dst - 3);
  238. uint32_t a = v << 8 | v >> 16;
  239. uint32_t b = v << 16 | v >> 8;
  240. uint32_t c = v << 24 | v;
  241. #else
  242. uint32_t v = AV_RL24(dst - 3);
  243. uint32_t a = v | v << 24;
  244. uint32_t b = v >> 8 | v << 16;
  245. uint32_t c = v >> 16 | v << 8;
  246. #endif
  247. while (len >= 12) {
  248. AV_WN32(dst, a);
  249. AV_WN32(dst + 4, b);
  250. AV_WN32(dst + 8, c);
  251. dst += 12;
  252. len -= 12;
  253. }
  254. if (len >= 4) {
  255. AV_WN32(dst, a);
  256. dst += 4;
  257. len -= 4;
  258. }
  259. if (len >= 4) {
  260. AV_WN32(dst, b);
  261. dst += 4;
  262. len -= 4;
  263. }
  264. while (len--) {
  265. *dst = dst[-3];
  266. dst++;
  267. }
  268. }
  269. static void fill32(uint8_t *dst, int len)
  270. {
  271. uint32_t v = AV_RN32(dst - 4);
  272. while (len >= 4) {
  273. AV_WN32(dst, v);
  274. dst += 4;
  275. len -= 4;
  276. }
  277. while (len--) {
  278. *dst = dst[-4];
  279. dst++;
  280. }
  281. }
  282. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  283. {
  284. const uint8_t *src = &dst[-back];
  285. if (back <= 1) {
  286. memset(dst, *src, cnt);
  287. } else if (back == 2) {
  288. fill16(dst, cnt);
  289. } else if (back == 3) {
  290. fill24(dst, cnt);
  291. } else if (back == 4) {
  292. fill32(dst, cnt);
  293. } else {
  294. if (cnt >= 16) {
  295. int blocklen = back;
  296. while (cnt > blocklen) {
  297. memcpy(dst, src, blocklen);
  298. dst += blocklen;
  299. cnt -= blocklen;
  300. blocklen <<= 1;
  301. }
  302. memcpy(dst, src, cnt);
  303. return;
  304. }
  305. if (cnt >= 8) {
  306. AV_COPY32U(dst, src);
  307. AV_COPY32U(dst + 4, src + 4);
  308. src += 8;
  309. dst += 8;
  310. cnt -= 8;
  311. }
  312. if (cnt >= 4) {
  313. AV_COPY32U(dst, src);
  314. src += 4;
  315. dst += 4;
  316. cnt -= 4;
  317. }
  318. if (cnt >= 2) {
  319. AV_COPY16U(dst, src);
  320. src += 2;
  321. dst += 2;
  322. cnt -= 2;
  323. }
  324. if (cnt)
  325. *dst = *src;
  326. }
  327. }