SDL_stdinc.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. /**
  19. * \file SDL_stdinc.h
  20. *
  21. * This is a general header that includes C language support.
  22. */
  23. #ifndef SDL_stdinc_h_
  24. #define SDL_stdinc_h_
  25. #include <SDL3/SDL_platform_defines.h>
  26. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  27. #include <inttypes.h>
  28. #endif
  29. #include <stdarg.h>
  30. #include <stdint.h>
  31. #include <wchar.h>
  32. #ifndef SDL_DISABLE_ALLOCA
  33. # ifndef alloca
  34. # ifdef HAVE_ALLOCA_H
  35. # include <alloca.h>
  36. # elif defined(__NETBSD__)
  37. # if defined(__STRICT_ANSI__)
  38. # define SDL_DISABLE_ALLOCA
  39. # else
  40. # include <stdlib.h>
  41. # endif
  42. # elif defined(__GNUC__)
  43. # define alloca __builtin_alloca
  44. # elif defined(_MSC_VER)
  45. # include <malloc.h>
  46. # define alloca _alloca
  47. # elif defined(__WATCOMC__)
  48. # include <malloc.h>
  49. # elif defined(__BORLANDC__)
  50. # include <malloc.h>
  51. # elif defined(__DMC__)
  52. # include <stdlib.h>
  53. # elif defined(__AIX__)
  54. # pragma alloca
  55. # elif defined(__MRC__)
  56. void *alloca(unsigned);
  57. # else
  58. char *alloca();
  59. # endif
  60. # endif
  61. #endif
  62. #ifdef SIZE_MAX
  63. # define SDL_SIZE_MAX SIZE_MAX
  64. #else
  65. # define SDL_SIZE_MAX ((size_t) -1)
  66. #endif
  67. /**
  68. * Check if the compiler supports a given builtin.
  69. * Supported by virtually all clang versions and recent gcc. Use this
  70. * instead of checking the clang version if possible.
  71. */
  72. #ifdef __has_builtin
  73. #define SDL_HAS_BUILTIN(x) __has_builtin(x)
  74. #else
  75. #define SDL_HAS_BUILTIN(x) 0
  76. #endif
  77. /**
  78. * The number of elements in an array.
  79. */
  80. #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
  81. #define SDL_TABLESIZE(table) SDL_arraysize(table)
  82. /**
  83. * Macro useful for building other macros with strings in them
  84. *
  85. * e.g. #define LOG_ERROR(X) OutputDebugString(SDL_STRINGIFY_ARG(__FUNCTION__) ": " X "\n")
  86. */
  87. #define SDL_STRINGIFY_ARG(arg) #arg
  88. /**
  89. * \name Cast operators
  90. *
  91. * Use proper C++ casts when compiled as C++ to be compatible with the option
  92. * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
  93. */
  94. /* @{ */
  95. #ifdef __cplusplus
  96. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
  97. #define SDL_static_cast(type, expression) static_cast<type>(expression)
  98. #define SDL_const_cast(type, expression) const_cast<type>(expression)
  99. #else
  100. #define SDL_reinterpret_cast(type, expression) ((type)(expression))
  101. #define SDL_static_cast(type, expression) ((type)(expression))
  102. #define SDL_const_cast(type, expression) ((type)(expression))
  103. #endif
  104. /* @} *//* Cast operators */
  105. /* Define a four character code as a Uint32 */
  106. #define SDL_FOURCC(A, B, C, D) \
  107. ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
  108. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
  109. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
  110. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
  111. /**
  112. * \name Basic data types
  113. */
  114. /* @{ */
  115. /**
  116. * A boolean type.
  117. */
  118. #define SDL_FALSE 0
  119. #define SDL_TRUE 1
  120. typedef unsigned int SDL_bool;
  121. /**
  122. * A signed 8-bit integer type.
  123. */
  124. #define SDL_MAX_SINT8 ((Sint8)0x7F) /* 127 */
  125. #define SDL_MIN_SINT8 ((Sint8)(~0x7F)) /* -128 */
  126. typedef int8_t Sint8;
  127. /**
  128. * An unsigned 8-bit integer type.
  129. */
  130. #define SDL_MAX_UINT8 ((Uint8)0xFF) /* 255 */
  131. #define SDL_MIN_UINT8 ((Uint8)0x00) /* 0 */
  132. typedef uint8_t Uint8;
  133. /**
  134. * A signed 16-bit integer type.
  135. */
  136. #define SDL_MAX_SINT16 ((Sint16)0x7FFF) /* 32767 */
  137. #define SDL_MIN_SINT16 ((Sint16)(~0x7FFF)) /* -32768 */
  138. typedef int16_t Sint16;
  139. /**
  140. * An unsigned 16-bit integer type.
  141. */
  142. #define SDL_MAX_UINT16 ((Uint16)0xFFFF) /* 65535 */
  143. #define SDL_MIN_UINT16 ((Uint16)0x0000) /* 0 */
  144. typedef uint16_t Uint16;
  145. /**
  146. * A signed 32-bit integer type.
  147. */
  148. #define SDL_MAX_SINT32 ((Sint32)0x7FFFFFFF) /* 2147483647 */
  149. #define SDL_MIN_SINT32 ((Sint32)(~0x7FFFFFFF)) /* -2147483648 */
  150. typedef int32_t Sint32;
  151. /**
  152. * An unsigned 32-bit integer type.
  153. */
  154. #define SDL_MAX_UINT32 ((Uint32)0xFFFFFFFFu) /* 4294967295 */
  155. #define SDL_MIN_UINT32 ((Uint32)0x00000000) /* 0 */
  156. typedef uint32_t Uint32;
  157. /**
  158. * A signed 64-bit integer type.
  159. */
  160. #define SDL_MAX_SINT64 ((Sint64)0x7FFFFFFFFFFFFFFFll) /* 9223372036854775807 */
  161. #define SDL_MIN_SINT64 ((Sint64)(~0x7FFFFFFFFFFFFFFFll)) /* -9223372036854775808 */
  162. typedef int64_t Sint64;
  163. /**
  164. * An unsigned 64-bit integer type.
  165. */
  166. #define SDL_MAX_UINT64 ((Uint64)0xFFFFFFFFFFFFFFFFull) /* 18446744073709551615 */
  167. #define SDL_MIN_UINT64 ((Uint64)(0x0000000000000000ull)) /* 0 */
  168. typedef uint64_t Uint64;
  169. /* @} *//* Basic data types */
  170. /**
  171. * \name Floating-point constants
  172. */
  173. /* @{ */
  174. #ifdef FLT_EPSILON
  175. #define SDL_FLT_EPSILON FLT_EPSILON
  176. #else
  177. #define SDL_FLT_EPSILON 1.1920928955078125e-07F /* 0x0.000002p0 */
  178. #endif
  179. /* @} *//* Floating-point constants */
  180. /* Make sure we have macros for printing width-based integers.
  181. * <stdint.h> should define these but this is not true all platforms.
  182. * (for example win32) */
  183. #ifndef SDL_PRIs64
  184. #ifdef PRIs64
  185. #define SDL_PRIs64 PRIs64
  186. #elif defined(__WIN32__) || defined(__GDK__)
  187. #define SDL_PRIs64 "I64d"
  188. #elif defined(__LP64__) && !defined(__APPLE__)
  189. #define SDL_PRIs64 "ld"
  190. #else
  191. #define SDL_PRIs64 "lld"
  192. #endif
  193. #endif
  194. #ifndef SDL_PRIu64
  195. #ifdef PRIu64
  196. #define SDL_PRIu64 PRIu64
  197. #elif defined(__WIN32__) || defined(__GDK__)
  198. #define SDL_PRIu64 "I64u"
  199. #elif defined(__LP64__) && !defined(__APPLE__)
  200. #define SDL_PRIu64 "lu"
  201. #else
  202. #define SDL_PRIu64 "llu"
  203. #endif
  204. #endif
  205. #ifndef SDL_PRIx64
  206. #ifdef PRIx64
  207. #define SDL_PRIx64 PRIx64
  208. #elif defined(__WIN32__) || defined(__GDK__)
  209. #define SDL_PRIx64 "I64x"
  210. #elif defined(__LP64__) && !defined(__APPLE__)
  211. #define SDL_PRIx64 "lx"
  212. #else
  213. #define SDL_PRIx64 "llx"
  214. #endif
  215. #endif
  216. #ifndef SDL_PRIX64
  217. #ifdef PRIX64
  218. #define SDL_PRIX64 PRIX64
  219. #elif defined(__WIN32__) || defined(__GDK__)
  220. #define SDL_PRIX64 "I64X"
  221. #elif defined(__LP64__) && !defined(__APPLE__)
  222. #define SDL_PRIX64 "lX"
  223. #else
  224. #define SDL_PRIX64 "llX"
  225. #endif
  226. #endif
  227. #ifndef SDL_PRIs32
  228. #ifdef PRId32
  229. #define SDL_PRIs32 PRId32
  230. #else
  231. #define SDL_PRIs32 "d"
  232. #endif
  233. #endif
  234. #ifndef SDL_PRIu32
  235. #ifdef PRIu32
  236. #define SDL_PRIu32 PRIu32
  237. #else
  238. #define SDL_PRIu32 "u"
  239. #endif
  240. #endif
  241. #ifndef SDL_PRIx32
  242. #ifdef PRIx32
  243. #define SDL_PRIx32 PRIx32
  244. #else
  245. #define SDL_PRIx32 "x"
  246. #endif
  247. #endif
  248. #ifndef SDL_PRIX32
  249. #ifdef PRIX32
  250. #define SDL_PRIX32 PRIX32
  251. #else
  252. #define SDL_PRIX32 "X"
  253. #endif
  254. #endif
  255. /* Annotations to help code analysis tools */
  256. #ifdef SDL_DISABLE_ANALYZE_MACROS
  257. #define SDL_IN_BYTECAP(x)
  258. #define SDL_INOUT_Z_CAP(x)
  259. #define SDL_OUT_Z_CAP(x)
  260. #define SDL_OUT_CAP(x)
  261. #define SDL_OUT_BYTECAP(x)
  262. #define SDL_OUT_Z_BYTECAP(x)
  263. #define SDL_PRINTF_FORMAT_STRING
  264. #define SDL_SCANF_FORMAT_STRING
  265. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  266. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  267. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  268. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  269. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  270. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber )
  271. #else
  272. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  273. #include <sal.h>
  274. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  275. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  276. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  277. #define SDL_OUT_CAP(x) _Out_cap_(x)
  278. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  279. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  280. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  281. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  282. #else
  283. #define SDL_IN_BYTECAP(x)
  284. #define SDL_INOUT_Z_CAP(x)
  285. #define SDL_OUT_Z_CAP(x)
  286. #define SDL_OUT_CAP(x)
  287. #define SDL_OUT_BYTECAP(x)
  288. #define SDL_OUT_Z_BYTECAP(x)
  289. #define SDL_PRINTF_FORMAT_STRING
  290. #define SDL_SCANF_FORMAT_STRING
  291. #endif
  292. #ifdef __GNUC__
  293. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  294. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  295. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  296. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  297. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  298. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wscanf__, fmtargnumber, fmtargnumber+1 ))) */
  299. #else
  300. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  301. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  302. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  303. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  304. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  305. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber )
  306. #endif
  307. #endif /* SDL_DISABLE_ANALYZE_MACROS */
  308. #ifndef SDL_COMPILE_TIME_ASSERT
  309. #ifdef __cplusplus
  310. #if (__cplusplus >= 201103L)
  311. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  312. #endif
  313. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  314. #define SDL_COMPILE_TIME_ASSERT(name, x) _Static_assert(x, #x)
  315. #endif
  316. #endif /* !SDL_COMPILE_TIME_ASSERT */
  317. #ifndef SDL_COMPILE_TIME_ASSERT
  318. /* universal, but may trigger -Wunused-local-typedefs */
  319. #define SDL_COMPILE_TIME_ASSERT(name, x) \
  320. typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
  321. #endif
  322. /** \cond */
  323. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  324. SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
  325. SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
  326. SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
  327. SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
  328. SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
  329. SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
  330. SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
  331. SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
  332. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  333. /** \endcond */
  334. /* Check to make sure enums are the size of ints, for structure packing.
  335. For both Watcom C/C++ and Borland C/C++ the compiler option that makes
  336. enums having the size of an int must be enabled.
  337. This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
  338. */
  339. /** \cond */
  340. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  341. #if !defined(__ANDROID__) && !defined(__VITA__) && !defined(__3DS__)
  342. /* TODO: include/SDL_stdinc.h:174: error: size of array 'SDL_dummy_enum' is negative */
  343. typedef enum
  344. {
  345. DUMMY_ENUM_VALUE
  346. } SDL_DUMMY_ENUM;
  347. SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
  348. #endif
  349. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  350. /** \endcond */
  351. #include <SDL3/SDL_begin_code.h>
  352. /* Set up for C function definitions, even when using C++ */
  353. #ifdef __cplusplus
  354. extern "C" {
  355. #endif
  356. #ifndef SDL_DISABLE_ALLOCA
  357. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  358. #define SDL_stack_free(data)
  359. #else
  360. #define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*(count))
  361. #define SDL_stack_free(data) SDL_free(data)
  362. #endif
  363. extern DECLSPEC SDL_MALLOC void *SDLCALL SDL_malloc(size_t size);
  364. extern DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) void *SDLCALL SDL_calloc(size_t nmemb, size_t size);
  365. extern DECLSPEC SDL_ALLOC_SIZE(2) void *SDLCALL SDL_realloc(void *mem, size_t size);
  366. extern DECLSPEC void SDLCALL SDL_free(void *mem);
  367. typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
  368. typedef void *(SDLCALL *SDL_calloc_func)(size_t nmemb, size_t size);
  369. typedef void *(SDLCALL *SDL_realloc_func)(void *mem, size_t size);
  370. typedef void (SDLCALL *SDL_free_func)(void *mem);
  371. /**
  372. * Get the original set of SDL memory functions
  373. *
  374. * \param malloc_func filled with malloc function
  375. * \param calloc_func filled with calloc function
  376. * \param realloc_func filled with realloc function
  377. * \param free_func filled with free function
  378. *
  379. * \since This function is available since SDL 3.0.0.
  380. */
  381. extern DECLSPEC void SDLCALL SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
  382. SDL_calloc_func *calloc_func,
  383. SDL_realloc_func *realloc_func,
  384. SDL_free_func *free_func);
  385. /**
  386. * Get the current set of SDL memory functions
  387. *
  388. * \param malloc_func filled with malloc function
  389. * \param calloc_func filled with calloc function
  390. * \param realloc_func filled with realloc function
  391. * \param free_func filled with free function
  392. *
  393. * \since This function is available since SDL 3.0.0.
  394. */
  395. extern DECLSPEC void SDLCALL SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
  396. SDL_calloc_func *calloc_func,
  397. SDL_realloc_func *realloc_func,
  398. SDL_free_func *free_func);
  399. /**
  400. * Replace SDL's memory allocation functions with a custom set
  401. *
  402. * \param malloc_func custom malloc function
  403. * \param calloc_func custom calloc function
  404. * \param realloc_func custom realloc function
  405. * \param free_func custom free function
  406. * \returns 0 on success or a negative error code on failure; call
  407. * SDL_GetError() for more information.
  408. *
  409. * \since This function is available since SDL 3.0.0.
  410. */
  411. extern DECLSPEC int SDLCALL SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
  412. SDL_calloc_func calloc_func,
  413. SDL_realloc_func realloc_func,
  414. SDL_free_func free_func);
  415. /**
  416. * Allocate memory aligned to a specific value
  417. *
  418. * If `alignment` is less than the size of `void *`, then it will be increased
  419. * to match that.
  420. *
  421. * The returned memory address will be a multiple of the alignment value, and
  422. * the amount of memory allocated will be a multiple of the alignment value.
  423. *
  424. * The memory returned by this function must be freed with SDL_aligned_free()
  425. *
  426. * \param alignment the alignment requested
  427. * \param size the size to allocate
  428. * \returns a pointer to the aligned memory
  429. *
  430. * \since This function is available since SDL 3.0.0.
  431. *
  432. * \sa SDL_aligned_free
  433. */
  434. extern DECLSPEC SDL_MALLOC void *SDLCALL SDL_aligned_alloc(size_t alignment, size_t size);
  435. /**
  436. * Free memory allocated by SDL_aligned_alloc()
  437. *
  438. * \since This function is available since SDL 3.0.0.
  439. *
  440. * \sa SDL_aligned_alloc
  441. */
  442. extern DECLSPEC void SDLCALL SDL_aligned_free(void *mem);
  443. /**
  444. * Get the number of outstanding (unfreed) allocations
  445. *
  446. * \returns the number of allocations
  447. *
  448. * \since This function is available since SDL 3.0.0.
  449. */
  450. extern DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
  451. extern DECLSPEC char *SDLCALL SDL_getenv(const char *name);
  452. extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite);
  453. extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (const void *, const void *));
  454. extern DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (const void *, const void *));
  455. extern DECLSPEC int SDLCALL SDL_abs(int x);
  456. /* NOTE: these double-evaluate their arguments, so you should never have side effects in the parameters */
  457. #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
  458. #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
  459. #define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
  460. extern DECLSPEC int SDLCALL SDL_isalpha(int x);
  461. extern DECLSPEC int SDLCALL SDL_isalnum(int x);
  462. extern DECLSPEC int SDLCALL SDL_isblank(int x);
  463. extern DECLSPEC int SDLCALL SDL_iscntrl(int x);
  464. extern DECLSPEC int SDLCALL SDL_isdigit(int x);
  465. extern DECLSPEC int SDLCALL SDL_isxdigit(int x);
  466. extern DECLSPEC int SDLCALL SDL_ispunct(int x);
  467. extern DECLSPEC int SDLCALL SDL_isspace(int x);
  468. extern DECLSPEC int SDLCALL SDL_isupper(int x);
  469. extern DECLSPEC int SDLCALL SDL_islower(int x);
  470. extern DECLSPEC int SDLCALL SDL_isprint(int x);
  471. extern DECLSPEC int SDLCALL SDL_isgraph(int x);
  472. extern DECLSPEC int SDLCALL SDL_toupper(int x);
  473. extern DECLSPEC int SDLCALL SDL_tolower(int x);
  474. extern DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
  475. extern DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
  476. extern DECLSPEC void *SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
  477. extern DECLSPEC void *SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
  478. #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
  479. #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
  480. #define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
  481. #define SDL_copyp(dst, src) \
  482. { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
  483. SDL_memcpy((dst), (src), sizeof (*(src)))
  484. extern DECLSPEC void *SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  485. extern DECLSPEC void *SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  486. extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
  487. extern DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
  488. extern DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
  489. extern DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  490. extern DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  491. extern DECLSPEC wchar_t *SDLCALL SDL_wcsdup(const wchar_t *wstr);
  492. extern DECLSPEC wchar_t *SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
  493. extern DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
  494. extern DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  495. extern DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
  496. extern DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t len);
  497. extern DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
  498. extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
  499. extern DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
  500. extern DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  501. extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
  502. extern DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  503. extern DECLSPEC SDL_MALLOC char *SDLCALL SDL_strdup(const char *str);
  504. extern DECLSPEC SDL_MALLOC char *SDLCALL SDL_strndup(const char *str, size_t maxlen);
  505. extern DECLSPEC char *SDLCALL SDL_strrev(char *str);
  506. extern DECLSPEC char *SDLCALL SDL_strupr(char *str);
  507. extern DECLSPEC char *SDLCALL SDL_strlwr(char *str);
  508. extern DECLSPEC char *SDLCALL SDL_strchr(const char *str, int c);
  509. extern DECLSPEC char *SDLCALL SDL_strrchr(const char *str, int c);
  510. extern DECLSPEC char *SDLCALL SDL_strstr(const char *haystack, const char *needle);
  511. extern DECLSPEC char *SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
  512. extern DECLSPEC char *SDLCALL SDL_strtok_r(char *s1, const char *s2, char **saveptr);
  513. extern DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
  514. extern DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
  515. extern DECLSPEC char *SDLCALL SDL_itoa(int value, char *str, int radix);
  516. extern DECLSPEC char *SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
  517. extern DECLSPEC char *SDLCALL SDL_ltoa(long value, char *str, int radix);
  518. extern DECLSPEC char *SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
  519. extern DECLSPEC char *SDLCALL SDL_lltoa(Sint64 value, char *str, int radix);
  520. extern DECLSPEC char *SDLCALL SDL_ulltoa(Uint64 value, char *str, int radix);
  521. extern DECLSPEC int SDLCALL SDL_atoi(const char *str);
  522. extern DECLSPEC double SDLCALL SDL_atof(const char *str);
  523. extern DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
  524. extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
  525. extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int base);
  526. extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *str, char **endp, int base);
  527. extern DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
  528. extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
  529. extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
  530. extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
  531. extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t len);
  532. extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
  533. extern DECLSPEC int SDLCALL SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap) SDL_SCANF_VARARG_FUNCV(2);
  534. extern DECLSPEC int SDLCALL SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ... ) SDL_PRINTF_VARARG_FUNC(3);
  535. extern DECLSPEC int SDLCALL SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ... ) SDL_WPRINTF_VARARG_FUNC(3);
  536. extern DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(3);
  537. extern DECLSPEC int SDLCALL SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap);
  538. extern DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  539. extern DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  540. #ifndef SDL_PI_D
  541. #define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
  542. #endif
  543. #ifndef SDL_PI_F
  544. #define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
  545. #endif
  546. /**
  547. * Use this function to compute arc cosine of `x`.
  548. *
  549. * The definition of `y = acos(x)` is `x = cos(y)`.
  550. *
  551. * Domain: `-1 <= x <= 1`
  552. *
  553. * Range: `0 <= y <= Pi`
  554. *
  555. * \param x floating point value, in radians.
  556. * \returns arc cosine of `x`.
  557. *
  558. * \since This function is available since SDL 3.0.0.
  559. */
  560. extern DECLSPEC double SDLCALL SDL_acos(double x);
  561. extern DECLSPEC float SDLCALL SDL_acosf(float x);
  562. extern DECLSPEC double SDLCALL SDL_asin(double x);
  563. extern DECLSPEC float SDLCALL SDL_asinf(float x);
  564. extern DECLSPEC double SDLCALL SDL_atan(double x);
  565. extern DECLSPEC float SDLCALL SDL_atanf(float x);
  566. extern DECLSPEC double SDLCALL SDL_atan2(double y, double x);
  567. extern DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
  568. extern DECLSPEC double SDLCALL SDL_ceil(double x);
  569. extern DECLSPEC float SDLCALL SDL_ceilf(float x);
  570. extern DECLSPEC double SDLCALL SDL_copysign(double x, double y);
  571. extern DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
  572. extern DECLSPEC double SDLCALL SDL_cos(double x);
  573. extern DECLSPEC float SDLCALL SDL_cosf(float x);
  574. extern DECLSPEC double SDLCALL SDL_exp(double x);
  575. extern DECLSPEC float SDLCALL SDL_expf(float x);
  576. extern DECLSPEC double SDLCALL SDL_fabs(double x);
  577. extern DECLSPEC float SDLCALL SDL_fabsf(float x);
  578. extern DECLSPEC double SDLCALL SDL_floor(double x);
  579. extern DECLSPEC float SDLCALL SDL_floorf(float x);
  580. extern DECLSPEC double SDLCALL SDL_trunc(double x);
  581. extern DECLSPEC float SDLCALL SDL_truncf(float x);
  582. extern DECLSPEC double SDLCALL SDL_fmod(double x, double y);
  583. extern DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
  584. extern DECLSPEC double SDLCALL SDL_log(double x);
  585. extern DECLSPEC float SDLCALL SDL_logf(float x);
  586. extern DECLSPEC double SDLCALL SDL_log10(double x);
  587. extern DECLSPEC float SDLCALL SDL_log10f(float x);
  588. extern DECLSPEC double SDLCALL SDL_modf(double x, double *y);
  589. extern DECLSPEC float SDLCALL SDL_modff(float x, float *y);
  590. extern DECLSPEC double SDLCALL SDL_pow(double x, double y);
  591. extern DECLSPEC float SDLCALL SDL_powf(float x, float y);
  592. extern DECLSPEC double SDLCALL SDL_round(double x);
  593. extern DECLSPEC float SDLCALL SDL_roundf(float x);
  594. extern DECLSPEC long SDLCALL SDL_lround(double x);
  595. extern DECLSPEC long SDLCALL SDL_lroundf(float x);
  596. extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
  597. extern DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
  598. extern DECLSPEC double SDLCALL SDL_sin(double x);
  599. extern DECLSPEC float SDLCALL SDL_sinf(float x);
  600. extern DECLSPEC double SDLCALL SDL_sqrt(double x);
  601. extern DECLSPEC float SDLCALL SDL_sqrtf(float x);
  602. extern DECLSPEC double SDLCALL SDL_tan(double x);
  603. extern DECLSPEC float SDLCALL SDL_tanf(float x);
  604. /* The SDL implementation of iconv() returns these error codes */
  605. #define SDL_ICONV_ERROR (size_t)-1
  606. #define SDL_ICONV_E2BIG (size_t)-2
  607. #define SDL_ICONV_EILSEQ (size_t)-3
  608. #define SDL_ICONV_EINVAL (size_t)-4
  609. /* SDL_iconv_* are now always real symbols/types, not macros or inlined. */
  610. typedef struct SDL_iconv_data_t *SDL_iconv_t;
  611. extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
  612. const char *fromcode);
  613. extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
  614. extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
  615. size_t * inbytesleft, char **outbuf,
  616. size_t * outbytesleft);
  617. /**
  618. * This function converts a buffer or string between encodings in one pass,
  619. * returning a string that must be freed with SDL_free() or NULL on error.
  620. *
  621. * \since This function is available since SDL 3.0.0.
  622. */
  623. extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
  624. const char *fromcode,
  625. const char *inbuf,
  626. size_t inbytesleft);
  627. #define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
  628. #define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
  629. #define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
  630. #define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", (char *)S, (SDL_wcslen(S)+1)*sizeof(wchar_t))
  631. /* force builds using Clang's static analysis tools to use literal C runtime
  632. here, since there are possibly tests that are ineffective otherwise. */
  633. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  634. /* The analyzer knows about strlcpy even when the system doesn't provide it */
  635. #ifndef HAVE_STRLCPY
  636. size_t strlcpy(char* dst, const char* src, size_t size);
  637. #endif
  638. /* The analyzer knows about strlcat even when the system doesn't provide it */
  639. #ifndef HAVE_STRLCAT
  640. size_t strlcat(char* dst, const char* src, size_t size);
  641. #endif
  642. #ifndef HAVE_WCSLCPY
  643. size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
  644. #endif
  645. #ifndef HAVE_WCSLCAT
  646. size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
  647. #endif
  648. /* Starting LLVM 16, the analyser errors out if these functions do not have
  649. their prototype defined (clang-diagnostic-implicit-function-declaration) */
  650. #include <stdlib.h>
  651. #include <string.h>
  652. #include <stdio.h>
  653. #define SDL_malloc malloc
  654. #define SDL_calloc calloc
  655. #define SDL_realloc realloc
  656. #define SDL_free free
  657. #define SDL_memset memset
  658. #define SDL_memcpy memcpy
  659. #define SDL_memmove memmove
  660. #define SDL_memcmp memcmp
  661. #define SDL_strlcpy strlcpy
  662. #define SDL_strlcat strlcat
  663. #define SDL_strlen strlen
  664. #define SDL_wcslen wcslen
  665. #define SDL_wcslcpy wcslcpy
  666. #define SDL_wcslcat wcslcat
  667. #define SDL_strdup strdup
  668. #define SDL_wcsdup wcsdup
  669. #define SDL_strchr strchr
  670. #define SDL_strrchr strrchr
  671. #define SDL_strstr strstr
  672. #define SDL_wcsstr wcsstr
  673. #define SDL_strtok_r strtok_r
  674. #define SDL_strcmp strcmp
  675. #define SDL_wcscmp wcscmp
  676. #define SDL_strncmp strncmp
  677. #define SDL_wcsncmp wcsncmp
  678. #define SDL_strcasecmp strcasecmp
  679. #define SDL_strncasecmp strncasecmp
  680. #define SDL_sscanf sscanf
  681. #define SDL_vsscanf vsscanf
  682. #define SDL_snprintf snprintf
  683. #define SDL_vsnprintf vsnprintf
  684. #endif
  685. SDL_FORCE_INLINE void *SDL_memcpy4(SDL_OUT_BYTECAP(dwords*4) void *dst, SDL_IN_BYTECAP(dwords*4) const void *src, size_t dwords)
  686. {
  687. return SDL_memcpy(dst, src, dwords * 4);
  688. }
  689. /**
  690. * If a * b would overflow, return -1. Otherwise store a * b via ret
  691. * and return 0.
  692. *
  693. * \since This function is available since SDL 3.0.0.
  694. */
  695. SDL_FORCE_INLINE int SDL_size_mul_overflow (size_t a,
  696. size_t b,
  697. size_t *ret)
  698. {
  699. if (a != 0 && b > SDL_SIZE_MAX / a) {
  700. return -1;
  701. }
  702. *ret = a * b;
  703. return 0;
  704. }
  705. #if SDL_HAS_BUILTIN(__builtin_mul_overflow)
  706. /* This needs to be wrapped in an inline rather than being a direct #define,
  707. * because __builtin_mul_overflow() is type-generic, but we want to be
  708. * consistent about interpreting a and b as size_t. */
  709. SDL_FORCE_INLINE int SDL_size_mul_overflow_builtin (size_t a,
  710. size_t b,
  711. size_t *ret)
  712. {
  713. return __builtin_mul_overflow(a, b, ret) == 0 ? 0 : -1;
  714. }
  715. #define SDL_size_mul_overflow(a, b, ret) (SDL_size_mul_overflow_builtin(a, b, ret))
  716. #endif
  717. /**
  718. * If a + b would overflow, return -1. Otherwise store a + b via ret
  719. * and return 0.
  720. *
  721. * \since This function is available since SDL 3.0.0.
  722. */
  723. SDL_FORCE_INLINE int SDL_size_add_overflow (size_t a,
  724. size_t b,
  725. size_t *ret)
  726. {
  727. if (b > SDL_SIZE_MAX - a) {
  728. return -1;
  729. }
  730. *ret = a + b;
  731. return 0;
  732. }
  733. #if SDL_HAS_BUILTIN(__builtin_add_overflow)
  734. /* This needs to be wrapped in an inline rather than being a direct #define,
  735. * the same as the call to __builtin_mul_overflow() above. */
  736. SDL_FORCE_INLINE int SDL_size_add_overflow_builtin (size_t a,
  737. size_t b,
  738. size_t *ret)
  739. {
  740. return __builtin_add_overflow(a, b, ret) == 0 ? 0 : -1;
  741. }
  742. #define SDL_size_add_overflow(a, b, ret) (SDL_size_add_overflow_builtin(a, b, ret))
  743. #endif
  744. /* This is a generic function pointer which should be cast to the type you expect */
  745. #ifdef SDL_FUNCTION_POINTER_IS_VOID_POINTER
  746. typedef void *SDL_FunctionPointer;
  747. #else
  748. typedef void (*SDL_FunctionPointer)(void);
  749. #endif
  750. /* Ends C function definitions when using C++ */
  751. #ifdef __cplusplus
  752. }
  753. #endif
  754. #include <SDL3/SDL_close_code.h>
  755. #endif /* SDL_stdinc_h_ */