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.

466 lines
10KB

  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 possibly 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. #ifndef __DJGPP__
  83. ptr = memalign(ALIGN, size);
  84. #else
  85. ptr = memalign(size, ALIGN);
  86. #endif
  87. /* Why 64?
  88. * Indeed, we should align it:
  89. * on 4 for 386
  90. * on 16 for 486
  91. * on 32 for 586, PPro - K6-III
  92. * on 64 for K7 (maybe for P3 too).
  93. * Because L1 and L2 caches are aligned on those values.
  94. * But I don't want to code such logic here!
  95. */
  96. /* Why 32?
  97. * For AVX ASM. SSE / NEON needs only 16.
  98. * Why not larger? Because I did not see a difference in benchmarks ...
  99. */
  100. /* benchmarks with P3
  101. * memalign(64) + 1 3071, 3051, 3032
  102. * memalign(64) + 2 3051, 3032, 3041
  103. * memalign(64) + 4 2911, 2896, 2915
  104. * memalign(64) + 8 2545, 2554, 2550
  105. * memalign(64) + 16 2543, 2572, 2563
  106. * memalign(64) + 32 2546, 2545, 2571
  107. * memalign(64) + 64 2570, 2533, 2558
  108. *
  109. * BTW, malloc seems to do 8-byte alignment by default here.
  110. */
  111. #else
  112. ptr = malloc(size);
  113. #endif
  114. if(!ptr && !size) {
  115. size = 1;
  116. ptr= av_malloc(1);
  117. }
  118. #if CONFIG_MEMORY_POISONING
  119. if (ptr)
  120. memset(ptr, FF_MEMORY_POISON, size);
  121. #endif
  122. return ptr;
  123. }
  124. void *av_realloc(void *ptr, size_t size)
  125. {
  126. #if CONFIG_MEMALIGN_HACK
  127. int diff;
  128. #endif
  129. /* let's disallow possibly ambiguous cases */
  130. if (size > (max_alloc_size - 32))
  131. return NULL;
  132. #if CONFIG_MEMALIGN_HACK
  133. //FIXME this isn't aligned correctly, though it probably isn't needed
  134. if (!ptr)
  135. return av_malloc(size);
  136. diff = ((char *)ptr)[-1];
  137. av_assert0(diff>0 && diff<=ALIGN);
  138. ptr = realloc((char *)ptr - diff, size + diff);
  139. if (ptr)
  140. ptr = (char *)ptr + diff;
  141. return ptr;
  142. #elif HAVE_ALIGNED_MALLOC
  143. return _aligned_realloc(ptr, size + !size, ALIGN);
  144. #else
  145. return realloc(ptr, size + !size);
  146. #endif
  147. }
  148. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
  149. {
  150. size_t size;
  151. void *r;
  152. if (av_size_mult(elsize, nelem, &size)) {
  153. av_free(ptr);
  154. return NULL;
  155. }
  156. r = av_realloc(ptr, size);
  157. if (!r && size)
  158. av_free(ptr);
  159. return r;
  160. }
  161. int av_reallocp(void *ptr, size_t size)
  162. {
  163. void **ptrptr = ptr;
  164. void *ret;
  165. if (!size) {
  166. av_freep(ptr);
  167. return 0;
  168. }
  169. ret = av_realloc(*ptrptr, size);
  170. if (!ret) {
  171. av_freep(ptr);
  172. return AVERROR(ENOMEM);
  173. }
  174. *ptrptr = ret;
  175. return 0;
  176. }
  177. void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
  178. {
  179. if (!size || nmemb >= INT_MAX / size)
  180. return NULL;
  181. return av_realloc(ptr, nmemb * size);
  182. }
  183. int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
  184. {
  185. void **ptrptr = ptr;
  186. *ptrptr = av_realloc_f(*ptrptr, nmemb, size);
  187. if (!*ptrptr && nmemb && size)
  188. return AVERROR(ENOMEM);
  189. return 0;
  190. }
  191. void av_free(void *ptr)
  192. {
  193. #if CONFIG_MEMALIGN_HACK
  194. if (ptr) {
  195. int v= ((char *)ptr)[-1];
  196. av_assert0(v>0 && v<=ALIGN);
  197. free((char *)ptr - v);
  198. }
  199. #elif HAVE_ALIGNED_MALLOC
  200. _aligned_free(ptr);
  201. #else
  202. free(ptr);
  203. #endif
  204. }
  205. void av_freep(void *arg)
  206. {
  207. void **ptr = (void **)arg;
  208. av_free(*ptr);
  209. *ptr = NULL;
  210. }
  211. void *av_mallocz(size_t size)
  212. {
  213. void *ptr = av_malloc(size);
  214. if (ptr)
  215. memset(ptr, 0, size);
  216. return ptr;
  217. }
  218. void *av_calloc(size_t nmemb, size_t size)
  219. {
  220. if (size <= 0 || nmemb >= INT_MAX / size)
  221. return NULL;
  222. return av_mallocz(nmemb * size);
  223. }
  224. char *av_strdup(const char *s)
  225. {
  226. char *ptr = NULL;
  227. if (s) {
  228. int len = strlen(s) + 1;
  229. ptr = av_realloc(NULL, len);
  230. if (ptr)
  231. memcpy(ptr, s, len);
  232. }
  233. return ptr;
  234. }
  235. void *av_memdup(const void *p, size_t size)
  236. {
  237. void *ptr = NULL;
  238. if (p) {
  239. ptr = av_malloc(size);
  240. if (ptr)
  241. memcpy(ptr, p, size);
  242. }
  243. return ptr;
  244. }
  245. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
  246. {
  247. /* see similar ffmpeg.c:grow_array() */
  248. int nb, nb_alloc;
  249. intptr_t *tab;
  250. nb = *nb_ptr;
  251. tab = *(intptr_t**)tab_ptr;
  252. if ((nb & (nb - 1)) == 0) {
  253. if (nb == 0) {
  254. nb_alloc = 1;
  255. } else {
  256. if (nb > INT_MAX / (2 * sizeof(intptr_t)))
  257. goto fail;
  258. nb_alloc = nb * 2;
  259. }
  260. tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
  261. if (!tab)
  262. goto fail;
  263. *(intptr_t**)tab_ptr = tab;
  264. }
  265. tab[nb++] = (intptr_t)elem;
  266. *nb_ptr = nb;
  267. return;
  268. fail:
  269. av_freep(tab_ptr);
  270. *nb_ptr = 0;
  271. }
  272. void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
  273. const uint8_t *elem_data)
  274. {
  275. int nb = *nb_ptr, nb_alloc;
  276. uint8_t *tab = *tab_ptr, *tab_elem_data;
  277. if ((nb & (nb - 1)) == 0) {
  278. if (nb == 0) {
  279. nb_alloc = 1;
  280. } else {
  281. if (nb > INT_MAX / (2 * elem_size))
  282. goto fail;
  283. nb_alloc = nb * 2;
  284. }
  285. tab = av_realloc(tab, nb_alloc * elem_size);
  286. if (!tab)
  287. goto fail;
  288. *tab_ptr = tab;
  289. }
  290. *nb_ptr = nb + 1;
  291. tab_elem_data = tab + nb*elem_size;
  292. if (elem_data)
  293. memcpy(tab_elem_data, elem_data, elem_size);
  294. else if (CONFIG_MEMORY_POISONING)
  295. memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
  296. return tab_elem_data;
  297. fail:
  298. av_freep(tab_ptr);
  299. *nb_ptr = 0;
  300. return NULL;
  301. }
  302. static void fill16(uint8_t *dst, int len)
  303. {
  304. uint32_t v = AV_RN16(dst - 2);
  305. v |= v << 16;
  306. while (len >= 4) {
  307. AV_WN32(dst, v);
  308. dst += 4;
  309. len -= 4;
  310. }
  311. while (len--) {
  312. *dst = dst[-2];
  313. dst++;
  314. }
  315. }
  316. static void fill24(uint8_t *dst, int len)
  317. {
  318. #if HAVE_BIGENDIAN
  319. uint32_t v = AV_RB24(dst - 3);
  320. uint32_t a = v << 8 | v >> 16;
  321. uint32_t b = v << 16 | v >> 8;
  322. uint32_t c = v << 24 | v;
  323. #else
  324. uint32_t v = AV_RL24(dst - 3);
  325. uint32_t a = v | v << 24;
  326. uint32_t b = v >> 8 | v << 16;
  327. uint32_t c = v >> 16 | v << 8;
  328. #endif
  329. while (len >= 12) {
  330. AV_WN32(dst, a);
  331. AV_WN32(dst + 4, b);
  332. AV_WN32(dst + 8, c);
  333. dst += 12;
  334. len -= 12;
  335. }
  336. if (len >= 4) {
  337. AV_WN32(dst, a);
  338. dst += 4;
  339. len -= 4;
  340. }
  341. if (len >= 4) {
  342. AV_WN32(dst, b);
  343. dst += 4;
  344. len -= 4;
  345. }
  346. while (len--) {
  347. *dst = dst[-3];
  348. dst++;
  349. }
  350. }
  351. static void fill32(uint8_t *dst, int len)
  352. {
  353. uint32_t v = AV_RN32(dst - 4);
  354. while (len >= 4) {
  355. AV_WN32(dst, v);
  356. dst += 4;
  357. len -= 4;
  358. }
  359. while (len--) {
  360. *dst = dst[-4];
  361. dst++;
  362. }
  363. }
  364. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  365. {
  366. const uint8_t *src = &dst[-back];
  367. if (!back)
  368. return;
  369. if (back == 1) {
  370. memset(dst, *src, cnt);
  371. } else if (back == 2) {
  372. fill16(dst, cnt);
  373. } else if (back == 3) {
  374. fill24(dst, cnt);
  375. } else if (back == 4) {
  376. fill32(dst, cnt);
  377. } else {
  378. if (cnt >= 16) {
  379. int blocklen = back;
  380. while (cnt > blocklen) {
  381. memcpy(dst, src, blocklen);
  382. dst += blocklen;
  383. cnt -= blocklen;
  384. blocklen <<= 1;
  385. }
  386. memcpy(dst, src, cnt);
  387. return;
  388. }
  389. if (cnt >= 8) {
  390. AV_COPY32U(dst, src);
  391. AV_COPY32U(dst + 4, src + 4);
  392. src += 8;
  393. dst += 8;
  394. cnt -= 8;
  395. }
  396. if (cnt >= 4) {
  397. AV_COPY32U(dst, src);
  398. src += 4;
  399. dst += 4;
  400. cnt -= 4;
  401. }
  402. if (cnt >= 2) {
  403. AV_COPY16U(dst, src);
  404. src += 2;
  405. dst += 2;
  406. cnt -= 2;
  407. }
  408. if (cnt)
  409. *dst = *src;
  410. }
  411. }