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

#define DISABLE_JUMP_MAGIC   1

Definition at line 61 of file SDL_dynapi.c.

#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;

Definition at line 159 of file SDL_dynapi.c.

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

Definition at line 159 of file SDL_dynapi.c.

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

Definition at line 159 of file SDL_dynapi.c.

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

Definition at line 159 of file SDL_dynapi.c.

#define SDL_DYNAPI_PROC (   rc,
  fn,
  params,
  args,
  ret 
)
Value:
static rc SDLCALL fn##_DEFAULT params { \
SDL_InitDynamicAPI(); \
ret jump_table.fn args; \
}

Definition at line 159 of file SDL_dynapi.c.

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

Definition at line 159 of file SDL_dynapi.c.

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

Definition at line 159 of file SDL_dynapi.c.

#define SDL_DYNAPI_PROC_NO_VARARGS   1

Definition at line 161 of file SDL_dynapi.c.

#define SDL_DYNAPI_PROC_NO_VARARGS   1

Definition at line 161 of file SDL_dynapi.c.

#define SDL_DYNAPI_VARARGS (   _static,
  name,
  initcall 
)

Definition at line 72 of file SDL_dynapi.c.

#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); \
}

Definition at line 65 of file SDL_dynapi.c.

#define SDL_DYNAPI_VERSION   1

Definition at line 49 of file SDL_dynapi.c.

#define WIN32_LEAN_AND_MEAN   1

Definition at line 217 of file SDL_dynapi.c.

Typedef Documentation

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

Definition at line 203 of file SDL_dynapi.c.

Function Documentation

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

Definition at line 220 of file SDL_dynapi.c.

{
HANDLE lib = LoadLibraryA(fname);
void *retval = NULL;
if (lib) {
retval = GetProcAddress(lib, sym);
if (retval == NULL) {
FreeLibrary(lib);
}
}
return retval;
}
static Sint32 initialize_jumptable ( Uint32  apiver,
void table,
Uint32  tablesize 
)
static

Definition at line 174 of file SDL_dynapi.c.

{
if (apiver != SDL_DYNAPI_VERSION) {
/* !!! FIXME: can maybe handle older versions? */
return -1; /* not compatible. */
} else if (tablesize > sizeof (jump_table)) {
return -1; /* newer version of SDL with functions we can't provide. */
}
/* Init our jump table first. */
#define SDL_DYNAPI_PROC(rc,fn,params,args,ret) jump_table.fn = fn##_REAL;
#include "SDL_dynapi_procs.h"
#undef SDL_DYNAPI_PROC
/* Then the external table... */
if (output_jump_table != &jump_table) {
jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
}
/* Safe to call SDL functions now; jump table is initialized! */
return 0; /* success! */
}
Sint32 SDL_DYNAPI_entry ( Uint32  apiver,
void table,
Uint32  tablesize 
)

Definition at line 207 of file SDL_dynapi.c.

{
return initialize_jumptable(apiver, table, tablesize);
}
static void SDL_InitDynamicAPI ( void  )
static

Definition at line 297 of file SDL_dynapi.c.

{
/* So the theory is that every function in the jump table defaults to
* calling this function, and then replaces itself with a version that
* doesn't call this function anymore. But it's possible that, in an
* extreme corner case, you can have a second thread hit this function
* while the jump table is being initialized by the first.
* In this case, a spinlock is really painful compared to what spinlocks
* _should_ be used for, but this would only happen once, and should be
* insanely rare, as you would have to spin a thread outside of SDL (as
* SDL_CreateThread() would also call this function before building the
* new thread).
*/
static SDL_bool already_initialized = SDL_FALSE;
/* SDL_AtomicLock calls SDL mutex functions to emulate if
SDL_ATOMIC_DISABLED, which we can't do here, so in such a
configuration, you're on your own. */
#if !SDL_ATOMIC_DISABLED
static SDL_SpinLock lock = 0;
SDL_AtomicLock_REAL(&lock);
#endif
if (!already_initialized) {
already_initialized = SDL_TRUE;
}
#if !SDL_ATOMIC_DISABLED
SDL_AtomicUnlock_REAL(&lock);
#endif
}
static void SDL_InitDynamicAPILocked ( void  )
static

Definition at line 268 of file SDL_dynapi.c.

{
const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
if (libname) {
entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
if (!entry) {
/* !!! FIXME: fail to startup here instead? */
/* !!! FIXME: definitely warn user. */
/* Just fill in the function pointers from this library. */
}
}
if (!entry || (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0)) {
/* !!! FIXME: fail to startup here instead? */
/* !!! FIXME: definitely warn user. */
/* Just fill in the function pointers from this library. */
if (!entry) {
/* !!! FIXME: now we're screwed. Should definitely abort now. */
}
}
}
/* we intentionally never close the newly-loaded lib, of course. */
}

Variable Documentation

SDL_DYNAPI_jump_table jump_table
static

Definition at line 134 of file SDL_dynapi.c.