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.

406 lines
8.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 "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 *val;
  127. if (!size) {
  128. av_freep(ptr);
  129. return 0;
  130. }
  131. memcpy(&val, ptr, sizeof(val));
  132. val = av_realloc(val, size);
  133. if (!val) {
  134. av_freep(ptr);
  135. return AVERROR(ENOMEM);
  136. }
  137. memcpy(ptr, &val, sizeof(val));
  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 *val;
  149. if (!size || nmemb >= INT_MAX / size)
  150. return AVERROR(ENOMEM);
  151. if (!nmemb) {
  152. av_freep(ptr);
  153. return 0;
  154. }
  155. memcpy(&val, ptr, sizeof(val));
  156. val = av_realloc(val, nmemb * size);
  157. if (!val) {
  158. av_freep(ptr);
  159. return AVERROR(ENOMEM);
  160. }
  161. memcpy(ptr, &val, sizeof(val));
  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 *val;
  178. memcpy(&val, arg, sizeof(val));
  179. memcpy(arg, &(void *){ NULL }, sizeof(val));
  180. av_free(val);
  181. }
  182. void *av_mallocz(size_t size)
  183. {
  184. void *ptr = av_malloc(size);
  185. if (ptr)
  186. memset(ptr, 0, size);
  187. return ptr;
  188. }
  189. char *av_strdup(const char *s)
  190. {
  191. char *ptr = NULL;
  192. if (s) {
  193. int len = strlen(s) + 1;
  194. ptr = av_realloc(NULL, len);
  195. if (ptr)
  196. memcpy(ptr, s, len);
  197. }
  198. return ptr;
  199. }
  200. char *av_strndup(const char *s, size_t len)
  201. {
  202. char *ret = NULL, *end;
  203. if (!s)
  204. return NULL;
  205. end = memchr(s, 0, len);
  206. if (end)
  207. len = end - s;
  208. ret = av_realloc(NULL, len + 1);
  209. if (!ret)
  210. return NULL;
  211. memcpy(ret, s, len);
  212. ret[len] = 0;
  213. return ret;
  214. }
  215. static void fill16(uint8_t *dst, int len)
  216. {
  217. uint32_t v = AV_RN16(dst - 2);
  218. v |= v << 16;
  219. while (len >= 4) {
  220. AV_WN32(dst, v);
  221. dst += 4;
  222. len -= 4;
  223. }
  224. while (len--) {
  225. *dst = dst[-2];
  226. dst++;
  227. }
  228. }
  229. static void fill24(uint8_t *dst, int len)
  230. {
  231. #if HAVE_BIGENDIAN
  232. uint32_t v = AV_RB24(dst - 3);
  233. uint32_t a = v << 8 | v >> 16;
  234. uint32_t b = v << 16 | v >> 8;
  235. uint32_t c = v << 24 | v;
  236. #else
  237. uint32_t v = AV_RL24(dst - 3);
  238. uint32_t a = v | v << 24;
  239. uint32_t b = v >> 8 | v << 16;
  240. uint32_t c = v >> 16 | v << 8;
  241. #endif
  242. while (len >= 12) {
  243. AV_WN32(dst, a);
  244. AV_WN32(dst + 4, b);
  245. AV_WN32(dst + 8, c);
  246. dst += 12;
  247. len -= 12;
  248. }
  249. if (len >= 4) {
  250. AV_WN32(dst, a);
  251. dst += 4;
  252. len -= 4;
  253. }
  254. if (len >= 4) {
  255. AV_WN32(dst, b);
  256. dst += 4;
  257. len -= 4;
  258. }
  259. while (len--) {
  260. *dst = dst[-3];
  261. dst++;
  262. }
  263. }
  264. static void fill32(uint8_t *dst, int len)
  265. {
  266. uint32_t v = AV_RN32(dst - 4);
  267. while (len >= 4) {
  268. AV_WN32(dst, v);
  269. dst += 4;
  270. len -= 4;
  271. }
  272. while (len--) {
  273. *dst = dst[-4];
  274. dst++;
  275. }
  276. }
  277. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  278. {
  279. const uint8_t *src = &dst[-back];
  280. if (!back)
  281. return;
  282. if (back == 1) {
  283. memset(dst, *src, cnt);
  284. } else if (back == 2) {
  285. fill16(dst, cnt);
  286. } else if (back == 3) {
  287. fill24(dst, cnt);
  288. } else if (back == 4) {
  289. fill32(dst, cnt);
  290. } else {
  291. if (cnt >= 16) {
  292. int blocklen = back;
  293. while (cnt > blocklen) {
  294. memcpy(dst, src, blocklen);
  295. dst += blocklen;
  296. cnt -= blocklen;
  297. blocklen <<= 1;
  298. }
  299. memcpy(dst, src, cnt);
  300. return;
  301. }
  302. if (cnt >= 8) {
  303. AV_COPY32U(dst, src);
  304. AV_COPY32U(dst + 4, src + 4);
  305. src += 8;
  306. dst += 8;
  307. cnt -= 8;
  308. }
  309. if (cnt >= 4) {
  310. AV_COPY32U(dst, src);
  311. src += 4;
  312. dst += 4;
  313. cnt -= 4;
  314. }
  315. if (cnt >= 2) {
  316. AV_COPY16U(dst, src);
  317. src += 2;
  318. dst += 2;
  319. cnt -= 2;
  320. }
  321. if (cnt)
  322. *dst = *src;
  323. }
  324. }
  325. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  326. {
  327. if (min_size < *size)
  328. return ptr;
  329. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  330. ptr = av_realloc(ptr, min_size);
  331. /* we could set this to the unmodified min_size but this is safer
  332. * if the user lost the ptr and uses NULL now
  333. */
  334. if (!ptr)
  335. min_size = 0;
  336. *size = min_size;
  337. return ptr;
  338. }
  339. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  340. {
  341. void **p = ptr;
  342. if (min_size < *size)
  343. return;
  344. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  345. av_free(*p);
  346. *p = av_malloc(min_size);
  347. if (!*p)
  348. min_size = 0;
  349. *size = min_size;
  350. }