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.

383 lines
8.2KB

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