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.

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