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.

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