mem.c 11 KB

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