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.

474 lines
11KB

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