mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-22 15:25:36 +00:00
Replaced SDL_SIMDAlloc(), SDL_SIMDRealloc(), and SDL_SIMDFree() with SDL_aligned_alloc() and SDL_aligned_free()
Fixes https://github.com/libsdl-org/SDL/issues/5641
This commit is contained in:
@@ -139,7 +139,7 @@ static int CPU_haveCPUID(void)
|
||||
: "%eax", "%ecx"
|
||||
);
|
||||
#elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
|
||||
/* Technically, if this is being compiled under __x86_64__ then it has
|
||||
/* Technically, if this is being compiled under __x86_64__ then it has
|
||||
CPUid by definition. But it's nice to be able to prove it. :) */
|
||||
__asm__ (
|
||||
" pushfq # Get original EFLAGS \n"
|
||||
@@ -1094,91 +1094,6 @@ SDL_SIMDGetAlignment(void)
|
||||
return SDL_SIMDAlignment;
|
||||
}
|
||||
|
||||
void *
|
||||
SDL_SIMDAlloc(const size_t len)
|
||||
{
|
||||
const size_t alignment = SDL_SIMDGetAlignment();
|
||||
const size_t padding = (alignment - (len % alignment)) % alignment;
|
||||
Uint8 *retval = NULL;
|
||||
Uint8 *ptr;
|
||||
size_t to_allocate;
|
||||
|
||||
/* alignment + padding + sizeof (void *) is bounded (a few hundred
|
||||
* bytes max), so no need to check for overflow within that argument */
|
||||
if (SDL_size_add_overflow(len, alignment + padding + sizeof(void *), &to_allocate)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ptr = (Uint8 *)SDL_malloc(to_allocate);
|
||||
if (ptr) {
|
||||
/* store the actual allocated pointer right before our aligned pointer. */
|
||||
retval = ptr + sizeof(void *);
|
||||
retval += alignment - (((size_t)retval) % alignment);
|
||||
*(((void **)retval) - 1) = ptr;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
void *
|
||||
SDL_SIMDRealloc(void *mem, const size_t len)
|
||||
{
|
||||
const size_t alignment = SDL_SIMDGetAlignment();
|
||||
const size_t padding = (alignment - (len % alignment)) % alignment;
|
||||
Uint8 *retval = (Uint8 *)mem;
|
||||
void *oldmem = mem;
|
||||
size_t memdiff = 0, ptrdiff;
|
||||
Uint8 *ptr;
|
||||
size_t to_allocate;
|
||||
|
||||
/* alignment + padding + sizeof (void *) is bounded (a few hundred
|
||||
* bytes max), so no need to check for overflow within that argument */
|
||||
if (SDL_size_add_overflow(len, alignment + padding + sizeof(void *), &to_allocate)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mem) {
|
||||
mem = *(((void **)mem) - 1);
|
||||
|
||||
/* Check the delta between the real pointer and user pointer */
|
||||
memdiff = ((size_t)oldmem) - ((size_t)mem);
|
||||
}
|
||||
|
||||
ptr = (Uint8 *)SDL_realloc(mem, to_allocate);
|
||||
|
||||
if (ptr == NULL) {
|
||||
return NULL; /* Out of memory, bail! */
|
||||
}
|
||||
|
||||
/* Store the actual allocated pointer right before our aligned pointer. */
|
||||
retval = ptr + sizeof(void *);
|
||||
retval += alignment - (((size_t)retval) % alignment);
|
||||
|
||||
/* Make sure the delta is the same! */
|
||||
if (mem) {
|
||||
ptrdiff = ((size_t)retval) - ((size_t)ptr);
|
||||
if (memdiff != ptrdiff) { /* Delta has changed, copy to new offset! */
|
||||
oldmem = (void *)(((uintptr_t)ptr) + memdiff);
|
||||
|
||||
/* Even though the data past the old `len` is undefined, this is the
|
||||
* only length value we have, and it guarantees that we copy all the
|
||||
* previous memory anyhow.
|
||||
*/
|
||||
SDL_memmove(retval, oldmem, len);
|
||||
}
|
||||
}
|
||||
|
||||
/* Actually store the allocated pointer, finally. */
|
||||
*(((void **)retval) - 1) = ptr;
|
||||
return retval;
|
||||
}
|
||||
|
||||
void SDL_SIMDFree(void *ptr)
|
||||
{
|
||||
if (ptr) {
|
||||
SDL_free(*(((void **)ptr) - 1));
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST_MAIN
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -555,10 +555,7 @@ SDL3_0.0.0 {
|
||||
SDL_RumbleJoystick;
|
||||
SDL_RumbleJoystickTriggers;
|
||||
SDL_RunApp;
|
||||
SDL_SIMDAlloc;
|
||||
SDL_SIMDFree;
|
||||
SDL_SIMDGetAlignment;
|
||||
SDL_SIMDRealloc;
|
||||
SDL_SaveBMP_RW;
|
||||
SDL_ScreenKeyboardShown;
|
||||
SDL_ScreenSaverEnabled;
|
||||
@@ -843,6 +840,8 @@ SDL3_0.0.0 {
|
||||
SDL_modff;
|
||||
SDL_GetRenderVSync;
|
||||
SDL_PlayAudioDevice;
|
||||
SDL_aligned_alloc;
|
||||
SDL_aligned_free;
|
||||
# extra symbols go here (don't modify this line)
|
||||
local: *;
|
||||
};
|
||||
|
||||
@@ -581,10 +581,7 @@
|
||||
#define SDL_RumbleJoystick SDL_RumbleJoystick_REAL
|
||||
#define SDL_RumbleJoystickTriggers SDL_RumbleJoystickTriggers_REAL
|
||||
#define SDL_RunApp SDL_RunApp_REAL
|
||||
#define SDL_SIMDAlloc SDL_SIMDAlloc_REAL
|
||||
#define SDL_SIMDFree SDL_SIMDFree_REAL
|
||||
#define SDL_SIMDGetAlignment SDL_SIMDGetAlignment_REAL
|
||||
#define SDL_SIMDRealloc SDL_SIMDRealloc_REAL
|
||||
#define SDL_SaveBMP_RW SDL_SaveBMP_RW_REAL
|
||||
#define SDL_ScreenKeyboardShown SDL_ScreenKeyboardShown_REAL
|
||||
#define SDL_ScreenSaverEnabled SDL_ScreenSaverEnabled_REAL
|
||||
@@ -871,3 +868,5 @@
|
||||
#define SDL_modff SDL_modff_REAL
|
||||
#define SDL_GetRenderVSync SDL_GetRenderVSync_REAL
|
||||
#define SDL_PlayAudioDevice SDL_PlayAudioDevice_REAL
|
||||
#define SDL_aligned_alloc SDL_aligned_alloc_REAL
|
||||
#define SDL_aligned_free SDL_aligned_free_REAL
|
||||
|
||||
@@ -637,10 +637,7 @@ SDL_DYNAPI_PROC(int,SDL_RumbleGamepadTriggers,(SDL_Gamepad *a, Uint16 b, Uint16
|
||||
SDL_DYNAPI_PROC(int,SDL_RumbleJoystick,(SDL_Joystick *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_RumbleJoystickTriggers,(SDL_Joystick *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_RunApp,(int a, char *b[], SDL_main_func c, void *d),(a,b,c,d),return)
|
||||
SDL_DYNAPI_PROC(void*,SDL_SIMDAlloc,(const size_t a),(a),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_SIMDFree,(void *a),(a),)
|
||||
SDL_DYNAPI_PROC(size_t,SDL_SIMDGetAlignment,(void),(),return)
|
||||
SDL_DYNAPI_PROC(void*,SDL_SIMDRealloc,(void *a, const size_t b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_SaveBMP_RW,(SDL_Surface *a, SDL_RWops *b, int c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_ScreenKeyboardShown,(SDL_Window *a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_ScreenSaverEnabled,(void),(),return)
|
||||
@@ -916,3 +913,5 @@ SDL_DYNAPI_PROC(double,SDL_modf,(double a, double *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_modff,(float a, float *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetRenderVSync,(SDL_Renderer *a, int *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_PlayAudioDevice,(SDL_AudioDeviceID a),(a),)
|
||||
SDL_DYNAPI_PROC(void*,SDL_aligned_alloc,(size_t a, size_t b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_aligned_free,(void *a),(a),)
|
||||
|
||||
@@ -63,7 +63,7 @@ SDL_SW_CreateYUVTexture(Uint32 format, int w, int h)
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
swdata->pixels = (Uint8 *)SDL_SIMDAlloc(dst_size);
|
||||
swdata->pixels = (Uint8 *)SDL_aligned_alloc(SDL_SIMDGetAlignment(), dst_size);
|
||||
if (!swdata->pixels) {
|
||||
SDL_SW_DestroyYUVTexture(swdata);
|
||||
SDL_OutOfMemory();
|
||||
@@ -394,7 +394,7 @@ int SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture *swdata, const SDL_Rect *srcrect,
|
||||
void SDL_SW_DestroyYUVTexture(SDL_SW_YUVTexture *swdata)
|
||||
{
|
||||
if (swdata) {
|
||||
SDL_SIMDFree(swdata->pixels);
|
||||
SDL_aligned_free(swdata->pixels);
|
||||
SDL_DestroySurface(swdata->stretch);
|
||||
SDL_DestroySurface(swdata->display);
|
||||
SDL_free(swdata);
|
||||
|
||||
@@ -686,3 +686,40 @@ int SDL_isblank(int x)
|
||||
return ((x) == ' ') || ((x) == '\t');
|
||||
}
|
||||
#endif
|
||||
|
||||
void *SDL_aligned_alloc(size_t alignment, size_t size)
|
||||
{
|
||||
size_t padding;
|
||||
Uint8 *retval = NULL;
|
||||
|
||||
if (alignment < sizeof(void*)) {
|
||||
alignment = sizeof(void*);
|
||||
}
|
||||
padding = (alignment - (size % alignment));
|
||||
|
||||
if (SDL_size_add_overflow(size, alignment, &size) == 0 &&
|
||||
SDL_size_add_overflow(size, sizeof(void *), &size) == 0 &&
|
||||
SDL_size_add_overflow(size, padding, &size) == 0) {
|
||||
void *original = SDL_malloc(size);
|
||||
if (original) {
|
||||
/* Make sure we have enough space to store the original pointer */
|
||||
retval = (Uint8 *)original + sizeof(original);
|
||||
|
||||
/* Align the pointer we're going to return */
|
||||
retval += alignment - (((size_t)retval) % alignment);
|
||||
|
||||
/* Store the original pointer right before the returned value */
|
||||
SDL_memcpy(retval - sizeof(original), &original, sizeof(original));
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
void SDL_aligned_free(void *mem)
|
||||
{
|
||||
if (mem) {
|
||||
void *original;
|
||||
SDL_memcpy(&original, ((Uint8 *)mem - sizeof(original)), sizeof(original));
|
||||
SDL_free(original);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1215,7 +1215,7 @@ static int RLEAlphaSurface(SDL_Surface *surface)
|
||||
/* Now that we have it encoded, release the original pixels */
|
||||
if (!(surface->flags & SDL_PREALLOC)) {
|
||||
if (surface->flags & SDL_SIMD_ALIGNED) {
|
||||
SDL_SIMDFree(surface->pixels);
|
||||
SDL_aligned_free(surface->pixels);
|
||||
surface->flags &= ~SDL_SIMD_ALIGNED;
|
||||
} else {
|
||||
SDL_free(surface->pixels);
|
||||
@@ -1382,7 +1382,7 @@ static int RLEColorkeySurface(SDL_Surface *surface)
|
||||
/* Now that we have it encoded, release the original pixels */
|
||||
if (!(surface->flags & SDL_PREALLOC)) {
|
||||
if (surface->flags & SDL_SIMD_ALIGNED) {
|
||||
SDL_SIMDFree(surface->pixels);
|
||||
SDL_aligned_free(surface->pixels);
|
||||
surface->flags &= ~SDL_SIMD_ALIGNED;
|
||||
} else {
|
||||
SDL_free(surface->pixels);
|
||||
@@ -1482,6 +1482,7 @@ static SDL_bool UnRLEAlpha(SDL_Surface *surface)
|
||||
RLEDestFormat *, SDL_PixelFormat *);
|
||||
int w = surface->w;
|
||||
int bpp = df->BytesPerPixel;
|
||||
size_t size;
|
||||
|
||||
if (bpp == 2) {
|
||||
uncopy_opaque = uncopy_opaque_16;
|
||||
@@ -1490,7 +1491,11 @@ static SDL_bool UnRLEAlpha(SDL_Surface *surface)
|
||||
uncopy_opaque = uncopy_transl = uncopy_32;
|
||||
}
|
||||
|
||||
surface->pixels = SDL_SIMDAlloc((size_t)surface->h * surface->pitch);
|
||||
if (SDL_size_mul_overflow(surface->h, surface->pitch, &size)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
surface->pixels = SDL_aligned_alloc(SDL_SIMDGetAlignment(), size);
|
||||
if (surface->pixels == NULL) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
@@ -1554,9 +1559,15 @@ void SDL_UnRLESurface(SDL_Surface *surface, int recode)
|
||||
if (recode && !(surface->flags & SDL_PREALLOC)) {
|
||||
if (surface->map->info.flags & SDL_COPY_RLE_COLORKEY) {
|
||||
SDL_Rect full;
|
||||
size_t size;
|
||||
|
||||
/* re-create the original surface */
|
||||
surface->pixels = SDL_SIMDAlloc((size_t)surface->h * surface->pitch);
|
||||
if (SDL_size_mul_overflow(surface->h, surface->pitch, &size)) {
|
||||
/* Memory corruption? */
|
||||
surface->flags |= SDL_RLEACCEL;
|
||||
return;
|
||||
}
|
||||
surface->pixels = SDL_aligned_alloc(SDL_SIMDGetAlignment(), size);
|
||||
if (surface->pixels == NULL) {
|
||||
/* Oh crap... */
|
||||
surface->flags |= SDL_RLEACCEL;
|
||||
|
||||
@@ -165,7 +165,7 @@ SDL_CreateSurface(int width, int height, Uint32 format)
|
||||
}
|
||||
}
|
||||
|
||||
surface->pixels = SDL_SIMDAlloc(size);
|
||||
surface->pixels = SDL_aligned_alloc(SDL_SIMDGetAlignment(), size);
|
||||
if (!surface->pixels) {
|
||||
SDL_DestroySurface(surface);
|
||||
SDL_OutOfMemory();
|
||||
@@ -1553,7 +1553,7 @@ void SDL_DestroySurface(SDL_Surface *surface)
|
||||
/* Don't free */
|
||||
} else if (surface->flags & SDL_SIMD_ALIGNED) {
|
||||
/* Free aligned */
|
||||
SDL_SIMDFree(surface->pixels);
|
||||
SDL_aligned_free(surface->pixels);
|
||||
} else {
|
||||
/* Normal */
|
||||
SDL_free(surface->pixels);
|
||||
|
||||
Reference in New Issue
Block a user