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.

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