mem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. int av_dynarray_add_nofree(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. return AVERROR(ENOMEM);
  255. });
  256. return 0;
  257. }
  258. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
  259. {
  260. void **tab = *(void ***)tab_ptr;
  261. AV_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, {
  262. tab[*nb_ptr] = elem;
  263. *(void ***)tab_ptr = tab;
  264. }, {
  265. *nb_ptr = 0;
  266. av_freep(tab_ptr);
  267. });
  268. }
  269. void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
  270. const uint8_t *elem_data)
  271. {
  272. uint8_t *tab_elem_data = NULL;
  273. AV_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, {
  274. tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size;
  275. if (elem_data)
  276. memcpy(tab_elem_data, elem_data, elem_size);
  277. else if (CONFIG_MEMORY_POISONING)
  278. memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
  279. }, {
  280. av_freep(tab_ptr);
  281. *nb_ptr = 0;
  282. });
  283. return tab_elem_data;
  284. }
  285. static void fill16(uint8_t *dst, int len)
  286. {
  287. uint32_t v = AV_RN16(dst - 2);
  288. v |= v << 16;
  289. while (len >= 4) {
  290. AV_WN32(dst, v);
  291. dst += 4;
  292. len -= 4;
  293. }
  294. while (len--) {
  295. *dst = dst[-2];
  296. dst++;
  297. }
  298. }
  299. static void fill24(uint8_t *dst, int len)
  300. {
  301. #if HAVE_BIGENDIAN
  302. uint32_t v = AV_RB24(dst - 3);
  303. uint32_t a = v << 8 | v >> 16;
  304. uint32_t b = v << 16 | v >> 8;
  305. uint32_t c = v << 24 | v;
  306. #else
  307. uint32_t v = AV_RL24(dst - 3);
  308. uint32_t a = v | v << 24;
  309. uint32_t b = v >> 8 | v << 16;
  310. uint32_t c = v >> 16 | v << 8;
  311. #endif
  312. while (len >= 12) {
  313. AV_WN32(dst, a);
  314. AV_WN32(dst + 4, b);
  315. AV_WN32(dst + 8, c);
  316. dst += 12;
  317. len -= 12;
  318. }
  319. if (len >= 4) {
  320. AV_WN32(dst, a);
  321. dst += 4;
  322. len -= 4;
  323. }
  324. if (len >= 4) {
  325. AV_WN32(dst, b);
  326. dst += 4;
  327. len -= 4;
  328. }
  329. while (len--) {
  330. *dst = dst[-3];
  331. dst++;
  332. }
  333. }
  334. static void fill32(uint8_t *dst, int len)
  335. {
  336. uint32_t v = AV_RN32(dst - 4);
  337. while (len >= 4) {
  338. AV_WN32(dst, v);
  339. dst += 4;
  340. len -= 4;
  341. }
  342. while (len--) {
  343. *dst = dst[-4];
  344. dst++;
  345. }
  346. }
  347. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  348. {
  349. const uint8_t *src = &dst[-back];
  350. if (!back)
  351. return;
  352. if (back == 1) {
  353. memset(dst, *src, cnt);
  354. } else if (back == 2) {
  355. fill16(dst, cnt);
  356. } else if (back == 3) {
  357. fill24(dst, cnt);
  358. } else if (back == 4) {
  359. fill32(dst, cnt);
  360. } else {
  361. if (cnt >= 16) {
  362. int blocklen = back;
  363. while (cnt > blocklen) {
  364. memcpy(dst, src, blocklen);
  365. dst += blocklen;
  366. cnt -= blocklen;
  367. blocklen <<= 1;
  368. }
  369. memcpy(dst, src, cnt);
  370. return;
  371. }
  372. if (cnt >= 8) {
  373. AV_COPY32U(dst, src);
  374. AV_COPY32U(dst + 4, src + 4);
  375. src += 8;
  376. dst += 8;
  377. cnt -= 8;
  378. }
  379. if (cnt >= 4) {
  380. AV_COPY32U(dst, src);
  381. src += 4;
  382. dst += 4;
  383. cnt -= 4;
  384. }
  385. if (cnt >= 2) {
  386. AV_COPY16U(dst, src);
  387. src += 2;
  388. dst += 2;
  389. cnt -= 2;
  390. }
  391. if (cnt)
  392. *dst = *src;
  393. }
  394. }
  395. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  396. {
  397. if (min_size < *size)
  398. return ptr;
  399. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  400. ptr = av_realloc(ptr, min_size);
  401. /* we could set this to the unmodified min_size but this is safer
  402. * if the user lost the ptr and uses NULL now
  403. */
  404. if (!ptr)
  405. min_size = 0;
  406. *size = min_size;
  407. return ptr;
  408. }
  409. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  410. {
  411. void **p = ptr;
  412. if (min_size < *size)
  413. return 0;
  414. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  415. av_free(*p);
  416. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  417. if (!*p)
  418. min_size = 0;
  419. *size = min_size;
  420. return 1;
  421. }
  422. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  423. {
  424. ff_fast_malloc(ptr, size, min_size, 0);
  425. }