Path: blob/master/thirdparty/sdl/include/SDL3/SDL_begin_code.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/* WIKI CATEGORY: BeginCode */2223/**24* # CategoryBeginCode25*26* `SDL_begin_code.h` sets things up for C dynamic library function27* definitions, static inlined functions, and structures aligned at 4-byte28* alignment. If you don't like ugly C preprocessor code, don't look at this29* file. :)30*31* SDL's headers use this; applications generally should not include this32* header directly.33*/3435/* This shouldn't be nested -- included it around code only. */36#ifdef SDL_begin_code_h37#error Nested inclusion of SDL_begin_code.h38#endif39#define SDL_begin_code_h4041#ifdef SDL_WIKI_DOCUMENTATION_SECTION4243/**44* A macro to tag a symbol as deprecated.45*46* A function is marked deprecated by adding this macro to its declaration:47*48* ```c49* extern SDL_DEPRECATED int ThisFunctionWasABadIdea(void);50* ```51*52* Compilers with deprecation support can give a warning when a deprecated53* function is used. This symbol may be used in SDL's headers, but apps are54* welcome to use it for their own interfaces as well.55*56* SDL, on occasion, might deprecate a function for various reasons. However,57* SDL never removes symbols before major versions, so deprecated interfaces58* in SDL3 will remain available until SDL4, where it would be expected an app59* would have to take steps to migrate anyhow.60*61* On compilers without a deprecation mechanism, this is defined to nothing,62* and using a deprecated function will not generate a warning.63*64* \since This macro is available since SDL 3.2.0.65*/66#define SDL_DEPRECATED __attribute__((deprecated))6768/**69* A macro to tag a symbol as a public API.70*71* SDL uses this macro for all its public functions. On some targets, it is72* used to signal to the compiler that this function needs to be exported from73* a shared library, but it might have other side effects.74*75* This symbol is used in SDL's headers, but apps and other libraries are76* welcome to use it for their own interfaces as well.77*78* \since This macro is available since SDL 3.2.0.79*/80#define SDL_DECLSPEC __attribute__ ((visibility("default")))8182/**83* A macro to set a function's calling conventions.84*85* SDL uses this macro for all its public functions, and any callbacks it86* defines. This macro guarantees that calling conventions match between SDL87* and the app, even if the two were built with different compilers or88* optimization settings.89*90* When writing a callback function, it is very important for it to be91* correctly tagged with SDLCALL, as mismatched calling conventions can cause92* strange behaviors and can be difficult to diagnose. Plus, on many93* platforms, SDLCALL is defined to nothing, so compilers won't be able to94* warn that the tag is missing.95*96* This symbol is used in SDL's headers, but apps and other libraries are97* welcome to use it for their own interfaces as well.98*99* \since This macro is available since SDL 3.2.0.100*/101#define SDLCALL __cdecl102103/**104* A macro to request a function be inlined.105*106* This is a hint to the compiler to inline a function. The compiler is free107* to ignore this request. On compilers without inline support, this is108* defined to nothing.109*110* \since This macro is available since SDL 3.2.0.111*/112#define SDL_INLINE __inline113114/**115* A macro to demand a function be inlined.116*117* This is a command to the compiler to inline a function. SDL uses this macro118* in its public headers for a handful of simple functions. On compilers119* without forceinline support, this is defined to `static SDL_INLINE`, which120* is often good enough.121*122* This symbol is used in SDL's headers, but apps and other libraries are123* welcome to use it for their own interfaces as well.124*125* \since This macro is available since SDL 3.2.0.126*/127#define SDL_FORCE_INLINE __forceinline128129/**130* A macro to tag a function as never-returning.131*132* This is a hint to the compiler that a function does not return. An example133* of a function like this is the C runtime's exit() function.134*135* This hint can lead to code optimizations, and help analyzers understand136* code flow better. On compilers without noreturn support, this is defined to137* nothing.138*139* This symbol is used in SDL's headers, but apps and other libraries are140* welcome to use it for their own interfaces as well.141*142* \since This macro is available since SDL 3.2.0.143*/144#define SDL_NORETURN __attribute__((noreturn))145146/**147* A macro to tag a function as never-returning (for analysis purposes).148*149* This is almost identical to SDL_NORETURN, except functions marked with this150* _can_ actually return. The difference is that this isn't used for code151* generation, but rather static analyzers use this information to assume152* truths about program state and available code paths. Specifically, this tag153* is useful for writing an assertion mechanism. Indeed, SDL_assert uses this154* tag behind the scenes. Generally, apps that don't understand the specific155* use-case for this tag should avoid using it directly.156*157* On compilers without analyzer_noreturn support, this is defined to nothing.158*159* This symbol is used in SDL's headers, but apps and other libraries are160* welcome to use it for their own interfaces as well.161*162* \since This macro is available since SDL 3.2.0.163*/164#define SDL_ANALYZER_NORETURN __attribute__((analyzer_noreturn))165166167/**168* A macro to signal that a case statement without a `break` is intentional.169*170* C compilers have gotten more aggressive about warning when a switch's171* `case` block does not end with a `break` or other flow control statement,172* flowing into the next case's code, as this is a common accident that leads173* to strange bugs. But sometimes falling through to the next case is the174* correct and desired behavior. This symbol lets an app communicate this175* intention to the compiler, so it doesn't generate a warning.176*177* It is used like this:178*179* ```c180* switch (x) {181* case 1:182* DoSomethingOnlyForOne();183* SDL_FALLTHROUGH; // tell the compiler this was intentional.184* case 2:185* DoSomethingForOneAndTwo();186* break;187* }188* ```189*190* \since This macro is available since SDL 3.2.0.191*/192#define SDL_FALLTHROUGH [[fallthrough]]193194/**195* A macro to tag a function's return value as critical.196*197* This is a hint to the compiler that a function's return value should not be198* ignored.199*200* If an NODISCARD function's return value is thrown away (the function is201* called as if it returns `void`), the compiler will issue a warning.202*203* While it's generally good practice to check return values for errors, often204* times legitimate programs do not for good reasons. Be careful about what205* functions are tagged as NODISCARD. It operates best when used on a function206* that's failure is surprising and catastrophic; a good example would be a207* program that checks the return values of all its file write function calls208* but not the call to close the file, which it assumes incorrectly never209* fails.210*211* Function callers that want to throw away a NODISCARD return value can call212* the function with a `(void)` cast, which informs the compiler the act is213* intentional.214*215* On compilers without nodiscard support, this is defined to nothing.216*217* \since This macro is available since SDL 3.2.0.218*/219#define SDL_NODISCARD [[nodiscard]]220221/**222* A macro to tag a function as an allocator.223*224* This is a hint to the compiler that a function is an allocator, like225* malloc(), with certain rules. A description of how GCC treats this hint is226* here:227*228* https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute229*230* On compilers without allocator tag support, this is defined to nothing.231*232* Most apps don't need to, and should not, use this directly.233*234* \since This macro is available since SDL 3.2.0.235*/236#define SDL_MALLOC __declspec(allocator) __desclspec(restrict)237238/**239* A macro to tag a function as returning a certain allocation.240*241* This is a hint to the compiler that a function allocates and returns a242* specific amount of memory based on one of its arguments. For example, the C243* runtime's malloc() function could use this macro with an argument of 1244* (first argument to malloc is the size of the allocation).245*246* On compilers without alloc_size support, this is defined to nothing.247*248* Most apps don't need to, and should not, use this directly.249*250* \since This macro is available since SDL 3.2.0.251*/252#define SDL_ALLOC_SIZE(p) __attribute__((alloc_size(p)))253254/**255* A macro to tag a pointer variable, to help with pointer aliasing.256*257* A good explanation of the restrict keyword is here:258*259* https://en.wikipedia.org/wiki/Restrict260*261* On compilers without restrict support, this is defined to nothing.262*263* \since This macro is available since SDL 3.2.0.264*/265#define SDL_RESTRICT __restrict__266267/**268* Check if the compiler supports a given builtin functionality.269*270* This allows preprocessor checks for things that otherwise might fail to271* compile.272*273* Supported by virtually all clang versions and more-recent GCCs. Use this274* instead of checking the clang version if possible.275*276* On compilers without has_builtin support, this is defined to 0 (always277* false).278*279* \since This macro is available since SDL 3.2.0.280*/281#define SDL_HAS_BUILTIN(x) __has_builtin(x)282283/* end of wiki documentation section. */284#endif285286#ifndef SDL_HAS_BUILTIN287#ifdef __has_builtin288#define SDL_HAS_BUILTIN(x) __has_builtin(x)289#else290#define SDL_HAS_BUILTIN(x) 0291#endif292#endif293294#ifndef SDL_DEPRECATED295# if defined(__GNUC__) && (__GNUC__ >= 4) /* technically, this arrived in gcc 3.1, but oh well. */296# define SDL_DEPRECATED __attribute__((deprecated))297# elif defined(_MSC_VER)298# define SDL_DEPRECATED __declspec(deprecated)299# else300# define SDL_DEPRECATED301# endif302#endif303304#ifndef SDL_UNUSED305# ifdef __GNUC__306# define SDL_UNUSED __attribute__((unused))307# else308# define SDL_UNUSED309# endif310#endif311312/* Some compilers use a special export keyword */313#ifndef SDL_DECLSPEC314# if defined(SDL_PLATFORM_WINDOWS)315# ifdef DLL_EXPORT316# define SDL_DECLSPEC __declspec(dllexport)317# else318# define SDL_DECLSPEC319# endif320# else321# if defined(__GNUC__) && __GNUC__ >= 4322# define SDL_DECLSPEC __attribute__ ((visibility("default")))323# else324# define SDL_DECLSPEC325# endif326# endif327#endif328329/* By default SDL uses the C calling convention */330#ifndef SDLCALL331#if defined(SDL_PLATFORM_WINDOWS) && !defined(__GNUC__)332#define SDLCALL __cdecl333#else334#define SDLCALL335#endif336#endif /* SDLCALL */337338/* Force structure packing at 4 byte alignment.339This is necessary if the header is included in code which has structure340packing set to an alternate value, say for loading structures from disk.341The packing is reset to the previous value in SDL_close_code.h342*/343#if defined(_MSC_VER) || defined(__MWERKS__) || defined(__BORLANDC__)344#ifdef _MSC_VER345#pragma warning(disable: 4103)346#endif347#ifdef __clang__348#pragma clang diagnostic ignored "-Wpragma-pack"349#endif350#ifdef __BORLANDC__351#pragma nopackwarning352#endif353#ifdef _WIN64354/* Use 8-byte alignment on 64-bit architectures, so pointers are aligned */355#pragma pack(push,8)356#else357#pragma pack(push,4)358#endif359#endif /* Compiler needs structure packing set */360361#ifndef SDL_INLINE362#ifdef __GNUC__363#define SDL_INLINE __inline__364#elif defined(_MSC_VER) || defined(__BORLANDC__) || \365defined(__DMC__) || defined(__SC__) || \366defined(__WATCOMC__) || defined(__LCC__) || \367defined(__DECC) || defined(__CC_ARM)368#define SDL_INLINE __inline369#ifndef __inline__370#define __inline__ __inline371#endif372#else373#define SDL_INLINE inline374#ifndef __inline__375#define __inline__ inline376#endif377#endif378#endif /* SDL_INLINE not defined */379380#ifndef SDL_FORCE_INLINE381#ifdef _MSC_VER382#define SDL_FORCE_INLINE __forceinline383#elif ( (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) )384#define SDL_FORCE_INLINE __attribute__((always_inline)) static __inline__385#else386#define SDL_FORCE_INLINE static SDL_INLINE387#endif388#endif /* SDL_FORCE_INLINE not defined */389390#ifndef SDL_NORETURN391#ifdef __GNUC__392#define SDL_NORETURN __attribute__((noreturn))393#elif defined(_MSC_VER)394#define SDL_NORETURN __declspec(noreturn)395#else396#define SDL_NORETURN397#endif398#endif /* SDL_NORETURN not defined */399400#ifdef __clang__401#if __has_feature(attribute_analyzer_noreturn)402#define SDL_ANALYZER_NORETURN __attribute__((analyzer_noreturn))403#endif404#endif405406#ifndef SDL_ANALYZER_NORETURN407#define SDL_ANALYZER_NORETURN408#endif409410/* Apparently this is needed by several Windows compilers */411#ifndef __MACH__412#ifndef NULL413#ifdef __cplusplus414#define NULL 0415#else416#define NULL ((void *)0)417#endif418#endif /* NULL */419#endif /* ! macOS - breaks precompiled headers */420421#ifndef SDL_FALLTHROUGH422#if (defined(__cplusplus) && __cplusplus >= 201703L) || \423(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L)424#define SDL_FALLTHROUGH [[fallthrough]]425#else426#if defined(__has_attribute) && !defined(__SUNPRO_C) && !defined(__SUNPRO_CC)427#define SDL_HAS_FALLTHROUGH __has_attribute(__fallthrough__)428#else429#define SDL_HAS_FALLTHROUGH 0430#endif /* __has_attribute */431#if SDL_HAS_FALLTHROUGH && \432((defined(__GNUC__) && __GNUC__ >= 7) || \433(defined(__clang_major__) && __clang_major__ >= 10))434#define SDL_FALLTHROUGH __attribute__((__fallthrough__))435#else436#define SDL_FALLTHROUGH do {} while (0) /* fallthrough */437#endif /* SDL_HAS_FALLTHROUGH */438#undef SDL_HAS_FALLTHROUGH439#endif /* C++17 or C2x */440#endif /* SDL_FALLTHROUGH not defined */441442#ifndef SDL_NODISCARD443#if (defined(__cplusplus) && __cplusplus >= 201703L) || \444(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)445#define SDL_NODISCARD [[nodiscard]]446#elif ( (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) )447#define SDL_NODISCARD __attribute__((warn_unused_result))448#elif defined(_MSC_VER) && (_MSC_VER >= 1700)449#define SDL_NODISCARD _Check_return_450#else451#define SDL_NODISCARD452#endif /* C++17 or C23 */453#endif /* SDL_NODISCARD not defined */454455#ifndef SDL_MALLOC456#if defined(__GNUC__) && (__GNUC__ >= 3)457#define SDL_MALLOC __attribute__((malloc))458/** FIXME459#elif defined(_MSC_VER)460#define SDL_MALLOC __declspec(allocator) __desclspec(restrict)461**/462#else463#define SDL_MALLOC464#endif465#endif /* SDL_MALLOC not defined */466467#ifndef SDL_ALLOC_SIZE468#if (defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))469#define SDL_ALLOC_SIZE(p) __attribute__((alloc_size(p)))470#elif defined(_MSC_VER)471#define SDL_ALLOC_SIZE(p)472#else473#define SDL_ALLOC_SIZE(p)474#endif475#endif /* SDL_ALLOC_SIZE not defined */476477#ifndef SDL_ALLOC_SIZE2478#if (defined(__clang__) && __clang_major__ >= 4) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))479#define SDL_ALLOC_SIZE2(p1, p2) __attribute__((alloc_size(p1, p2)))480#elif defined(_MSC_VER)481#define SDL_ALLOC_SIZE2(p1, p2)482#else483#define SDL_ALLOC_SIZE2(p1, p2)484#endif485#endif /* SDL_ALLOC_SIZE2 not defined */486487488