From 04be04c33861fce0a6d9bc39328e85ccb4321e15 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 7 May 2024 15:04:17 -0700
Subject: [PATCH] Removed mostly unused SDL_errorcode
---
include/SDL3/SDL_error.h | 33 +++++++++++--------------------
src/SDL_error.c | 24 +++++++---------------
src/dynapi/SDL_dynapi.c | 2 +-
src/dynapi/SDL_dynapi.sym | 4 ++--
src/dynapi/SDL_dynapi_overrides.h | 4 ++--
src/dynapi/SDL_dynapi_procs.h | 4 ++--
src/file/SDL_iostream.c | 18 ++++++++---------
src/test/SDL_test_harness.c | 6 +++---
src/video/SDL_bmp.c | 6 +++---
9 files changed, 41 insertions(+), 60 deletions(-)
diff --git a/include/SDL3/SDL_error.h b/include/SDL3/SDL_error.h
index 3839faedd36b2..b46749eb0cbb4 100644
--- a/include/SDL3/SDL_error.h
+++ b/include/SDL3/SDL_error.h
@@ -65,6 +65,17 @@ extern "C" {
*/
extern DECLSPEC int SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);
+/**
+ * Set an error indicating that memory allocation failed.
+ *
+ * This function does not do any memory allocation.
+ *
+ * \returns -1
+ *
+ * \since This function is available since SDL 3.0.0.
+ */
+extern DECLSPEC int SDLCALL SDL_OutOfMemory(void);
+
/**
* Retrieve a message about the last error that occurred on the current
* thread.
@@ -121,28 +132,8 @@ extern DECLSPEC int SDLCALL SDL_ClearError(void);
* Private error reporting function - used internally.
*/
/* @{ */
-#define SDL_OutOfMemory() SDL_Error(SDL_ENOMEM)
-#define SDL_Unsupported() SDL_Error(SDL_UNSUPPORTED)
+#define SDL_Unsupported() SDL_SetError("That operation is not supported")
#define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))
-typedef enum SDL_errorcode
-{
- SDL_ENOMEM,
- SDL_EFREAD,
- SDL_EFWRITE,
- SDL_EFSEEK,
- SDL_UNSUPPORTED,
- SDL_LASTERROR
-} SDL_errorcode;
-
-/**
- * Set an SDL error from a list of error codes.
- *
- * \param code Error code
- * \returns unconditionally -1.
- *
- * \since This function is available since SDL 3.0.0.
- */
-extern DECLSPEC int SDLCALL SDL_Error(SDL_errorcode code);
/* @} *//* Internal error functions */
/* Ends C function definitions when using C++ */
diff --git a/src/SDL_error.c b/src/SDL_error.c
index 4133d95462969..0ce4a95f74eb7 100644
--- a/src/SDL_error.c
+++ b/src/SDL_error.c
@@ -59,7 +59,6 @@ int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
return -1;
}
-/* Available for backwards compatibility */
const char *SDL_GetError(void)
{
const SDL_error *error = SDL_GetErrBuf(SDL_FALSE);
@@ -88,22 +87,13 @@ int SDL_ClearError(void)
return 0;
}
-/* Very common errors go here */
-int SDL_Error(SDL_errorcode code)
+int SDL_OutOfMemory(void)
{
- switch (code) {
- case SDL_ENOMEM:
- SDL_GetErrBuf(SDL_TRUE)->error = SDL_ErrorCodeOutOfMemory;
- return -1;
- case SDL_EFREAD:
- return SDL_SetError("Error reading from datastream");
- case SDL_EFWRITE:
- return SDL_SetError("Error writing to datastream");
- case SDL_EFSEEK:
- return SDL_SetError("Error seeking in datastream");
- case SDL_UNSUPPORTED:
- return SDL_SetError("That operation is not supported");
- default:
- return SDL_SetError("Unknown SDL error");
+ SDL_error *error = SDL_GetErrBuf(SDL_TRUE);
+
+ if (error) {
+ error->error = SDL_ErrorCodeOutOfMemory;
}
+ return -1;
}
+
diff --git a/src/dynapi/SDL_dynapi.c b/src/dynapi/SDL_dynapi.c
index 127a1bd0e159e..7daf7d54e356c 100644
--- a/src/dynapi/SDL_dynapi.c
+++ b/src/dynapi/SDL_dynapi.c
@@ -60,7 +60,7 @@ extern "C" {
static void SDL_InitDynamicAPI(void);
/* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
- Even self-contained stuff might call SDL_Error and break everything. */
+ Even self-contained stuff might call SDL_SetError() and break everything. */
/* behold, the macro salsa! */
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index 084253d48f8e3..2ba2be484a3a8 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -127,7 +127,6 @@ SDL3_0.0.0 {
SDL_EnumerateDirectory;
SDL_EnumerateProperties;
SDL_EnumerateStorageDirectory;
- SDL_Error;
SDL_EventEnabled;
SDL_FillSurfaceRect;
SDL_FillSurfaceRects;
@@ -597,6 +596,7 @@ SDL3_0.0.0 {
SDL_OpenTitleStorage;
SDL_OpenURL;
SDL_OpenUserStorage;
+ SDL_OutOfMemory;
SDL_PauseAudioDevice;
SDL_PauseHaptic;
SDL_PeepEvents;
@@ -846,6 +846,7 @@ SDL3_0.0.0 {
SDL_WriteS32LE;
SDL_WriteS64BE;
SDL_WriteS64LE;
+ SDL_WriteStorageFile;
SDL_WriteU16BE;
SDL_WriteU16LE;
SDL_WriteU32BE;
@@ -1014,7 +1015,6 @@ SDL3_0.0.0 {
SDL_wcsnstr;
SDL_wcsstr;
SDL_wcstol;
- SDL_WriteStorageFile;
# extra symbols go here (don't modify this line)
local: *;
};
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 13c9dfa7fcef5..7dbc24eb4c8a8 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -152,7 +152,6 @@
#define SDL_EnumerateDirectory SDL_EnumerateDirectory_REAL
#define SDL_EnumerateProperties SDL_EnumerateProperties_REAL
#define SDL_EnumerateStorageDirectory SDL_EnumerateStorageDirectory_REAL
-#define SDL_Error SDL_Error_REAL
#define SDL_EventEnabled SDL_EventEnabled_REAL
#define SDL_FillSurfaceRect SDL_FillSurfaceRect_REAL
#define SDL_FillSurfaceRects SDL_FillSurfaceRects_REAL
@@ -622,6 +621,7 @@
#define SDL_OpenTitleStorage SDL_OpenTitleStorage_REAL
#define SDL_OpenURL SDL_OpenURL_REAL
#define SDL_OpenUserStorage SDL_OpenUserStorage_REAL
+#define SDL_OutOfMemory SDL_OutOfMemory_REAL
#define SDL_PauseAudioDevice SDL_PauseAudioDevice_REAL
#define SDL_PauseHaptic SDL_PauseHaptic_REAL
#define SDL_PeepEvents SDL_PeepEvents_REAL
@@ -870,6 +870,7 @@
#define SDL_WriteS32LE SDL_WriteS32LE_REAL
#define SDL_WriteS64BE SDL_WriteS64BE_REAL
#define SDL_WriteS64LE SDL_WriteS64LE_REAL
+#define SDL_WriteStorageFile SDL_WriteStorageFile_REAL
#define SDL_WriteU16BE SDL_WriteU16BE_REAL
#define SDL_WriteU16LE SDL_WriteU16LE_REAL
#define SDL_WriteU32BE SDL_WriteU32BE_REAL
@@ -1038,4 +1039,3 @@
#define SDL_wcsnstr SDL_wcsnstr_REAL
#define SDL_wcsstr SDL_wcsstr_REAL
#define SDL_wcstol SDL_wcstol_REAL
-#define SDL_WriteStorageFile SDL_WriteStorageFile_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index bc27e299041f8..5b40471956896 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -190,7 +190,6 @@ SDL_DYNAPI_PROC(int,SDL_EnterAppMainCallbacks,(int a, char *b[], SDL_AppInit_fun
SDL_DYNAPI_PROC(int,SDL_EnumerateDirectory,(const char *a, SDL_EnumerateDirectoryCallback b, void *c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_EnumerateProperties,(SDL_PropertiesID a, SDL_EnumeratePropertiesCallback b, void *c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_EnumerateStorageDirectory,(SDL_Storage *a, const char *b, SDL_EnumerateDirectoryCallback c, void *d),(a,b,c,d),return)
-SDL_DYNAPI_PROC(int,SDL_Error,(SDL_errorcode a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_EventEnabled,(Uint32 a),(a),return)
SDL_DYNAPI_PROC(int,SDL_FillSurfaceRect,(SDL_Surface *a, const SDL_Rect *b, Uint32 c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_FillSurfaceRects,(SDL_Surface *a, const SDL_Rect *b, int c, Uint32 d),(a,b,c,d),return)
@@ -644,6 +643,7 @@ SDL_DYNAPI_PROC(SDL_Storage*,SDL_OpenStorage,(const SDL_StorageInterface *a, voi
SDL_DYNAPI_PROC(SDL_Storage*,SDL_OpenTitleStorage,(const char *a, SDL_PropertiesID b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_OpenURL,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_Storage*,SDL_OpenUserStorage,(const char *a, const char *b, SDL_PropertiesID c),(a,b,c),return)
+SDL_DYNAPI_PROC(int,SDL_OutOfMemory,(void),(),return)
SDL_DYNAPI_PROC(int,SDL_PauseAudioDevice,(SDL_AudioDeviceID a),(a),return)
SDL_DYNAPI_PROC(int,SDL_PauseHaptic,(SDL_Haptic *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_PeepEvents,(SDL_Event *a, int b, SDL_EventAction c, Uint32 d, Uint32 e),(a,b,c,d,e),return)
@@ -890,6 +890,7 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS32BE,(SDL_IOStream *a, Sint32 b),(a,b),return
SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS32LE,(SDL_IOStream *a, Sint32 b),(a,b),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS64BE,(SDL_IOStream *a, Sint64 b),(a,b),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_WriteS64LE,(SDL_IOStream *a, Sint64 b),(a,b),return)
+SDL_DYNAPI_PROC(int,SDL_WriteStorageFile,(SDL_Storage *a, const char *b, const void *c, Uint64 d),(a,b,c,d),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_WriteU16BE,(SDL_IOStream *a, Uint16 b),(a,b),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_WriteU16LE,(SDL_IOStream *a, Uint16 b),(a,b),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_WriteU32BE,(SDL_IOStream *a, Uint32 b),(a,b),return)
@@ -1054,4 +1055,3 @@ SDL_DYNAPI_PROC(size_t,SDL_wcsnlen,(const wchar_t *a, size_t b),(a,b),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsnstr,(const wchar_t *a, const wchar_t *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsstr,(const wchar_t *a, const wchar_t *b),(a,b),return)
SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return)
-SDL_DYNAPI_PROC(int,SDL_WriteStorageFile,(SDL_Storage *a, const char *b, const void *c, Uint64 d),(a,b,c,d),return)
diff --git a/src/file/SDL_iostream.c b/src/file/SDL_iostream.c
index 5edc73fef342e..259c0ffe1bf74 100644
--- a/src/file/SDL_iostream.c
+++ b/src/file/SDL_iostream.c
@@ -230,7 +230,7 @@ static size_t SDLCALL windows_file_read(void *userdata, void *ptr, size_t size,
if (total_need < READAHEAD_BUFFER_SIZE) {
if (!ReadFile(iodata->h, iodata->data, READAHEAD_BUFFER_SIZE, &bytes, NULL)) {
- SDL_Error(SDL_EFREAD);
+ SDL_SetError("Error reading from datastream");
return 0;
}
read_ahead = SDL_min(total_need, bytes);
@@ -240,7 +240,7 @@ static size_t SDLCALL windows_file_read(void *userdata, void *ptr, size_t size,
total_read += read_ahead;
} else {
if (!ReadFile(iodata->h, ptr, (DWORD)total_need, &bytes, NULL)) {
- SDL_Error(SDL_EFREAD);
+ SDL_SetError("Error reading from datastream");
return 0;
}
total_read += bytes;
@@ -256,7 +256,7 @@ static size_t SDLCALL windows_file_write(void *userdata, const void *ptr, size_t
if (iodata->left) {
if (!SetFilePointer(iodata->h, -(LONG)iodata->left, NULL, FILE_CURRENT)) {
- SDL_Error(SDL_EFSEEK);
+ SDL_SetError("Error seeking in datastream");
return 0;
}
iodata->left = 0;
@@ -267,13 +267,13 @@ static size_t SDLCALL windows_file_write(void *userdata, const void *ptr, size_t
LARGE_INTEGER windowsoffset;
windowsoffset.QuadPart = 0;
if (!SetFilePointerEx(iodata->h, windowsoffset, &windowsoffset, FILE_END)) {
- SDL_Error(SDL_EFSEEK);
+ SDL_SetError("Error seeking in datastream");
return 0;
}
}
if (!WriteFile(iodata->h, ptr, (DWORD)total_bytes, &bytes, NULL)) {
- SDL_Error(SDL_EFWRITE);
+ SDL_SetError("Error writing to datastream");
return 0;
}
@@ -371,7 +371,7 @@ static Sint64 SDLCALL stdio_seek(void *userdata, Sint64 offset, int whence)
}
return pos;
}
- return SDL_Error(SDL_EFSEEK);
+ return SDL_SetError("Error seeking in datastream");
}
static size_t SDLCALL stdio_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
@@ -379,7 +379,7 @@ static size_t SDLCALL stdio_read(void *userdata, void *ptr, size_t size, SDL_IOS
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
const size_t bytes = fread(ptr, 1, size, iodata->fp);
if (bytes == 0 && ferror(iodata->fp)) {
- SDL_Error(SDL_EFREAD);
+ SDL_SetError("Error reading from datastream");
}
return bytes;
}
@@ -389,7 +389,7 @@ static size_t SDLCALL stdio_write(void *userdata, const void *ptr, size_t size,
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
const size_t bytes = fwrite(ptr, 1, size, iodata->fp);
if (bytes == 0 && ferror(iodata->fp)) {
- SDL_Error(SDL_EFWRITE);
+ SDL_SetError("Error writing to datastream");
}
return bytes;
}
@@ -400,7 +400,7 @@ static int SDLCALL stdio_close(void *userdata)
int status = 0;
if (iodata->autoclose) {
if (fclose(iodata->fp) != 0) {
- status = SDL_Error(SDL_EFWRITE);
+ status = SDL_SetError("Error writing to datastream");
}
}
SDL_free(iodata);
diff --git a/src/test/SDL_test_harness.c b/src/test/SDL_test_harness.c
index 909368242239a..e86276f035870 100644
--- a/src/test/SDL_test_harness.c
+++ b/src/test/SDL_test_harness.c
@@ -76,7 +76,7 @@ char *SDLTest_GenerateRunSeed(const int length)
seed = (char *)SDL_malloc((length + 1) * sizeof(char));
if (!seed) {
SDLTest_LogError("SDL_malloc for run seed output buffer failed.");
- SDL_Error(SDL_ENOMEM);
+ SDL_OutOfMemory();
return NULL;
}
@@ -151,7 +151,7 @@ static Uint64 SDLTest_GenerateExecKey(const char *runSeed, const char *suiteName
buffer = (char *)SDL_malloc(entireStringLength);
if (!buffer) {
SDLTest_LogError("Failed to allocate buffer for execKey generation.");
- SDL_Error(SDL_ENOMEM);
+ SDL_OutOfMemory();
return 0;
}
(void)SDL_snprintf(buffer, entireStringLength, "%s%s%s%d", runSeed, suiteName, testName, iteration);
@@ -457,7 +457,7 @@ int SDLTest_RunSuites(SDLTest_TestSuiteReference *testSuites[], const char *user
failedTests = (const SDLTest_TestCaseReference **)SDL_malloc(totalNumberOfTests * sizeof(SDLTest_TestCaseReference *));
if (!failedTests) {
SDLTest_LogError("Unable to allocate cache for failed tests");
- SDL_Error(SDL_ENOMEM);
+ SDL_OutOfMemory();
return -1;
}
diff --git a/src/video/SDL_bmp.c b/src/video/SDL_bmp.c
index 8a1d39d3bb6a9..7993c7789ea2e 100644
--- a/src/video/SDL_bmp.c
+++ b/src/video/SDL_bmp.c
@@ -442,7 +442,7 @@ SDL_Surface *SDL_LoadBMP_IO(SDL_IOStream *src, SDL_bool closeio)
palette = (surface->format)->palette;
if (palette) {
if (SDL_SeekIO(src, fp_offset + 14 + biSize, SDL_IO_SEEK_SET) < 0) {
- SDL_Error(SDL_EFSEEK);
+ SDL_SetError("Error seeking in datastream");
goto done;
}
@@ -493,13 +493,13 @@ SDL_Surface *SDL_LoadBMP_IO(SDL_IOStream *src, SDL_bool closeio)
/* Read the surface pixels. Note that the bmp image is upside down */
if (SDL_SeekIO(src, fp_offset + bfOffBits, SDL_IO_SEEK_SET) < 0) {
- SDL_Error(SDL_EFSEEK);
+ SDL_SetError("Error seeking in datastream");
goto done;
}
if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
was_error = readRlePixels(surface, src, biCompression == BI_RLE8);
if (was_error) {
- SDL_Error(SDL_EFREAD);
+ SDL_SetError("Error reading from datastream");
}
goto done;
}