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:
Sam Lantinga
2023-01-09 17:42:16 -08:00
parent 9597c482fa
commit 2aa9569b3e
13 changed files with 132 additions and 194 deletions

View File

@@ -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);
}
}