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 61 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;
#define SDLCALL
Definition: SDL_internal.h:49
const GLfloat * params

Definition at line 159 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 159 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 159 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC [4/7]

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

Definition at line 159 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:134

Definition at line 159 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 159 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 159 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC_NO_VARARGS [1/2]

#define SDL_DYNAPI_PROC_NO_VARARGS   1

Definition at line 161 of file SDL_dynapi.c.

◆ SDL_DYNAPI_PROC_NO_VARARGS [2/2]

#define SDL_DYNAPI_PROC_NO_VARARGS   1

Definition at line 161 of file SDL_dynapi.c.

◆ SDL_DYNAPI_VARARGS

#define SDL_DYNAPI_VARARGS (   _static,
  name,
  initcall 
)

Definition at line 72 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); \
}
#define SDL_Log
GLuint const GLchar * name
#define SDL_PRINTF_FORMAT_STRING
Definition: SDL_stdinc.h:300

Definition at line 65 of file SDL_dynapi.c.

◆ SDL_DYNAPI_VERSION

#define SDL_DYNAPI_VERSION   1

Definition at line 49 of file SDL_dynapi.c.

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN   1

Definition at line 217 of file SDL_dynapi.c.

Typedef Documentation

◆ SDL_DYNAPI_ENTRYFN

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

Definition at line 203 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 220 of file SDL_dynapi.c.

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

References NULL, and retval.

Referenced by SDL_InitDynamicAPILocked().

◆ initialize_jumptable()

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

Definition at line 174 of file SDL_dynapi.c.

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

References jump_table, and SDL_DYNAPI_VERSION.

Referenced by SDL_DYNAPI_entry(), and SDL_InitDynamicAPILocked().

◆ SDL_DYNAPI_entry()

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

Definition at line 207 of file SDL_dynapi.c.

208{
209 return initialize_jumptable(apiver, table, tablesize);
210}
static Sint32 initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
Definition: SDL_dynapi.c:174

References initialize_jumptable().

◆ SDL_InitDynamicAPI()

static void SDL_InitDynamicAPI ( void  )
static

Definition at line 297 of file SDL_dynapi.c.

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

References lock, SDL_FALSE, SDL_InitDynamicAPILocked(), and SDL_TRUE.

◆ SDL_InitDynamicAPILocked()

static void SDL_InitDynamicAPILocked ( void  )
static

Definition at line 268 of file SDL_dynapi.c.

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

References get_sdlapi_entry(), initialize_jumptable(), jump_table, NULL, and SDL_DYNAPI_VERSION.

Referenced by SDL_InitDynamicAPI().

Variable Documentation

◆ jump_table

SDL_DYNAPI_jump_table jump_table
static
Initial value:
= {
#define SDL_DYNAPI_PROC(rc,fn,params,args,ret)
}

Definition at line 134 of file SDL_dynapi.c.

Referenced by initialize_jumptable(), and SDL_InitDynamicAPILocked().