diff --git a/Common/MemoryUtil.h b/Common/MemoryUtil.h index 2aaf0ddd89..00dd786aa4 100644 --- a/Common/MemoryUtil.h +++ b/Common/MemoryUtil.h @@ -47,7 +47,8 @@ void FreeMemoryPages(void* ptr, size_t size); // Regular aligned memory. Don't try to apply memory protection willy-nilly to memory allocated this way as in-page alignment is unknown (though could be checked). // No longer asserts, will return nullptr on failure. -void* AllocateAlignedMemory(size_t size, size_t alignment); +// WARNING: Not necessarily malloc-compatible! +void *AllocateAlignedMemory(size_t size, size_t alignment); void FreeAlignedMemory(void* ptr); int GetMemoryProtectPageSize(); diff --git a/ext/at3_standalone/get_bits.cpp b/ext/at3_standalone/get_bits.cpp index 50d3969219..18374d80dd 100644 --- a/ext/at3_standalone/get_bits.cpp +++ b/ext/at3_standalone/get_bits.cpp @@ -81,7 +81,7 @@ static int alloc_table(VLC *vlc, int size, int use_static) if (use_static) abort(); // cannot do anything, init_vlc() is used with too little memory vlc->table_allocated += (1 << vlc->bits); - vlc->table = (VLC_TYPE(*)[2])av_realloc_f(vlc->table, vlc->table_allocated, sizeof(VLC_TYPE) * 2); + vlc->table = (VLC_TYPE(*)[2])realloc(vlc->table, vlc->table_allocated); if (!vlc->table) { vlc->table_allocated = 0; vlc->table_size = 0; @@ -311,14 +311,15 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, } else { av_free(buf); if (ret < 0) { - av_freep(&vlc->table); + free(vlc->table); + vlc->table = 0; return ret; } } return 0; } -void ff_free_vlc(VLC *vlc) -{ - av_freep(&vlc->table); +void ff_free_vlc(VLC *vlc) { + free(vlc->table); + vlc->table = nullptr; } diff --git a/ext/at3_standalone/mem.cpp b/ext/at3_standalone/mem.cpp index 4577223662..37e6787b62 100644 --- a/ext/at3_standalone/mem.cpp +++ b/ext/at3_standalone/mem.cpp @@ -31,6 +31,8 @@ #include #include +#include "Common/MemoryUtil.h" + #include "compat.h" #include "intreadwrite.h" #include "mem.h" @@ -39,8 +41,7 @@ * Multiply two size_t values checking for overflow. * @return 0 if success, AVERROR(EINVAL) if overflow. */ -static inline int av_size_mult(size_t a, size_t b, size_t *r) -{ +static inline int av_size_mult(size_t a, size_t b, size_t *r) { size_t t = a * b; /* Hack inspired from glibc: only try the division if nelem and elsize * are both greater than sqrt(SIZE_MAX). */ @@ -50,55 +51,30 @@ static inline int av_size_mult(size_t a, size_t b, size_t *r) return 0; } -#define ALIGN (HAVE_AVX ? 32 : 16) - -void *av_malloc(size_t size) -{ +void *av_malloc(size_t size) { void *ptr = NULL; - ptr = malloc(size); - if(!ptr && !size) { - size = 1; - ptr= av_malloc(1); + + // Some code requires av malloc to have an alignment of 32 at least. See #20155 + ptr = AllocateAlignedMemory(size, 32); + if (!ptr && !size) { + // Compensate for platforms that don't allow zero-size allocations (not sure if this is actually an issue) + return av_malloc(1); } return ptr; } -void *av_realloc(void *ptr, size_t size) -{ - return realloc(ptr, size + !size); +void av_free(void *ptr) { + FreeAlignedMemory(ptr); } -void *av_realloc_f(void *ptr, size_t nelem, size_t elsize) -{ - size_t size; - void *r; - - if (av_size_mult(elsize, nelem, &size)) { - av_free(ptr); - return NULL; - } - r = av_realloc(ptr, size); - if (!r && size) - av_free(ptr); - return r; -} - -void av_free(void *ptr) -{ - free(ptr); -} - -void av_freep(void *arg) -{ +void av_freep(void *arg) { void *val; - memcpy(&val, arg, sizeof(val)); memset(arg, 0, sizeof(val)); av_free(val); } -void *av_mallocz(size_t size) -{ +void *av_mallocz(size_t size) { void *ptr = av_malloc(size); if (ptr) memset(ptr, 0, size);