27#define _XOPEN_SOURCE 600
52#define malloc AV_JOIN(MALLOC_PREFIX, malloc)
53#define memalign AV_JOIN(MALLOC_PREFIX, memalign)
54#define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
55#define realloc AV_JOIN(MALLOC_PREFIX, realloc)
56#define free AV_JOIN(MALLOC_PREFIX, free)
58void *malloc(
size_t size);
60int posix_memalign(
void **ptr,
size_t align,
size_t size);
61void *realloc(
void *ptr,
size_t size);
66#define ALIGN (HAVE_SIMD_ALIGN_64 ? 64 : (HAVE_SIMD_ALIGN_32 ? 32 : 16))
68#define FF_MEMORY_POISON 0x2a
72#if CONFIG_MEMORY_POISONING
84#if HAVE_ASAN && HAVE_ALIGNED_MALLOC && !HAVE_POSIX_MEMALIGN && !HAVE_MEMALIGN
85#define ASAN_ALIGNED_ALLOC 1
87static int asan_aligned_alloc_needed(
void)
93 uint8_t *
p = _aligned_malloc(16,
ALIGN);
94 int probed = !
p || !__asan_address_is_poisoned(p + 16);
106typedef struct AsanAlignedHeader {
111static void *asan_aligned_malloc(
size_t size)
113 AsanAlignedHeader *hdr;
117 if (
size > SIZE_MAX -
ALIGN -
sizeof(*hdr))
120 base = malloc(total);
124 hdr = (AsanAlignedHeader *)ptr - 1;
132static AsanAlignedHeader *asan_aligned_header(
void *ptr)
134 AsanAlignedHeader *hdr = (AsanAlignedHeader *)ptr - 1;
139static void asan_aligned_free(
void *ptr)
142 free(asan_aligned_header(ptr)->
base);
145static void *asan_aligned_realloc(
void *ptr,
size_t size)
147 AsanAlignedHeader *hdr;
151 return asan_aligned_malloc(
size);
152 ret = asan_aligned_malloc(
size);
155 hdr = asan_aligned_header(ptr);
156 memcpy(ret, ptr,
FFMIN(
size, hdr->size));
161#define ASAN_ALIGNED_ALLOC 0
179#if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_mul_overflow)
180 if (__builtin_mul_overflow(
a,
b, &t))
186 if ((
a |
b) >= ((
size_t)1 << (
sizeof(
size_t) * 4)) &&
a && t /
a !=
b)
200#if ASAN_ALIGNED_ALLOC
201 if (asan_aligned_alloc_needed())
202 ptr = asan_aligned_malloc(
size);
205#elif HAVE_POSIX_MEMALIGN
209#elif HAVE_ALIGNED_MALLOC
259#if ASAN_ALIGNED_ALLOC
260 if (asan_aligned_alloc_needed())
261 ret = asan_aligned_realloc(ptr,
size + !
size);
264#elif HAVE_ALIGNED_MALLOC
298 memcpy(&
val, ptr,
sizeof(
val));
306 memcpy(ptr, &
val,
sizeof(
val));
330 memcpy(&
val, ptr,
sizeof(
val));
332 memcpy(ptr, &
val,
sizeof(
val));
341#if ASAN_ALIGNED_ALLOC
342 if (asan_aligned_alloc_needed())
343 asan_aligned_free(ptr);
346#elif HAVE_ALIGNED_MALLOC
366 memset(ptr, 0,
size);
382 size_t len = strlen(
s) + 1;
395 const char *end = memchr(
s, 0,
len);
414 memcpy(ptr, p,
size);
422 memcpy(&
tab, tab_ptr,
sizeof(
tab));
426 memcpy(tab_ptr, &
tab,
sizeof(
tab));
436 memcpy(&
tab, tab_ptr,
sizeof(
tab));
440 memcpy(tab_ptr, &
tab,
sizeof(
tab));
448 const uint8_t *elem_data)
450 uint8_t *tab_elem_data =
NULL;
453 tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size;
455 memcpy(tab_elem_data, elem_data, elem_size);
462 return tab_elem_data;
487 uint32_t
a = v << 8 | v >> 16;
488 uint32_t
b = v << 16 | v >> 8;
489 uint32_t
c = v << 24 | v;
492 uint32_t
a = v | v << 24;
493 uint32_t
b = v >> 8 | v << 16;
494 uint32_t
c = v >> 16 | v << 8;
528 uint64_t v2= v + ((uint64_t)v<<32);
553 const uint8_t *
src = &
dst[-back];
559 }
else if (back == 2) {
561 }
else if (back == 3) {
563 }
else if (back == 4) {
568 while (cnt > blocklen) {
569 memcpy(
dst,
src, blocklen);
605 if (min_size <= *
size)
610 max_size =
FFMIN(max_size, UINT_MAX);
612 if (min_size > max_size) {
617 min_size =
FFMIN(max_size,
FFMAX(min_size + min_size / 16 + 32, min_size));
631static inline void fast_malloc(
void *ptr,
unsigned int *
size,
size_t min_size,
int zero_realloc)
636 memcpy(&
val, ptr,
sizeof(
val));
637 if (min_size <= *
size) {
644 max_size =
FFMIN(max_size, UINT_MAX);
646 if (min_size > max_size) {
651 min_size =
FFMIN(max_size,
FFMAX(min_size + min_size / 16 + 32, min_size));
654 memcpy(ptr, &
val,
sizeof(
val));
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double val(void *priv, double ch)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static const uint8_t *BS_FUNC align(BSCTX *bc)
Skip bits to a byte boundary.
#define FF_DYNARRAY_ADD(av_size_max, av_elt_size, av_array, av_size, av_success, av_failure)
Add an element to a dynamic array.
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
Allocate and clear a buffer, reusing the given one if large enough.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
void av_max_alloc(size_t max)
Set the maximum size that may be allocated in one block.
int av_size_mult(size_t a, size_t b, size_t *r)
Multiply two size_t values checking for overflow.
Macro definitions for various function/variable attributes.
common internal API header
Utility Preprocessor macros.
static atomic_size_t max_alloc_size
static void fill32(uint8_t *dst, int len)
static void poison_memory(void *ptr, size_t size)
static int size_mult(size_t a, size_t b, size_t *r)
static void fill16(uint8_t *dst, int len)
void * av_calloc(size_t nmemb, size_t size)
static void fill24(uint8_t *dst, int len)
static void fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
Memory handling functions.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
#define FF_ASAN_POISON(ptr, size)
#define FF_MEM_UNDEFINED(ptr, size)
#define FF_ASAN_UNPOISON(ptr, size)
#define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure)
#define atomic_load_explicit(object, order)
#define atomic_store_explicit(object, desired, order)
#define av_malloc_array(a, b)
#define av_realloc_f(p, o, n)
static const struct twinvq_data tab