8000 at3_standalone: Make all allocations aligned. by hrydgard · Pull Request #20162 · hrydgard/ppsspp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

at3_standalone: Make all allocations aligned. #20162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Common/MemoryUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions ext/at3_standalone/get_bits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
52 changes: 14 additions & 38 deletions ext/at3_standalone/mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <stdlib.h>
#include <string.h>

#include "Common/MemoryUtil.h"

#include "compat.h"
#include "intreadwrite.h"
#include "mem.h"
Expand All @@ -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). */
Expand All @@ -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);
}
return ptr;
}

void *av_realloc(void *ptr, size_t size)
{
return realloc(ptr, size + !size);
}

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;
// 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);
}
r = av_realloc(ptr, size);
if (!r && size)
av_free(ptr);
return r;
return ptr;
}

void av_free(void *ptr)
{
free(ptr);
void av_free(void *ptr) {
FreeAlignedMemory(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);
Expand Down
Loading
0