| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * default memory allocator for libavutil | ||
| 3 | * Copyright (c) 2002 Fabrice Bellard | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg 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 | * FFmpeg 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 FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * default memory allocator for libavutil | ||
| 25 | */ | ||
| 26 | |||
| 27 | #define _XOPEN_SOURCE 600 | ||
| 28 | |||
| 29 | #include "config.h" | ||
| 30 | |||
| 31 | #include <limits.h> | ||
| 32 | #include <stdint.h> | ||
| 33 | #include <stdlib.h> | ||
| 34 | #include <stdatomic.h> | ||
| 35 | #include <string.h> | ||
| 36 | #if HAVE_MALLOC_H | ||
| 37 | #include <malloc.h> | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #include "attributes.h" | ||
| 41 | #include "avassert.h" | ||
| 42 | #include "dynarray.h" | ||
| 43 | #include "error.h" | ||
| 44 | #include "internal.h" | ||
| 45 | #include "intreadwrite.h" | ||
| 46 | #include "macros.h" | ||
| 47 | #include "mem.h" | ||
| 48 | |||
| 49 | #ifdef MALLOC_PREFIX | ||
| 50 | |||
| 51 | #define malloc AV_JOIN(MALLOC_PREFIX, malloc) | ||
| 52 | #define memalign AV_JOIN(MALLOC_PREFIX, memalign) | ||
| 53 | #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign) | ||
| 54 | #define realloc AV_JOIN(MALLOC_PREFIX, realloc) | ||
| 55 | #define free AV_JOIN(MALLOC_PREFIX, free) | ||
| 56 | |||
| 57 | void *malloc(size_t size); | ||
| 58 | void *memalign(size_t align, size_t size); | ||
| 59 | int posix_memalign(void **ptr, size_t align, size_t size); | ||
| 60 | void *realloc(void *ptr, size_t size); | ||
| 61 | void free(void *ptr); | ||
| 62 | |||
| 63 | #endif /* MALLOC_PREFIX */ | ||
| 64 | |||
| 65 | #define ALIGN (HAVE_SIMD_ALIGN_64 ? 64 : (HAVE_SIMD_ALIGN_32 ? 32 : 16)) | ||
| 66 | |||
| 67 | #define FF_MEMORY_POISON 0x2a | ||
| 68 | |||
| 69 | /* NOTE: if you want to override these functions with your own | ||
| 70 | * implementations (not recommended) you have to link libav* as | ||
| 71 | * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags. | ||
| 72 | * Note that this will cost performance. */ | ||
| 73 | |||
| 74 | static atomic_size_t max_alloc_size = INT_MAX; | ||
| 75 | |||
| 76 | 2 | void av_max_alloc(size_t max){ | |
| 77 | 2 | atomic_store_explicit(&max_alloc_size, max, memory_order_relaxed); | |
| 78 | 2 | } | |
| 79 | |||
| 80 | 11273311 | static int size_mult(size_t a, size_t b, size_t *r) | |
| 81 | { | ||
| 82 | size_t t; | ||
| 83 | |||
| 84 | #if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_mul_overflow) | ||
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11273311 times.
|
11273311 | if (__builtin_mul_overflow(a, b, &t)) |
| 86 | ✗ | return AVERROR(EINVAL); | |
| 87 | #else | ||
| 88 | t = a * b; | ||
| 89 | /* Hack inspired from glibc: don't try the division if nelem and elsize | ||
| 90 | * are both less than sqrt(SIZE_MAX). */ | ||
| 91 | if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b) | ||
| 92 | return AVERROR(EINVAL); | ||
| 93 | #endif | ||
| 94 | 11273311 | *r = t; | |
| 95 | 11273311 | return 0; | |
| 96 | } | ||
| 97 | |||
| 98 | 29459013 | void *av_malloc(size_t size) | |
| 99 | { | ||
| 100 | 29459013 | void *ptr = NULL; | |
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29459011 times.
|
29459013 | if (size > atomic_load_explicit(&max_alloc_size, memory_order_relaxed)) |
| 103 | 2 | return NULL; | |
| 104 | |||
| 105 | #if HAVE_POSIX_MEMALIGN | ||
| 106 |
2/2✓ Branch 0 taken 29439676 times.
✓ Branch 1 taken 19335 times.
|
29459011 | if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation |
| 107 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 29439676 times.
|
29439676 | if (posix_memalign(&ptr, ALIGN, size)) |
| 108 | ✗ | ptr = NULL; | |
| 109 | #elif HAVE_ALIGNED_MALLOC | ||
| 110 | ptr = _aligned_malloc(size, ALIGN); | ||
| 111 | #elif HAVE_MEMALIGN | ||
| 112 | #ifndef __DJGPP__ | ||
| 113 | ptr = memalign(ALIGN, size); | ||
| 114 | #else | ||
| 115 | ptr = memalign(size, ALIGN); | ||
| 116 | #endif | ||
| 117 | /* Why 64? | ||
| 118 | * Indeed, we should align it: | ||
| 119 | * on 4 for 386 | ||
| 120 | * on 16 for 486 | ||
| 121 | * on 32 for 586, PPro - K6-III | ||
| 122 | * on 64 for K7 (maybe for P3 too). | ||
| 123 | * Because L1 and L2 caches are aligned on those values. | ||
| 124 | * But I don't want to code such logic here! | ||
| 125 | */ | ||
| 126 | /* Why 32? | ||
| 127 | * For AVX ASM. SSE / NEON needs only 16. | ||
| 128 | * Why not larger? Because I did not see a difference in benchmarks ... | ||
| 129 | */ | ||
| 130 | /* benchmarks with P3 | ||
| 131 | * memalign(64) + 1 3071, 3051, 3032 | ||
| 132 | * memalign(64) + 2 3051, 3032, 3041 | ||
| 133 | * memalign(64) + 4 2911, 2896, 2915 | ||
| 134 | * memalign(64) + 8 2545, 2554, 2550 | ||
| 135 | * memalign(64) + 16 2543, 2572, 2563 | ||
| 136 | * memalign(64) + 32 2546, 2545, 2571 | ||
| 137 | * memalign(64) + 64 2570, 2533, 2558 | ||
| 138 | * | ||
| 139 | * BTW, malloc seems to do 8-byte alignment by default here. | ||
| 140 | */ | ||
| 141 | #else | ||
| 142 | ptr = malloc(size); | ||
| 143 | #endif | ||
| 144 |
3/4✓ Branch 0 taken 19335 times.
✓ Branch 1 taken 29439676 times.
✓ Branch 2 taken 19335 times.
✗ Branch 3 not taken.
|
29459011 | if(!ptr && !size) { |
| 145 | 19335 | size = 1; | |
| 146 | 19335 | ptr= av_malloc(1); | |
| 147 | } | ||
| 148 | #if CONFIG_MEMORY_POISONING | ||
| 149 |
1/2✓ Branch 0 taken 29459011 times.
✗ Branch 1 not taken.
|
29459011 | if (ptr) |
| 150 | 29459011 | memset(ptr, FF_MEMORY_POISON, size); | |
| 151 | #endif | ||
| 152 | 29459011 | return ptr; | |
| 153 | } | ||
| 154 | |||
| 155 | 13781423 | void *av_realloc(void *ptr, size_t size) | |
| 156 | { | ||
| 157 | void *ret; | ||
| 158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13781423 times.
|
13781423 | if (size > atomic_load_explicit(&max_alloc_size, memory_order_relaxed)) |
| 159 | ✗ | return NULL; | |
| 160 | |||
| 161 | #if HAVE_ALIGNED_MALLOC | ||
| 162 | ret = _aligned_realloc(ptr, size + !size, ALIGN); | ||
| 163 | #else | ||
| 164 | 13781423 | ret = realloc(ptr, size + !size); | |
| 165 | #endif | ||
| 166 | #if CONFIG_MEMORY_POISONING | ||
| 167 |
3/4✓ Branch 0 taken 13781423 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7122227 times.
✓ Branch 3 taken 6659196 times.
|
13781423 | if (ret && !ptr) |
| 168 | 7122227 | memset(ret, FF_MEMORY_POISON, size); | |
| 169 | #endif | ||
| 170 | 13781423 | return ret; | |
| 171 | } | ||
| 172 | |||
| 173 | 65440 | void *av_realloc_f(void *ptr, size_t nelem, size_t elsize) | |
| 174 | { | ||
| 175 | size_t size; | ||
| 176 | void *r; | ||
| 177 | |||
| 178 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 65440 times.
|
65440 | if (size_mult(elsize, nelem, &size)) { |
| 179 | ✗ | av_free(ptr); | |
| 180 | ✗ | return NULL; | |
| 181 | } | ||
| 182 | 65440 | r = av_realloc(ptr, size); | |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65440 times.
|
65440 | if (!r) |
| 184 | ✗ | av_free(ptr); | |
| 185 | 65440 | return r; | |
| 186 | } | ||
| 187 | |||
| 188 | 21021 | int av_reallocp(void *ptr, size_t size) | |
| 189 | { | ||
| 190 | void *val; | ||
| 191 | |||
| 192 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 21012 times.
|
21021 | if (!size) { |
| 193 | 9 | av_freep(ptr); | |
| 194 | 9 | return 0; | |
| 195 | } | ||
| 196 | |||
| 197 | 21012 | memcpy(&val, ptr, sizeof(val)); | |
| 198 | 21012 | val = av_realloc(val, size); | |
| 199 | |||
| 200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21012 times.
|
21012 | if (!val) { |
| 201 | ✗ | av_freep(ptr); | |
| 202 | ✗ | return AVERROR(ENOMEM); | |
| 203 | } | ||
| 204 | |||
| 205 | 21012 | memcpy(ptr, &val, sizeof(val)); | |
| 206 | 21012 | return 0; | |
| 207 | } | ||
| 208 | |||
| 209 | 560192 | void *av_malloc_array(size_t nmemb, size_t size) | |
| 210 | { | ||
| 211 | size_t result; | ||
| 212 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 560192 times.
|
560192 | if (size_mult(nmemb, size, &result) < 0) |
| 213 | ✗ | return NULL; | |
| 214 | 560192 | return av_malloc(result); | |
| 215 | } | ||
| 216 | |||
| 217 | 6951091 | void *av_realloc_array(void *ptr, size_t nmemb, size_t size) | |
| 218 | { | ||
| 219 | size_t result; | ||
| 220 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6951091 times.
|
6951091 | if (size_mult(nmemb, size, &result) < 0) |
| 221 | ✗ | return NULL; | |
| 222 | 6951091 | return av_realloc(ptr, result); | |
| 223 | } | ||
| 224 | |||
| 225 | 6833 | int av_reallocp_array(void *ptr, size_t nmemb, size_t size) | |
| 226 | { | ||
| 227 | void *val; | ||
| 228 | |||
| 229 | 6833 | memcpy(&val, ptr, sizeof(val)); | |
| 230 | 6833 | val = av_realloc_f(val, nmemb, size); | |
| 231 | 6833 | memcpy(ptr, &val, sizeof(val)); | |
| 232 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 6833 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
6833 | if (!val && nmemb && size) |
| 233 | ✗ | return AVERROR(ENOMEM); | |
| 234 | |||
| 235 | 6833 | return 0; | |
| 236 | } | ||
| 237 | |||
| 238 | 84933617 | void av_free(void *ptr) | |
| 239 | { | ||
| 240 | #if HAVE_ALIGNED_MALLOC | ||
| 241 | _aligned_free(ptr); | ||
| 242 | #else | ||
| 243 | 84933617 | free(ptr); | |
| 244 | #endif | ||
| 245 | 84933617 | } | |
| 246 | |||
| 247 | 72563462 | void av_freep(void *arg) | |
| 248 | { | ||
| 249 | void *val; | ||
| 250 | |||
| 251 | 72563462 | memcpy(&val, arg, sizeof(val)); | |
| 252 | 72563462 | memcpy(arg, &(void *){ NULL }, sizeof(val)); | |
| 253 | 72563462 | av_free(val); | |
| 254 | 72563462 | } | |
| 255 | |||
| 256 | 23740147 | void *av_mallocz(size_t size) | |
| 257 | { | ||
| 258 | 23740147 | void *ptr = av_malloc(size); | |
| 259 |
2/2✓ Branch 0 taken 23740146 times.
✓ Branch 1 taken 1 times.
|
23740147 | if (ptr) |
| 260 | 23740146 | memset(ptr, 0, size); | |
| 261 | 23740147 | return ptr; | |
| 262 | } | ||
| 263 | |||
| 264 | 3694467 | void *av_calloc(size_t nmemb, size_t size) | |
| 265 | { | ||
| 266 | size_t result; | ||
| 267 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3694467 times.
|
3694467 | if (size_mult(nmemb, size, &result) < 0) |
| 268 | ✗ | return NULL; | |
| 269 | 3694467 | return av_mallocz(result); | |
| 270 | } | ||
| 271 | |||
| 272 | 2290480 | char *av_strdup(const char *s) | |
| 273 | { | ||
| 274 | 2290480 | char *ptr = NULL; | |
| 275 |
2/2✓ Branch 0 taken 2290479 times.
✓ Branch 1 taken 1 times.
|
2290480 | if (s) { |
| 276 | 2290479 | size_t len = strlen(s) + 1; | |
| 277 | 2290479 | ptr = av_realloc(NULL, len); | |
| 278 |
1/2✓ Branch 0 taken 2290479 times.
✗ Branch 1 not taken.
|
2290479 | if (ptr) |
| 279 | 2290479 | memcpy(ptr, s, len); | |
| 280 | } | ||
| 281 | 2290480 | return ptr; | |
| 282 | } | ||
| 283 | |||
| 284 | 1 | char *av_strndup(const char *s, size_t len) | |
| 285 | { | ||
| 286 | 1 | char *ret = NULL, *end; | |
| 287 | |||
| 288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!s) |
| 289 | ✗ | return NULL; | |
| 290 | |||
| 291 | 1 | end = memchr(s, 0, len); | |
| 292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (end) |
| 293 | ✗ | len = end - s; | |
| 294 | |||
| 295 | 1 | ret = av_realloc(NULL, len + 1); | |
| 296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!ret) |
| 297 | ✗ | return NULL; | |
| 298 | |||
| 299 | 1 | memcpy(ret, s, len); | |
| 300 | 1 | ret[len] = 0; | |
| 301 | 1 | return ret; | |
| 302 | } | ||
| 303 | |||
| 304 | 211041 | void *av_memdup(const void *p, size_t size) | |
| 305 | { | ||
| 306 | 211041 | void *ptr = NULL; | |
| 307 |
1/2✓ Branch 0 taken 211041 times.
✗ Branch 1 not taken.
|
211041 | if (p) { |
| 308 | 211041 | ptr = av_malloc(size); | |
| 309 |
1/2✓ Branch 0 taken 211041 times.
✗ Branch 1 not taken.
|
211041 | if (ptr) |
| 310 | 211041 | memcpy(ptr, p, size); | |
| 311 | } | ||
| 312 | 211041 | return ptr; | |
| 313 | } | ||
| 314 | |||
| 315 | 150259 | int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem) | |
| 316 | { | ||
| 317 | void **tab; | ||
| 318 | 150259 | memcpy(&tab, tab_ptr, sizeof(tab)); | |
| 319 | |||
| 320 |
7/10✓ Branch 0 taken 145350 times.
✓ Branch 1 taken 4909 times.
✓ Branch 2 taken 21004 times.
✓ Branch 3 taken 124346 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 145350 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 145350 times.
✓ Branch 9 taken 150259 times.
✗ Branch 10 not taken.
|
150259 | FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, { |
| 321 | tab[*nb_ptr] = elem; | ||
| 322 | memcpy(tab_ptr, &tab, sizeof(tab)); | ||
| 323 | }, { | ||
| 324 | return AVERROR(ENOMEM); | ||
| 325 | }); | ||
| 326 | 150259 | return 0; | |
| 327 | } | ||
| 328 | |||
| 329 | 1351 | void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem) | |
| 330 | { | ||
| 331 | void **tab; | ||
| 332 | 1351 | memcpy(&tab, tab_ptr, sizeof(tab)); | |
| 333 | |||
| 334 |
7/10✓ Branch 0 taken 246 times.
✓ Branch 1 taken 1105 times.
✓ Branch 2 taken 154 times.
✓ Branch 3 taken 92 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 246 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 246 times.
✓ Branch 9 taken 1351 times.
✗ Branch 10 not taken.
|
1351 | FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, { |
| 335 | tab[*nb_ptr] = elem; | ||
| 336 | memcpy(tab_ptr, &tab, sizeof(tab)); | ||
| 337 | }, { | ||
| 338 | *nb_ptr = 0; | ||
| 339 | av_freep(tab_ptr); | ||
| 340 | }); | ||
| 341 | 1351 | } | |
| 342 | |||
| 343 | 1190862 | void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, | |
| 344 | const uint8_t *elem_data) | ||
| 345 | { | ||
| 346 | 1190862 | uint8_t *tab_elem_data = NULL; | |
| 347 | |||
| 348 |
9/12✓ Branch 0 taken 573188 times.
✓ Branch 1 taken 617674 times.
✓ Branch 2 taken 388638 times.
✓ Branch 3 taken 184550 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 573188 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 573188 times.
✓ Branch 9 taken 1190862 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 234 times.
✓ Branch 12 taken 1190628 times.
|
1190862 | FF_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, { |
| 349 | tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size; | ||
| 350 | if (elem_data) | ||
| 351 | memcpy(tab_elem_data, elem_data, elem_size); | ||
| 352 | else if (CONFIG_MEMORY_POISONING) | ||
| 353 | memset(tab_elem_data, FF_MEMORY_POISON, elem_size); | ||
| 354 | }, { | ||
| 355 | av_freep(tab_ptr); | ||
| 356 | *nb_ptr = 0; | ||
| 357 | }); | ||
| 358 | 1190862 | return tab_elem_data; | |
| 359 | } | ||
| 360 | |||
| 361 | 147132 | static void fill16(uint8_t *dst, int len) | |
| 362 | { | ||
| 363 | 147132 | uint32_t v = AV_RN16(dst - 2); | |
| 364 | |||
| 365 | 147132 | v |= v << 16; | |
| 366 | |||
| 367 |
2/2✓ Branch 0 taken 82898346 times.
✓ Branch 1 taken 147132 times.
|
83045478 | while (len >= 4) { |
| 368 | 82898346 | AV_WN32(dst, v); | |
| 369 | 82898346 | dst += 4; | |
| 370 | 82898346 | len -= 4; | |
| 371 | } | ||
| 372 | |||
| 373 |
2/2✓ Branch 0 taken 282093 times.
✓ Branch 1 taken 147132 times.
|
429225 | while (len--) { |
| 374 | 282093 | *dst = dst[-2]; | |
| 375 | 282093 | dst++; | |
| 376 | } | ||
| 377 | 147132 | } | |
| 378 | |||
| 379 | 738 | static void fill24(uint8_t *dst, int len) | |
| 380 | { | ||
| 381 | #if HAVE_BIGENDIAN | ||
| 382 | uint32_t v = AV_RB24(dst - 3); | ||
| 383 | uint32_t a = v << 8 | v >> 16; | ||
| 384 | uint32_t b = v << 16 | v >> 8; | ||
| 385 | uint32_t c = v << 24 | v; | ||
| 386 | #else | ||
| 387 | 738 | uint32_t v = AV_RL24(dst - 3); | |
| 388 | 738 | uint32_t a = v | v << 24; | |
| 389 | 738 | uint32_t b = v >> 8 | v << 16; | |
| 390 | 738 | uint32_t c = v >> 16 | v << 8; | |
| 391 | #endif | ||
| 392 | |||
| 393 |
2/2✓ Branch 0 taken 2508 times.
✓ Branch 1 taken 738 times.
|
3246 | while (len >= 12) { |
| 394 | 2508 | AV_WN32(dst, a); | |
| 395 | 2508 | AV_WN32(dst + 4, b); | |
| 396 | 2508 | AV_WN32(dst + 8, c); | |
| 397 | 2508 | dst += 12; | |
| 398 | 2508 | len -= 12; | |
| 399 | } | ||
| 400 | |||
| 401 |
2/2✓ Branch 0 taken 283 times.
✓ Branch 1 taken 455 times.
|
738 | if (len >= 4) { |
| 402 | 283 | AV_WN32(dst, a); | |
| 403 | 283 | dst += 4; | |
| 404 | 283 | len -= 4; | |
| 405 | } | ||
| 406 | |||
| 407 |
2/2✓ Branch 0 taken 157 times.
✓ Branch 1 taken 581 times.
|
738 | if (len >= 4) { |
| 408 | 157 | AV_WN32(dst, b); | |
| 409 | 157 | dst += 4; | |
| 410 | 157 | len -= 4; | |
| 411 | } | ||
| 412 | |||
| 413 |
2/2✓ Branch 0 taken 1580 times.
✓ Branch 1 taken 738 times.
|
2318 | while (len--) { |
| 414 | 1580 | *dst = dst[-3]; | |
| 415 | 1580 | dst++; | |
| 416 | } | ||
| 417 | 738 | } | |
| 418 | |||
| 419 | 15472 | static void fill32(uint8_t *dst, int len) | |
| 420 | { | ||
| 421 | 15472 | uint32_t v = AV_RN32(dst - 4); | |
| 422 | |||
| 423 | #if HAVE_FAST_64BIT | ||
| 424 | 15472 | uint64_t v2= v + ((uint64_t)v<<32); | |
| 425 |
2/2✓ Branch 0 taken 105234 times.
✓ Branch 1 taken 15472 times.
|
120706 | while (len >= 32) { |
| 426 | 105234 | AV_WN64(dst , v2); | |
| 427 | 105234 | AV_WN64(dst+ 8, v2); | |
| 428 | 105234 | AV_WN64(dst+16, v2); | |
| 429 | 105234 | AV_WN64(dst+24, v2); | |
| 430 | 105234 | dst += 32; | |
| 431 | 105234 | len -= 32; | |
| 432 | } | ||
| 433 | #endif | ||
| 434 | |||
| 435 |
2/2✓ Branch 0 taken 60901 times.
✓ Branch 1 taken 15472 times.
|
76373 | while (len >= 4) { |
| 436 | 60901 | AV_WN32(dst, v); | |
| 437 | 60901 | dst += 4; | |
| 438 | 60901 | len -= 4; | |
| 439 | } | ||
| 440 | |||
| 441 |
2/2✓ Branch 0 taken 2266 times.
✓ Branch 1 taken 15472 times.
|
17738 | while (len--) { |
| 442 | 2266 | *dst = dst[-4]; | |
| 443 | 2266 | dst++; | |
| 444 | } | ||
| 445 | 15472 | } | |
| 446 | |||
| 447 | 808219 | void av_memcpy_backptr(uint8_t *dst, int back, int cnt) | |
| 448 | { | ||
| 449 | 808219 | const uint8_t *src = &dst[-back]; | |
| 450 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 808219 times.
|
808219 | if (!back) |
| 451 | ✗ | return; | |
| 452 | |||
| 453 |
2/2✓ Branch 0 taken 5265 times.
✓ Branch 1 taken 802954 times.
|
808219 | if (back == 1) { |
| 454 | 5265 | memset(dst, *src, cnt); | |
| 455 |
2/2✓ Branch 0 taken 147132 times.
✓ Branch 1 taken 655822 times.
|
802954 | } else if (back == 2) { |
| 456 | 147132 | fill16(dst, cnt); | |
| 457 |
2/2✓ Branch 0 taken 738 times.
✓ Branch 1 taken 655084 times.
|
655822 | } else if (back == 3) { |
| 458 | 738 | fill24(dst, cnt); | |
| 459 |
2/2✓ Branch 0 taken 15472 times.
✓ Branch 1 taken 639612 times.
|
655084 | } else if (back == 4) { |
| 460 | 15472 | fill32(dst, cnt); | |
| 461 | } else { | ||
| 462 |
2/2✓ Branch 0 taken 104834 times.
✓ Branch 1 taken 534778 times.
|
639612 | if (cnt >= 16) { |
| 463 | 104834 | int blocklen = back; | |
| 464 |
2/2✓ Branch 0 taken 37416 times.
✓ Branch 1 taken 104834 times.
|
142250 | while (cnt > blocklen) { |
| 465 | 37416 | memcpy(dst, src, blocklen); | |
| 466 | 37416 | dst += blocklen; | |
| 467 | 37416 | cnt -= blocklen; | |
| 468 | 37416 | blocklen <<= 1; | |
| 469 | } | ||
| 470 | 104834 | memcpy(dst, src, cnt); | |
| 471 | 104834 | return; | |
| 472 | } | ||
| 473 |
2/2✓ Branch 0 taken 116277 times.
✓ Branch 1 taken 418501 times.
|
534778 | if (cnt >= 8) { |
| 474 | 116277 | AV_COPY32U(dst, src); | |
| 475 | 116277 | AV_COPY32U(dst + 4, src + 4); | |
| 476 | 116277 | src += 8; | |
| 477 | 116277 | dst += 8; | |
| 478 | 116277 | cnt -= 8; | |
| 479 | } | ||
| 480 |
2/2✓ Branch 0 taken 383459 times.
✓ Branch 1 taken 151319 times.
|
534778 | if (cnt >= 4) { |
| 481 | 383459 | AV_COPY32U(dst, src); | |
| 482 | 383459 | src += 4; | |
| 483 | 383459 | dst += 4; | |
| 484 | 383459 | cnt -= 4; | |
| 485 | } | ||
| 486 |
2/2✓ Branch 0 taken 192819 times.
✓ Branch 1 taken 341959 times.
|
534778 | if (cnt >= 2) { |
| 487 | 192819 | AV_COPY16U(dst, src); | |
| 488 | 192819 | src += 2; | |
| 489 | 192819 | dst += 2; | |
| 490 | 192819 | cnt -= 2; | |
| 491 | } | ||
| 492 |
2/2✓ Branch 0 taken 118885 times.
✓ Branch 1 taken 415893 times.
|
534778 | if (cnt) |
| 493 | 118885 | *dst = *src; | |
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | 1903474 | void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) | |
| 498 | { | ||
| 499 | size_t max_size; | ||
| 500 | |||
| 501 |
2/2✓ Branch 0 taken 1790030 times.
✓ Branch 1 taken 113444 times.
|
1903474 | if (min_size <= *size) |
| 502 | 1790030 | return ptr; | |
| 503 | |||
| 504 | 113444 | max_size = atomic_load_explicit(&max_alloc_size, memory_order_relaxed); | |
| 505 | /* *size is an unsigned, so the real maximum is <= UINT_MAX. */ | ||
| 506 | 113444 | max_size = FFMIN(max_size, UINT_MAX); | |
| 507 | |||
| 508 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113444 times.
|
113444 | if (min_size > max_size) { |
| 509 | ✗ | *size = 0; | |
| 510 | ✗ | return NULL; | |
| 511 | } | ||
| 512 | |||
| 513 | 113444 | min_size = FFMIN(max_size, FFMAX(min_size + min_size / 16 + 32, min_size)); | |
| 514 | |||
| 515 | 113444 | ptr = av_realloc(ptr, min_size); | |
| 516 | /* we could set this to the unmodified min_size but this is safer | ||
| 517 | * if the user lost the ptr and uses NULL now | ||
| 518 | */ | ||
| 519 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113444 times.
|
113444 | if (!ptr) |
| 520 | ✗ | min_size = 0; | |
| 521 | |||
| 522 | 113444 | *size = min_size; | |
| 523 | |||
| 524 | 113444 | return ptr; | |
| 525 | } | ||
| 526 | |||
| 527 | 356644 | static inline void fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc) | |
| 528 | { | ||
| 529 | size_t max_size; | ||
| 530 | void *val; | ||
| 531 | |||
| 532 | 356644 | memcpy(&val, ptr, sizeof(val)); | |
| 533 |
2/2✓ Branch 0 taken 310042 times.
✓ Branch 1 taken 46602 times.
|
356644 | if (min_size <= *size) { |
| 534 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 310042 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
310042 | av_assert0(val || !min_size); |
| 535 | 310042 | return; | |
| 536 | } | ||
| 537 | |||
| 538 | 46602 | max_size = atomic_load_explicit(&max_alloc_size, memory_order_relaxed); | |
| 539 | /* *size is an unsigned, so the real maximum is <= UINT_MAX. */ | ||
| 540 | 46602 | max_size = FFMIN(max_size, UINT_MAX); | |
| 541 | |||
| 542 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46602 times.
|
46602 | if (min_size > max_size) { |
| 543 | ✗ | av_freep(ptr); | |
| 544 | ✗ | *size = 0; | |
| 545 | ✗ | return; | |
| 546 | } | ||
| 547 | 46602 | min_size = FFMIN(max_size, FFMAX(min_size + min_size / 16 + 32, min_size)); | |
| 548 | 46602 | av_freep(ptr); | |
| 549 |
2/2✓ Branch 0 taken 44297 times.
✓ Branch 1 taken 2305 times.
|
46602 | val = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size); |
| 550 | 46602 | memcpy(ptr, &val, sizeof(val)); | |
| 551 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46602 times.
|
46602 | if (!val) |
| 552 | ✗ | min_size = 0; | |
| 553 | 46602 | *size = min_size; | |
| 554 | 46602 | return; | |
| 555 | } | ||
| 556 | |||
| 557 | 92675 | void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size) | |
| 558 | { | ||
| 559 | 92675 | fast_malloc(ptr, size, min_size, 0); | |
| 560 | 92675 | } | |
| 561 | |||
| 562 | 263969 | void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size) | |
| 563 | { | ||
| 564 | 263969 | fast_malloc(ptr, size, min_size, 1); | |
| 565 | 263969 | } | |
| 566 | |||
| 567 | 2121 | int av_size_mult(size_t a, size_t b, size_t *r) | |
| 568 | { | ||
| 569 | 2121 | return size_mult(a, b, r); | |
| 570 | } | ||
| 571 |