mem.c 8.3 KB

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