SDL_begin_code.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /* WIKI CATEGORY: BeginCode */
  19. /**
  20. * SDL_begin_code.h sets things up for C dynamic library function definitions,
  21. * static inlined functions, and structures aligned at 4-byte alignment.
  22. * If you don't like ugly C preprocessor code, don't look at this file. :)
  23. *
  24. * SDL's headers use this; applications generally should not include this
  25. * header directly.
  26. */
  27. /* This shouldn't be nested -- included it around code only. */
  28. #ifdef SDL_begin_code_h
  29. #error Nested inclusion of SDL_begin_code.h
  30. #endif
  31. #define SDL_begin_code_h
  32. #ifndef SDL_DEPRECATED
  33. # if defined(__GNUC__) && (__GNUC__ >= 4) /* technically, this arrived in gcc 3.1, but oh well. */
  34. # define SDL_DEPRECATED __attribute__((deprecated))
  35. # elif defined(_MSC_VER)
  36. # define SDL_DEPRECATED __declspec(deprecated)
  37. # else
  38. # define SDL_DEPRECATED
  39. # endif
  40. #endif
  41. #ifndef SDL_UNUSED
  42. # ifdef __GNUC__
  43. # define SDL_UNUSED __attribute__((unused))
  44. # else
  45. # define SDL_UNUSED
  46. # endif
  47. #endif
  48. /* Some compilers use a special export keyword */
  49. #ifndef SDL_DECLSPEC
  50. # if defined(SDL_PLATFORM_WINDOWS)
  51. # ifdef DLL_EXPORT
  52. # define SDL_DECLSPEC __declspec(dllexport)
  53. # else
  54. # define SDL_DECLSPEC
  55. # endif
  56. # else
  57. # if defined(__GNUC__) && __GNUC__ >= 4
  58. # define SDL_DECLSPEC __attribute__ ((visibility("default")))
  59. # else
  60. # define SDL_DECLSPEC
  61. # endif
  62. # endif
  63. #endif
  64. /* By default SDL uses the C calling convention */
  65. #ifndef SDLCALL
  66. #if defined(SDL_PLATFORM_WINDOWS) && !defined(__GNUC__)
  67. #define SDLCALL __cdecl
  68. #else
  69. #define SDLCALL
  70. #endif
  71. #endif /* SDLCALL */
  72. /* Force structure packing at 4 byte alignment.
  73. This is necessary if the header is included in code which has structure
  74. packing set to an alternate value, say for loading structures from disk.
  75. The packing is reset to the previous value in SDL_close_code.h
  76. */
  77. #if defined(_MSC_VER) || defined(__MWERKS__) || defined(__BORLANDC__)
  78. #ifdef _MSC_VER
  79. #pragma warning(disable: 4103)
  80. #endif
  81. #ifdef __clang__
  82. #pragma clang diagnostic ignored "-Wpragma-pack"
  83. #endif
  84. #ifdef __BORLANDC__
  85. #pragma nopackwarning
  86. #endif
  87. #ifdef _WIN64
  88. /* Use 8-byte alignment on 64-bit architectures, so pointers are aligned */
  89. #pragma pack(push,8)
  90. #else
  91. #pragma pack(push,4)
  92. #endif
  93. #endif /* Compiler needs structure packing set */
  94. #ifndef SDL_INLINE
  95. #ifdef __GNUC__
  96. #define SDL_INLINE __inline__
  97. #elif defined(_MSC_VER) || defined(__BORLANDC__) || \
  98. defined(__DMC__) || defined(__SC__) || \
  99. defined(__WATCOMC__) || defined(__LCC__) || \
  100. defined(__DECC) || defined(__CC_ARM)
  101. #define SDL_INLINE __inline
  102. #ifndef __inline__
  103. #define __inline__ __inline
  104. #endif
  105. #else
  106. #define SDL_INLINE inline
  107. #ifndef __inline__
  108. #define __inline__ inline
  109. #endif
  110. #endif
  111. #endif /* SDL_INLINE not defined */
  112. #ifndef SDL_FORCE_INLINE
  113. #ifdef _MSC_VER
  114. #define SDL_FORCE_INLINE __forceinline
  115. #elif ( (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) )
  116. #define SDL_FORCE_INLINE __attribute__((always_inline)) static __inline__
  117. #else
  118. #define SDL_FORCE_INLINE static SDL_INLINE
  119. #endif
  120. #endif /* SDL_FORCE_INLINE not defined */
  121. #ifndef SDL_NORETURN
  122. #ifdef __GNUC__
  123. #define SDL_NORETURN __attribute__((noreturn))
  124. #elif defined(_MSC_VER)
  125. #define SDL_NORETURN __declspec(noreturn)
  126. #else
  127. #define SDL_NORETURN
  128. #endif
  129. #endif /* SDL_NORETURN not defined */
  130. #ifdef __clang__
  131. #if __has_feature(attribute_analyzer_noreturn)
  132. #define SDL_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
  133. #endif
  134. #endif
  135. #ifndef SDL_ANALYZER_NORETURN
  136. #define SDL_ANALYZER_NORETURN
  137. #endif
  138. /* Apparently this is needed by several Windows compilers */
  139. #ifndef __MACH__
  140. #ifndef NULL
  141. #ifdef __cplusplus
  142. #define NULL 0
  143. #else
  144. #define NULL ((void *)0)
  145. #endif
  146. #endif /* NULL */
  147. #endif /* ! macOS - breaks precompiled headers */
  148. #ifndef SDL_FALLTHROUGH
  149. #if (defined(__cplusplus) && __cplusplus >= 201703L) || \
  150. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L)
  151. #define SDL_FALLTHROUGH [[fallthrough]]
  152. #else
  153. #if defined(__has_attribute) && !defined(__SUNPRO_C) && !defined(__SUNPRO_CC)
  154. #define SDL_HAS_FALLTHROUGH __has_attribute(__fallthrough__)
  155. #else
  156. #define SDL_HAS_FALLTHROUGH 0
  157. #endif /* __has_attribute */
  158. #if SDL_HAS_FALLTHROUGH && \
  159. ((defined(__GNUC__) && __GNUC__ >= 7) || \
  160. (defined(__clang_major__) && __clang_major__ >= 10))
  161. #define SDL_FALLTHROUGH __attribute__((__fallthrough__))
  162. #else
  163. #define SDL_FALLTHROUGH do {} while (0) /* fallthrough */
  164. #endif /* SDL_HAS_FALLTHROUGH */
  165. #undef SDL_HAS_FALLTHROUGH
  166. #endif /* C++17 or C2x */
  167. #endif /* SDL_FALLTHROUGH not defined */
  168. #ifndef SDL_NODISCARD
  169. #if (defined(__cplusplus) && __cplusplus >= 201703L) || \
  170. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
  171. #define SDL_NODISCARD [[nodiscard]]
  172. #elif ( (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) )
  173. #define SDL_NODISCARD __attribute__((warn_unused_result))
  174. #elif defined(_MSC_VER) && (_MSC_VER >= 1700)
  175. #define SDL_NODISCARD _Check_return_
  176. #else
  177. #define SDL_NODISCARD
  178. #endif /* C++17 or C23 */
  179. #endif /* SDL_NODISCARD not defined */
  180. #ifndef SDL_MALLOC
  181. #if defined(__GNUC__) && (__GNUC__ >= 3)
  182. #define SDL_MALLOC __attribute__((malloc))
  183. /** FIXME
  184. #elif defined(_MSC_VER)
  185. #define SDL_MALLOC __declspec(allocator) __desclspec(restrict)
  186. **/
  187. #else
  188. #define SDL_MALLOC
  189. #endif
  190. #endif /* SDL_MALLOC not defined */
  191. #ifndef SDL_ALLOC_SIZE
  192. #if (defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  193. #define SDL_ALLOC_SIZE(p) __attribute__((alloc_size(p)))
  194. #elif defined(_MSC_VER)
  195. #define SDL_ALLOC_SIZE(p)
  196. #else
  197. #define SDL_ALLOC_SIZE(p)
  198. #endif
  199. #endif /* SDL_ALLOC_SIZE not defined */
  200. #ifndef SDL_ALLOC_SIZE2
  201. #if (defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  202. #define SDL_ALLOC_SIZE2(p1, p2) __attribute__((alloc_size(p1, p2)))
  203. #elif defined(_MSC_VER)
  204. #define SDL_ALLOC_SIZE2(p1, p2)
  205. #else
  206. #define SDL_ALLOC_SIZE2(p1, p2)
  207. #endif
  208. #endif /* SDL_ALLOC_SIZE2 not defined */