mem.c 11 KB

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