SDL_stdinc.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  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 <string.h>
  32. #include <wchar.h>
  33. #ifndef SDL_DISABLE_ALLOCA
  34. # ifndef alloca
  35. # ifdef HAVE_ALLOCA_H
  36. # include <alloca.h>
  37. # elif defined(SDL_PLATFORM_NETBSD)
  38. # if defined(__STRICT_ANSI__)
  39. # define SDL_DISABLE_ALLOCA
  40. # else
  41. # include <stdlib.h>
  42. # endif
  43. # elif defined(__GNUC__)
  44. # define alloca __builtin_alloca
  45. # elif defined(_MSC_VER)
  46. # include <malloc.h>
  47. # define alloca _alloca
  48. # elif defined(__WATCOMC__)
  49. # include <malloc.h>
  50. # elif defined(__BORLANDC__)
  51. # include <malloc.h>
  52. # elif defined(__DMC__)
  53. # include <stdlib.h>
  54. # elif defined(SDL_PLATFORM_AIX)
  55. # pragma alloca
  56. # elif defined(__MRC__)
  57. void *alloca(unsigned);
  58. # else
  59. char *alloca();
  60. # endif
  61. # endif
  62. #endif
  63. #ifdef SIZE_MAX
  64. # define SDL_SIZE_MAX SIZE_MAX
  65. #else
  66. # define SDL_SIZE_MAX ((size_t) -1)
  67. #endif
  68. /**
  69. * Check if the compiler supports a given builtin.
  70. * Supported by virtually all clang versions and recent gcc. Use this
  71. * instead of checking the clang version if possible.
  72. */
  73. #ifdef __has_builtin
  74. #define SDL_HAS_BUILTIN(x) __has_builtin(x)
  75. #else
  76. #define SDL_HAS_BUILTIN(x) 0
  77. #endif
  78. /**
  79. * The number of elements in an array.
  80. */
  81. #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
  82. #define SDL_TABLESIZE(table) SDL_arraysize(table)
  83. /**
  84. * Macro useful for building other macros with strings in them.
  85. *
  86. * For example:
  87. *
  88. * ```c
  89. * #define LOG_ERROR(X) OutputDebugString(SDL_STRINGIFY_ARG(__FUNCTION__) ": " X "\n")`
  90. * ```
  91. */
  92. #define SDL_STRINGIFY_ARG(arg) #arg
  93. /**
  94. * \name Cast operators
  95. *
  96. * Use proper C++ casts when compiled as C++ to be compatible with the option
  97. * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
  98. */
  99. /* @{ */
  100. #ifdef __cplusplus
  101. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
  102. #define SDL_static_cast(type, expression) static_cast<type>(expression)
  103. #define SDL_const_cast(type, expression) const_cast<type>(expression)
  104. #else
  105. #define SDL_reinterpret_cast(type, expression) ((type)(expression))
  106. #define SDL_static_cast(type, expression) ((type)(expression))
  107. #define SDL_const_cast(type, expression) ((type)(expression))
  108. #endif
  109. /* @} *//* Cast operators */
  110. /* Define a four character code as a Uint32 */
  111. #define SDL_FOURCC(A, B, C, D) \
  112. ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
  113. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
  114. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
  115. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
  116. /**
  117. * \name Basic data types
  118. */
  119. /* @{ */
  120. /**
  121. * A boolean type.
  122. */
  123. #define SDL_FALSE 0
  124. #define SDL_TRUE 1
  125. typedef int SDL_bool;
  126. /**
  127. * A signed 8-bit integer type.
  128. */
  129. #define SDL_MAX_SINT8 ((Sint8)0x7F) /* 127 */
  130. #define SDL_MIN_SINT8 ((Sint8)(~0x7F)) /* -128 */
  131. typedef int8_t Sint8;
  132. /**
  133. * An unsigned 8-bit integer type.
  134. */
  135. #define SDL_MAX_UINT8 ((Uint8)0xFF) /* 255 */
  136. #define SDL_MIN_UINT8 ((Uint8)0x00) /* 0 */
  137. typedef uint8_t Uint8;
  138. /**
  139. * A signed 16-bit integer type.
  140. */
  141. #define SDL_MAX_SINT16 ((Sint16)0x7FFF) /* 32767 */
  142. #define SDL_MIN_SINT16 ((Sint16)(~0x7FFF)) /* -32768 */
  143. typedef int16_t Sint16;
  144. /**
  145. * An unsigned 16-bit integer type.
  146. */
  147. #define SDL_MAX_UINT16 ((Uint16)0xFFFF) /* 65535 */
  148. #define SDL_MIN_UINT16 ((Uint16)0x0000) /* 0 */
  149. typedef uint16_t Uint16;
  150. /**
  151. * A signed 32-bit integer type.
  152. */
  153. #define SDL_MAX_SINT32 ((Sint32)0x7FFFFFFF) /* 2147483647 */
  154. #define SDL_MIN_SINT32 ((Sint32)(~0x7FFFFFFF)) /* -2147483648 */
  155. typedef int32_t Sint32;
  156. /**
  157. * An unsigned 32-bit integer type.
  158. */
  159. #define SDL_MAX_UINT32 ((Uint32)0xFFFFFFFFu) /* 4294967295 */
  160. #define SDL_MIN_UINT32 ((Uint32)0x00000000) /* 0 */
  161. typedef uint32_t Uint32;
  162. /**
  163. * A signed 64-bit integer type.
  164. */
  165. #define SDL_MAX_SINT64 ((Sint64)0x7FFFFFFFFFFFFFFFll) /* 9223372036854775807 */
  166. #define SDL_MIN_SINT64 ((Sint64)(~0x7FFFFFFFFFFFFFFFll)) /* -9223372036854775808 */
  167. typedef int64_t Sint64;
  168. /**
  169. * An unsigned 64-bit integer type.
  170. */
  171. #define SDL_MAX_UINT64 ((Uint64)0xFFFFFFFFFFFFFFFFull) /* 18446744073709551615 */
  172. #define SDL_MIN_UINT64 ((Uint64)(0x0000000000000000ull)) /* 0 */
  173. typedef uint64_t Uint64;
  174. /**
  175. * SDL times are signed, 64-bit integers representing nanoseconds since the
  176. * Unix epoch (Jan 1, 1970).
  177. *
  178. * They can be converted between POSIX time_t values with SDL_NS_TO_SECONDS()
  179. * and SDL_SECONDS_TO_NS(), and between Windows FILETIME values with
  180. * SDL_TimeToWindows() and SDL_TimeFromWindows().
  181. */
  182. #define SDL_MAX_TIME SDL_MAX_SINT64
  183. #define SDL_MIN_TIME SDL_MIN_SINT64
  184. typedef Sint64 SDL_Time;
  185. /* @} *//* Basic data types */
  186. /**
  187. * \name Floating-point constants
  188. */
  189. /* @{ */
  190. #ifdef FLT_EPSILON
  191. #define SDL_FLT_EPSILON FLT_EPSILON
  192. #else
  193. #define SDL_FLT_EPSILON 1.1920928955078125e-07F /* 0x0.000002p0 */
  194. #endif
  195. /* @} *//* Floating-point constants */
  196. /* Make sure we have macros for printing width-based integers.
  197. * <stdint.h> should define these but this is not true all platforms.
  198. * (for example win32) */
  199. #ifndef SDL_PRIs64
  200. #ifdef PRIs64
  201. #define SDL_PRIs64 PRIs64
  202. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  203. #define SDL_PRIs64 "I64d"
  204. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  205. #define SDL_PRIs64 "ld"
  206. #else
  207. #define SDL_PRIs64 "lld"
  208. #endif
  209. #endif
  210. #ifndef SDL_PRIu64
  211. #ifdef PRIu64
  212. #define SDL_PRIu64 PRIu64
  213. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  214. #define SDL_PRIu64 "I64u"
  215. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  216. #define SDL_PRIu64 "lu"
  217. #else
  218. #define SDL_PRIu64 "llu"
  219. #endif
  220. #endif
  221. #ifndef SDL_PRIx64
  222. #ifdef PRIx64
  223. #define SDL_PRIx64 PRIx64
  224. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  225. #define SDL_PRIx64 "I64x"
  226. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  227. #define SDL_PRIx64 "lx"
  228. #else
  229. #define SDL_PRIx64 "llx"
  230. #endif
  231. #endif
  232. #ifndef SDL_PRIX64
  233. #ifdef PRIX64
  234. #define SDL_PRIX64 PRIX64
  235. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  236. #define SDL_PRIX64 "I64X"
  237. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  238. #define SDL_PRIX64 "lX"
  239. #else
  240. #define SDL_PRIX64 "llX"
  241. #endif
  242. #endif
  243. #ifndef SDL_PRIs32
  244. #ifdef PRId32
  245. #define SDL_PRIs32 PRId32
  246. #else
  247. #define SDL_PRIs32 "d"
  248. #endif
  249. #endif
  250. #ifndef SDL_PRIu32
  251. #ifdef PRIu32
  252. #define SDL_PRIu32 PRIu32
  253. #else
  254. #define SDL_PRIu32 "u"
  255. #endif
  256. #endif
  257. #ifndef SDL_PRIx32
  258. #ifdef PRIx32
  259. #define SDL_PRIx32 PRIx32
  260. #else
  261. #define SDL_PRIx32 "x"
  262. #endif
  263. #endif
  264. #ifndef SDL_PRIX32
  265. #ifdef PRIX32
  266. #define SDL_PRIX32 PRIX32
  267. #else
  268. #define SDL_PRIX32 "X"
  269. #endif
  270. #endif
  271. /* Annotations to help code analysis tools */
  272. #ifdef SDL_DISABLE_ANALYZE_MACROS
  273. #define SDL_IN_BYTECAP(x)
  274. #define SDL_INOUT_Z_CAP(x)
  275. #define SDL_OUT_Z_CAP(x)
  276. #define SDL_OUT_CAP(x)
  277. #define SDL_OUT_BYTECAP(x)
  278. #define SDL_OUT_Z_BYTECAP(x)
  279. #define SDL_PRINTF_FORMAT_STRING
  280. #define SDL_SCANF_FORMAT_STRING
  281. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  282. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  283. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  284. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  285. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  286. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber )
  287. #else
  288. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  289. #include <sal.h>
  290. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  291. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  292. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  293. #define SDL_OUT_CAP(x) _Out_cap_(x)
  294. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  295. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  296. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  297. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  298. #else
  299. #define SDL_IN_BYTECAP(x)
  300. #define SDL_INOUT_Z_CAP(x)
  301. #define SDL_OUT_Z_CAP(x)
  302. #define SDL_OUT_CAP(x)
  303. #define SDL_OUT_BYTECAP(x)
  304. #define SDL_OUT_Z_BYTECAP(x)
  305. #define SDL_PRINTF_FORMAT_STRING
  306. #define SDL_SCANF_FORMAT_STRING
  307. #endif
  308. #ifdef __GNUC__
  309. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  310. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  311. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  312. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  313. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  314. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wscanf__, fmtargnumber, fmtargnumber+1 ))) */
  315. #else
  316. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  317. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  318. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  319. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  320. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  321. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber )
  322. #endif
  323. #endif /* SDL_DISABLE_ANALYZE_MACROS */
  324. #ifndef SDL_COMPILE_TIME_ASSERT
  325. #ifdef __cplusplus
  326. #if (__cplusplus >= 201103L)
  327. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  328. #endif
  329. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  330. #define SDL_COMPILE_TIME_ASSERT(name, x) _Static_assert(x, #x)
  331. #endif
  332. #endif /* !SDL_COMPILE_TIME_ASSERT */
  333. #ifndef SDL_COMPILE_TIME_ASSERT
  334. /* universal, but may trigger -Wunused-local-typedefs */
  335. #define SDL_COMPILE_TIME_ASSERT(name, x) \
  336. typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
  337. #endif
  338. /** \cond */
  339. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  340. SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
  341. SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
  342. SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
  343. SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
  344. SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
  345. SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
  346. SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
  347. SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
  348. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  349. /** \endcond */
  350. /* Check to make sure enums are the size of ints, for structure packing.
  351. For both Watcom C/C++ and Borland C/C++ the compiler option that makes
  352. enums having the size of an int must be enabled.
  353. This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
  354. */
  355. /** \cond */
  356. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  357. #if !defined(SDL_PLATFORM_VITA) && !defined(SDL_PLATFORM_3DS)
  358. /* TODO: include/SDL_stdinc.h:390: error: size of array 'SDL_dummy_enum' is negative */
  359. typedef enum
  360. {
  361. DUMMY_ENUM_VALUE
  362. } SDL_DUMMY_ENUM;
  363. SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
  364. #endif
  365. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  366. /** \endcond */
  367. #include <SDL3/SDL_begin_code.h>
  368. /* Set up for C function definitions, even when using C++ */
  369. #ifdef __cplusplus
  370. extern "C" {
  371. #endif
  372. #ifndef SDL_DISABLE_ALLOCA
  373. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  374. #define SDL_stack_free(data)
  375. #else
  376. #define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*(count))
  377. #define SDL_stack_free(data) SDL_free(data)
  378. #endif
  379. extern DECLSPEC SDL_MALLOC void *SDLCALL SDL_malloc(size_t size);
  380. extern DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) void *SDLCALL SDL_calloc(size_t nmemb, size_t size);
  381. extern DECLSPEC SDL_ALLOC_SIZE(2) void *SDLCALL SDL_realloc(void *mem, size_t size);
  382. extern DECLSPEC void SDLCALL SDL_free(void *mem);
  383. typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
  384. typedef void *(SDLCALL *SDL_calloc_func)(size_t nmemb, size_t size);
  385. typedef void *(SDLCALL *SDL_realloc_func)(void *mem, size_t size);
  386. typedef void (SDLCALL *SDL_free_func)(void *mem);
  387. /**
  388. * Get the original set of SDL memory functions.
  389. *
  390. * \param malloc_func filled with malloc function
  391. * \param calloc_func filled with calloc function
  392. * \param realloc_func filled with realloc function
  393. * \param free_func filled with free function
  394. *
  395. * \since This function is available since SDL 3.0.0.
  396. */
  397. extern DECLSPEC void SDLCALL SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
  398. SDL_calloc_func *calloc_func,
  399. SDL_realloc_func *realloc_func,
  400. SDL_free_func *free_func);
  401. /**
  402. * Get the current set of SDL memory functions.
  403. *
  404. * \param malloc_func filled with malloc function
  405. * \param calloc_func filled with calloc function
  406. * \param realloc_func filled with realloc function
  407. * \param free_func filled with free function
  408. *
  409. * \since This function is available since SDL 3.0.0.
  410. */
  411. extern DECLSPEC void SDLCALL SDL_GetMemoryFunctions(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. * Replace SDL's memory allocation functions with a custom set.
  417. *
  418. * \param malloc_func custom malloc function
  419. * \param calloc_func custom calloc function
  420. * \param realloc_func custom realloc function
  421. * \param free_func custom free function
  422. * \returns 0 on success or a negative error code on failure; call
  423. * SDL_GetError() for more information.
  424. *
  425. * \since This function is available since SDL 3.0.0.
  426. */
  427. extern DECLSPEC int SDLCALL SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
  428. SDL_calloc_func calloc_func,
  429. SDL_realloc_func realloc_func,
  430. SDL_free_func free_func);
  431. /**
  432. * Allocate memory aligned to a specific value.
  433. *
  434. * If `alignment` is less than the size of `void *`, then it will be increased
  435. * to match that.
  436. *
  437. * The returned memory address will be a multiple of the alignment value, and
  438. * the amount of memory allocated will be a multiple of the alignment value.
  439. *
  440. * The memory returned by this function must be freed with SDL_aligned_free()
  441. *
  442. * \param alignment the alignment requested
  443. * \param size the size to allocate
  444. * \returns a pointer to the aligned memory
  445. *
  446. * \since This function is available since SDL 3.0.0.
  447. *
  448. * \sa SDL_aligned_free
  449. */
  450. extern DECLSPEC SDL_MALLOC void *SDLCALL SDL_aligned_alloc(size_t alignment, size_t size);
  451. /**
  452. * Free memory allocated by SDL_aligned_alloc().
  453. *
  454. * \since This function is available since SDL 3.0.0.
  455. *
  456. * \sa SDL_aligned_alloc
  457. */
  458. extern DECLSPEC void SDLCALL SDL_aligned_free(void *mem);
  459. /**
  460. * Get the number of outstanding (unfreed) allocations.
  461. *
  462. * \returns the number of allocations
  463. *
  464. * \since This function is available since SDL 3.0.0.
  465. */
  466. extern DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
  467. extern DECLSPEC char *SDLCALL SDL_getenv(const char *name);
  468. extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite);
  469. extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (const void *, const void *));
  470. 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 *));
  471. extern DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (void *, const void *, const void *), void *userdata);
  472. extern DECLSPEC void * SDLCALL SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (void *, const void *, const void *), void *userdata);
  473. extern DECLSPEC int SDLCALL SDL_abs(int x);
  474. /* NOTE: these double-evaluate their arguments, so you should never have side effects in the parameters */
  475. #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
  476. #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
  477. #define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
  478. /**
  479. * Query if a character is alphabetic (a letter).
  480. *
  481. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  482. * for English 'a-z' and 'A-Z' as true.
  483. *
  484. * \param x character value to check.
  485. * \returns non-zero if x falls within the character class, zero otherwise.
  486. *
  487. * \threadsafety It is safe to call this function from any thread.
  488. *
  489. * \since This function is available since SDL 3.0.0.
  490. */
  491. extern DECLSPEC int SDLCALL SDL_isalpha(int x);
  492. /**
  493. * Query if a character is alphabetic (a letter) or a number.
  494. *
  495. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  496. * for English 'a-z', 'A-Z', and '0-9' as true.
  497. *
  498. * \param x character value to check.
  499. * \returns non-zero if x falls within the character class, zero otherwise.
  500. *
  501. * \threadsafety It is safe to call this function from any thread.
  502. *
  503. * \since This function is available since SDL 3.0.0.
  504. */
  505. extern DECLSPEC int SDLCALL SDL_isalnum(int x);
  506. /**
  507. * Report if a character is blank (a space or tab).
  508. *
  509. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  510. * 0x20 (space) or 0x9 (tab) as true.
  511. *
  512. * \param x character value to check.
  513. * \returns non-zero if x falls within the character class, zero otherwise.
  514. *
  515. * \threadsafety It is safe to call this function from any thread.
  516. *
  517. * \since This function is available since SDL 3.0.0.
  518. */
  519. extern DECLSPEC int SDLCALL SDL_isblank(int x);
  520. /**
  521. * Report if a character is a control character.
  522. *
  523. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  524. * 0 through 0x1F, and 0x7F, as true.
  525. *
  526. * \param x character value to check.
  527. * \returns non-zero if x falls within the character class, zero otherwise.
  528. *
  529. * \threadsafety It is safe to call this function from any thread.
  530. *
  531. * \since This function is available since SDL 3.0.0.
  532. */
  533. extern DECLSPEC int SDLCALL SDL_iscntrl(int x);
  534. /**
  535. * Report if a character is a numeric digit.
  536. *
  537. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  538. * '0' (0x30) through '9' (0x39), as true.
  539. *
  540. * \param x character value to check.
  541. * \returns non-zero if x falls within the character class, zero otherwise.
  542. *
  543. * \threadsafety It is safe to call this function from any thread.
  544. *
  545. * \since This function is available since SDL 3.0.0.
  546. */
  547. extern DECLSPEC int SDLCALL SDL_isdigit(int x);
  548. /**
  549. * Report if a character is a hexadecimal digit.
  550. *
  551. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  552. * 'A' through 'F', 'a' through 'f', and '0' through '9', as true.
  553. *
  554. * \param x character value to check.
  555. * \returns non-zero if x falls within the character class, zero otherwise.
  556. *
  557. * \threadsafety It is safe to call this function from any thread.
  558. *
  559. * \since This function is available since SDL 3.0.0.
  560. */
  561. extern DECLSPEC int SDLCALL SDL_isxdigit(int x);
  562. /**
  563. * Report if a character is a punctuation mark.
  564. *
  565. * **WARNING**: Regardless of system locale, this is equivalent to
  566. * `((SDL_isgraph(x)) && (!SDL_isalnum(x)))`.
  567. *
  568. * \param x character value to check.
  569. * \returns non-zero if x falls within the character class, zero otherwise.
  570. *
  571. * \threadsafety It is safe to call this function from any thread.
  572. *
  573. * \since This function is available since SDL 3.0.0.
  574. *
  575. * \sa SDL_isgraph
  576. * \sa SDL_isalnum
  577. */
  578. extern DECLSPEC int SDLCALL SDL_ispunct(int x);
  579. /**
  580. * Report if a character is whitespace.
  581. *
  582. * **WARNING**: Regardless of system locale, this will only treat the
  583. * following ASCII values as true:
  584. *
  585. * - space (0x20)
  586. * - tab (0x09)
  587. * - newline (0x0A)
  588. * - vertical tab (0x0B)
  589. * - form feed (0x0C)
  590. * - return (0x0D)
  591. *
  592. * \param x character value to check.
  593. * \returns non-zero if x falls within the character class, zero otherwise.
  594. *
  595. * \threadsafety It is safe to call this function from any thread.
  596. *
  597. * \since This function is available since SDL 3.0.0.
  598. */
  599. extern DECLSPEC int SDLCALL SDL_isspace(int x);
  600. /**
  601. * Report if a character is upper case.
  602. *
  603. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  604. * 'A' through 'Z' as true.
  605. *
  606. * \param x character value to check.
  607. * \returns non-zero if x falls within the character class, zero otherwise.
  608. *
  609. * \threadsafety It is safe to call this function from any thread.
  610. *
  611. * \since This function is available since SDL 3.0.0.
  612. */
  613. extern DECLSPEC int SDLCALL SDL_isupper(int x);
  614. /**
  615. * Report if a character is lower case.
  616. *
  617. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  618. * 'a' through 'z' as true.
  619. *
  620. * \param x character value to check.
  621. * \returns non-zero if x falls within the character class, zero otherwise.
  622. *
  623. * \threadsafety It is safe to call this function from any thread.
  624. *
  625. * \since This function is available since SDL 3.0.0.
  626. */
  627. extern DECLSPEC int SDLCALL SDL_islower(int x);
  628. /**
  629. * Report if a character is "printable".
  630. *
  631. * Be advised that "printable" has a definition that goes back to text
  632. * terminals from the dawn of computing, making this a sort of special case
  633. * function that is not suitable for Unicode (or most any) text management.
  634. *
  635. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  636. * ' ' (0x20) through '~' (0x7E) as true.
  637. *
  638. * \param x character value to check.
  639. * \returns non-zero if x falls within the character class, zero otherwise.
  640. *
  641. * \threadsafety It is safe to call this function from any thread.
  642. *
  643. * \since This function is available since SDL 3.0.0.
  644. */
  645. extern DECLSPEC int SDLCALL SDL_isprint(int x);
  646. /**
  647. * Report if a character is any "printable" except space.
  648. *
  649. * Be advised that "printable" has a definition that goes back to text
  650. * terminals from the dawn of computing, making this a sort of special case
  651. * function that is not suitable for Unicode (or most any) text management.
  652. *
  653. * **WARNING**: Regardless of system locale, this is equivalent to
  654. * `(SDL_isprint(x)) && ((x) != ' ')`.
  655. *
  656. * \param x character value to check.
  657. * \returns non-zero if x falls within the character class, zero otherwise.
  658. *
  659. * \threadsafety It is safe to call this function from any thread.
  660. *
  661. * \since This function is available since SDL 3.0.0.
  662. *
  663. * \sa SDL_isprint
  664. */
  665. extern DECLSPEC int SDLCALL SDL_isgraph(int x);
  666. /**
  667. * Convert low-ASCII English letters to uppercase.
  668. *
  669. * **WARNING**: Regardless of system locale, this will only convert ASCII
  670. * values 'a' through 'z' to uppercase.
  671. *
  672. * This function returns the uppercase equivalent of `x`. If a character
  673. * cannot be converted, or is already uppercase, this function returns `x`.
  674. *
  675. * \param x character value to check.
  676. * \returns Capitalized version of x, or x if no conversion available.
  677. *
  678. * \threadsafety It is safe to call this function from any thread.
  679. *
  680. * \since This function is available since SDL 3.0.0.
  681. */
  682. extern DECLSPEC int SDLCALL SDL_toupper(int x);
  683. /**
  684. * Convert low-ASCII English letters to lowercase.
  685. *
  686. * **WARNING**: Regardless of system locale, this will only convert ASCII
  687. * values 'A' through 'Z' to lowercase.
  688. *
  689. * This function returns the lowercase equivalent of `x`. If a character
  690. * cannot be converted, or is already lowercase, this function returns `x`.
  691. *
  692. * \param x character value to check.
  693. * \returns Lowercase version of x, or x if no conversion available.
  694. *
  695. * \threadsafety It is safe to call this function from any thread.
  696. *
  697. * \since This function is available since SDL 3.0.0.
  698. */
  699. extern DECLSPEC int SDLCALL SDL_tolower(int x);
  700. extern DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
  701. extern DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
  702. extern DECLSPEC void *SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  703. /* Take advantage of compiler optimizations for memcpy */
  704. #ifndef SDL_SLOW_MEMCPY
  705. #ifdef SDL_memcpy
  706. #undef SDL_memcpy
  707. #endif
  708. #define SDL_memcpy memcpy
  709. #endif
  710. #define SDL_copyp(dst, src) \
  711. { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
  712. SDL_memcpy((dst), (src), sizeof(*(src)))
  713. extern DECLSPEC void *SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  714. /* Take advantage of compiler optimizations for memmove */
  715. #ifndef SDL_SLOW_MEMMOVE
  716. #ifdef SDL_memmove
  717. #undef SDL_memmove
  718. #endif
  719. #define SDL_memmove memmove
  720. #endif
  721. extern DECLSPEC void *SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
  722. extern DECLSPEC void *SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
  723. /* Take advantage of compiler optimizations for memset */
  724. #ifndef SDL_SLOW_MEMSET
  725. #ifdef SDL_memset
  726. #undef SDL_memset
  727. #endif
  728. #define SDL_memset memset
  729. #endif
  730. #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
  731. #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
  732. #define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
  733. extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
  734. extern DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
  735. extern DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
  736. extern DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  737. extern DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  738. extern DECLSPEC wchar_t *SDLCALL SDL_wcsdup(const wchar_t *wstr);
  739. extern DECLSPEC wchar_t *SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
  740. extern DECLSPEC wchar_t *SDLCALL SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen);
  741. /**
  742. * Compare two null-terminated wide strings.
  743. *
  744. * This only compares wchar_t values until it hits a null-terminating
  745. * character; it does not care if the string is well-formed UTF-16 (or UTF-32,
  746. * depending on your platform's wchar_t size), or uses valid Unicode values.
  747. *
  748. * \param str1 The first string to compare. NULL is not permitted!
  749. * \param str2 The second string to compare. NULL is not permitted!
  750. * \returns less than zero if str1 is "less than" str2, greater than zero if
  751. * str1 is "greater than" str2, and zero if the strings match
  752. * exactly.
  753. *
  754. * \threadsafety It is safe to call this function from any thread.
  755. *
  756. * \since This function is available since SDL 3.0.0.
  757. */
  758. extern DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
  759. /**
  760. * Compare two wide strings up to a number of wchar_t values.
  761. *
  762. * This only compares wchar_t values; it does not care if the string is
  763. * well-formed UTF-16 (or UTF-32, depending on your platform's wchar_t size),
  764. * or uses valid Unicode values.
  765. *
  766. * Note that while this function is intended to be used with UTF-16 (or
  767. * UTF-32, depending on your platform's definition of wchar_t), it is
  768. * comparing raw wchar_t values and not Unicode codepoints: `maxlen` specifies
  769. * a wchar_t limit! If the limit lands in the middle of a multi-wchar UTF-16
  770. * sequence, it will only compare a portion of the final character.
  771. *
  772. * `maxlen` specifies a maximum number of wchar_t to compare; if the strings
  773. * match to this number of wide chars (or both have matched to a
  774. * null-terminator character before this count), they will be considered
  775. * equal.
  776. *
  777. * \param str1 The first string to compare. NULL is not permitted!
  778. * \param str2 The second string to compare. NULL is not permitted!
  779. * \param maxlen The maximum number of wchar_t to compare.
  780. * \returns less than zero if str1 is "less than" str2, greater than zero if
  781. * str1 is "greater than" str2, and zero if the strings match
  782. * exactly.
  783. *
  784. * \threadsafety It is safe to call this function from any thread.
  785. *
  786. * \since This function is available since SDL 3.0.0.
  787. */
  788. extern DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  789. /**
  790. * Compare two null-terminated wide strings, case-insensitively.
  791. *
  792. * This will work with Unicode strings, using a technique called
  793. * "case-folding" to handle the vast majority of case-sensitive human
  794. * languages regardless of system locale. It can deal with expanding values: a
  795. * German Eszett character can compare against two ASCII 's' chars and be
  796. * considered a match, for example. A notable exception: it does not handle
  797. * the Turkish 'i' character; human language is complicated!
  798. *
  799. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  800. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  801. * handles Unicode, it expects the string to be well-formed and not a
  802. * null-terminated string of arbitrary bytes. Characters that are not valid
  803. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  804. * CHARACTER), which is to say two strings of random bits may turn out to
  805. * match if they convert to the same amount of replacement characters.
  806. *
  807. * \param str1 The first string to compare. NULL is not permitted!
  808. * \param str2 The second string to compare. NULL is not permitted!
  809. * \returns less than zero if str1 is "less than" str2, greater than zero if
  810. * str1 is "greater than" str2, and zero if the strings match
  811. * exactly.
  812. *
  813. * \threadsafety It is safe to call this function from any thread.
  814. *
  815. * \since This function is available since SDL 3.0.0.
  816. */
  817. extern DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
  818. /**
  819. * Compare two wide strings, case-insensitively, up to a number of wchar_t.
  820. *
  821. * This will work with Unicode strings, using a technique called
  822. * "case-folding" to handle the vast majority of case-sensitive human
  823. * languages regardless of system locale. It can deal with expanding values: a
  824. * German Eszett character can compare against two ASCII 's' chars and be
  825. * considered a match, for example. A notable exception: it does not handle
  826. * the Turkish 'i' character; human language is complicated!
  827. *
  828. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  829. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  830. * handles Unicode, it expects the string to be well-formed and not a
  831. * null-terminated string of arbitrary bytes. Characters that are not valid
  832. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  833. * CHARACTER), which is to say two strings of random bits may turn out to
  834. * match if they convert to the same amount of replacement characters.
  835. *
  836. * Note that while this function might deal with variable-sized characters,
  837. * `maxlen` specifies a _wchar_ limit! If the limit lands in the middle of a
  838. * multi-byte UTF-16 sequence, it may convert a portion of the final character
  839. * to one or more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not
  840. * to overflow a buffer.
  841. *
  842. * `maxlen` specifies a maximum number of wchar_t values to compare; if the
  843. * strings match to this number of wchar_t (or both have matched to a
  844. * null-terminator character before this number of bytes), they will be
  845. * considered equal.
  846. *
  847. * \param str1 The first string to compare. NULL is not permitted!
  848. * \param str2 The second string to compare. NULL is not permitted!
  849. * \param maxlen The maximum number of wchar_t values to compare.
  850. * \returns less than zero if str1 is "less than" str2, greater than zero if
  851. * str1 is "greater than" str2, and zero if the strings match
  852. * exactly.
  853. *
  854. * \threadsafety It is safe to call this function from any thread.
  855. *
  856. * \since This function is available since SDL 3.0.0.
  857. */
  858. extern DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  859. extern DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
  860. extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
  861. extern DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
  862. extern DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  863. extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
  864. extern DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  865. extern DECLSPEC SDL_MALLOC char *SDLCALL SDL_strdup(const char *str);
  866. extern DECLSPEC SDL_MALLOC char *SDLCALL SDL_strndup(const char *str, size_t maxlen);
  867. extern DECLSPEC char *SDLCALL SDL_strrev(char *str);
  868. /**
  869. * Convert a string to uppercase.
  870. *
  871. * **WARNING**: Regardless of system locale, this will only convert ASCII
  872. * values 'A' through 'Z' to uppercase.
  873. *
  874. * This function operates on a null-terminated string of bytes--even if it is
  875. * malformed UTF-8!--and converts ASCII characters 'a' through 'z' to their
  876. * uppercase equivalents in-place, returning the original `str` pointer.
  877. *
  878. * \threadsafety It is safe to call this function from any thread.
  879. *
  880. * \since This function is available since SDL 3.0.0.
  881. *
  882. * \sa SDL_strlwr
  883. */
  884. extern DECLSPEC char *SDLCALL SDL_strupr(char *str);
  885. /**
  886. * Convert a string to lowercase.
  887. *
  888. * **WARNING**: Regardless of system locale, this will only convert ASCII
  889. * values 'A' through 'Z' to lowercase.
  890. *
  891. * This function operates on a null-terminated string of bytes--even if it is
  892. * malformed UTF-8!--and converts ASCII characters 'A' through 'Z' to their
  893. * lowercase equivalents in-place, returning the original `str` pointer.
  894. *
  895. * \param str The string to convert in-place.
  896. * \returns The `str` pointer passed into this function.
  897. *
  898. * \threadsafety It is safe to call this function from any thread.
  899. *
  900. * \since This function is available since SDL 3.0.0.
  901. *
  902. * \sa SDL_strupr
  903. */
  904. extern DECLSPEC char *SDLCALL SDL_strlwr(char *str);
  905. extern DECLSPEC char *SDLCALL SDL_strchr(const char *str, int c);
  906. extern DECLSPEC char *SDLCALL SDL_strrchr(const char *str, int c);
  907. extern DECLSPEC char *SDLCALL SDL_strstr(const char *haystack, const char *needle);
  908. extern DECLSPEC char *SDLCALL SDL_strnstr(const char *haystack, const char *needle, size_t maxlen);
  909. extern DECLSPEC char *SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
  910. extern DECLSPEC char *SDLCALL SDL_strtok_r(char *s1, const char *s2, char **saveptr);
  911. extern DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
  912. extern DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
  913. extern DECLSPEC char *SDLCALL SDL_itoa(int value, char *str, int radix);
  914. extern DECLSPEC char *SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
  915. extern DECLSPEC char *SDLCALL SDL_ltoa(long value, char *str, int radix);
  916. extern DECLSPEC char *SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
  917. extern DECLSPEC char *SDLCALL SDL_lltoa(Sint64 value, char *str, int radix);
  918. extern DECLSPEC char *SDLCALL SDL_ulltoa(Uint64 value, char *str, int radix);
  919. extern DECLSPEC int SDLCALL SDL_atoi(const char *str);
  920. extern DECLSPEC double SDLCALL SDL_atof(const char *str);
  921. extern DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
  922. extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
  923. extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int base);
  924. extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *str, char **endp, int base);
  925. extern DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
  926. /**
  927. * Compare two null-terminated UTF-8 strings.
  928. *
  929. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  930. * since effectively this function just compares bytes until it hits a
  931. * null-terminating character. Also due to the nature of UTF-8, this can be
  932. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  933. *
  934. * \param str1 The first string to compare. NULL is not permitted!
  935. * \param str2 The second string to compare. NULL is not permitted!
  936. * \returns less than zero if str1 is "less than" str2, greater than zero if
  937. * str1 is "greater than" str2, and zero if the strings match
  938. * exactly.
  939. *
  940. * \threadsafety It is safe to call this function from any thread.
  941. *
  942. * \since This function is available since SDL 3.0.0.
  943. */
  944. extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
  945. /**
  946. * Compare two UTF-8 strings up to a number of bytes.
  947. *
  948. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  949. * since effectively this function just compares bytes until it hits a
  950. * null-terminating character. Also due to the nature of UTF-8, this can be
  951. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  952. *
  953. * Note that while this function is intended to be used with UTF-8, it is
  954. * doing a bytewise comparison, and `maxlen` specifies a _byte_ limit! If the
  955. * limit lands in the middle of a multi-byte UTF-8 sequence, it will only
  956. * compare a portion of the final character.
  957. *
  958. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  959. * match to this number of bytes (or both have matched to a null-terminator
  960. * character before this number of bytes), they will be considered equal.
  961. *
  962. * \param str1 The first string to compare. NULL is not permitted!
  963. * \param str2 The second string to compare. NULL is not permitted!
  964. * \param maxlen The maximum number of _bytes_ to compare.
  965. * \returns less than zero if str1 is "less than" str2, greater than zero if
  966. * str1 is "greater than" str2, and zero if the strings match
  967. * exactly.
  968. *
  969. * \threadsafety It is safe to call this function from any thread.
  970. *
  971. * \since This function is available since SDL 3.0.0.
  972. */
  973. extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
  974. /**
  975. * Compare two null-terminated UTF-8 strings, case-insensitively.
  976. *
  977. * This will work with Unicode strings, using a technique called
  978. * "case-folding" to handle the vast majority of case-sensitive human
  979. * languages regardless of system locale. It can deal with expanding values: a
  980. * German Eszett character can compare against two ASCII 's' chars and be
  981. * considered a match, for example. A notable exception: it does not handle
  982. * the Turkish 'i' character; human language is complicated!
  983. *
  984. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  985. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  986. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  987. * CHARACTER), which is to say two strings of random bits may turn out to
  988. * match if they convert to the same amount of replacement characters.
  989. *
  990. * \param str1 The first string to compare. NULL is not permitted!
  991. * \param str2 The second string to compare. NULL is not permitted!
  992. * \returns less than zero if str1 is "less than" str2, greater than zero if
  993. * str1 is "greater than" str2, and zero if the strings match
  994. * exactly.
  995. *
  996. * \threadsafety It is safe to call this function from any thread.
  997. *
  998. * \since This function is available since SDL 3.0.0.
  999. */
  1000. extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
  1001. /**
  1002. * Compare two UTF-8 strings, case-insensitively, up to a number of bytes.
  1003. *
  1004. * This will work with Unicode strings, using a technique called
  1005. * "case-folding" to handle the vast majority of case-sensitive human
  1006. * languages regardless of system locale. It can deal with expanding values: a
  1007. * German Eszett character can compare against two ASCII 's' chars and be
  1008. * considered a match, for example. A notable exception: it does not handle
  1009. * the Turkish 'i' character; human language is complicated!
  1010. *
  1011. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  1012. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  1013. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  1014. * CHARACTER), which is to say two strings of random bits may turn out to
  1015. * match if they convert to the same amount of replacement characters.
  1016. *
  1017. * Note that while this function is intended to be used with UTF-8, `maxlen`
  1018. * specifies a _byte_ limit! If the limit lands in the middle of a multi-byte
  1019. * UTF-8 sequence, it may convert a portion of the final character to one or
  1020. * more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not to overflow
  1021. * a buffer.
  1022. *
  1023. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  1024. * match to this number of bytes (or both have matched to a null-terminator
  1025. * character before this number of bytes), they will be considered equal.
  1026. *
  1027. * \param str1 The first string to compare. NULL is not permitted!
  1028. * \param str2 The second string to compare. NULL is not permitted!
  1029. * \param maxlen The maximum number of bytes to compare.
  1030. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1031. * str1 is "greater than" str2, and zero if the strings match
  1032. * exactly.
  1033. *
  1034. * \threadsafety It is safe to call this function from any thread.
  1035. *
  1036. * \since This function is available since SDL 3.0.0.
  1037. */
  1038. extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
  1039. extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
  1040. extern DECLSPEC int SDLCALL SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap) SDL_SCANF_VARARG_FUNCV(2);
  1041. 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);
  1042. 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);
  1043. 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);
  1044. extern DECLSPEC int SDLCALL SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap);
  1045. extern DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  1046. extern DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  1047. #ifndef SDL_PI_D
  1048. #define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
  1049. #endif
  1050. #ifndef SDL_PI_F
  1051. #define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
  1052. #endif
  1053. /**
  1054. * Use this function to compute arc cosine of `x`.
  1055. *
  1056. * The definition of `y = acos(x)` is `x = cos(y)`.
  1057. *
  1058. * Domain: `-1 <= x <= 1`
  1059. *
  1060. * Range: `0 <= y <= Pi`
  1061. *
  1062. * \param x floating point value, in radians.
  1063. * \returns arc cosine of `x`.
  1064. *
  1065. * \threadsafety It is safe to call this function from any thread.
  1066. *
  1067. * \since This function is available since SDL 3.0.0.
  1068. */
  1069. extern DECLSPEC double SDLCALL SDL_acos(double x);
  1070. extern DECLSPEC float SDLCALL SDL_acosf(float x);
  1071. extern DECLSPEC double SDLCALL SDL_asin(double x);
  1072. extern DECLSPEC float SDLCALL SDL_asinf(float x);
  1073. extern DECLSPEC double SDLCALL SDL_atan(double x);
  1074. extern DECLSPEC float SDLCALL SDL_atanf(float x);
  1075. extern DECLSPEC double SDLCALL SDL_atan2(double y, double x);
  1076. extern DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
  1077. extern DECLSPEC double SDLCALL SDL_ceil(double x);
  1078. extern DECLSPEC float SDLCALL SDL_ceilf(float x);
  1079. extern DECLSPEC double SDLCALL SDL_copysign(double x, double y);
  1080. extern DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
  1081. extern DECLSPEC double SDLCALL SDL_cos(double x);
  1082. extern DECLSPEC float SDLCALL SDL_cosf(float x);
  1083. extern DECLSPEC double SDLCALL SDL_exp(double x);
  1084. extern DECLSPEC float SDLCALL SDL_expf(float x);
  1085. extern DECLSPEC double SDLCALL SDL_fabs(double x);
  1086. extern DECLSPEC float SDLCALL SDL_fabsf(float x);
  1087. extern DECLSPEC double SDLCALL SDL_floor(double x);
  1088. extern DECLSPEC float SDLCALL SDL_floorf(float x);
  1089. extern DECLSPEC double SDLCALL SDL_trunc(double x);
  1090. extern DECLSPEC float SDLCALL SDL_truncf(float x);
  1091. extern DECLSPEC double SDLCALL SDL_fmod(double x, double y);
  1092. extern DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
  1093. extern DECLSPEC double SDLCALL SDL_log(double x);
  1094. extern DECLSPEC float SDLCALL SDL_logf(float x);
  1095. extern DECLSPEC double SDLCALL SDL_log10(double x);
  1096. extern DECLSPEC float SDLCALL SDL_log10f(float x);
  1097. extern DECLSPEC double SDLCALL SDL_modf(double x, double *y);
  1098. extern DECLSPEC float SDLCALL SDL_modff(float x, float *y);
  1099. extern DECLSPEC double SDLCALL SDL_pow(double x, double y);
  1100. extern DECLSPEC float SDLCALL SDL_powf(float x, float y);
  1101. extern DECLSPEC double SDLCALL SDL_round(double x);
  1102. extern DECLSPEC float SDLCALL SDL_roundf(float x);
  1103. extern DECLSPEC long SDLCALL SDL_lround(double x);
  1104. extern DECLSPEC long SDLCALL SDL_lroundf(float x);
  1105. extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
  1106. extern DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
  1107. extern DECLSPEC double SDLCALL SDL_sin(double x);
  1108. extern DECLSPEC float SDLCALL SDL_sinf(float x);
  1109. extern DECLSPEC double SDLCALL SDL_sqrt(double x);
  1110. extern DECLSPEC float SDLCALL SDL_sqrtf(float x);
  1111. extern DECLSPEC double SDLCALL SDL_tan(double x);
  1112. extern DECLSPEC float SDLCALL SDL_tanf(float x);
  1113. /* The SDL implementation of iconv() returns these error codes */
  1114. #define SDL_ICONV_ERROR (size_t)-1
  1115. #define SDL_ICONV_E2BIG (size_t)-2
  1116. #define SDL_ICONV_EILSEQ (size_t)-3
  1117. #define SDL_ICONV_EINVAL (size_t)-4
  1118. /* SDL_iconv_* are now always real symbols/types, not macros or inlined. */
  1119. typedef struct SDL_iconv_data_t *SDL_iconv_t;
  1120. extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
  1121. const char *fromcode);
  1122. extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
  1123. extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
  1124. size_t * inbytesleft, char **outbuf,
  1125. size_t * outbytesleft);
  1126. /**
  1127. * This function converts a buffer or string between encodings in one pass,
  1128. * returning a string that must be freed with SDL_free() or NULL on error.
  1129. *
  1130. * \since This function is available since SDL 3.0.0.
  1131. */
  1132. extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
  1133. const char *fromcode,
  1134. const char *inbuf,
  1135. size_t inbytesleft);
  1136. #define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
  1137. #define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
  1138. #define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
  1139. #define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", (char *)S, (SDL_wcslen(S)+1)*sizeof(wchar_t))
  1140. /* force builds using Clang's static analysis tools to use literal C runtime
  1141. here, since there are possibly tests that are ineffective otherwise. */
  1142. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  1143. /* The analyzer knows about strlcpy even when the system doesn't provide it */
  1144. #if !defined(HAVE_STRLCPY) && !defined(strlcpy)
  1145. size_t strlcpy(char* dst, const char* src, size_t size);
  1146. #endif
  1147. /* The analyzer knows about strlcat even when the system doesn't provide it */
  1148. #if !defined(HAVE_STRLCAT) && !defined(strlcat)
  1149. size_t strlcat(char* dst, const char* src, size_t size);
  1150. #endif
  1151. #if !defined(HAVE_WCSLCPY) && !defined(wcslcpy)
  1152. size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
  1153. #endif
  1154. #if !defined(HAVE_WCSLCAT) && !defined(wcslcat)
  1155. size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
  1156. #endif
  1157. /* Starting LLVM 16, the analyser errors out if these functions do not have
  1158. their prototype defined (clang-diagnostic-implicit-function-declaration) */
  1159. #include <stdlib.h>
  1160. #include <stdio.h>
  1161. #define SDL_malloc malloc
  1162. #define SDL_calloc calloc
  1163. #define SDL_realloc realloc
  1164. #define SDL_free free
  1165. #ifndef SDL_memcpy
  1166. #define SDL_memcpy memcpy
  1167. #endif
  1168. #ifndef SDL_memmove
  1169. #define SDL_memmove memmove
  1170. #endif
  1171. #ifndef SDL_memset
  1172. #define SDL_memset memset
  1173. #endif
  1174. #define SDL_memcmp memcmp
  1175. #define SDL_strlcpy strlcpy
  1176. #define SDL_strlcat strlcat
  1177. #define SDL_strlen strlen
  1178. #define SDL_wcslen wcslen
  1179. #define SDL_wcslcpy wcslcpy
  1180. #define SDL_wcslcat wcslcat
  1181. #define SDL_strdup strdup
  1182. #define SDL_wcsdup wcsdup
  1183. #define SDL_strchr strchr
  1184. #define SDL_strrchr strrchr
  1185. #define SDL_strstr strstr
  1186. #define SDL_wcsstr wcsstr
  1187. #define SDL_strtok_r strtok_r
  1188. #define SDL_strcmp strcmp
  1189. #define SDL_wcscmp wcscmp
  1190. #define SDL_strncmp strncmp
  1191. #define SDL_wcsncmp wcsncmp
  1192. #define SDL_strcasecmp strcasecmp
  1193. #define SDL_strncasecmp strncasecmp
  1194. #define SDL_sscanf sscanf
  1195. #define SDL_vsscanf vsscanf
  1196. #define SDL_snprintf snprintf
  1197. #define SDL_vsnprintf vsnprintf
  1198. #endif
  1199. /**
  1200. * If a * b would overflow, return -1. Otherwise store a * b via ret
  1201. * and return 0.
  1202. *
  1203. * \since This function is available since SDL 3.0.0.
  1204. */
  1205. SDL_FORCE_INLINE int SDL_size_mul_overflow (size_t a,
  1206. size_t b,
  1207. size_t *ret)
  1208. {
  1209. if (a != 0 && b > SDL_SIZE_MAX / a) {
  1210. return -1;
  1211. }
  1212. *ret = a * b;
  1213. return 0;
  1214. }
  1215. #if SDL_HAS_BUILTIN(__builtin_mul_overflow)
  1216. /* This needs to be wrapped in an inline rather than being a direct #define,
  1217. * because __builtin_mul_overflow() is type-generic, but we want to be
  1218. * consistent about interpreting a and b as size_t. */
  1219. SDL_FORCE_INLINE int SDL_size_mul_overflow_builtin (size_t a,
  1220. size_t b,
  1221. size_t *ret)
  1222. {
  1223. return __builtin_mul_overflow(a, b, ret) == 0 ? 0 : -1;
  1224. }
  1225. #define SDL_size_mul_overflow(a, b, ret) (SDL_size_mul_overflow_builtin(a, b, ret))
  1226. #endif
  1227. /**
  1228. * If a + b would overflow, return -1. Otherwise store a + b via ret
  1229. * and return 0.
  1230. *
  1231. * \since This function is available since SDL 3.0.0.
  1232. */
  1233. SDL_FORCE_INLINE int SDL_size_add_overflow (size_t a,
  1234. size_t b,
  1235. size_t *ret)
  1236. {
  1237. if (b > SDL_SIZE_MAX - a) {
  1238. return -1;
  1239. }
  1240. *ret = a + b;
  1241. return 0;
  1242. }
  1243. #if SDL_HAS_BUILTIN(__builtin_add_overflow)
  1244. /* This needs to be wrapped in an inline rather than being a direct #define,
  1245. * the same as the call to __builtin_mul_overflow() above. */
  1246. SDL_FORCE_INLINE int SDL_size_add_overflow_builtin (size_t a,
  1247. size_t b,
  1248. size_t *ret)
  1249. {
  1250. return __builtin_add_overflow(a, b, ret) == 0 ? 0 : -1;
  1251. }
  1252. #define SDL_size_add_overflow(a, b, ret) (SDL_size_add_overflow_builtin(a, b, ret))
  1253. #endif
  1254. /* This is a generic function pointer which should be cast to the type you expect */
  1255. #ifdef SDL_FUNCTION_POINTER_IS_VOID_POINTER
  1256. typedef void *SDL_FunctionPointer;
  1257. #else
  1258. typedef void (*SDL_FunctionPointer)(void);
  1259. #endif
  1260. /* Ends C function definitions when using C++ */
  1261. #ifdef __cplusplus
  1262. }
  1263. #endif
  1264. #include <SDL3/SDL_close_code.h>
  1265. #endif /* SDL_stdinc_h_ */