SDL_error.c File Reference
`#include "./SDL_internal.h"`
`#include "SDL_log.h"`
`#include "SDL_error.h"`
`#include "SDL_error_c.h"`
Include dependency graph for SDL_error.c:

Go to the source code of this file.

## Macros

#define | SDL_ERRBUFIZE 1024

## Functions

SDL_error * | SDL_GetErrBuf (void)
static const char * | SDL_LookupString (const char *key)
int | SDL_SetError (SDL_PRINTF_FORMAT_STRING const char *fmt,...)
static char * | SDL_GetErrorMsg (char *errstr, int maxlen)
const char * | SDL_GetError (void)
void | SDL_ClearError (void)
int | SDL_Error (SDL_errorcode code)

## Macro Definition Documentation

#define SDL_ERRBUFIZE 1024

Definition at line 39 of file SDL_error.c.

Referenced by SDL_GetError().

## Function Documentation

void SDL_ClearError | ( | void |  | ) |

Definition at line 230 of file SDL_error.c.

References SDL_error::error, and SDL_GetErrBuf().

Referenced by SDL_Error().

{
SDL_error *error;
error = SDL_GetErrBuf();
error->error = 0;
}
SDL_error
Definition: SDL_error_c.h:33
SDL_GetErrBuf
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:205
SDL_error::error
int error
Definition: SDL_error_c.h:36

int SDL_Error | ( | SDL_errorcode | code | ) |

Definition at line 240 of file SDL_error.c.

References main, SDL_ClearError(), SDL_EFREAD, SDL_EFSEEK, SDL_EFWRITE, SDL_ENOMEM, SDL_GetError(), SDL_memset, SDL_SetError(), and SDL_UNSUPPORTED.

{
switch (code) {
case SDL_ENOMEM:
return SDL_SetError("Out of memory");
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_EFSEEK
Definition: SDL_error.h:60
SDL_ENOMEM
Definition: SDL_error.h:57
SDL_UNSUPPORTED
Definition: SDL_error.h:61
SDL_EFREAD
Definition: SDL_error.h:58
SDL_SetError
int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt,...)
Definition: SDL_error.c:53
SDL_EFWRITE
Definition: SDL_error.h:59

SDL_error* SDL_GetErrBuf | ( | void |  | ) |

Definition at line 205 of file SDL_thread.c.

Referenced by SDL_ClearError(), SDL_GetErrorMsg(), and SDL_SetError().

{
static SDL_SpinLock tls_lock;
static SDL_bool tls_being_created;
static SDL_TLSID tls_errbuf;
static SDL_error SDL_global_errbuf;
const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
SDL_error *errbuf;
/* tls_being_created is there simply to prevent recursion if SDL_TLSCreate() fails.
It also means it's possible for another thread to also use SDL_global_errbuf,
but that's very unlikely and hopefully won't cause issues.
*/
if (!tls_errbuf && !tls_being_created) {
SDL_AtomicLock(&tls_lock);
if (!tls_errbuf) {
SDL_TLSID slot;
tls_being_created = SDL_TRUE;
slot = SDL_TLSCreate();
tls_being_created = SDL_FALSE;
SDL_MemoryBarrierRelease();
tls_errbuf = slot;
}
SDL_AtomicUnlock(&tls_lock);
}
if (!tls_errbuf) {
return &SDL_global_errbuf;
}
SDL_MemoryBarrierAcquire();
errbuf = (SDL_error *)SDL_TLSGet(tls_errbuf);
if (errbuf == ALLOCATION_IN_PROGRESS) {
return &SDL_global_errbuf;
}
if (!errbuf) {
/* Mark that we're in the middle of allocating our buffer */
SDL_TLSSet(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL);
errbuf = (SDL_error *)SDL_malloc(sizeof(*errbuf));
if (!errbuf) {
SDL_TLSSet(tls_errbuf, NULL, NULL);
return &SDL_global_errbuf;
}
SDL_zerop(errbuf);
SDL_TLSSet(tls_errbuf, errbuf, SDL_free);
}
return errbuf;
}
SDL_TLSSet
int SDL_TLSSet(SDL_TLSID id, const void *value, void(*destructor)(void *))
Set the value associated with a thread local storage ID for the current thread.
Definition: SDL_thread.c:52
SDL_AtomicLock
#define SDL_AtomicLock
Definition: SDL_dynapi_overrides.h:70
SDL_MemoryBarrierRelease
#define SDL_MemoryBarrierRelease()
Definition: SDL_atomic.h:180
SDL_TLSGet
void * SDL_TLSGet(SDL_TLSID id)
Get the value associated with a thread local storage ID for the current thread.
Definition: SDL_thread.c:40
SDL_error
Definition: SDL_error_c.h:33
SDL_zerop
#define SDL_zerop(x)
Definition: SDL_stdinc.h:356
SDL_MemoryBarrierAcquire
#define SDL_MemoryBarrierAcquire()
Definition: SDL_atomic.h:181
SDL_TLSID
unsigned int SDL_TLSID
Definition: SDL_thread.h:52
SDL_AtomicUnlock
#define SDL_AtomicUnlock
Definition: SDL_dynapi_overrides.h:71
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
SDL_free
void SDL_free(void *mem)
SDL_TRUE
Definition: SDL_stdinc.h:129
NULL
#define NULL
Definition: begin_code.h:143
SDL_TLSCreate
SDL_TLSID SDL_TLSCreate()
Create an identifier that is globally visible to all threads but refers to data that is thread-specif...
Definition: SDL_thread.c:33
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:380
SDL_SpinLock
int SDL_SpinLock
Definition: SDL_atomic.h:89

const char* SDL_GetError | ( | void |  | ) |

Definition at line 222 of file SDL_error.c.

References SDL_ERRBUFIZE, and SDL_GetErrorMsg().

Referenced by SDL_Error(), and SDL_SetError().

{
static char errmsg[SDL_ERRBUFIZE];
return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
}
SDL_ERRBUFIZE
#define SDL_ERRBUFIZE
Definition: SDL_error.c:39
SDL_GetErrorMsg
static char * SDL_GetErrorMsg(char *errstr, int maxlen)
Definition: SDL_error.c:123

static char* SDL_GetErrorMsg | ( | char * | errstr,
|  | int | maxlen
| ) |  |

static

Definition at line 123 of file SDL_error.c.

References SDL_error::args, SDL_error::error, SDL_error::key, SDL_arraysize, SDL_GetErrBuf(), SDL_LookupString(), SDL_snprintf, SDL_error::value_f, SDL_error::value_i, and SDL_error::value_ptr.

Referenced by SDL_GetError().

{
SDL_error *error;
/* Clear the error string */
*errstr = '\0';
--maxlen;
/* Get the thread-safe error, and print it out */
error = SDL_GetErrBuf();
if (error->error) {
const char *fmt;
char *msg = errstr;
int len;
int argi;
fmt = SDL_LookupString(error->key);
argi = 0;
while (*fmt && (maxlen > 0)) {
if (*fmt == '%') {
char tmp[32], *spot = tmp;
*spot++ = *fmt++;
while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
&& spot < (tmp + SDL_arraysize(tmp) - 2)) {
*spot++ = *fmt++;
}
*spot++ = *fmt++;
*spot++ = '\0';
switch (spot[-2]) {
case '%':
*msg++ = '%';
maxlen -= 1;
break;
case 'c':
case 'i':
case 'd':
case 'u':
case 'o':
case 'x':
case 'X':
len =
SDL_snprintf(msg, maxlen, tmp,
error->args[argi++].value_i);
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
case 'f':
len =
SDL_snprintf(msg, maxlen, tmp,
error->args[argi++].value_f);
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
case 'p':
len =
SDL_snprintf(msg, maxlen, tmp,
error->args[argi++].value_ptr);
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
case 's':
len =
SDL_snprintf(msg, maxlen, tmp,
SDL_LookupString(error->args[argi++].
buf));
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
}
} else {
*msg++ = *fmt++;
maxlen -= 1;
}
}
/* slide back if we've overshot the end of our buffer. */
if (maxlen < 0) {
msg -= (-maxlen) + 1;
}
*msg = 0; /* NULL terminate the string */
}
return (errstr);
}
SDL_error::value_f
double value_f
Definition: SDL_error_c.h:54
SDL_LookupString
static const char * SDL_LookupString(const char *key)
Definition: SDL_error.c:44
SDL_error
Definition: SDL_error_c.h:33
SDL_GetErrBuf
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:205
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
SDL_error::error
int error
Definition: SDL_error_c.h:36
SDL_error::args
union SDL_error::@24 args[ERR_MAX_ARGS]
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: SDL_opengl_glext.h:2480
SDL_error::key
char key[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:43
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:46
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_error::value_ptr
void * value_ptr
Definition: SDL_error_c.h:49
SDL_error::value_i
int value_i
Definition: SDL_error_c.h:53

static const char* SDL_LookupString | ( | const char * | key | ) |

static

Definition at line 44 of file SDL_error.c.

Referenced by SDL_GetErrorMsg().

{
/* FIXME: Add code to lookup key in language string hash-table */
return key;
}

int SDL_SetError | ( | SDL_PRINTF_FORMAT_STRING const char * | fmt,
|  |  | ...
| ) |  |

Definition at line 53 of file SDL_error.c.

References SDL_error::argc, SDL_error::args, SDL_error::buf, ERR_MAX_ARGS, ERR_MAX_STRLEN, SDL_error::error, i, SDL_error::key, NULL, SDL_GetErrBuf(), SDL_GetError(), SDL_LOG_CATEGORY_ERROR, SDL_LogDebug, SDL_strlcpy, SDL_error::value_f, SDL_error::value_i, and SDL_error::value_ptr.

Referenced by SDL_Error().

{
va_list ap;
SDL_error *error;
/* Ignore call if invalid format pointer was passed */
if (fmt == NULL) return -1;
/* Copy in the key, mark error as valid */
error = SDL_GetErrBuf();
error->error = 1;
SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
va_start(ap, fmt);
error->argc = 0;
while (*fmt) {
if (*fmt++ == '%') {
while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
++fmt;
}
switch (*fmt++) {
case 0: /* Malformed format string.. */
--fmt;
break;
case 'c':
case 'i':
case 'd':
case 'u':
case 'o':
case 'x':
case 'X':
error->args[error->argc++].value_i = va_arg(ap, int);
break;
case 'f':
error->args[error->argc++].value_f = va_arg(ap, double);
break;
case 'p':
error->args[error->argc++].value_ptr = va_arg(ap, void *);
break;
case 's':
{
int i = error->argc;
const char *str = va_arg(ap, const char *);
if (str == NULL)
str = "(null)";
SDL_strlcpy((char *) error->args[i].buf, str,
ERR_MAX_STRLEN);
error->argc++;
}
break;
default:
break;
}
if (error->argc >= ERR_MAX_ARGS) {
break;
}
}
}
va_end(ap);
/* If we are in debug mode, print out an error message */
SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError());
return -1;
}
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:400
SDL_error::value_f
double value_f
Definition: SDL_error_c.h:54
SDL_error
Definition: SDL_error_c.h:33
SDL_GetErrBuf
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:205
ERR_MAX_STRLEN
#define ERR_MAX_STRLEN
Definition: SDL_error_c.h:30
SDL_GetError
const char * SDL_GetError(void)
Definition: SDL_error.c:222
SDL_error::error
int error
Definition: SDL_error_c.h:36
SDL_LogDebug
#define SDL_LogDebug
Definition: SDL_dynapi_overrides.h:39
SDL_error::args
union SDL_error::@24 args[ERR_MAX_ARGS]
ERR_MAX_ARGS
#define ERR_MAX_ARGS
Definition: SDL_error_c.h:31
SDL_error::key
char key[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:43
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
NULL
#define NULL
Definition: begin_code.h:143
SDL_error::buf
char buf[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:55
SDL_error::value_ptr
void * value_ptr
Definition: SDL_error_c.h:49
SDL_error::argc
int argc
Definition: SDL_error_c.h:46
SDL_error::value_i
int value_i
Definition: SDL_error_c.h:53
SDL_LOG_CATEGORY_ERROR
Definition: SDL_log.h:67