mem.c 11 KB

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