mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-01 15:38:29 +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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user