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.

343 lines
7.6KB

  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. ret = av_realloc(*ptrptr, size);
  128. if (!ret) {
  129. av_freep(ptr);
  130. return AVERROR(ENOMEM);
  131. }
  132. *ptrptr = ret;
  133. return 0;
  134. }
  135. void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
  136. {
  137. if (!size || nmemb >= INT_MAX / size)
  138. return NULL;
  139. return av_realloc(ptr, nmemb * size);
  140. }
  141. int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
  142. {
  143. void **ptrptr = ptr;
  144. void *ret;
  145. if (!size || nmemb >= INT_MAX / size)
  146. return AVERROR(ENOMEM);
  147. if (!nmemb) {
  148. av_freep(ptr);
  149. return 0;
  150. }
  151. ret = av_realloc(*ptrptr, nmemb * size);
  152. if (!ret) {
  153. av_freep(ptr);
  154. return AVERROR(ENOMEM);
  155. }
  156. *ptrptr = ret;
  157. return 0;
  158. }
  159. void av_free(void *ptr)
  160. {
  161. #if CONFIG_MEMALIGN_HACK
  162. if (ptr)
  163. free((char *)ptr - ((char *)ptr)[-1]);
  164. #elif HAVE_ALIGNED_MALLOC
  165. _aligned_free(ptr);
  166. #else
  167. free(ptr);
  168. #endif
  169. }
  170. void av_freep(void *arg)
  171. {
  172. void **ptr = (void **)arg;
  173. av_free(*ptr);
  174. *ptr = NULL;
  175. }
  176. void *av_mallocz(size_t size)
  177. {
  178. void *ptr = av_malloc(size);
  179. if (ptr)
  180. memset(ptr, 0, size);
  181. return ptr;
  182. }
  183. char *av_strdup(const char *s)
  184. {
  185. char *ptr = NULL;
  186. if (s) {
  187. int len = strlen(s) + 1;
  188. ptr = av_malloc(len);
  189. if (ptr)
  190. memcpy(ptr, s, len);
  191. }
  192. return ptr;
  193. }
  194. static void fill16(uint8_t *dst, int len)
  195. {
  196. uint32_t v = AV_RN16(dst - 2);
  197. v |= v << 16;
  198. while (len >= 4) {
  199. AV_WN32(dst, v);
  200. dst += 4;
  201. len -= 4;
  202. }
  203. while (len--) {
  204. *dst = dst[-2];
  205. dst++;
  206. }
  207. }
  208. static void fill24(uint8_t *dst, int len)
  209. {
  210. #if HAVE_BIGENDIAN
  211. uint32_t v = AV_RB24(dst - 3);
  212. uint32_t a = v << 8 | v >> 16;
  213. uint32_t b = v << 16 | v >> 8;
  214. uint32_t c = v << 24 | v;
  215. #else
  216. uint32_t v = AV_RL24(dst - 3);
  217. uint32_t a = v | v << 24;
  218. uint32_t b = v >> 8 | v << 16;
  219. uint32_t c = v >> 16 | v << 8;
  220. #endif
  221. while (len >= 12) {
  222. AV_WN32(dst, a);
  223. AV_WN32(dst + 4, b);
  224. AV_WN32(dst + 8, c);
  225. dst += 12;
  226. len -= 12;
  227. }
  228. if (len >= 4) {
  229. AV_WN32(dst, a);
  230. dst += 4;
  231. len -= 4;
  232. }
  233. if (len >= 4) {
  234. AV_WN32(dst, b);
  235. dst += 4;
  236. len -= 4;
  237. }
  238. while (len--) {
  239. *dst = dst[-3];
  240. dst++;
  241. }
  242. }
  243. static void fill32(uint8_t *dst, int len)
  244. {
  245. uint32_t v = AV_RN32(dst - 4);
  246. while (len >= 4) {
  247. AV_WN32(dst, v);
  248. dst += 4;
  249. len -= 4;
  250. }
  251. while (len--) {
  252. *dst = dst[-4];
  253. dst++;
  254. }
  255. }
  256. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  257. {
  258. const uint8_t *src = &dst[-back];
  259. if (!back)
  260. return;
  261. if (back == 1) {
  262. memset(dst, *src, cnt);
  263. } else if (back == 2) {
  264. fill16(dst, cnt);
  265. } else if (back == 3) {
  266. fill24(dst, cnt);
  267. } else if (back == 4) {
  268. fill32(dst, cnt);
  269. } else {
  270. if (cnt >= 16) {
  271. int blocklen = back;
  272. while (cnt > blocklen) {
  273. memcpy(dst, src, blocklen);
  274. dst += blocklen;
  275. cnt -= blocklen;
  276. blocklen <<= 1;
  277. }
  278. memcpy(dst, src, cnt);
  279. return;
  280. }
  281. if (cnt >= 8) {
  282. AV_COPY32U(dst, src);
  283. AV_COPY32U(dst + 4, src + 4);
  284. src += 8;
  285. dst += 8;
  286. cnt -= 8;
  287. }
  288. if (cnt >= 4) {
  289. AV_COPY32U(dst, src);
  290. src += 4;
  291. dst += 4;
  292. cnt -= 4;
  293. }
  294. if (cnt >= 2) {
  295. AV_COPY16U(dst, src);
  296. src += 2;
  297. dst += 2;
  298. cnt -= 2;
  299. }
  300. if (cnt)
  301. *dst = *src;
  302. }
  303. }