31
31
#include < stdlib.h>
32
32
#include < string.h>
33
33
34
+ #include " Common/MemoryUtil.h"
35
+
34
36
#include " compat.h"
35
37
#include " intreadwrite.h"
36
38
#include " mem.h"
39
41
* Multiply two size_t values checking for overflow.
40
42
* @return 0 if success, AVERROR(EINVAL) if overflow.
41
43
*/
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) {
44
45
size_t t = a * b;
45
46
/* Hack inspired from glibc: only try the division if nelem and elsize
46
47
* are both greater than sqrt(SIZE_MAX). */
@@ -50,55 +51,31 @@ static inline int av_size_mult(size_t a, size_t b, size_t *r)
50
51
return 0 ;
51
52
}
52
53
53
- #define ALIGN (HAVE_AVX ? 32 : 16 )
54
-
55
- void *av_malloc (size_t size)
56
- {
54
+ void *av_malloc (size_t size) {
57
55
void *ptr = NULL ;
58
- ptr = malloc (size);
59
- if (!ptr && !size) {
56
+
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)
60
61
size = 1 ;
61
- ptr= av_malloc (1 );
62
+ ptr = av_malloc (1 );
62
63
}
63
64
return ptr;
64
65
}
65
66
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 ;
79
- }
80
- r = av_realloc (ptr, size);
81
- if (!r && size)
82
- av_free (ptr);
83
- return r;
67
+ void av_free (void *ptr) {
68
+ FreeAlignedMemory (ptr);
84
69
}
85
70
86
- void av_free (void *ptr)
87
- {
88
- free (ptr);
89
- }
90
-
91
- void av_freep (void *arg)
92
- {
71
+ void av_freep (void *arg) {
93
72
void *val;
94
-
95
73
memcpy (&val, arg, sizeof (val));
96
74
memset (arg, 0 , sizeof (val));
97
75
av_free (val);
98
76
}
99
77
100
- void *av_mallocz (size_t size)
101
- {
78
+ void *av_mallocz (size_t size) {
102
79
void *ptr = av_malloc (size);
103
80
if (ptr)
104
81
memset (ptr, 0 , size);
0 commit comments