mem.c 11 KB

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