You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
8.4KB

  1. /*
  2. * default memory allocator for libavutil
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "config.h"
  26. #include <limits.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #if HAVE_MALLOC_H
  31. #include <malloc.h>
  32. #endif
  33. #include "avutil.h"
  34. #include "common.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. /* You can redefine av_malloc and av_free in your project to use your
  50. * memory allocator. You do not need to suppress this file because the
  51. * linker will do it automatically. */
  52. void *av_malloc(size_t size)
  53. {
  54. void *ptr = NULL;
  55. #if CONFIG_MEMALIGN_HACK
  56. long diff;
  57. #endif
  58. /* let's disallow possibly ambiguous cases */
  59. if (size > (INT_MAX - 32) || !size)
  60. return NULL;
  61. #if CONFIG_MEMALIGN_HACK
  62. ptr = malloc(size + 32);
  63. if (!ptr)
  64. return ptr;
  65. diff = ((-(long)ptr - 1) & 31) + 1;
  66. ptr = (char *)ptr + diff;
  67. ((char *)ptr)[-1] = diff;
  68. #elif HAVE_POSIX_MEMALIGN
  69. if (posix_memalign(&ptr, 32, size))
  70. ptr = NULL;
  71. #elif HAVE_ALIGNED_MALLOC
  72. ptr = _aligned_malloc(size, 32);
  73. #elif HAVE_MEMALIGN
  74. ptr = memalign(32, size);
  75. /* Why 64?
  76. * Indeed, we should align it:
  77. * on 4 for 386
  78. * on 16 for 486
  79. * on 32 for 586, PPro - K6-III
  80. * on 64 for K7 (maybe for P3 too).
  81. * Because L1 and L2 caches are aligned on those values.
  82. * But I don't want to code such logic here!
  83. */
  84. /* Why 32?
  85. * For AVX ASM. SSE / NEON needs only 16.
  86. * Why not larger? Because I did not see a difference in benchmarks ...
  87. */
  88. /* benchmarks with P3
  89. * memalign(64) + 1 3071, 3051, 3032
  90. * memalign(64) + 2 3051, 3032, 3041
  91. * memalign(64) + 4 2911, 2896, 2915
  92. * memalign(64) + 8 2545, 2554, 2550
  93. * memalign(64) + 16 2543, 2572, 2563
  94. * memalign(64) + 32 2546, 2545, 2571
  95. * memalign(64) + 64 2570, 2533, 2558
  96. *
  97. * BTW, malloc seems to do 8-byte alignment by default here.
  98. */
  99. #else
  100. ptr = malloc(size);
  101. #endif
  102. return ptr;
  103. }
  104. void *av_realloc(void *ptr, size_t size)
  105. {
  106. #if CONFIG_MEMALIGN_HACK
  107. int diff;
  108. #endif
  109. /* let's disallow possibly ambiguous cases */
  110. if (size > (INT_MAX - 16))
  111. return NULL;
  112. #if CONFIG_MEMALIGN_HACK
  113. //FIXME this isn't aligned correctly, though it probably isn't needed
  114. if (!ptr)
  115. return av_malloc(size);
  116. diff = ((char *)ptr)[-1];
  117. return (char *)realloc((char *)ptr - diff, size + diff) + diff;
  118. #elif HAVE_ALIGNED_MALLOC
  119. return _aligned_realloc(ptr, size, 32);
  120. #else
  121. return realloc(ptr, size);
  122. #endif
  123. }
  124. int av_reallocp(void *ptr, size_t size)
  125. {
  126. void **ptrptr = ptr;
  127. void *ret;
  128. if (!size) {
  129. av_freep(ptr);
  130. return 0;
  131. }
  132. ret = av_realloc(*ptrptr, size);
  133. if (!ret) {
  134. av_freep(ptr);
  135. return AVERROR(ENOMEM);
  136. }
  137. *ptrptr = ret;
  138. return 0;
  139. }
  140. void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
  141. {
  142. if (!size || nmemb >= INT_MAX / size)
  143. return NULL;
  144. return av_realloc(ptr, nmemb * size);
  145. }
  146. int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
  147. {
  148. void **ptrptr = ptr;
  149. void *ret;
  150. if (!size || nmemb >= INT_MAX / size)
  151. return AVERROR(ENOMEM);
  152. if (!nmemb) {
  153. av_freep(ptr);
  154. return 0;
  155. }
  156. ret = av_realloc(*ptrptr, nmemb * size);
  157. if (!ret) {
  158. av_freep(ptr);
  159. return AVERROR(ENOMEM);
  160. }
  161. *ptrptr = ret;
  162. return 0;
  163. }
  164. void av_free(void *ptr)
  165. {
  166. #if CONFIG_MEMALIGN_HACK
  167. if (ptr)
  168. free((char *)ptr - ((char *)ptr)[-1]);
  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. char *av_strdup(const char *s)
  189. {
  190. char *ptr = NULL;
  191. if (s) {
  192. int len = strlen(s) + 1;
  193. ptr = av_realloc(NULL, len);
  194. if (ptr)
  195. memcpy(ptr, s, len);
  196. }
  197. return ptr;
  198. }
  199. static void fill16(uint8_t *dst, int len)
  200. {
  201. uint32_t v = AV_RN16(dst - 2);
  202. v |= v << 16;
  203. while (len >= 4) {
  204. AV_WN32(dst, v);
  205. dst += 4;
  206. len -= 4;
  207. }
  208. while (len--) {
  209. *dst = dst[-2];
  210. dst++;
  211. }
  212. }
  213. static void fill24(uint8_t *dst, int len)
  214. {
  215. #if HAVE_BIGENDIAN
  216. uint32_t v = AV_RB24(dst - 3);
  217. uint32_t a = v << 8 | v >> 16;
  218. uint32_t b = v << 16 | v >> 8;
  219. uint32_t c = v << 24 | v;
  220. #else
  221. uint32_t v = AV_RL24(dst - 3);
  222. uint32_t a = v | v << 24;
  223. uint32_t b = v >> 8 | v << 16;
  224. uint32_t c = v >> 16 | v << 8;
  225. #endif
  226. while (len >= 12) {
  227. AV_WN32(dst, a);
  228. AV_WN32(dst + 4, b);
  229. AV_WN32(dst + 8, c);
  230. dst += 12;
  231. len -= 12;
  232. }
  233. if (len >= 4) {
  234. AV_WN32(dst, a);
  235. dst += 4;
  236. len -= 4;
  237. }
  238. if (len >= 4) {
  239. AV_WN32(dst, b);
  240. dst += 4;
  241. len -= 4;
  242. }
  243. while (len--) {
  244. *dst = dst[-3];
  245. dst++;
  246. }
  247. }
  248. static void fill32(uint8_t *dst, int len)
  249. {
  250. uint32_t v = AV_RN32(dst - 4);
  251. while (len >= 4) {
  252. AV_WN32(dst, v);
  253. dst += 4;
  254. len -= 4;
  255. }
  256. while (len--) {
  257. *dst = dst[-4];
  258. dst++;
  259. }
  260. }
  261. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  262. {
  263. const uint8_t *src = &dst[-back];
  264. if (!back)
  265. return;
  266. if (back == 1) {
  267. memset(dst, *src, cnt);
  268. } else if (back == 2) {
  269. fill16(dst, cnt);
  270. } else if (back == 3) {
  271. fill24(dst, cnt);
  272. } else if (back == 4) {
  273. fill32(dst, cnt);
  274. } else {
  275. if (cnt >= 16) {
  276. int blocklen = back;
  277. while (cnt > blocklen) {
  278. memcpy(dst, src, blocklen);
  279. dst += blocklen;
  280. cnt -= blocklen;
  281. blocklen <<= 1;
  282. }
  283. memcpy(dst, src, cnt);
  284. return;
  285. }
  286. if (cnt >= 8) {
  287. AV_COPY32U(dst, src);
  288. AV_COPY32U(dst + 4, src + 4);
  289. src += 8;
  290. dst += 8;
  291. cnt -= 8;
  292. }
  293. if (cnt >= 4) {
  294. AV_COPY32U(dst, src);
  295. src += 4;
  296. dst += 4;
  297. cnt -= 4;
  298. }
  299. if (cnt >= 2) {
  300. AV_COPY16U(dst, src);
  301. src += 2;
  302. dst += 2;
  303. cnt -= 2;
  304. }
  305. if (cnt)
  306. *dst = *src;
  307. }
  308. }
  309. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  310. {
  311. if (min_size < *size)
  312. return ptr;
  313. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  314. ptr = av_realloc(ptr, min_size);
  315. /* we could set this to the unmodified min_size but this is safer
  316. * if the user lost the ptr and uses NULL now
  317. */
  318. if (!ptr)
  319. min_size = 0;
  320. *size = min_size;
  321. return ptr;
  322. }
  323. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  324. {
  325. void **p = ptr;
  326. if (min_size < *size)
  327. return;
  328. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  329. av_free(*p);
  330. *p = av_malloc(min_size);
  331. if (!*p)
  332. min_size = 0;
  333. *size = min_size;
  334. }