SDL_dynapi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_config.h"
  19. #include "SDL_dynapi.h"
  20. #if SDL_DYNAMIC_API
  21. #define SDL_DYNAMIC_API_ENVVAR "SDL_DYNAMIC_API"
  22. #if defined(__OS2__)
  23. #define INCL_DOS
  24. #define INCL_DOSERRORS
  25. #include <os2.h>
  26. #include <dos.h>
  27. #endif
  28. #include "SDL.h"
  29. /* These headers have system specific definitions, so aren't included above */
  30. #include "SDL_syswm.h"
  31. #include "SDL_vulkan.h"
  32. /* This is the version of the dynamic API. This doesn't match the SDL version
  33. and should not change until there's been a major revamp in API/ABI.
  34. So 2.0.5 adds functions over 2.0.4? This number doesn't change;
  35. the sizeof(jump_table) changes instead. But 2.1.0 changes how a function
  36. works in an incompatible way or removes a function? This number changes,
  37. since sizeof(jump_table) isn't sufficient anymore. It's likely
  38. we'll forget to bump every time we add a function, so this is the
  39. failsafe switch for major API change decisions. Respect it and use it
  40. sparingly. */
  41. #define SDL_DYNAPI_VERSION 1
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. static void SDL_InitDynamicAPI(void);
  46. /* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
  47. Even self-contained stuff might call SDL_Error and break everything. */
  48. /* behold, the macro salsa! */
  49. /* !!! FIXME: ...disabled...until we write it. :) */
  50. #define DISABLE_JUMP_MAGIC 1
  51. #if DISABLE_JUMP_MAGIC
  52. /* Can't use the macro for varargs nonsense. This is atrocious. */
  53. #define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio) \
  54. _static void SDLCALL SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  55. { \
  56. va_list ap; \
  57. initcall; \
  58. va_start(ap, fmt); \
  59. jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
  60. va_end(ap); \
  61. }
  62. #define SDL_DYNAPI_VARARGS(_static, name, initcall) \
  63. _static int SDLCALL SDL_SetError##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  64. { \
  65. char buf[128], *str = buf; \
  66. int result; \
  67. va_list ap; \
  68. initcall; \
  69. va_start(ap, fmt); \
  70. result = jump_table.SDL_vsnprintf(buf, sizeof(buf), fmt, ap); \
  71. va_end(ap); \
  72. if (result >= 0 && (size_t)result >= sizeof(buf)) { \
  73. size_t len = (size_t)result + 1; \
  74. str = (char *)jump_table.SDL_malloc(len); \
  75. if (str) { \
  76. va_start(ap, fmt); \
  77. result = jump_table.SDL_vsnprintf(str, len, fmt, ap); \
  78. va_end(ap); \
  79. } \
  80. } \
  81. if (result >= 0) { \
  82. result = jump_table.SDL_SetError("%s", str); \
  83. } \
  84. if (str != buf) { \
  85. jump_table.SDL_free(str); \
  86. } \
  87. return result; \
  88. } \
  89. _static int SDLCALL SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) \
  90. { \
  91. int retval; \
  92. va_list ap; \
  93. initcall; \
  94. va_start(ap, fmt); \
  95. retval = jump_table.SDL_vsscanf(buf, fmt, ap); \
  96. va_end(ap); \
  97. return retval; \
  98. } \
  99. _static int SDLCALL SDL_snprintf##name(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  100. { \
  101. int retval; \
  102. va_list ap; \
  103. initcall; \
  104. va_start(ap, fmt); \
  105. retval = jump_table.SDL_vsnprintf(buf, maxlen, fmt, ap); \
  106. va_end(ap); \
  107. return retval; \
  108. } \
  109. _static int SDLCALL SDL_asprintf##name(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  110. { \
  111. int retval; \
  112. va_list ap; \
  113. initcall; \
  114. va_start(ap, fmt); \
  115. retval = jump_table.SDL_vasprintf(strp, fmt, ap); \
  116. va_end(ap); \
  117. return retval; \
  118. } \
  119. _static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  120. { \
  121. va_list ap; \
  122. initcall; \
  123. va_start(ap, fmt); \
  124. jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \
  125. va_end(ap); \
  126. } \
  127. _static void SDLCALL SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
  128. { \
  129. va_list ap; \
  130. initcall; \
  131. va_start(ap, fmt); \
  132. jump_table.SDL_LogMessageV(category, priority, fmt, ap); \
  133. va_end(ap); \
  134. } \
  135. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Verbose, VERBOSE) \
  136. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Debug, DEBUG) \
  137. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Info, INFO) \
  138. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Warn, WARN) \
  139. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Error, ERROR) \
  140. SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Critical, CRITICAL)
  141. #endif
  142. /* Typedefs for function pointers for jump table, and predeclare funcs */
  143. /* The DEFAULT funcs will init jump table and then call real function. */
  144. /* The REAL funcs are the actual functions, name-mangled to not clash. */
  145. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  146. typedef rc (SDLCALL *SDL_DYNAPIFN_##fn) params;\
  147. static rc SDLCALL fn##_DEFAULT params; \
  148. extern rc SDLCALL fn##_REAL params;
  149. #include "SDL_dynapi_procs.h"
  150. #undef SDL_DYNAPI_PROC
  151. /* The jump table! */
  152. typedef struct
  153. {
  154. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) SDL_DYNAPIFN_##fn fn;
  155. #include "SDL_dynapi_procs.h"
  156. #undef SDL_DYNAPI_PROC
  157. } SDL_DYNAPI_jump_table;
  158. /* Predeclare the default functions for initializing the jump table. */
  159. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) static rc SDLCALL fn##_DEFAULT params;
  160. #include "SDL_dynapi_procs.h"
  161. #undef SDL_DYNAPI_PROC
  162. /* The actual jump table. */
  163. static SDL_DYNAPI_jump_table jump_table = {
  164. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) fn##_DEFAULT,
  165. #include "SDL_dynapi_procs.h"
  166. #undef SDL_DYNAPI_PROC
  167. };
  168. /* Default functions init the function table then call right thing. */
  169. #if DISABLE_JUMP_MAGIC
  170. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  171. static rc SDLCALL fn##_DEFAULT params \
  172. { \
  173. SDL_InitDynamicAPI(); \
  174. ret jump_table.fn args; \
  175. }
  176. #define SDL_DYNAPI_PROC_NO_VARARGS
  177. #include "SDL_dynapi_procs.h"
  178. #undef SDL_DYNAPI_PROC
  179. #undef SDL_DYNAPI_PROC_NO_VARARGS
  180. SDL_DYNAPI_VARARGS(static, _DEFAULT, SDL_InitDynamicAPI())
  181. #else
  182. /* !!! FIXME: need the jump magic. */
  183. #error Write me.
  184. #endif
  185. /* Public API functions to jump into the jump table. */
  186. #if DISABLE_JUMP_MAGIC
  187. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) \
  188. rc SDLCALL fn params \
  189. { \
  190. ret jump_table.fn args; \
  191. }
  192. #define SDL_DYNAPI_PROC_NO_VARARGS
  193. #include "SDL_dynapi_procs.h"
  194. #undef SDL_DYNAPI_PROC
  195. #undef SDL_DYNAPI_PROC_NO_VARARGS
  196. SDL_DYNAPI_VARARGS(, , )
  197. #else
  198. /* !!! FIXME: need the jump magic. */
  199. #error Write me.
  200. #endif
  201. #define ENABLE_SDL_CALL_LOGGING 0
  202. #if ENABLE_SDL_CALL_LOGGING
  203. static int SDLCALL SDL_SetError_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  204. {
  205. char buf[512]; /* !!! FIXME: dynamic allocation */
  206. va_list ap;
  207. SDL_Log_REAL("SDL2CALL SDL_SetError");
  208. va_start(ap, fmt);
  209. SDL_vsnprintf_REAL(buf, sizeof(buf), fmt, ap);
  210. va_end(ap);
  211. return SDL_SetError_REAL("%s", buf);
  212. }
  213. static int SDLCALL SDL_sscanf_LOGSDLCALLS(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
  214. {
  215. int retval;
  216. va_list ap;
  217. SDL_Log_REAL("SDL2CALL SDL_sscanf");
  218. va_start(ap, fmt);
  219. retval = SDL_vsscanf_REAL(buf, fmt, ap);
  220. va_end(ap);
  221. return retval;
  222. }
  223. static int SDLCALL SDL_snprintf_LOGSDLCALLS(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  224. {
  225. int retval;
  226. va_list ap;
  227. SDL_Log_REAL("SDL2CALL SDL_snprintf");
  228. va_start(ap, fmt);
  229. retval = SDL_vsnprintf_REAL(buf, maxlen, fmt, ap);
  230. va_end(ap);
  231. return retval;
  232. }
  233. static int SDLCALL SDL_asprintf_LOGSDLCALLS(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  234. {
  235. int retval;
  236. va_list ap;
  237. SDL_Log_REAL("SDL2CALL SDL_asprintf");
  238. va_start(ap, fmt);
  239. retval = SDL_vasprintf_REAL(strp, fmt, ap);
  240. va_end(ap);
  241. return retval;
  242. }
  243. static void SDLCALL SDL_Log_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  244. {
  245. va_list ap;
  246. SDL_Log_REAL("SDL2CALL SDL_Log");
  247. va_start(ap, fmt);
  248. SDL_LogMessageV_REAL(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
  249. va_end(ap);
  250. }
  251. static void SDLCALL SDL_LogMessage_LOGSDLCALLS(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  252. {
  253. va_list ap;
  254. SDL_Log_REAL("SDL2CALL SDL_LogMessage");
  255. va_start(ap, fmt);
  256. SDL_LogMessageV_REAL(category, priority, fmt, ap);
  257. va_end(ap);
  258. }
  259. #define SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(logname, prio) \
  260. static void SDLCALL SDL_Log##logname##_LOGSDLCALLS(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
  261. va_list ap; va_start(ap, fmt); \
  262. SDL_Log_REAL("SDL2CALL SDL_Log%s", #logname); \
  263. SDL_LogMessageV_REAL(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
  264. va_end(ap); \
  265. }
  266. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Verbose, VERBOSE)
  267. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Debug, DEBUG)
  268. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Info, INFO)
  269. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Warn, WARN)
  270. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Error, ERROR)
  271. SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Critical, CRITICAL)
  272. #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
  273. rc SDLCALL fn##_LOGSDLCALLS params { SDL_Log_REAL("SDL2CALL %s", #fn); ret fn##_REAL args; }
  274. #define SDL_DYNAPI_PROC_NO_VARARGS
  275. #include "SDL_dynapi_procs.h"
  276. #undef SDL_DYNAPI_PROC
  277. #undef SDL_DYNAPI_PROC_NO_VARARGS
  278. #endif
  279. /* we make this a static function so we can call the correct one without the
  280. system's dynamic linker resolving to the wrong version of this. */
  281. static Sint32 initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
  282. {
  283. SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *)table;
  284. if (apiver != SDL_DYNAPI_VERSION) {
  285. /* !!! FIXME: can maybe handle older versions? */
  286. return -1; /* not compatible. */
  287. } else if (tablesize > sizeof(jump_table)) {
  288. return -1; /* newer version of SDL with functions we can't provide. */
  289. }
  290. /* Init our jump table first. */
  291. #if ENABLE_SDL_CALL_LOGGING
  292. {
  293. const char *env = SDL_getenv_REAL("SDL_DYNAPI_LOG_CALLS");
  294. const SDL_bool log_calls = (env && SDL_atoi_REAL(env));
  295. if (log_calls) {
  296. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_LOGSDLCALLS;
  297. #include "SDL_dynapi_procs.h"
  298. #undef SDL_DYNAPI_PROC
  299. } else {
  300. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_REAL;
  301. #include "SDL_dynapi_procs.h"
  302. #undef SDL_DYNAPI_PROC
  303. }
  304. }
  305. #else
  306. #define SDL_DYNAPI_PROC(rc, fn, params, args, ret) jump_table.fn = fn##_REAL;
  307. #include "SDL_dynapi_procs.h"
  308. #undef SDL_DYNAPI_PROC
  309. #endif
  310. /* Then the external table... */
  311. if (output_jump_table != &jump_table) {
  312. jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
  313. }
  314. /* Safe to call SDL functions now; jump table is initialized! */
  315. return 0; /* success! */
  316. }
  317. /* Here's the exported entry point that fills in the jump table. */
  318. /* Use specific types when an "int" might suffice to keep this sane. */
  319. typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
  320. extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
  321. Sint32 SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
  322. {
  323. return initialize_jumptable(apiver, table, tablesize);
  324. }
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. /* Obviously we can't use SDL_LoadObject() to load SDL. :) */
  329. /* Also obviously, we never close the loaded library. */
  330. #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
  331. #ifndef WIN32_LEAN_AND_MEAN
  332. #define WIN32_LEAN_AND_MEAN 1
  333. #endif
  334. #include <windows.h>
  335. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  336. {
  337. HMODULE lib = LoadLibraryA(fname);
  338. void *retval = NULL;
  339. if (lib) {
  340. retval = (void *) GetProcAddress(lib, sym);
  341. if (!retval) {
  342. FreeLibrary(lib);
  343. }
  344. }
  345. return retval;
  346. }
  347. #elif defined(unix) || defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__) || defined(__QNX__)
  348. #include <dlfcn.h>
  349. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  350. {
  351. void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
  352. void *retval = NULL;
  353. if (lib) {
  354. retval = dlsym(lib, sym);
  355. if (!retval) {
  356. dlclose(lib);
  357. }
  358. }
  359. return retval;
  360. }
  361. #elif defined(__OS2__)
  362. static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
  363. {
  364. HMODULE hmodule;
  365. PFN retval = NULL;
  366. char error[256];
  367. if (DosLoadModule(error, sizeof(error), fname, &hmodule) == NO_ERROR) {
  368. if (DosQueryProcAddr(hmodule, 0, sym, &retval) != NO_ERROR) {
  369. DosFreeModule(hmodule);
  370. }
  371. }
  372. return (void *)retval;
  373. }
  374. #else
  375. #error Please define your platform.
  376. #endif
  377. static void dynapi_warn(const char *msg)
  378. {
  379. const char *caption = "SDL Dynamic API Failure!";
  380. /* SDL_ShowSimpleMessageBox() is a too heavy for here. */
  381. #if (defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  382. MessageBoxA(NULL, msg, caption, MB_OK | MB_ICONERROR);
  383. #elif defined(HAVE_STDIO_H)
  384. fprintf(stderr, "\n\n%s\n%s\n\n", caption, msg);
  385. fflush(stderr);
  386. #endif
  387. }
  388. /* This is not declared in any header, although it is shared between some
  389. parts of SDL, because we don't want anything calling it without an
  390. extremely good reason. */
  391. #ifdef __cplusplus
  392. extern "C" {
  393. #endif
  394. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  395. #if defined(__WATCOMC__)
  396. #pragma aux SDL_ExitProcess aborts;
  397. #endif
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. static void SDL_InitDynamicAPILocked(void)
  402. {
  403. /* this can't use SDL_getenv_REAL, because it might allocate memory before the app can set their allocator */
  404. #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
  405. /* We've always used LoadLibraryA for this, so this has never worked with Unicode paths on Windows. Sorry. */
  406. char envbuf[512]; /* overflows will just report as environment variable being unset, but LoadLibraryA has a MAX_PATH of 260 anyhow, apparently. */
  407. const DWORD rc = GetEnvironmentVariableA(SDL_DYNAMIC_API_ENVVAR, envbuf, (DWORD) sizeof (envbuf));
  408. char *libname = ((rc != 0) && (rc < sizeof (envbuf))) ? envbuf : NULL;
  409. #else
  410. char *libname = getenv(SDL_DYNAMIC_API_ENVVAR);
  411. #endif
  412. SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
  413. SDL_bool use_internal = SDL_TRUE;
  414. if (libname) {
  415. while (*libname && !entry) {
  416. char *ptr = libname;
  417. while (SDL_TRUE) {
  418. const char ch = *ptr;
  419. if ((ch == ',') || (ch == '\0')) {
  420. *ptr = '\0';
  421. entry = (SDL_DYNAPI_ENTRYFN)get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
  422. *ptr = ch;
  423. libname = (ch == '\0') ? ptr : (ptr + 1);
  424. break;
  425. } else {
  426. ptr++;
  427. }
  428. }
  429. }
  430. if (!entry) {
  431. dynapi_warn("Couldn't load an overriding SDL library. Please fix or remove the " SDL_DYNAMIC_API_ENVVAR " environment variable. Using the default SDL.");
  432. /* Just fill in the function pointers from this library, later. */
  433. }
  434. }
  435. if (entry) {
  436. if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof(jump_table)) < 0) {
  437. dynapi_warn("Couldn't override SDL library. Using a newer SDL build might help. Please fix or remove the " SDL_DYNAMIC_API_ENVVAR " environment variable. Using the default SDL.");
  438. /* Just fill in the function pointers from this library, later. */
  439. } else {
  440. use_internal = SDL_FALSE; /* We overrode SDL! Don't use the internal version! */
  441. }
  442. }
  443. /* Just fill in the function pointers from this library. */
  444. if (use_internal) {
  445. if (initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof(jump_table)) < 0) {
  446. /* Now we're screwed. Should definitely abort now. */
  447. dynapi_warn("Failed to initialize internal SDL dynapi. As this would otherwise crash, we have to abort now.");
  448. SDL_ExitProcess(86);
  449. }
  450. }
  451. /* we intentionally never close the newly-loaded lib, of course. */
  452. }
  453. static void SDL_InitDynamicAPI(void)
  454. {
  455. /* So the theory is that every function in the jump table defaults to
  456. * calling this function, and then replaces itself with a version that
  457. * doesn't call this function anymore. But it's possible that, in an
  458. * extreme corner case, you can have a second thread hit this function
  459. * while the jump table is being initialized by the first.
  460. * In this case, a spinlock is really painful compared to what spinlocks
  461. * _should_ be used for, but this would only happen once, and should be
  462. * insanely rare, as you would have to spin a thread outside of SDL (as
  463. * SDL_CreateThread() would also call this function before building the
  464. * new thread).
  465. */
  466. static SDL_bool already_initialized = SDL_FALSE;
  467. /* SDL_AtomicLock calls SDL mutex functions to emulate if
  468. SDL_ATOMIC_DISABLED, which we can't do here, so in such a
  469. configuration, you're on your own. */
  470. #ifndef SDL_ATOMIC_DISABLED
  471. static SDL_SpinLock lock = 0;
  472. SDL_AtomicLock_REAL(&lock);
  473. #endif
  474. if (!already_initialized) {
  475. SDL_InitDynamicAPILocked();
  476. already_initialized = SDL_TRUE;
  477. }
  478. #ifndef SDL_ATOMIC_DISABLED
  479. SDL_AtomicUnlock_REAL(&lock);
  480. #endif
  481. }
  482. #endif /* SDL_DYNAMIC_API */
  483. /* vi: set ts=4 sw=4 expandtab: */