🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

838 lines
29 KiB

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