mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-16 06:45:59 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -45,8 +45,6 @@ SDL_Mutex *SDL_CreateMutex(void)
|
||||
SDL_free(mutex);
|
||||
mutex = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
#endif // !SDL_THREADS_DISABLED
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -44,7 +44,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
|
||||
|
||||
sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
|
||||
if (!sem) {
|
||||
SDL_OutOfMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -59,8 +59,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
|
||||
SDL_free(sem);
|
||||
sem = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return sem;
|
||||
|
@@ -52,8 +52,6 @@ SDL_Condition *SDL_CreateCondition(void)
|
||||
SDL_DestroyCondition(cond);
|
||||
cond = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
return cond;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -49,8 +49,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
|
||||
SDL_free(sem);
|
||||
sem = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return sem;
|
||||
|
@@ -46,8 +46,6 @@ SDL_Mutex *SDL_CreateMutex(void)
|
||||
SDL_free(mutex);
|
||||
mutex = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
return mutex;
|
||||
}
|
||||
|
@@ -41,8 +41,6 @@ SDL_RWLock *SDL_CreateRWLock(void)
|
||||
SDL_free(rwlock);
|
||||
rwlock = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
return rwlock;
|
||||
}
|
||||
|
@@ -48,8 +48,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
|
||||
SDL_free(sem);
|
||||
sem = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
return sem;
|
||||
}
|
||||
|
@@ -52,8 +52,6 @@ SDL_Condition *SDL_CreateCondition(void)
|
||||
SDL_DestroyCondition(cond);
|
||||
cond = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
return cond;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -50,8 +50,6 @@ SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
|
||||
SDL_free(sem);
|
||||
sem = NULL;
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return sem;
|
||||
|
@@ -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)
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user