mem.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "intreadwrite.h"
  37. #include "mem.h"
  38. #ifdef MALLOC_PREFIX
  39. #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
  40. #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
  41. #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
  42. #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
  43. #define free AV_JOIN(MALLOC_PREFIX, free)
  44. void *malloc(size_t size);
  45. void *memalign(size_t align, size_t size);
  46. int posix_memalign(void **ptr, size_t align, size_t size);
  47. void *realloc(void *ptr, size_t size);
  48. void free(void *ptr);
  49. #endif /* MALLOC_PREFIX */
  50. #define ALIGN (HAVE_AVX ? 32 : 16)
  51. /* NOTE: if you want to override these functions with your own
  52. * implementations (not recommended) you have to link libav* as
  53. * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
  54. * Note that this will cost performance. */
  55. static size_t max_alloc_size= INT_MAX;
  56. void av_max_alloc(size_t max){
  57. max_alloc_size = max;
  58. }
  59. void *av_malloc(size_t size)
  60. {
  61. void *ptr = NULL;
  62. #if CONFIG_MEMALIGN_HACK
  63. long diff;
  64. #endif
  65. /* let's disallow possible ambiguous cases */
  66. if (size > (max_alloc_size - 32))
  67. return NULL;
  68. #if CONFIG_MEMALIGN_HACK
  69. ptr = malloc(size + ALIGN);
  70. if (!ptr)
  71. return ptr;
  72. diff = ((~(long)ptr)&(ALIGN - 1)) + 1;
  73. ptr = (char *)ptr + diff;
  74. ((char *)ptr)[-1] = diff;
  75. #elif HAVE_POSIX_MEMALIGN
  76. if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
  77. if (posix_memalign(&ptr, ALIGN, size))
  78. ptr = NULL;
  79. #elif HAVE_ALIGNED_MALLOC
  80. ptr = _aligned_malloc(size, ALIGN);
  81. #elif HAVE_MEMALIGN
  82. #ifndef __DJGPP__
  83. ptr = memalign(ALIGN, size);
  84. #else
  85. ptr = memalign(size, ALIGN);
  86. #endif
  87. /* Why 64?
  88. * Indeed, we should align it:
  89. * on 4 for 386
  90. * on 16 for 486
  91. * on 32 for 586, PPro - K6-III
  92. * on 64 for K7 (maybe for P3 too).
  93. * Because L1 and L2 caches are aligned on those values.
  94. * But I don't want to code such logic here!
  95. */
  96. /* Why 32?
  97. * For AVX ASM. SSE / NEON needs only 16.
  98. * Why not larger? Because I did not see a difference in benchmarks ...
  99. */
  100. /* benchmarks with P3
  101. * memalign(64) + 1 3071, 3051, 3032
  102. * memalign(64) + 2 3051, 3032, 3041
  103. * memalign(64) + 4 2911, 2896, 2915
  104. * memalign(64) + 8 2545, 2554, 2550
  105. * memalign(64) + 16 2543, 2572, 2563
  106. * memalign(64) + 32 2546, 2545, 2571
  107. * memalign(64) + 64 2570, 2533, 2558
  108. *
  109. * BTW, malloc seems to do 8-byte alignment by default here.
  110. */
  111. #else
  112. ptr = malloc(size);
  113. #endif
  114. if(!ptr && !size) {
  115. size = 1;
  116. ptr= av_malloc(1);
  117. }
  118. #if CONFIG_MEMORY_POISONING
  119. if (ptr)
  120. memset(ptr, 0x2a, size);
  121. #endif
  122. return ptr;
  123. }
  124. void *av_realloc(void *ptr, size_t size)
  125. {
  126. #if CONFIG_MEMALIGN_HACK
  127. int diff;
  128. #endif
  129. /* let's disallow possible ambiguous cases */
  130. if (size > (max_alloc_size - 32))
  131. return NULL;
  132. #if CONFIG_MEMALIGN_HACK
  133. //FIXME this isn't aligned correctly, though it probably isn't needed
  134. if (!ptr)
  135. return av_malloc(size);
  136. diff = ((char *)ptr)[-1];
  137. av_assert0(diff>0 && diff<=ALIGN);
  138. ptr = realloc((char *)ptr - diff, size + diff);
  139. if (ptr)
  140. ptr = (char *)ptr + diff;
  141. return ptr;
  142. #elif HAVE_ALIGNED_MALLOC
  143. return _aligned_realloc(ptr, size + !size, ALIGN);
  144. #else
  145. return realloc(ptr, size + !size);
  146. #endif
  147. }
  148. void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
  149. {
  150. size_t size;
  151. void *r;
  152. if (av_size_mult(elsize, nelem, &size)) {
  153. av_free(ptr);
  154. return NULL;
  155. }
  156. r = av_realloc(ptr, size);
  157. if (!r && size)
  158. av_free(ptr);
  159. return r;
  160. }
  161. void av_free(void *ptr)
  162. {
  163. #if CONFIG_MEMALIGN_HACK
  164. if (ptr) {
  165. int v= ((char *)ptr)[-1];
  166. av_assert0(v>0 && v<=ALIGN);
  167. free((char *)ptr - v);
  168. }
  169. #elif HAVE_ALIGNED_MALLOC
  170. _aligned_free(ptr);
  171. #else
  172. free(ptr);
  173. #endif
  174. }
  175. void av_freep(void *arg)
  176. {
  177. void **ptr = (void **)arg;
  178. av_free(*ptr);
  179. *ptr = NULL;
  180. }
  181. void *av_mallocz(size_t size)
  182. {
  183. void *ptr = av_malloc(size);
  184. if (ptr)
  185. memset(ptr, 0, size);
  186. return ptr;
  187. }
  188. void *av_calloc(size_t nmemb, size_t size)
  189. {
  190. if (size <= 0 || nmemb >= INT_MAX / size)
  191. return NULL;
  192. return av_mallocz(nmemb * size);
  193. }
  194. char *av_strdup(const char *s)
  195. {
  196. char *ptr = NULL;
  197. if (s) {
  198. int len = strlen(s) + 1;
  199. ptr = av_malloc(len);
  200. if (ptr)
  201. memcpy(ptr, s, len);
  202. }
  203. return ptr;
  204. }
  205. /* add one element to a dynamic array */
  206. void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
  207. {
  208. /* see similar ffmpeg.c:grow_array() */
  209. int nb, nb_alloc;
  210. intptr_t *tab;
  211. nb = *nb_ptr;
  212. tab = *(intptr_t**)tab_ptr;
  213. if ((nb & (nb - 1)) == 0) {
  214. if (nb == 0)
  215. nb_alloc = 1;
  216. else
  217. nb_alloc = nb * 2;
  218. tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
  219. *(intptr_t**)tab_ptr = tab;
  220. }
  221. tab[nb++] = (intptr_t)elem;
  222. *nb_ptr = nb;
  223. }
  224. static void fill16(uint8_t *dst, int len)
  225. {
  226. uint32_t v = AV_RN16(dst - 2);
  227. v |= v << 16;
  228. while (len >= 4) {
  229. AV_WN32(dst, v);
  230. dst += 4;
  231. len -= 4;
  232. }
  233. while (len--) {
  234. *dst = dst[-2];
  235. dst++;
  236. }
  237. }
  238. static void fill24(uint8_t *dst, int len)
  239. {
  240. #if HAVE_BIGENDIAN
  241. uint32_t v = AV_RB24(dst - 3);
  242. uint32_t a = v << 8 | v >> 16;
  243. uint32_t b = v << 16 | v >> 8;
  244. uint32_t c = v << 24 | v;
  245. #else
  246. uint32_t v = AV_RL24(dst - 3);
  247. uint32_t a = v | v << 24;
  248. uint32_t b = v >> 8 | v << 16;
  249. uint32_t c = v >> 16 | v << 8;
  250. #endif
  251. while (len >= 12) {
  252. AV_WN32(dst, a);
  253. AV_WN32(dst + 4, b);
  254. AV_WN32(dst + 8, c);
  255. dst += 12;
  256. len -= 12;
  257. }
  258. if (len >= 4) {
  259. AV_WN32(dst, a);
  260. dst += 4;
  261. len -= 4;
  262. }
  263. if (len >= 4) {
  264. AV_WN32(dst, b);
  265. dst += 4;
  266. len -= 4;
  267. }
  268. while (len--) {
  269. *dst = dst[-3];
  270. dst++;
  271. }
  272. }
  273. static void fill32(uint8_t *dst, int len)
  274. {
  275. uint32_t v = AV_RN32(dst - 4);
  276. while (len >= 4) {
  277. AV_WN32(dst, v);
  278. dst += 4;
  279. len -= 4;
  280. }
  281. while (len--) {
  282. *dst = dst[-4];
  283. dst++;
  284. }
  285. }
  286. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  287. {
  288. const uint8_t *src = &dst[-back];
  289. if (!back)
  290. return;
  291. if (back == 1) {
  292. memset(dst, *src, cnt);
  293. } else if (back == 2) {
  294. fill16(dst, cnt);
  295. } else if (back == 3) {
  296. fill24(dst, cnt);
  297. } else if (back == 4) {
  298. fill32(dst, cnt);
  299. } else {
  300. if (cnt >= 16) {
  301. int blocklen = back;
  302. while (cnt > blocklen) {
  303. memcpy(dst, src, blocklen);
  304. dst += blocklen;
  305. cnt -= blocklen;
  306. blocklen <<= 1;
  307. }
  308. memcpy(dst, src, cnt);
  309. return;
  310. }
  311. if (cnt >= 8) {
  312. AV_COPY32U(dst, src);
  313. AV_COPY32U(dst + 4, src + 4);
  314. src += 8;
  315. dst += 8;
  316. cnt -= 8;
  317. }
  318. if (cnt >= 4) {
  319. AV_COPY32U(dst, src);
  320. src += 4;
  321. dst += 4;
  322. cnt -= 4;
  323. }
  324. if (cnt >= 2) {
  325. AV_COPY16U(dst, src);
  326. src += 2;
  327. dst += 2;
  328. cnt -= 2;
  329. }
  330. if (cnt)
  331. *dst = *src;
  332. }
  333. }