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.

217 lines
5.5KB

  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. /* here we can use OS-dependent allocation functions */
  37. #undef free
  38. #undef malloc
  39. #undef realloc
  40. #ifdef MALLOC_PREFIX
  41. #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
  42. #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
  43. #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
  44. #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
  45. #define free AV_JOIN(MALLOC_PREFIX, free)
  46. void *malloc(size_t size);
  47. void *memalign(size_t align, size_t size);
  48. int posix_memalign(void **ptr, size_t align, size_t size);
  49. void *realloc(void *ptr, size_t size);
  50. void free(void *ptr);
  51. #endif /* MALLOC_PREFIX */
  52. /* You can redefine av_malloc and av_free in your project to use your
  53. * memory allocator. You do not need to suppress this file because the
  54. * linker will do it automatically. */
  55. void *av_malloc(size_t size)
  56. {
  57. void *ptr = NULL;
  58. #if CONFIG_MEMALIGN_HACK
  59. long diff;
  60. #endif
  61. /* let's disallow possible ambiguous cases */
  62. if (size > (INT_MAX - 32) || !size)
  63. return NULL;
  64. #if CONFIG_MEMALIGN_HACK
  65. ptr = malloc(size + 32);
  66. if (!ptr)
  67. return ptr;
  68. diff = ((-(long)ptr - 1) & 31) + 1;
  69. ptr = (char *)ptr + diff;
  70. ((char *)ptr)[-1] = diff;
  71. #elif HAVE_POSIX_MEMALIGN
  72. if (posix_memalign(&ptr, 32, size))
  73. ptr = NULL;
  74. #elif HAVE_ALIGNED_MALLOC
  75. ptr = _aligned_malloc(size, 32);
  76. #elif HAVE_MEMALIGN
  77. ptr = memalign(32, size);
  78. /* Why 64?
  79. * Indeed, we should align it:
  80. * on 4 for 386
  81. * on 16 for 486
  82. * on 32 for 586, PPro - K6-III
  83. * on 64 for K7 (maybe for P3 too).
  84. * Because L1 and L2 caches are aligned on those values.
  85. * But I don't want to code such logic here!
  86. */
  87. /* Why 32?
  88. * For AVX ASM. SSE / NEON needs only 16.
  89. * Why not larger? Because I did not see a difference in benchmarks ...
  90. */
  91. /* benchmarks with P3
  92. * memalign(64) + 1 3071, 3051, 3032
  93. * memalign(64) + 2 3051, 3032, 3041
  94. * memalign(64) + 4 2911, 2896, 2915
  95. * memalign(64) + 8 2545, 2554, 2550
  96. * memalign(64) + 16 2543, 2572, 2563
  97. * memalign(64) + 32 2546, 2545, 2571
  98. * memalign(64) + 64 2570, 2533, 2558
  99. *
  100. * BTW, malloc seems to do 8-byte alignment by default here.
  101. */
  102. #else
  103. ptr = malloc(size);
  104. #endif
  105. return ptr;
  106. }
  107. void *av_realloc(void *ptr, size_t size)
  108. {
  109. #if CONFIG_MEMALIGN_HACK
  110. int diff;
  111. #endif
  112. /* let's disallow possible ambiguous cases */
  113. if (size > (INT_MAX - 16))
  114. return NULL;
  115. #if CONFIG_MEMALIGN_HACK
  116. //FIXME this isn't aligned correctly, though it probably isn't needed
  117. if (!ptr)
  118. return av_malloc(size);
  119. diff = ((char *)ptr)[-1];
  120. return (char *)realloc((char *)ptr - diff, size + diff) + diff;
  121. #elif HAVE_ALIGNED_MALLOC
  122. return _aligned_realloc(ptr, size, 32);
  123. #else
  124. return realloc(ptr, size);
  125. #endif
  126. }
  127. void av_free(void *ptr)
  128. {
  129. #if CONFIG_MEMALIGN_HACK
  130. if (ptr)
  131. free((char *)ptr - ((char *)ptr)[-1]);
  132. #elif HAVE_ALIGNED_MALLOC
  133. _aligned_free(ptr);
  134. #else
  135. free(ptr);
  136. #endif
  137. }
  138. void av_freep(void *arg)
  139. {
  140. void **ptr = (void **)arg;
  141. av_free(*ptr);
  142. *ptr = NULL;
  143. }
  144. void *av_mallocz(size_t size)
  145. {
  146. void *ptr = av_malloc(size);
  147. if (ptr)
  148. memset(ptr, 0, size);
  149. return ptr;
  150. }
  151. char *av_strdup(const char *s)
  152. {
  153. char *ptr = NULL;
  154. if (s) {
  155. int len = strlen(s) + 1;
  156. ptr = av_malloc(len);
  157. if (ptr)
  158. memcpy(ptr, s, len);
  159. }
  160. return ptr;
  161. }
  162. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  163. {
  164. const uint8_t *src = &dst[-back];
  165. if (back == 1) {
  166. memset(dst, *src, cnt);
  167. } else {
  168. if (cnt >= 4) {
  169. AV_COPY16U(dst, src);
  170. AV_COPY16U(dst + 2, src + 2);
  171. src += 4;
  172. dst += 4;
  173. cnt -= 4;
  174. }
  175. if (cnt >= 8) {
  176. AV_COPY16U(dst, src);
  177. AV_COPY16U(dst + 2, src + 2);
  178. AV_COPY16U(dst + 4, src + 4);
  179. AV_COPY16U(dst + 6, src + 6);
  180. src += 8;
  181. dst += 8;
  182. cnt -= 8;
  183. }
  184. if (cnt > 0) {
  185. int blocklen = back;
  186. while (cnt > blocklen) {
  187. memcpy(dst, src, blocklen);
  188. dst += blocklen;
  189. cnt -= blocklen;
  190. blocklen <<= 1;
  191. }
  192. memcpy(dst, src, cnt);
  193. }
  194. }
  195. }