Skip to content

Commit 6162158

Browse files
committed
at3_standalone: Make all allocations aligned.
Replace av_realloc with regular realloc, as there's no aligned_realloc and pointers are not compatible with regular free.
1 parent 2309226 commit 6162158

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

Common/MemoryUtil.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ void FreeMemoryPages(void* ptr, size_t size);
4747

4848
// 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).
4949
// No longer asserts, will return nullptr on failure.
50-
void* AllocateAlignedMemory(size_t size, size_t alignment);
50+
// WARNING: Not necessarily malloc-compatible!
51+
void *AllocateAlignedMemory(size_t size, size_t alignment);
5152
void FreeAlignedMemory(void* ptr);
5253

5354
int GetMemoryProtectPageSize();

ext/at3_standalone/get_bits.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int alloc_table(VLC *vlc, int size, int use_static)
8181
if (use_static)
8282
abort(); // cannot do anything, init_vlc() is used with too little memory
8383
vlc->table_allocated += (1 << vlc->bits);
84-
vlc->table = (VLC_TYPE(*)[2])av_realloc_f(vlc->table, vlc->table_allocated, sizeof(VLC_TYPE) * 2);
84+
vlc->table = (VLC_TYPE(*)[2])realloc(vlc->table, vlc->table_allocated, sizeof(VLC_TYPE) * 2);
8585
if (!vlc->table) {
8686
vlc->table_allocated = 0;
8787
vlc->table_size = 0;
@@ -311,14 +311,15 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes,
311311
} else {
312312
av_free(buf);
313313
if (ret < 0) {
314-
av_freep(&vlc->table);
314+
free(vlc->table);
315+
vlc->table = 0;
315316
return ret;
316317
}
317318
}
318319
return 0;
319320
}
320321

321-
void ff_free_vlc(VLC *vlc)
322-
{
323-
av_freep(&vlc->table);
322+
void ff_free_vlc(VLC *vlc) {
323+
free(vlc->table);
324+
vlc->table = nullptr;
324325
}

ext/at3_standalone/mem.cpp

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
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"
@@ -39,8 +41,7 @@
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,31 @@ 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) {
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)
6061
size = 1;
61-
ptr= av_malloc(1);
62+
ptr = av_malloc(1);
6263
}
6364
return ptr;
6465
}
6566

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);
8469
}
8570

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) {
9372
void *val;
94-
9573
memcpy(&val, arg, sizeof(val));
9674
memset(arg, 0, sizeof(val));
9775
av_free(val);
9876
}
9977

100-
void *av_mallocz(size_t size)
101-
{
78+
void *av_mallocz(size_t size) {
10279
void *ptr = av_malloc(size);
10380
if (ptr)
10481
memset(ptr, 0, size);

0 commit comments

Comments
 (0)