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.

347 lines
7.7KB

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