SDL_begin_code.h 5.9 KB

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