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.

298 lines
6.8KB

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