SDL_begin_code.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* 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. /* This shouldn't be nested -- included it around code only. */
  25. #ifdef SDL_begin_code_h
  26. #error Nested inclusion of SDL_begin_code.h
  27. #endif
  28. #define SDL_begin_code_h
  29. #ifndef SDL_DEPRECATED
  30. # if defined(__GNUC__) && (__GNUC__ >= 4) /* technically, this arrived in gcc 3.1, but oh well. */
  31. # define SDL_DEPRECATED __attribute__((deprecated))
  32. # elif defined(_MSC_VER)
  33. # define SDL_DEPRECATED __declspec(deprecated)
  34. # else
  35. # define SDL_DEPRECATED
  36. # endif
  37. #endif
  38. #ifndef SDL_UNUSED
  39. # ifdef __GNUC__
  40. # define SDL_UNUSED __attribute__((unused))
  41. # else
  42. # define SDL_UNUSED
  43. # endif
  44. #endif
  45. /* Some compilers use a special export keyword */
  46. #ifndef SDL_DECLSPEC
  47. # if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_CYGWIN) || defined(SDL_PLATFORM_GDK)
  48. # ifdef DLL_EXPORT
  49. # define SDL_DECLSPEC __declspec(dllexport)
  50. # else
  51. # define SDL_DECLSPEC
  52. # endif
  53. # else
  54. # if defined(__GNUC__) && __GNUC__ >= 4
  55. # define SDL_DECLSPEC __attribute__ ((visibility("default")))
  56. # else
  57. # define SDL_DECLSPEC
  58. # endif
  59. # endif
  60. #endif
  61. /* This is used to mark functions that return memory that need to be freed with SDL_free() */
  62. #ifndef SDL_DECLSPEC_FREE
  63. #define SDL_DECLSPEC_FREE SDL_DECLSPEC
  64. #endif
  65. /* By default SDL uses the C calling convention */
  66. #ifndef SDLCALL
  67. #if (defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINRT) || defined(SDL_PLATFORM_GDK)) && !defined(__GNUC__)
  68. #define SDLCALL __cdecl
  69. #else
  70. #define SDLCALL
  71. #endif
  72. #endif /* SDLCALL */
  73. /* Force structure packing at 4 byte alignment.
  74. This is necessary if the header is included in code which has structure
  75. packing set to an alternate value, say for loading structures from disk.
  76. The packing is reset to the previous value in SDL_close_code.h
  77. */
  78. #if defined(_MSC_VER) || defined(__MWERKS__) || defined(__BORLANDC__)
  79. #ifdef _MSC_VER
  80. #pragma warning(disable: 4103)
  81. #endif
  82. #ifdef __clang__
  83. #pragma clang diagnostic ignored "-Wpragma-pack"
  84. #endif
  85. #ifdef __BORLANDC__
  86. #pragma nopackwarning
  87. #endif
  88. #ifdef _WIN64
  89. /* Use 8-byte alignment on 64-bit architectures, so pointers are aligned */
  90. #pragma pack(push,8)
  91. #else
  92. #pragma pack(push,4)
  93. #endif
  94. #endif /* Compiler needs structure packing set */
  95. #ifndef SDL_INLINE
  96. #ifdef __GNUC__
  97. #define SDL_INLINE __inline__
  98. #elif defined(_MSC_VER) || defined(__BORLANDC__) || \
  99. defined(__DMC__) || defined(__SC__) || \
  100. defined(__WATCOMC__) || defined(__LCC__) || \
  101. defined(__DECC) || defined(__CC_ARM)
  102. #define SDL_INLINE __inline
  103. #ifndef __inline__
  104. #define __inline__ __inline
  105. #endif
  106. #else
  107. #define SDL_INLINE inline
  108. #ifndef __inline__
  109. #define __inline__ inline
  110. #endif
  111. #endif
  112. #endif /* SDL_INLINE not defined */
  113. #ifndef SDL_FORCE_INLINE
  114. #ifdef _MSC_VER
  115. #define SDL_FORCE_INLINE __forceinline
  116. #elif ( (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) )
  117. #define SDL_FORCE_INLINE __attribute__((always_inline)) static __inline__
  118. #else
  119. #define SDL_FORCE_INLINE static SDL_INLINE
  120. #endif
  121. #endif /* SDL_FORCE_INLINE not defined */
  122. #ifndef SDL_NORETURN
  123. #ifdef __GNUC__
  124. #define SDL_NORETURN __attribute__((noreturn))
  125. #elif defined(_MSC_VER)
  126. #define SDL_NORETURN __declspec(noreturn)
  127. #else
  128. #define SDL_NORETURN
  129. #endif
  130. #endif /* SDL_NORETURN not defined */
  131. #ifdef __clang__
  132. #if __has_feature(attribute_analyzer_noreturn)
  133. #define SDL_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
  134. #endif
  135. #endif
  136. #ifndef SDL_ANALYZER_NORETURN
  137. #define SDL_ANALYZER_NORETURN
  138. #endif
  139. /* Apparently this is needed by several Windows compilers */
  140. #ifndef __MACH__
  141. #ifndef NULL
  142. #ifdef __cplusplus
  143. #define NULL 0
  144. #else
  145. #define NULL ((void *)0)
  146. #endif
  147. #endif /* NULL */
  148. #endif /* ! macOS - breaks precompiled headers */
  149. #ifndef SDL_FALLTHROUGH
  150. #if (defined(__cplusplus) && __cplusplus >= 201703L) || \
  151. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L)
  152. #define SDL_FALLTHROUGH [[fallthrough]]
  153. #else
  154. #if defined(__has_attribute) && !defined(__SUNPRO_C) && !defined(__SUNPRO_CC)
  155. #define SDL_HAS_FALLTHROUGH __has_attribute(__fallthrough__)
  156. #else
  157. #define SDL_HAS_FALLTHROUGH 0
  158. #endif /* __has_attribute */
  159. #if SDL_HAS_FALLTHROUGH && \
  160. ((defined(__GNUC__) && __GNUC__ >= 7) || \
  161. (defined(__clang_major__) && __clang_major__ >= 10))
  162. #define SDL_FALLTHROUGH __attribute__((__fallthrough__))
  163. #else
  164. #define SDL_FALLTHROUGH do {} while (0) /* fallthrough */
  165. #endif /* SDL_HAS_FALLTHROUGH */
  166. #undef SDL_HAS_FALLTHROUGH
  167. #endif /* C++17 or C2x */
  168. #endif /* SDL_FALLTHROUGH not defined */
  169. #ifndef SDL_NODISCARD
  170. #if (defined(__cplusplus) && __cplusplus >= 201703L) || \
  171. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
  172. #define SDL_NODISCARD [[nodiscard]]
  173. #elif ( (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) )
  174. #define SDL_NODISCARD __attribute__((warn_unused_result))
  175. #elif defined(_MSC_VER) && (_MSC_VER >= 1700)
  176. #define SDL_NODISCARD _Check_return_
  177. #else
  178. #define SDL_NODISCARD
  179. #endif /* C++17 or C23 */
  180. #endif /* SDL_NODISCARD not defined */
  181. #ifndef SDL_MALLOC
  182. #if defined(__GNUC__) && (__GNUC__ >= 3)
  183. #define SDL_MALLOC __attribute__((malloc))
  184. /** FIXME
  185. #elif defined(_MSC_VER)
  186. #define SDL_MALLOC __declspec(allocator) __desclspec(restrict)
  187. **/
  188. #else
  189. #define SDL_MALLOC
  190. #endif
  191. #endif /* SDL_MALLOC not defined */
  192. #ifndef SDL_ALLOC_SIZE
  193. #if (defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  194. #define SDL_ALLOC_SIZE(p) __attribute__((alloc_size(p)))
  195. #elif defined(_MSC_VER)
  196. #define SDL_ALLOC_SIZE(p)
  197. #else
  198. #define SDL_ALLOC_SIZE(p)
  199. #endif
  200. #endif /* SDL_ALLOC_SIZE not defined */
  201. #ifndef SDL_ALLOC_SIZE2
  202. #if (defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  203. #define SDL_ALLOC_SIZE2(p1, p2) __attribute__((alloc_size(p1, p2)))
  204. #elif defined(_MSC_VER)
  205. #define SDL_ALLOC_SIZE2(p1, p2)
  206. #else
  207. #define SDL_ALLOC_SIZE2(p1, p2)
  208. #endif
  209. #endif /* SDL_ALLOC_SIZE2 not defined */