mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-09 03:16:26 +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>
|
||||
|
Reference in New Issue
Block a user