3131#include < stdlib.h>
3232#include < string.h>
3333
34+ #include " Common/MemoryUtil.h"
35+
3436#include " compat.h"
3537#include " intreadwrite.h"
3638#include " mem.h"
3941 * Multiply two size_t values checking for overflow.
4042 * @return 0 if success, AVERROR(EINVAL) if overflow.
4143 */
42- static inline int av_size_mult (size_t a, size_t b, size_t *r)
43- {
44+ static inline int av_size_mult (size_t a, size_t b, size_t *r) {
4445 size_t t = a * b;
4546 /* Hack inspired from glibc: only try the division if nelem and elsize
4647 * 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)
5051 return 0 ;
5152}
5253
53- #define ALIGN (HAVE_AVX ? 32 : 16 )
54-
55- void *av_malloc (size_t size)
56- {
54+ void *av_malloc (size_t size) {
5755 void *ptr = NULL ;
58- ptr = malloc (size);
59- if (!ptr && !size) {
60- size = 1 ;
61- ptr= av_malloc (1 );
62- }
63- return ptr;
64- }
6556
66- void *av_realloc (void *ptr, size_t size)
67- {
68- return realloc (ptr, size + !size);
69- }
70-
71- void *av_realloc_f (void *ptr, size_t nelem, size_t elsize)
72- {
73- size_t size;
74- void *r;
75-
76- if (av_size_mult (elsize, nelem, &size)) {
77- av_free (ptr);
78- return NULL ;
57+ // Some code requires av malloc to have an alignment of 32 at least. See #20155
58+ ptr = AllocateAlignedMemory (size, 32 );
59+ if (!ptr && !size) {
60+ // Compensate for platforms that don't allow zero-size allocations (not sure if this is actually an issue)
61+ return av_malloc (1 );
7962 }
80- r = av_realloc (ptr, size);
81- if (!r && size)
82- av_free (ptr);
83- return r;
63+ return ptr;
8464}
8565
86- void av_free (void *ptr)
87- {
88- free (ptr);
66+ void av_free (void *ptr) {
67+ FreeAlignedMemory (ptr);
8968}
9069
91- void av_freep (void *arg)
92- {
70+ void av_freep (void *arg) {
9371 void *val;
94-
9572 memcpy (&val, arg, sizeof (val));
9673 memset (arg, 0 , sizeof (val));
9774 av_free (val);
9875}
9976
100- void *av_mallocz (size_t size)
101- {
77+ void *av_mallocz (size_t size) {
10278 void *ptr = av_malloc (size);
10379 if (ptr)
10480 memset (ptr, 0 , size);
0 commit comments