SDL_stdinc.h 32 KB

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