SDL  2.0
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_errorSDL_GetErrBuf (void)
 
static const char * SDL_LookupString (const char *key)
 
static char * SDL_GetErrorMsg (char *errstr, int maxlen)
 
int SDL_SetError (SDL_PRINTF_FORMAT_STRING const char *fmt,...)
 
const char * SDL_GetError (void)
 
void SDL_ClearError (void)
 
int SDL_Error (SDL_errorcode code)
 

Macro Definition Documentation

◆ SDL_ERRBUFIZE

#define SDL_ERRBUFIZE   1024

Definition at line 39 of file SDL_error.c.

Referenced by SDL_GetError(), and SDL_SetError().

Function Documentation

◆ SDL_ClearError()

void SDL_ClearError ( void  )

Definition at line 138 of file SDL_error.c.

References SDL_error::error, and SDL_GetErrBuf().

Referenced by SDL_Error().

139 {
140  SDL_error *error;
141 
142  error = SDL_GetErrBuf();
143  error->error = 0;
144 }
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:206

◆ SDL_Error()

int SDL_Error ( SDL_errorcode  code)

Definition at line 148 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.

149 {
150  switch (code) {
151  case SDL_ENOMEM:
152  return SDL_SetError("Out of memory");
153  case SDL_EFREAD:
154  return SDL_SetError("Error reading from datastream");
155  case SDL_EFWRITE:
156  return SDL_SetError("Error writing to datastream");
157  case SDL_EFSEEK:
158  return SDL_SetError("Error seeking in datastream");
159  case SDL_UNSUPPORTED:
160  return SDL_SetError("That operation is not supported");
161  default:
162  return SDL_SetError("Unknown SDL error");
163  }
164 }
int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt,...)
Definition: SDL_error.c:55

◆ SDL_GetErrBuf()

SDL_error* SDL_GetErrBuf ( void  )

Definition at line 206 of file SDL_thread.c.

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

207 {
208  static SDL_SpinLock tls_lock;
209  static SDL_bool tls_being_created;
210  static SDL_TLSID tls_errbuf;
211  static SDL_error SDL_global_errbuf;
212  const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
213  SDL_error *errbuf;
214 
215  /* tls_being_created is there simply to prevent recursion if SDL_TLSCreate() fails.
216  It also means it's possible for another thread to also use SDL_global_errbuf,
217  but that's very unlikely and hopefully won't cause issues.
218  */
219  if (!tls_errbuf && !tls_being_created) {
220  SDL_AtomicLock(&tls_lock);
221  if (!tls_errbuf) {
222  SDL_TLSID slot;
223  tls_being_created = SDL_TRUE;
224  slot = SDL_TLSCreate();
225  tls_being_created = SDL_FALSE;
227  tls_errbuf = slot;
228  }
229  SDL_AtomicUnlock(&tls_lock);
230  }
231  if (!tls_errbuf) {
232  return &SDL_global_errbuf;
233  }
234 
236  errbuf = (SDL_error *)SDL_TLSGet(tls_errbuf);
237  if (errbuf == ALLOCATION_IN_PROGRESS) {
238  return &SDL_global_errbuf;
239  }
240  if (!errbuf) {
241  /* Mark that we're in the middle of allocating our buffer */
242  SDL_TLSSet(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL);
243  errbuf = (SDL_error *)SDL_malloc(sizeof(*errbuf));
244  if (!errbuf) {
245  SDL_TLSSet(tls_errbuf, NULL, NULL);
246  return &SDL_global_errbuf;
247  }
248  SDL_zerop(errbuf);
249  SDL_TLSSet(tls_errbuf, errbuf, SDL_free);
250  }
251  return errbuf;
252 }
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:53
#define SDL_AtomicLock
#define SDL_MemoryBarrierRelease()
Definition: SDL_atomic.h:186
void * SDL_TLSGet(SDL_TLSID id)
Get the value associated with a thread local storage ID for the current thread.
Definition: SDL_thread.c:41
#define SDL_zerop(x)
Definition: SDL_stdinc.h:370
#define SDL_MemoryBarrierAcquire()
Definition: SDL_atomic.h:187
unsigned int SDL_TLSID
Definition: SDL_thread.h:52
#define SDL_AtomicUnlock
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
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:34
#define SDL_malloc
int SDL_SpinLock
Definition: SDL_atomic.h:89

◆ SDL_GetError()

const char* SDL_GetError ( void  )

Definition at line 130 of file SDL_error.c.

References SDL_ERRBUFIZE, and SDL_GetErrorMsg().

Referenced by SDL_Error().

131 {
132  static char errmsg[SDL_ERRBUFIZE];
133 
134  return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
135 }
#define SDL_ERRBUFIZE
Definition: SDL_error.c:39
static char * SDL_GetErrorMsg(char *errstr, int maxlen)
Definition: SDL_error.c:196

◆ SDL_GetErrorMsg()

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

Definition at line 196 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_LookupString(), and SDL_SetError().

197 {
198  SDL_error *error;
199 
200  /* Clear the error string */
201  *errstr = '\0';
202  --maxlen;
203 
204  /* Get the thread-safe error, and print it out */
205  error = SDL_GetErrBuf();
206  if (error->error) {
207  const char *fmt;
208  char *msg = errstr;
209  int len;
210  int argi;
211 
212  fmt = SDL_LookupString(error->key);
213  argi = 0;
214  while (*fmt && (maxlen > 0)) {
215  if (*fmt == '%') {
216  char tmp[32], *spot = tmp;
217  *spot++ = *fmt++;
218  while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
219  && spot < (tmp + SDL_arraysize(tmp) - 2)) {
220  *spot++ = *fmt++;
221  }
222  *spot++ = *fmt++;
223  *spot++ = '\0';
224  switch (spot[-2]) {
225  case '%':
226  *msg++ = '%';
227  maxlen -= 1;
228  break;
229  case 'c':
230  case 'i':
231  case 'd':
232  case 'u':
233  case 'o':
234  case 'x':
235  case 'X':
236  len =
237  SDL_snprintf(msg, maxlen, tmp,
238  error->args[argi++].value_i);
239  if (len > 0) {
240  msg += len;
241  maxlen -= len;
242  }
243  break;
244 
245  case 'f':
246  len =
247  SDL_snprintf(msg, maxlen, tmp,
248  error->args[argi++].value_f);
249  if (len > 0) {
250  msg += len;
251  maxlen -= len;
252  }
253  break;
254 
255  case 'p':
256  len =
257  SDL_snprintf(msg, maxlen, tmp,
258  error->args[argi++].value_ptr);
259  if (len > 0) {
260  msg += len;
261  maxlen -= len;
262  }
263  break;
264 
265  case 's':
266  len =
267  SDL_snprintf(msg, maxlen, tmp,
268  SDL_LookupString(error->args[argi++].
269  buf));
270  if (len > 0) {
271  msg += len;
272  maxlen -= len;
273  }
274  break;
275 
276  }
277  } else {
278  *msg++ = *fmt++;
279  maxlen -= 1;
280  }
281  }
282 
283  /* slide back if we've overshot the end of our buffer. */
284  if (maxlen < 0) {
285  msg -= (-maxlen) + 1;
286  }
287 
288  *msg = 0; /* NULL terminate the string */
289  }
290  return (errstr);
291 }
union SDL_error::@29 args[ERR_MAX_ARGS]
double value_f
Definition: SDL_error_c.h:54
static const char * SDL_LookupString(const char *key)
Definition: SDL_error.c:44
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:206
GLenum GLsizei len
GLenum GLuint GLenum GLsizei const GLchar * buf
char key[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:43
#define SDL_snprintf
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
void * value_ptr
Definition: SDL_error_c.h:49
int value_i
Definition: SDL_error_c.h:53

◆ SDL_LookupString()

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

Definition at line 44 of file SDL_error.c.

References SDL_GetErrorMsg().

Referenced by SDL_GetErrorMsg().

45 {
46  /* FIXME: Add code to lookup key in language string hash-table */
47  return key;
48 }
GLuint64 key
Definition: gl2ext.h:2192

◆ SDL_SetError()

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

Definition at line 55 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_ERRBUFIZE, SDL_GetErrBuf(), SDL_GetErrorMsg(), SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_DEBUG, SDL_LogDebug, SDL_LogGetPriority, SDL_strlcpy, SDL_error::value_f, SDL_error::value_i, and SDL_error::value_ptr.

Referenced by SDL_Error().

56 {
57  va_list ap;
58  SDL_error *error;
59 
60  /* Ignore call if invalid format pointer was passed */
61  if (fmt == NULL) return -1;
62 
63  /* Copy in the key, mark error as valid */
64  error = SDL_GetErrBuf();
65  error->error = 1;
66  SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
67 
68  va_start(ap, fmt);
69  error->argc = 0;
70  while (*fmt) {
71  if (*fmt++ == '%') {
72  while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
73  ++fmt;
74  }
75  switch (*fmt++) {
76  case 0: /* Malformed format string.. */
77  --fmt;
78  break;
79  case 'c':
80  case 'i':
81  case 'd':
82  case 'u':
83  case 'o':
84  case 'x':
85  case 'X':
86  error->args[error->argc++].value_i = va_arg(ap, int);
87  break;
88  case 'f':
89  error->args[error->argc++].value_f = va_arg(ap, double);
90  break;
91  case 'p':
92  error->args[error->argc++].value_ptr = va_arg(ap, void *);
93  break;
94  case 's':
95  {
96  int i = error->argc;
97  const char *str = va_arg(ap, const char *);
98  if (str == NULL)
99  str = "(null)";
100  SDL_strlcpy((char *) error->args[i].buf, str,
102  error->argc++;
103  }
104  break;
105  default:
106  break;
107  }
108  if (error->argc >= ERR_MAX_ARGS) {
109  break;
110  }
111  }
112  }
113  va_end(ap);
114 
116  /* If we are in debug mode, print out an error message
117  * Avoid stomping on the static buffer in GetError, just
118  * in case this is called while processing a ShowMessageBox to
119  * show an error already in that static buffer.
120  */
121  char errmsg[SDL_ERRBUFIZE];
122  SDL_GetErrorMsg(errmsg, sizeof(errmsg));
123  SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg);
124  }
125  return -1;
126 }
#define SDL_strlcpy
#define SDL_ERRBUFIZE
Definition: SDL_error.c:39
union SDL_error::@29 args[ERR_MAX_ARGS]
double value_f
Definition: SDL_error_c.h:54
#define SDL_LogGetPriority
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:206
#define ERR_MAX_STRLEN
Definition: SDL_error_c.h:30
#define SDL_LogDebug
#define ERR_MAX_ARGS
Definition: SDL_error_c.h:31
char key[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:43
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:50
#define NULL
Definition: begin_code.h:164
char buf[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:55
void * value_ptr
Definition: SDL_error_c.h:49
int value_i
Definition: SDL_error_c.h:53
static char * SDL_GetErrorMsg(char *errstr, int maxlen)
Definition: SDL_error.c:196