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

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