error: SDL's allocators now call SDL_OutOfMemory on error.

This means the allocator's caller doesn't need to use SDL_OutOfMemory directly
if the allocation fails.

This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc
(all of these regardless of if the app supplied a custom allocator or we're
using system malloc() or an internal copy of dlmalloc under the hood),
SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup...
probably others. If it returns something you can pass to SDL_free, it should
work.

The caller might still need to use SDL_OutOfMemory if something that wasn't
SDL allocated the memory: operator new in C++ code, Objective-C's alloc
message, win32 GlobalAlloc, etc.

Fixes #8642.
This commit is contained in:
Ryan C. Gordon
2023-11-30 00:14:27 -05:00
parent 70b65d4170
commit 447b508a77
197 changed files with 313 additions and 742 deletions

View File

@@ -60,7 +60,7 @@ int SDL_SetTLS(SDL_TLSID id, const void *value, void(SDLCALL *destructor)(void *
newlimit = (id + TLS_ALLOC_CHUNKSIZE);
new_storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage) + (newlimit - 1) * sizeof(storage->array[0]));
if (!new_storage) {
return SDL_OutOfMemory();
return -1;
}
storage = new_storage;
storage->limit = newlimit;
@@ -186,10 +186,7 @@ int SDL_Generic_SetTLSData(SDL_TLSData *data)
}
SDL_UnlockMutex(SDL_generic_TLS_mutex);
if (!entry) {
return SDL_OutOfMemory();
}
return 0;
return entry ? 0 : -1;
}
/* Non-thread-safe global error variable */
@@ -331,7 +328,6 @@ SDL_Thread *SDL_CreateThreadWithStackSize(int(SDLCALL *fn)(void *),
/* Allocate memory for the thread info structure */
thread = (SDL_Thread *)SDL_calloc(1, sizeof(*thread));
if (!thread) {
SDL_OutOfMemory();
return NULL;
}
thread->status = -1;
@@ -341,7 +337,6 @@ SDL_Thread *SDL_CreateThreadWithStackSize(int(SDLCALL *fn)(void *),
if (name) {
thread->name = SDL_strdup(name);
if (!thread->name) {
SDL_OutOfMemory();
SDL_free(thread);
return NULL;
}

View File

@@ -64,8 +64,6 @@ SDL_Condition *SDL_CreateCondition_generic(void)
SDL_DestroyCondition_generic((SDL_Condition *)cond);
cond = NULL;
}
} else {
SDL_OutOfMemory();
}
return (SDL_Condition *)cond;
}

View File

@@ -45,8 +45,6 @@ SDL_Mutex *SDL_CreateMutex(void)
SDL_free(mutex);
mutex = NULL;
}
} else {
SDL_OutOfMemory();
}
#endif // !SDL_THREADS_DISABLED

View File

@@ -59,7 +59,6 @@ SDL_RWLock *SDL_CreateRWLock_generic(void)
SDL_RWLock *rwlock = (SDL_RWLock *) SDL_calloc(1, sizeof (*rwlock));
if (!rwlock) {
SDL_OutOfMemory();
return NULL;
}

View File

@@ -67,7 +67,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
if (!sem) {
SDL_OutOfMemory();
return NULL;
}
sem->count = initial_value;

View File

@@ -37,8 +37,6 @@ SDL_Condition *SDL_CreateCondition(void)
SDL_Condition *cond = (SDL_Condition *)SDL_malloc(sizeof(SDL_Condition));
if (cond) {
CondVar_Init(&cond->cond_variable);
} else {
SDL_OutOfMemory();
}
return cond;
}

View File

@@ -31,8 +31,6 @@ SDL_Mutex *SDL_CreateMutex(void)
SDL_Mutex *mutex = (SDL_Mutex *)SDL_malloc(sizeof(*mutex));
if (mutex) {
RecursiveLock_Init(&mutex->lock);
} else {
SDL_OutOfMemory();
}
return mutex;
}

View File

@@ -44,7 +44,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
if (!sem) {
SDL_OutOfMemory();
return NULL;
}

View File

@@ -59,8 +59,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
SDL_free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return sem;

View File

@@ -52,8 +52,6 @@ SDL_Condition *SDL_CreateCondition(void)
SDL_DestroyCondition(cond);
cond = NULL;
}
} else {
SDL_OutOfMemory();
}
return cond;
}

View File

@@ -52,8 +52,6 @@ SDL_Mutex *SDL_CreateMutex(void)
mutex = NULL;
SDL_SetError("Error trying to create mutex: %lx", res);
}
} else {
SDL_OutOfMemory();
}
return mutex;
}

View File

@@ -49,8 +49,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
SDL_free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return sem;

View File

@@ -46,8 +46,6 @@ SDL_Mutex *SDL_CreateMutex(void)
SDL_free(mutex);
mutex = NULL;
}
} else {
SDL_OutOfMemory();
}
return mutex;
}

View File

@@ -41,8 +41,6 @@ SDL_RWLock *SDL_CreateRWLock(void)
SDL_free(rwlock);
rwlock = NULL;
}
} else {
SDL_OutOfMemory();
}
return rwlock;
}

View File

@@ -48,8 +48,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
SDL_free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return sem;
}

View File

@@ -52,8 +52,6 @@ SDL_Condition *SDL_CreateCondition(void)
SDL_DestroyCondition(cond);
cond = NULL;
}
} else {
SDL_OutOfMemory();
}
return cond;
}

View File

@@ -48,8 +48,6 @@ SDL_Mutex *SDL_CreateMutex(void)
mutex = NULL;
SDL_SetError("Error trying to create mutex: %x", res);
}
} else {
SDL_OutOfMemory();
}
return mutex;
}

View File

@@ -50,8 +50,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
SDL_free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return sem;

View File

@@ -80,23 +80,14 @@ typedef struct SDL_cond_cv
static SDL_Condition *SDL_CreateCondition_cv(void)
{
SDL_cond_cv *cond;
/* Relies on CONDITION_VARIABLE_INIT == 0. */
cond = (SDL_cond_cv *)SDL_calloc(1, sizeof(*cond));
if (!cond) {
SDL_OutOfMemory();
}
return (SDL_Condition *)cond;
return (SDL_Condition *)SDL_calloc(1, sizeof(SDL_cond_cv));
}
static void SDL_DestroyCondition_cv(SDL_Condition *cond)
{
if (cond) {
/* There are no kernel allocated resources */
SDL_free(cond);
}
/* There are no kernel allocated resources */
SDL_free(cond);
}
static int SDL_SignalCondition_cv(SDL_Condition *_cond)

View File

@@ -58,15 +58,10 @@ static pfnTryAcquireSRWLockExclusive pTryAcquireSRWLockExclusive = NULL;
static SDL_Mutex *SDL_CreateMutex_srw(void)
{
SDL_mutex_srw *mutex;
mutex = (SDL_mutex_srw *)SDL_calloc(1, sizeof(*mutex));
if (!mutex) {
SDL_OutOfMemory();
SDL_mutex_srw *mutex = (SDL_mutex_srw *)SDL_calloc(1, sizeof(*mutex));
if (mutex) {
pInitializeSRWLock(&mutex->srw);
}
pInitializeSRWLock(&mutex->srw);
return (SDL_Mutex *)mutex;
}
@@ -153,8 +148,6 @@ static SDL_Mutex *SDL_CreateMutex_cs(void)
#else
InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
#endif
} else {
SDL_OutOfMemory();
}
return (SDL_Mutex *)mutex;
}

View File

@@ -88,10 +88,9 @@ typedef struct SDL_rwlock_srw
static SDL_RWLock *SDL_CreateRWLock_srw(void)
{
SDL_rwlock_srw *rwlock = (SDL_rwlock_srw *)SDL_calloc(1, sizeof(*rwlock));
if (!rwlock) {
SDL_OutOfMemory();
if (rwlock) {
pInitializeSRWLock(&rwlock->srw);
}
pInitializeSRWLock(&rwlock->srw);
return (SDL_RWLock *)rwlock;
}

View File

@@ -85,17 +85,13 @@ static SDL_Semaphore *SDL_CreateSemaphore_atom(Uint32 initial_value)
sem = (SDL_sem_atom *)SDL_malloc(sizeof(*sem));
if (sem) {
sem->count = initial_value;
} else {
SDL_OutOfMemory();
}
return (SDL_Semaphore *)sem;
}
static void SDL_DestroySemaphore_atom(SDL_Semaphore *sem)
{
if (sem) {
SDL_free(sem);
}
SDL_free(sem);
}
static int SDL_WaitSemaphoreTimeoutNS_atom(SDL_Semaphore *_sem, Sint64 timeoutNS)
@@ -226,6 +222,7 @@ static SDL_Semaphore *SDL_CreateSemaphore_kern(Uint32 initial_value)
sem = (SDL_sem_kern *)SDL_malloc(sizeof(*sem));
if (sem) {
/* Create the semaphore, with max value 32K */
// !!! FIXME: CreateSemaphoreEx is available in Vista and later, so if XP support is dropped, we can lose this #ifdef.
#ifdef __WINRT__
sem->id = CreateSemaphoreEx(NULL, initial_value, 32 * 1024, NULL, 0, SEMAPHORE_ALL_ACCESS);
#else
@@ -237,8 +234,6 @@ static SDL_Semaphore *SDL_CreateSemaphore_kern(Uint32 initial_value)
SDL_free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return (SDL_Semaphore *)sem;
}