SDL  2.0
SDL_dynapi.c File Reference
#include "SDL_config.h"
#include "SDL_dynapi.h"
#include "SDL.h"
#include "SDL_syswm.h"
#include "SDL_vulkan.h"
#include "SDL_dynapi_procs.h"
#include <windows.h>
+ Include dependency graph for SDL_dynapi.c:

Go to the source code of this file.

Data Structures

struct  SDL_DYNAPI_jump_table
 

Macros

#define SDL_DYNAPI_VERSION   1
 
#define DISABLE_JUMP_MAGIC   1
 
#define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio)
 
#define SDL_DYNAPI_VARARGS(_static, name, initcall)
 
#define SDL_DYNAPI_PROC(rc, fn, params, args, ret)
 
#define SDL_DYNAPI_PROC(rc, fn, params, args, ret)   SDL_DYNAPIFN_##fn fn;
 
#define SDL_DYNAPI_PROC(rc, fn, params, args, ret)   static rc SDLCALL fn##_DEFAULT params;
 
#define SDL_DYNAPI_PROC(rc, fn, params, args, ret)   fn##_DEFAULT,
 
#define SDL_DYNAPI_PROC(rc, fn, params, args, ret)
 
#define SDL_DYNAPI_PROC_NO_VARARGS   1
 
#define SDL_DYNAPI_PROC(rc, fn, params, args, ret)   rc SDLCALL fn params { ret jump_table.fn args; }
 
#define SDL_DYNAPI_PROC_NO_VARARGS   1
 
#define SDL_DYNAPI_PROC(rc, fn, params, args, ret)   jump_table.fn = fn##_REAL;
 
#define WIN32_LEAN_AND_MEAN   1
 

Typedefs

typedef Sint32(* SDL_DYNAPI_ENTRYFN) (Uint32 apiver, void *table, Uint32 tablesize)
 

Functions

static void SDL_InitDynamicAPI (void)
 
static Sint32 initialize_jumptable (Uint32 apiver, void *table, Uint32 tablesize)
 
Sint32 SDL_DYNAPI_entry (Uint32, void *, Uint32)
 
static SDL_INLINE voidget_sdlapi_entry (const char *fname, const char *sym)
 
static void SDL_InitDynamicAPILocked (void)
 

Variables

static SDL_DYNAPI_jump_table jump_table
 

Macro Definition Documentation

◆ DISABLE_JUMP_MAGIC

#define DISABLE_JUMP_MAGIC   1

Definition at line 60 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [1/7]

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)
Value:
typedef rc (SDLCALL *SDL_DYNAPIFN_##fn) params; \
static rc SDLCALL fn##_DEFAULT params; \
extern rc SDLCALL fn##_REAL params;
const GLfloat * params
#define SDLCALL
Definition: SDL_internal.h:45

Definition at line 158 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [2/7]

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)    SDL_DYNAPIFN_##fn fn;

Definition at line 158 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [3/7]

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)    static rc SDLCALL fn##_DEFAULT params;

Definition at line 158 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [4/7]

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)    fn##_DEFAULT,

Definition at line 158 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [5/7]

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)
Value:
static rc SDLCALL fn##_DEFAULT params { \
SDL_InitDynamicAPI(); \
ret jump_table.fn args; \
}
static SDL_DYNAPI_jump_table jump_table
Definition: SDL_dynapi.c:133
const GLfloat * params
#define SDLCALL
Definition: SDL_internal.h:45

Definition at line 158 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [6/7]

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)    rc SDLCALL fn params { ret jump_table.fn args; }

Definition at line 158 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [7/7]

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)    jump_table.fn = fn##_REAL;

Definition at line 158 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC_NO_VARARGS [1/2]

#define SDL_DYNAPI_PROC_NO_VARARGS   1

Definition at line 160 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC_NO_VARARGS [2/2]

#define SDL_DYNAPI_PROC_NO_VARARGS   1

Definition at line 160 of file SDL_dynapi.c.

◆ SDL_DYNAPI_VARARGS

#define SDL_DYNAPI_VARARGS (   _static,
  name,
  initcall 
)

Definition at line 71 of file SDL_dynapi.c.

◆ SDL_DYNAPI_VARARGS_LOGFN

#define SDL_DYNAPI_VARARGS_LOGFN (   _static,
  name,
  initcall,
  logname,
  prio 
)
Value:
_static void SDLCALL SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
va_list ap; initcall; va_start(ap, fmt); \
jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
va_end(ap); \
}
GLuint const GLchar * name
#define SDL_Log
#define SDL_PRINTF_FORMAT_STRING
Definition: SDL_stdinc.h:278
#define SDLCALL
Definition: SDL_internal.h:45

Definition at line 64 of file SDL_dynapi.c.

◆ SDL_DYNAPI_VERSION

#define SDL_DYNAPI_VERSION   1

Definition at line 48 of file SDL_dynapi.c.

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN   1

Definition at line 216 of file SDL_dynapi.c.

Typedef Documentation

◆ SDL_DYNAPI_ENTRYFN

typedef Sint32( * SDL_DYNAPI_ENTRYFN) (Uint32 apiver, void *table, Uint32 tablesize)

Definition at line 202 of file SDL_dynapi.c.

Function Documentation

◆ get_sdlapi_entry()

static SDL_INLINE void* get_sdlapi_entry ( const char *  fname,
const char *  sym 
)
static

Definition at line 219 of file SDL_dynapi.c.

220 {
221  HANDLE lib = LoadLibraryA(fname);
222  void *retval = NULL;
223  if (lib) {
224  retval = GetProcAddress(lib, sym);
225  if (retval == NULL) {
226  FreeLibrary(lib);
227  }
228  }
229  return retval;
230 }
SDL_bool retval
#define NULL
Definition: begin_code.h:164

◆ initialize_jumptable()

static Sint32 initialize_jumptable ( Uint32  apiver,
void table,
Uint32  tablesize 
)
static

Definition at line 173 of file SDL_dynapi.c.

174 {
175  SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
176 
177  if (apiver != SDL_DYNAPI_VERSION) {
178  /* !!! FIXME: can maybe handle older versions? */
179  return -1; /* not compatible. */
180  } else if (tablesize > sizeof (jump_table)) {
181  return -1; /* newer version of SDL with functions we can't provide. */
182  }
183 
184  /* Init our jump table first. */
185  #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) jump_table.fn = fn##_REAL;
186  #include "SDL_dynapi_procs.h"
187  #undef SDL_DYNAPI_PROC
188 
189  /* Then the external table... */
190  if (output_jump_table != &jump_table) {
191  jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
192  }
193 
194  /* Safe to call SDL functions now; jump table is initialized! */
195 
196  return 0; /* success! */
197 }
GLenum GLsizei GLenum GLenum const void * table
#define SDL_DYNAPI_VERSION
Definition: SDL_dynapi.c:48
static SDL_DYNAPI_jump_table jump_table
Definition: SDL_dynapi.c:133

◆ SDL_DYNAPI_entry()

Sint32 SDL_DYNAPI_entry ( Uint32  apiver,
void table,
Uint32  tablesize 
)

Definition at line 206 of file SDL_dynapi.c.

207 {
208  return initialize_jumptable(apiver, table, tablesize);
209 }
GLenum GLsizei GLenum GLenum const void * table
static Sint32 initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
Definition: SDL_dynapi.c:173

◆ SDL_InitDynamicAPI()

static void SDL_InitDynamicAPI ( void  )
static

Definition at line 296 of file SDL_dynapi.c.

297 {
298  /* So the theory is that every function in the jump table defaults to
299  * calling this function, and then replaces itself with a version that
300  * doesn't call this function anymore. But it's possible that, in an
301  * extreme corner case, you can have a second thread hit this function
302  * while the jump table is being initialized by the first.
303  * In this case, a spinlock is really painful compared to what spinlocks
304  * _should_ be used for, but this would only happen once, and should be
305  * insanely rare, as you would have to spin a thread outside of SDL (as
306  * SDL_CreateThread() would also call this function before building the
307  * new thread).
308  */
309  static SDL_bool already_initialized = SDL_FALSE;
310 
311  /* SDL_AtomicLock calls SDL mutex functions to emulate if
312  SDL_ATOMIC_DISABLED, which we can't do here, so in such a
313  configuration, you're on your own. */
314  #if !SDL_ATOMIC_DISABLED
315  static SDL_SpinLock lock = 0;
316  SDL_AtomicLock_REAL(&lock);
317  #endif
318 
319  if (!already_initialized) {
321  already_initialized = SDL_TRUE;
322  }
323 
324  #if !SDL_ATOMIC_DISABLED
325  SDL_AtomicUnlock_REAL(&lock);
326  #endif
327 }
static void SDL_InitDynamicAPILocked(void)
Definition: SDL_dynapi.c:267
SDL_bool
Definition: SDL_stdinc.h:139
SDL_mutex * lock
Definition: SDL_events.c:78
int SDL_SpinLock
Definition: SDL_atomic.h:89

◆ SDL_InitDynamicAPILocked()

static void SDL_InitDynamicAPILocked ( void  )
static

Definition at line 267 of file SDL_dynapi.c.

268 {
269  const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
270  SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
271 
272  if (libname) {
273  entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
274  if (!entry) {
275  /* !!! FIXME: fail to startup here instead? */
276  /* !!! FIXME: definitely warn user. */
277  /* Just fill in the function pointers from this library. */
278  }
279  }
280 
281  if (!entry || (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0)) {
282  /* !!! FIXME: fail to startup here instead? */
283  /* !!! FIXME: definitely warn user. */
284  /* Just fill in the function pointers from this library. */
285  if (!entry) {
287  /* !!! FIXME: now we're screwed. Should definitely abort now. */
288  }
289  }
290  }
291 
292  /* we intentionally never close the newly-loaded lib, of course. */
293 }
static SDL_INLINE void * get_sdlapi_entry(const char *fname, const char *sym)
Definition: SDL_dynapi.c:219
Sint32(* SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize)
Definition: SDL_dynapi.c:202
#define SDL_DYNAPI_VERSION
Definition: SDL_dynapi.c:48
static SDL_DYNAPI_jump_table jump_table
Definition: SDL_dynapi.c:133
#define NULL
Definition: begin_code.h:164
static Sint32 initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
Definition: SDL_dynapi.c:173

Variable Documentation

◆ jump_table

SDL_DYNAPI_jump_table jump_table
static

Definition at line 133 of file SDL_dynapi.c.